this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Filter out invalid notifications

+35 -2
+35 -2
src/utils/group-notifications.jsx
··· 1 + // This is like very lame "type-checking" lol 2 + const notificationTypeKeys = { 3 + mention: ['account', 'status'], 4 + status: ['account', 'status'], 5 + reblog: ['account', 'status'], 6 + follow: ['account'], 7 + follow_request: ['account'], 8 + favourite: ['account', 'status'], 9 + poll: ['status'], 10 + update: ['status'], 11 + }; 12 + function fixNotifications(notifications) { 13 + return notifications.filter((notification) => { 14 + const { type, id, createdAt } = notification; 15 + if (!type) { 16 + console.warn('Notification missing type', notification); 17 + return false; 18 + } 19 + if (!id || !createdAt) { 20 + console.warn('Notification missing id or createdAt', notification); 21 + // Continue processing this despite missing id or createdAt 22 + } 23 + const keys = notificationTypeKeys[type]; 24 + if (keys?.length) { 25 + return keys.every((key) => !!notification[key]); 26 + } 27 + return true; // skip other types 28 + }); 29 + } 30 + 1 31 function groupNotifications(notifications) { 32 + // Filter out invalid notifications 33 + notifications = fixNotifications(notifications); 34 + 2 35 // Create new flat list of notifications 3 36 // Combine sibling notifications based on type and status id 4 37 // Concat all notification.account into an array of _accounts ··· 7 40 for (let i = 0, j = 0; i < notifications.length; i++) { 8 41 const notification = notifications[i]; 9 42 const { id, status, account, type, createdAt } = notification; 10 - const date = new Date(createdAt).toLocaleDateString(); 43 + const date = createdAt ? new Date(createdAt).toLocaleDateString() : ''; 11 44 let virtualType = type; 12 45 if (type === 'favourite' || type === 'reblog') { 13 46 virtualType = 'favourite+reblog'; ··· 50 83 for (let i = 0, j = 0; i < cleanNotifications.length; i++) { 51 84 const notification = cleanNotifications[i]; 52 85 const { id, account, _accounts, type, createdAt } = notification; 53 - const date = new Date(createdAt).toLocaleDateString(); 86 + const date = createdAt ? new Date(createdAt).toLocaleDateString() : ''; 54 87 if (type === 'favourite+reblog' && account && _accounts.length === 1) { 55 88 const key = `${account?.id}-${type}-${date}`; 56 89 const mappedNotification = notificationsMap2[key];