Fix duplicate notifications in notification groups when using slow mode (#33014)
This commit is contained in:
		@@ -17,6 +17,7 @@ export const NOTIFICATIONS_GROUP_MAX_AVATARS = 8;
 | 
				
			|||||||
interface BaseNotificationGroup
 | 
					interface BaseNotificationGroup
 | 
				
			||||||
  extends Omit<BaseNotificationGroupJSON, 'sample_account_ids'> {
 | 
					  extends Omit<BaseNotificationGroupJSON, 'sample_account_ids'> {
 | 
				
			||||||
  sampleAccountIds: string[];
 | 
					  sampleAccountIds: string[];
 | 
				
			||||||
 | 
					  partial: boolean;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
interface BaseNotificationWithStatus<Type extends NotificationWithStatusType>
 | 
					interface BaseNotificationWithStatus<Type extends NotificationWithStatusType>
 | 
				
			||||||
@@ -142,6 +143,7 @@ export function createNotificationGroupFromJSON(
 | 
				
			|||||||
      return {
 | 
					      return {
 | 
				
			||||||
        statusId: statusId ?? undefined,
 | 
					        statusId: statusId ?? undefined,
 | 
				
			||||||
        sampleAccountIds,
 | 
					        sampleAccountIds,
 | 
				
			||||||
 | 
					        partial: false,
 | 
				
			||||||
        ...groupWithoutStatus,
 | 
					        ...groupWithoutStatus,
 | 
				
			||||||
      };
 | 
					      };
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@@ -150,12 +152,14 @@ export function createNotificationGroupFromJSON(
 | 
				
			|||||||
      return {
 | 
					      return {
 | 
				
			||||||
        report: createReportFromJSON(report),
 | 
					        report: createReportFromJSON(report),
 | 
				
			||||||
        sampleAccountIds,
 | 
					        sampleAccountIds,
 | 
				
			||||||
 | 
					        partial: false,
 | 
				
			||||||
        ...groupWithoutTargetAccount,
 | 
					        ...groupWithoutTargetAccount,
 | 
				
			||||||
      };
 | 
					      };
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    case 'severed_relationships':
 | 
					    case 'severed_relationships':
 | 
				
			||||||
      return {
 | 
					      return {
 | 
				
			||||||
        ...group,
 | 
					        ...group,
 | 
				
			||||||
 | 
					        partial: false,
 | 
				
			||||||
        event: createAccountRelationshipSeveranceEventFromJSON(group.event),
 | 
					        event: createAccountRelationshipSeveranceEventFromJSON(group.event),
 | 
				
			||||||
        sampleAccountIds,
 | 
					        sampleAccountIds,
 | 
				
			||||||
      };
 | 
					      };
 | 
				
			||||||
@@ -163,6 +167,7 @@ export function createNotificationGroupFromJSON(
 | 
				
			|||||||
      const { moderation_warning, ...groupWithoutModerationWarning } = group;
 | 
					      const { moderation_warning, ...groupWithoutModerationWarning } = group;
 | 
				
			||||||
      return {
 | 
					      return {
 | 
				
			||||||
        ...groupWithoutModerationWarning,
 | 
					        ...groupWithoutModerationWarning,
 | 
				
			||||||
 | 
					        partial: false,
 | 
				
			||||||
        moderationWarning: createAccountWarningFromJSON(moderation_warning),
 | 
					        moderationWarning: createAccountWarningFromJSON(moderation_warning),
 | 
				
			||||||
        sampleAccountIds,
 | 
					        sampleAccountIds,
 | 
				
			||||||
      };
 | 
					      };
 | 
				
			||||||
@@ -171,6 +176,7 @@ export function createNotificationGroupFromJSON(
 | 
				
			|||||||
      const { annual_report, ...groupWithoutAnnualReport } = group;
 | 
					      const { annual_report, ...groupWithoutAnnualReport } = group;
 | 
				
			||||||
      return {
 | 
					      return {
 | 
				
			||||||
        ...groupWithoutAnnualReport,
 | 
					        ...groupWithoutAnnualReport,
 | 
				
			||||||
 | 
					        partial: false,
 | 
				
			||||||
        annualReport: createAnnualReportEventFromJSON(annual_report),
 | 
					        annualReport: createAnnualReportEventFromJSON(annual_report),
 | 
				
			||||||
        sampleAccountIds,
 | 
					        sampleAccountIds,
 | 
				
			||||||
      };
 | 
					      };
 | 
				
			||||||
@@ -178,6 +184,7 @@ export function createNotificationGroupFromJSON(
 | 
				
			|||||||
    default:
 | 
					    default:
 | 
				
			||||||
      return {
 | 
					      return {
 | 
				
			||||||
        sampleAccountIds,
 | 
					        sampleAccountIds,
 | 
				
			||||||
 | 
					        partial: false,
 | 
				
			||||||
        ...group,
 | 
					        ...group,
 | 
				
			||||||
      };
 | 
					      };
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
@@ -185,17 +192,17 @@ export function createNotificationGroupFromJSON(
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
export function createNotificationGroupFromNotificationJSON(
 | 
					export function createNotificationGroupFromNotificationJSON(
 | 
				
			||||||
  notification: ApiNotificationJSON,
 | 
					  notification: ApiNotificationJSON,
 | 
				
			||||||
) {
 | 
					): NotificationGroup {
 | 
				
			||||||
  const group = {
 | 
					  const group = {
 | 
				
			||||||
    sampleAccountIds: [notification.account.id],
 | 
					    sampleAccountIds: [notification.account.id],
 | 
				
			||||||
    group_key: notification.group_key,
 | 
					    group_key: notification.group_key,
 | 
				
			||||||
    notifications_count: 1,
 | 
					    notifications_count: 1,
 | 
				
			||||||
    type: notification.type,
 | 
					 | 
				
			||||||
    most_recent_notification_id: notification.id,
 | 
					    most_recent_notification_id: notification.id,
 | 
				
			||||||
    page_min_id: notification.id,
 | 
					    page_min_id: notification.id,
 | 
				
			||||||
    page_max_id: notification.id,
 | 
					    page_max_id: notification.id,
 | 
				
			||||||
    latest_page_notification_at: notification.created_at,
 | 
					    latest_page_notification_at: notification.created_at,
 | 
				
			||||||
  } as NotificationGroup;
 | 
					    partial: true,
 | 
				
			||||||
 | 
					  };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  switch (notification.type) {
 | 
					  switch (notification.type) {
 | 
				
			||||||
    case 'favourite':
 | 
					    case 'favourite':
 | 
				
			||||||
@@ -204,12 +211,21 @@ export function createNotificationGroupFromNotificationJSON(
 | 
				
			|||||||
    case 'mention':
 | 
					    case 'mention':
 | 
				
			||||||
    case 'poll':
 | 
					    case 'poll':
 | 
				
			||||||
    case 'update':
 | 
					    case 'update':
 | 
				
			||||||
      return { ...group, statusId: notification.status?.id };
 | 
					      return {
 | 
				
			||||||
 | 
					        ...group,
 | 
				
			||||||
 | 
					        type: notification.type,
 | 
				
			||||||
 | 
					        statusId: notification.status?.id,
 | 
				
			||||||
 | 
					      };
 | 
				
			||||||
    case 'admin.report':
 | 
					    case 'admin.report':
 | 
				
			||||||
      return { ...group, report: createReportFromJSON(notification.report) };
 | 
					      return {
 | 
				
			||||||
 | 
					        ...group,
 | 
				
			||||||
 | 
					        type: notification.type,
 | 
				
			||||||
 | 
					        report: createReportFromJSON(notification.report),
 | 
				
			||||||
 | 
					      };
 | 
				
			||||||
    case 'severed_relationships':
 | 
					    case 'severed_relationships':
 | 
				
			||||||
      return {
 | 
					      return {
 | 
				
			||||||
        ...group,
 | 
					        ...group,
 | 
				
			||||||
 | 
					        type: notification.type,
 | 
				
			||||||
        event: createAccountRelationshipSeveranceEventFromJSON(
 | 
					        event: createAccountRelationshipSeveranceEventFromJSON(
 | 
				
			||||||
          notification.event,
 | 
					          notification.event,
 | 
				
			||||||
        ),
 | 
					        ),
 | 
				
			||||||
@@ -217,11 +233,15 @@ export function createNotificationGroupFromNotificationJSON(
 | 
				
			|||||||
    case 'moderation_warning':
 | 
					    case 'moderation_warning':
 | 
				
			||||||
      return {
 | 
					      return {
 | 
				
			||||||
        ...group,
 | 
					        ...group,
 | 
				
			||||||
 | 
					        type: notification.type,
 | 
				
			||||||
        moderationWarning: createAccountWarningFromJSON(
 | 
					        moderationWarning: createAccountWarningFromJSON(
 | 
				
			||||||
          notification.moderation_warning,
 | 
					          notification.moderation_warning,
 | 
				
			||||||
        ),
 | 
					        ),
 | 
				
			||||||
      };
 | 
					      };
 | 
				
			||||||
    default:
 | 
					    default:
 | 
				
			||||||
      return group;
 | 
					      return {
 | 
				
			||||||
 | 
					        ...group,
 | 
				
			||||||
 | 
					        type: notification.type,
 | 
				
			||||||
 | 
					      };
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -534,10 +534,13 @@ export const notificationGroupsReducer = createReducer<NotificationGroupsState>(
 | 
				
			|||||||
            if (existingGroupIndex > -1) {
 | 
					            if (existingGroupIndex > -1) {
 | 
				
			||||||
              const existingGroup = state.groups[existingGroupIndex];
 | 
					              const existingGroup = state.groups[existingGroupIndex];
 | 
				
			||||||
              if (existingGroup && existingGroup.type !== 'gap') {
 | 
					              if (existingGroup && existingGroup.type !== 'gap') {
 | 
				
			||||||
                group.notifications_count += existingGroup.notifications_count;
 | 
					                if (group.partial) {
 | 
				
			||||||
                group.sampleAccountIds = group.sampleAccountIds
 | 
					                  group.notifications_count +=
 | 
				
			||||||
                  .concat(existingGroup.sampleAccountIds)
 | 
					                    existingGroup.notifications_count;
 | 
				
			||||||
                  .slice(0, NOTIFICATIONS_GROUP_MAX_AVATARS);
 | 
					                  group.sampleAccountIds = group.sampleAccountIds
 | 
				
			||||||
 | 
					                    .concat(existingGroup.sampleAccountIds)
 | 
				
			||||||
 | 
					                    .slice(0, NOTIFICATIONS_GROUP_MAX_AVATARS);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
                state.groups.splice(existingGroupIndex, 1);
 | 
					                state.groups.splice(existingGroupIndex, 1);
 | 
				
			||||||
              }
 | 
					              }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user