Skip to content

Commit

Permalink
[Glitch] Fix duplicate notifications in notification groups when usin…
Browse files Browse the repository at this point in the history
…g slow mode

Port 4bfb888 to glitch-soc

Signed-off-by: Claire <claire.github-309c@sitedethib.com>
  • Loading branch information
ClearlyClaire committed Dec 2, 2024
1 parent 624b942 commit df0b641
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
32 changes: 26 additions & 6 deletions app/javascript/flavours/glitch/models/notification_group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const NOTIFICATIONS_GROUP_MAX_AVATARS = 8;
interface BaseNotificationGroup
extends Omit<BaseNotificationGroupJSON, 'sample_account_ids'> {
sampleAccountIds: string[];
partial: boolean;
}

interface BaseNotificationWithStatus<Type extends NotificationWithStatusType>
Expand Down Expand Up @@ -128,6 +129,7 @@ export function createNotificationGroupFromJSON(
return {
statusId: statusId ?? undefined,
sampleAccountIds,
partial: false,
...groupWithoutStatus,
};
}
Expand All @@ -136,12 +138,14 @@ export function createNotificationGroupFromJSON(
return {
report: createReportFromJSON(report),
sampleAccountIds,
partial: false,
...groupWithoutTargetAccount,
};
}
case 'severed_relationships':
return {
...group,
partial: false,
event: createAccountRelationshipSeveranceEventFromJSON(group.event),
sampleAccountIds,
};
Expand All @@ -150,31 +154,34 @@ export function createNotificationGroupFromJSON(
const { moderation_warning, ...groupWithoutModerationWarning } = group;
return {
...groupWithoutModerationWarning,
partial: false,
moderationWarning: createAccountWarningFromJSON(moderation_warning),
sampleAccountIds,
};
}

default:
return {
sampleAccountIds,
partial: false,
...group,
};
}
}

export function createNotificationGroupFromNotificationJSON(
notification: ApiNotificationJSON,
) {
): NotificationGroup {
const group = {
sampleAccountIds: [notification.account.id],
group_key: notification.group_key,
notifications_count: 1,
type: notification.type,
most_recent_notification_id: notification.id,
page_min_id: notification.id,
page_max_id: notification.id,
latest_page_notification_at: notification.created_at,
} as NotificationGroup;
partial: true,
};

switch (notification.type) {
case 'favourite':
Expand All @@ -183,24 +190,37 @@ export function createNotificationGroupFromNotificationJSON(
case 'mention':
case 'poll':
case 'update':
return { ...group, statusId: notification.status?.id };
return {
...group,
type: notification.type,
statusId: notification.status?.id,
};
case 'admin.report':
return { ...group, report: createReportFromJSON(notification.report) };
return {
...group,
type: notification.type,
report: createReportFromJSON(notification.report),
};
case 'severed_relationships':
return {
...group,
type: notification.type,
event: createAccountRelationshipSeveranceEventFromJSON(
notification.event,
),
};
case 'moderation_warning':
return {
...group,
type: notification.type,
moderationWarning: createAccountWarningFromJSON(
notification.moderation_warning,
),
};
default:
return group;
return {
...group,
type: notification.type,
};
}
}
11 changes: 7 additions & 4 deletions app/javascript/flavours/glitch/reducers/notification_groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,10 +534,13 @@ export const notificationGroupsReducer = createReducer<NotificationGroupsState>(
if (existingGroupIndex > -1) {
const existingGroup = state.groups[existingGroupIndex];
if (existingGroup && existingGroup.type !== 'gap') {
group.notifications_count += existingGroup.notifications_count;
group.sampleAccountIds = group.sampleAccountIds
.concat(existingGroup.sampleAccountIds)
.slice(0, NOTIFICATIONS_GROUP_MAX_AVATARS);
if (group.partial) {
group.notifications_count +=
existingGroup.notifications_count;
group.sampleAccountIds = group.sampleAccountIds
.concat(existingGroup.sampleAccountIds)
.slice(0, NOTIFICATIONS_GROUP_MAX_AVATARS);
}
state.groups.splice(existingGroupIndex, 1);
}
}
Expand Down

0 comments on commit df0b641

Please sign in to comment.