Skip to content

Commit

Permalink
Fix unsupported grouped notifications from streaming causing duplicat…
Browse files Browse the repository at this point in the history
…e IDs (mastodon#32243)
  • Loading branch information
ClearlyClaire committed Oct 4, 2024
1 parent 81cd489 commit 6d5aa58
Showing 1 changed file with 38 additions and 35 deletions.
73 changes: 38 additions & 35 deletions app/javascript/mastodon/reducers/notification_groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,50 +206,53 @@ function processNewNotification(
groups: NotificationGroupsState['groups'],
notification: ApiNotificationJSON,
) {
if (shouldGroupNotificationType(notification.type)) {
const existingGroupIndex = groups.findIndex(
(group) =>
group.type !== 'gap' && group.group_key === notification.group_key,
);
if (!shouldGroupNotificationType(notification.type)) {
notification = {
...notification,
group_key: `ungrouped-${notification.id}`,
};
}

// 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;
}
const existingGroupIndex = groups.findIndex(
(group) =>
group.type !== 'gap' && group.group_key === notification.group_key,
);

if (existingGroupIndex > -1) {
const existingGroup = groups[existingGroupIndex];
// 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 (
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();
if (existingGroupIndex > -1) {
const existingGroup = groups[existingGroupIndex];

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;
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();

groups.splice(existingGroupIndex, 1);
mergeGapsAround(groups, existingGroupIndex);
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.unshift(existingGroup);
groups.splice(existingGroupIndex, 1);
mergeGapsAround(groups, existingGroupIndex);

return;
}
groups.unshift(existingGroup);
}
} else {
// We have not found an existing group, create a new one
groups.unshift(createNotificationGroupFromNotificationJSON(notification));
}

// We have not found an existing group, create a new one
groups.unshift(createNotificationGroupFromNotificationJSON(notification));
}

function trimNotifications(state: NotificationGroupsState) {
Expand Down

0 comments on commit 6d5aa58

Please sign in to comment.