2024-08-28 04:41:08 -07:00
|
|
|
import { getNotificationStatus } from 'pl-fe/features/notifications/components/notification';
|
2024-08-11 01:48:58 -07:00
|
|
|
|
2024-08-18 08:50:56 -07:00
|
|
|
import { normalizeAccount } from './account';
|
|
|
|
|
2024-09-09 02:33:02 -07:00
|
|
|
import type { OrderedMap as ImmutableOrderedMap } from 'immutable';
|
2024-08-11 01:48:58 -07:00
|
|
|
import type { Notification as BaseNotification } from 'pl-api';
|
2024-09-09 02:33:02 -07:00
|
|
|
import type { MinifiedNotification } from 'pl-fe/reducers/notifications';
|
2024-08-11 01:48:58 -07:00
|
|
|
|
|
|
|
const STATUS_NOTIFICATION_TYPES = [
|
|
|
|
'favourite',
|
|
|
|
'reblog',
|
|
|
|
'emoji_reaction',
|
|
|
|
'event_reminder',
|
|
|
|
'participation_accepted',
|
|
|
|
'participation_request',
|
|
|
|
];
|
|
|
|
|
|
|
|
const normalizeNotification = (notification: BaseNotification) => ({
|
|
|
|
...notification,
|
2024-09-09 02:33:02 -07:00
|
|
|
duplicate: false,
|
2024-08-18 08:50:56 -07:00
|
|
|
account: normalizeAccount(notification.account),
|
2024-08-22 07:57:35 -07:00
|
|
|
account_id: notification.account.id,
|
2024-08-18 08:50:56 -07:00
|
|
|
accounts: [normalizeAccount(notification.account)],
|
2024-08-22 07:57:35 -07:00
|
|
|
account_ids: [notification.account.id],
|
2022-03-11 10:13:36 -08:00
|
|
|
});
|
|
|
|
|
2024-09-09 02:33:02 -07:00
|
|
|
const normalizeNotifications = (notifications: Array<BaseNotification>, stateNotifications?: ImmutableOrderedMap<string, MinifiedNotification>) => {
|
2024-08-11 01:48:58 -07:00
|
|
|
const deduplicatedNotifications: Notification[] = [];
|
|
|
|
|
|
|
|
for (const notification of notifications) {
|
2024-09-09 02:33:02 -07:00
|
|
|
const existingNotification = stateNotifications?.get(notification.id);
|
|
|
|
|
|
|
|
// Do not update grouped notifications
|
|
|
|
if (existingNotification && (existingNotification.duplicate || existingNotification.account_ids.length)) continue;
|
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
if (STATUS_NOTIFICATION_TYPES.includes(notification.type)) {
|
|
|
|
const existingNotification = deduplicatedNotifications
|
|
|
|
.find(deduplicated =>
|
|
|
|
deduplicated.type === notification.type
|
|
|
|
&& ((notification.type === 'emoji_reaction' && deduplicated.type === 'emoji_reaction') ? notification.emoji === deduplicated.emoji : true)
|
|
|
|
&& getNotificationStatus(deduplicated)?.id === getNotificationStatus(notification)?.id,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (existingNotification) {
|
2024-08-27 11:46:03 -07:00
|
|
|
existingNotification.accounts.push(normalizeAccount(notification.account));
|
|
|
|
existingNotification.account_ids.push(notification.account.id);
|
2024-09-09 02:33:02 -07:00
|
|
|
deduplicatedNotifications.push({ ...normalizeNotification(notification), duplicate: true });
|
2024-08-11 01:48:58 -07:00
|
|
|
} else {
|
|
|
|
deduplicatedNotifications.push(normalizeNotification(notification));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
deduplicatedNotifications.push(normalizeNotification(notification));
|
|
|
|
}
|
2023-05-23 05:57:36 -07:00
|
|
|
}
|
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
return deduplicatedNotifications;
|
2023-05-23 05:57:36 -07:00
|
|
|
};
|
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
type Notification = ReturnType<typeof normalizeNotification>;
|
2024-05-13 10:00:42 -07:00
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
export { normalizeNotification, normalizeNotifications, type Notification };
|