notifications: fixes
This commit is contained in:
@@ -4,6 +4,9 @@ import moment from 'moment';
|
||||
import Bluebird from 'bluebird';
|
||||
import baseModel from '../../libs/baseModel';
|
||||
import * as Tasks from '../task';
|
||||
import {
|
||||
model as UserNotification,
|
||||
} from '../userNotification';
|
||||
|
||||
import schema from './schema';
|
||||
|
||||
@@ -15,6 +18,8 @@ schema.plugin(baseModel, {
|
||||
plainObj._tmp = originalDoc._tmp; // be sure to send down drop notifs
|
||||
delete plainObj.filters;
|
||||
|
||||
plainObj.notifications = UserNotification.convertNotificationsToSafeJson(originalDoc.notifications);
|
||||
|
||||
return plainObj;
|
||||
},
|
||||
});
|
||||
@@ -255,7 +260,7 @@ schema.pre('save', true, function preSaveUser (next, done) {
|
||||
|
||||
// Sometimes there can be more than 1 notification
|
||||
const existingNotifications = this.notifications.filter(notification => {
|
||||
return notification.type === 'UNALLOCATED_STATS_POINTS';
|
||||
return notification && notification.type === 'UNALLOCATED_STATS_POINTS';
|
||||
});
|
||||
|
||||
const existingNotificationsLength = existingNotifications.length;
|
||||
@@ -277,7 +282,7 @@ schema.pre('save', true, function preSaveUser (next, done) {
|
||||
let notificationsRemoved = 0;
|
||||
|
||||
this.notifications = this.notifications.filter(notification => {
|
||||
if (notification.type !== 'UNALLOCATED_STATS_POINTS') return true;
|
||||
if (notification && notification.type !== 'UNALLOCATED_STATS_POINTS') return true;
|
||||
if (notificationsRemoved === notificationsToRemove) return true;
|
||||
|
||||
notificationsRemoved++;
|
||||
|
||||
@@ -163,10 +163,12 @@ schema.methods.addNotification = function addUserNotification (type, data = {},
|
||||
*/
|
||||
schema.statics.pushNotification = async function pushNotification (query, type, data = {}, seen = false) {
|
||||
let newNotification = new UserNotification({type, data, seen});
|
||||
|
||||
let validationResult = newNotification.validateSync();
|
||||
if (validationResult) {
|
||||
throw validationResult;
|
||||
}
|
||||
|
||||
await this.update(query, {$push: {notifications: newNotification.toObject()}}, {multi: true}).exec();
|
||||
};
|
||||
|
||||
|
||||
@@ -38,10 +38,14 @@ export let schema = new Schema({
|
||||
type: String,
|
||||
default: uuid,
|
||||
validate: [validator.isUUID, 'Invalid uuid.'],
|
||||
// required: true, // @TODO: Add these back once we figure out the issue with notifications
|
||||
// @TODO: Add these back once we figure out the issue with notifications
|
||||
// See Fix for https://github.com/HabitRPG/habitica/issues/9923
|
||||
// required: true,
|
||||
},
|
||||
type: {
|
||||
type: String,
|
||||
// @TODO: Add these back once we figure out the issue with notifications
|
||||
// See Fix for https://github.com/HabitRPG/habitica/issues/9923
|
||||
// required: true,
|
||||
enum: NOTIFICATION_TYPES,
|
||||
},
|
||||
@@ -60,6 +64,24 @@ export let schema = new Schema({
|
||||
_id: false, // use id instead of _id
|
||||
});
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
schema.statics.convertNotificationsToSafeJson = function convertNotificationsToSafeJson (notifications) {
|
||||
return 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;
|
||||
return true;
|
||||
}).map(n => {
|
||||
return n.toJSON();
|
||||
});
|
||||
};
|
||||
|
||||
schema.plugin(baseModel, {
|
||||
noSet: ['_id', 'id'],
|
||||
// timestamps: true, // Temporarily removed to debug a possible bug
|
||||
|
||||
Reference in New Issue
Block a user