refactor notifications cleanup

This commit is contained in:
Matteo Pagliazzi
2020-03-01 20:06:24 +01:00
parent 343f276705
commit 118e3580d6
3 changed files with 51 additions and 23 deletions

View File

@@ -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, {