mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 15:17:25 +01:00
refactor notifications cleanup
This commit is contained in:
@@ -88,30 +88,30 @@ export const schema = new Schema({
|
||||
});
|
||||
|
||||
/**
|
||||
* Convert notifications to JSON making sure to return only valid data.
|
||||
* Fix for https://github.com/HabitRPG/habitica/issues/9923#issuecomment-362869881
|
||||
* @TODO Remove once https://github.com/HabitRPG/habitica/issues/9923
|
||||
* is fixed
|
||||
* Remove invalid data from an array of notifications.
|
||||
* Fix for https://github.com/HabitRPG/habitica/issues/9923
|
||||
* Called by user's post init hook (models/user/hooks.js)
|
||||
*/
|
||||
schema.statics.convertNotificationsToSafeJson = function convNotifsToSafeJson (notifications) {
|
||||
schema.statics.cleanupCorruptData = function cleanupCorruptNotificationsData (notifications) {
|
||||
if (!notifications) return notifications;
|
||||
|
||||
let filteredNotifications = notifications.filter(n => {
|
||||
// Exclude notifications with a nullish value
|
||||
if (!n) return false;
|
||||
// Exclude notifications without an id or a type
|
||||
if (!n.id || !n.type) return false;
|
||||
let filteredNotifications = notifications.filter(notification => {
|
||||
// Exclude notifications with a nullish value, no id or no type
|
||||
if (!notification || !notification.id || !notification.type) return false;
|
||||
return true;
|
||||
});
|
||||
|
||||
// Remove duplicate NEW_CHAT_MESSAGES notifications
|
||||
// can be caused by a race condition when adding a new notification of this type
|
||||
// in group.sendChat if two messages are posted at the same time
|
||||
filteredNotifications = _.uniqWith(filteredNotifications, (val, otherVal) => {
|
||||
if (val.type === otherVal.type && val.type === 'NEW_CHAT_MESSAGE') {
|
||||
if (val.type === 'NEW_CHAT_MESSAGE' && val.type === otherVal.type) {
|
||||
return val.data.group.id === otherVal.data.group.id;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
return filteredNotifications.map(n => n.toJSON());
|
||||
return filteredNotifications;
|
||||
};
|
||||
|
||||
schema.plugin(baseModel, {
|
||||
|
||||
Reference in New Issue
Block a user