Filter out invalid notifications

This commit is contained in:
Lim Chee Aun 2023-10-29 23:27:01 +08:00
parent b28d2d590f
commit 146e5d1a7e

View file

@ -1,4 +1,37 @@
// This is like very lame "type-checking" lol
const notificationTypeKeys = {
mention: ['account', 'status'],
status: ['account', 'status'],
reblog: ['account', 'status'],
follow: ['account'],
follow_request: ['account'],
favourite: ['account', 'status'],
poll: ['status'],
update: ['status'],
};
function fixNotifications(notifications) {
return notifications.filter((notification) => {
const { type, id, createdAt } = notification;
if (!type) {
console.warn('Notification missing type', notification);
return false;
}
if (!id || !createdAt) {
console.warn('Notification missing id or createdAt', notification);
// Continue processing this despite missing id or createdAt
}
const keys = notificationTypeKeys[type];
if (keys?.length) {
return keys.every((key) => !!notification[key]);
}
return true; // skip other types
});
}
function groupNotifications(notifications) {
// Filter out invalid notifications
notifications = fixNotifications(notifications);
// Create new flat list of notifications
// Combine sibling notifications based on type and status id
// Concat all notification.account into an array of _accounts
@ -7,7 +40,7 @@ function groupNotifications(notifications) {
for (let i = 0, j = 0; i < notifications.length; i++) {
const notification = notifications[i];
const { id, status, account, type, createdAt } = notification;
const date = new Date(createdAt).toLocaleDateString();
const date = createdAt ? new Date(createdAt).toLocaleDateString() : '';
let virtualType = type;
if (type === 'favourite' || type === 'reblog') {
virtualType = 'favourite+reblog';
@ -50,7 +83,7 @@ function groupNotifications(notifications) {
for (let i = 0, j = 0; i < cleanNotifications.length; i++) {
const notification = cleanNotifications[i];
const { id, account, _accounts, type, createdAt } = notification;
const date = new Date(createdAt).toLocaleDateString();
const date = createdAt ? new Date(createdAt).toLocaleDateString() : '';
if (type === 'favourite+reblog' && account && _accounts.length === 1) {
const key = `${account?.id}-${type}-${date}`;
const mappedNotification = notificationsMap2[key];