From 524fc8563b1304c2d736e70b41fbae3d15b44c2a Mon Sep 17 00:00:00 2001 From: Renaud Chaput Date: Wed, 14 Aug 2024 08:47:15 +0200 Subject: [PATCH] [Glitch] Fix notification screen crashing in rare cases where the status no longer exists Port 0d85a79f19fef0c604e8fecfd5dfa9654adc4b95 to glitch-soc Signed-off-by: Claire --- .../flavours/glitch/actions/notification_groups.ts | 4 ++-- app/javascript/flavours/glitch/api_types/notifications.ts | 4 ++-- .../notifications_v2/components/notification_mention.tsx | 6 +++++- .../components/notification_with_status.tsx | 4 +++- app/javascript/flavours/glitch/models/notification_group.ts | 6 +++--- 5 files changed, 15 insertions(+), 9 deletions(-) diff --git a/app/javascript/flavours/glitch/actions/notification_groups.ts b/app/javascript/flavours/glitch/actions/notification_groups.ts index 2f0174d117..49a1a1362f 100644 --- a/app/javascript/flavours/glitch/actions/notification_groups.ts +++ b/app/javascript/flavours/glitch/actions/notification_groups.ts @@ -47,7 +47,7 @@ function dispatchAssociatedRecords( fetchedAccounts.push(notification.moderation_warning.target_account); } - if ('status' in notification) { + if ('status' in notification && notification.status) { fetchedStatuses.push(notification.status); } }); @@ -119,7 +119,7 @@ export const processNewNotificationForGroups = createAppAsyncThunk( if ( (notification.type === 'mention' || notification.type === 'update') && - notification.status.filtered + notification.status?.filtered ) { const filters = notification.status.filtered.filter((result) => result.filter.context.includes('notifications'), diff --git a/app/javascript/flavours/glitch/api_types/notifications.ts b/app/javascript/flavours/glitch/api_types/notifications.ts index 09dc8efe31..758840a581 100644 --- a/app/javascript/flavours/glitch/api_types/notifications.ts +++ b/app/javascript/flavours/glitch/api_types/notifications.ts @@ -60,12 +60,12 @@ export interface BaseNotificationGroupJSON { interface NotificationGroupWithStatusJSON extends BaseNotificationGroupJSON { type: NotificationWithStatusType; - status_id: string; + status_id: string | null; } interface NotificationWithStatusJSON extends BaseNotificationJSON { type: NotificationWithStatusType; - status: ApiStatusJSON; + status: ApiStatusJSON | null; } interface ReportNotificationGroupJSON extends BaseNotificationGroupJSON { diff --git a/app/javascript/flavours/glitch/features/notifications_v2/components/notification_mention.tsx b/app/javascript/flavours/glitch/features/notifications_v2/components/notification_mention.tsx index 91e7445ceb..7c7d949374 100644 --- a/app/javascript/flavours/glitch/features/notifications_v2/components/notification_mention.tsx +++ b/app/javascript/flavours/glitch/features/notifications_v2/components/notification_mention.tsx @@ -37,7 +37,11 @@ export const NotificationMention: React.FC<{ unread: boolean; }> = ({ notification, unread }) => { const [isDirect, isReply] = useAppSelector((state) => { - const status = state.statuses.get(notification.statusId) as Status; + const status = state.statuses.get(notification.statusId) as + | Status + | undefined; + + if (!status) return [false, false] as const; return [ status.get('visibility') === 'direct', diff --git a/app/javascript/flavours/glitch/features/notifications_v2/components/notification_with_status.tsx b/app/javascript/flavours/glitch/features/notifications_v2/components/notification_with_status.tsx index 23e56ce3a5..8543baa537 100644 --- a/app/javascript/flavours/glitch/features/notifications_v2/components/notification_with_status.tsx +++ b/app/javascript/flavours/glitch/features/notifications_v2/components/notification_with_status.tsx @@ -26,7 +26,7 @@ export const NotificationWithStatus: React.FC<{ icon: IconProp; iconId: string; accountIds: string[]; - statusId: string; + statusId: string | undefined; count: number; labelRenderer: LabelRenderer; unread: boolean; @@ -80,6 +80,8 @@ export const NotificationWithStatus: React.FC<{ [dispatch, statusId], ); + if (!statusId) return null; + return (
extends BaseNotificationGroup { type: Type; - statusId: string; + statusId: string | undefined; } interface BaseNotification @@ -126,7 +126,7 @@ export function createNotificationGroupFromJSON( case 'update': { const { status_id: statusId, ...groupWithoutStatus } = group; return { - statusId, + statusId: statusId ?? undefined, sampleAccountIds, ...groupWithoutStatus, }; @@ -183,7 +183,7 @@ export function createNotificationGroupFromNotificationJSON( case 'mention': case 'poll': case 'update': - return { ...group, statusId: notification.status.id }; + return { ...group, statusId: notification.status?.id }; case 'admin.report': return { ...group, report: createReportFromJSON(notification.report) }; case 'severed_relationships':