From 8ac00533ff1354a121b66c5148e9cce70e4e73e6 Mon Sep 17 00:00:00 2001 From: Renaud Chaput Date: Tue, 1 Oct 2024 10:22:14 +0200 Subject: [PATCH] Fix follow notifications from streaming being grouped (#32179) --- .../mastodon/actions/notification_groups.ts | 4 + .../mastodon/reducers/notification_groups.ts | 79 ++++++++++--------- 2 files changed, 46 insertions(+), 37 deletions(-) diff --git a/app/javascript/mastodon/actions/notification_groups.ts b/app/javascript/mastodon/actions/notification_groups.ts index b40b04f8cc..a359913e61 100644 --- a/app/javascript/mastodon/actions/notification_groups.ts +++ b/app/javascript/mastodon/actions/notification_groups.ts @@ -70,6 +70,10 @@ function dispatchAssociatedRecords( const supportedGroupedNotificationTypes = ['favourite', 'reblog']; +export function shouldGroupNotificationType(type: string) { + return supportedGroupedNotificationTypes.includes(type); +} + export const fetchNotifications = createDataLoadingThunk( 'notificationGroups/fetch', async (_params, { getState }) => diff --git a/app/javascript/mastodon/reducers/notification_groups.ts b/app/javascript/mastodon/reducers/notification_groups.ts index f3c83ccd8d..375e643876 100644 --- a/app/javascript/mastodon/reducers/notification_groups.ts +++ b/app/javascript/mastodon/reducers/notification_groups.ts @@ -21,6 +21,7 @@ import { unmountNotifications, refreshStaleNotificationGroups, pollRecentNotifications, + shouldGroupNotificationType, } from 'mastodon/actions/notification_groups'; import { disconnectTimeline, @@ -205,46 +206,50 @@ function processNewNotification( groups: NotificationGroupsState['groups'], notification: ApiNotificationJSON, ) { - const existingGroupIndex = groups.findIndex( - (group) => - group.type !== 'gap' && group.group_key === notification.group_key, - ); + if (shouldGroupNotificationType(notification.type)) { + const existingGroupIndex = groups.findIndex( + (group) => + group.type !== 'gap' && group.group_key === notification.group_key, + ); - // In any case, we are going to add a group at the top - // If there is currently a gap at the top, now is the time to update it - if (groups.length > 0 && groups[0]?.type === 'gap') { - groups[0].maxId = notification.id; - } - - if (existingGroupIndex > -1) { - const existingGroup = groups[existingGroupIndex]; - - if ( - existingGroup && - existingGroup.type !== 'gap' && - !existingGroup.sampleAccountIds.includes(notification.account.id) // This can happen for example if you like, then unlike, then like again the same post - ) { - // Update the existing group - if ( - existingGroup.sampleAccountIds.unshift(notification.account.id) > - NOTIFICATIONS_GROUP_MAX_AVATARS - ) - existingGroup.sampleAccountIds.pop(); - - existingGroup.most_recent_notification_id = notification.id; - existingGroup.page_max_id = notification.id; - existingGroup.latest_page_notification_at = notification.created_at; - existingGroup.notifications_count += 1; - - groups.splice(existingGroupIndex, 1); - mergeGapsAround(groups, existingGroupIndex); - - groups.unshift(existingGroup); + // In any case, we are going to add a group at the top + // If there is currently a gap at the top, now is the time to update it + if (groups.length > 0 && groups[0]?.type === 'gap') { + groups[0].maxId = notification.id; + } + + if (existingGroupIndex > -1) { + const existingGroup = groups[existingGroupIndex]; + + if ( + existingGroup && + existingGroup.type !== 'gap' && + !existingGroup.sampleAccountIds.includes(notification.account.id) // This can happen for example if you like, then unlike, then like again the same post + ) { + // Update the existing group + if ( + existingGroup.sampleAccountIds.unshift(notification.account.id) > + NOTIFICATIONS_GROUP_MAX_AVATARS + ) + existingGroup.sampleAccountIds.pop(); + + existingGroup.most_recent_notification_id = notification.id; + existingGroup.page_max_id = notification.id; + existingGroup.latest_page_notification_at = notification.created_at; + existingGroup.notifications_count += 1; + + groups.splice(existingGroupIndex, 1); + mergeGapsAround(groups, existingGroupIndex); + + groups.unshift(existingGroup); + + return; + } } - } else { - // Create a new group - groups.unshift(createNotificationGroupFromNotificationJSON(notification)); } + + // We have not found an existing group, create a new one + groups.unshift(createNotificationGroupFromNotificationJSON(notification)); } function trimNotifications(state: NotificationGroupsState) {