Mostly migrate pl-fe notifications to notification groups

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak
2024-11-03 00:18:29 +01:00
parent 40c0c7512d
commit fe5653cce9
11 changed files with 301 additions and 223 deletions

View File

@ -1,61 +1,20 @@
import { getNotificationStatus } from 'pl-fe/features/notifications/components/notification';
import omit from 'lodash/omit';
import { normalizeAccount } from './account';
import type { Notification as BaseNotification, NotificationGroup } from 'pl-api';
import type { OrderedMap as ImmutableOrderedMap } from 'immutable';
import type { Notification as BaseNotification } from 'pl-api';
import type { MinifiedNotification } from 'pl-fe/reducers/notifications';
const STATUS_NOTIFICATION_TYPES = [
'favourite',
'reblog',
'emoji_reaction',
'event_reminder',
'participation_accepted',
'participation_request',
];
const normalizeNotification = (notification: BaseNotification) => ({
...notification,
duplicate: false,
account: normalizeAccount(notification.account),
account_id: notification.account.id,
accounts: [normalizeAccount(notification.account)],
account_ids: [notification.account.id],
const normalizeNotification = (notification: BaseNotification): NotificationGroup => ({
...(omit(notification, ['account', 'status', 'target'])),
group_key: notification.id,
notifications_count: 1,
most_recent_notification_id: notification.id,
page_min_id: notification.id,
page_max_id: notification.id,
latest_page_notification_at: notification.created_at,
sample_account_ids: [notification.account.id],
// @ts-ignore
status_id: notification.status?.id,
// @ts-ignore
target_id: notification.target?.id,
});
const normalizeNotifications = (notifications: Array<BaseNotification>, stateNotifications?: ImmutableOrderedMap<string, MinifiedNotification>) => {
const deduplicatedNotifications: Notification[] = [];
for (const notification of notifications) {
const existingNotification = stateNotifications?.get(notification.id);
// Do not update grouped notifications
if (existingNotification && (existingNotification.duplicate || existingNotification.account_ids.length)) continue;
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) {
existingNotification.accounts.push(normalizeAccount(notification.account));
existingNotification.account_ids.push(notification.account.id);
deduplicatedNotifications.push({ ...normalizeNotification(notification), duplicate: true });
} else {
deduplicatedNotifications.push(normalizeNotification(notification));
}
} else {
deduplicatedNotifications.push(normalizeNotification(notification));
}
}
return deduplicatedNotifications;
};
type Notification = ReturnType<typeof normalizeNotification>;
export { normalizeNotification, normalizeNotifications, type Notification };
export { normalizeNotification };