notifications: fixes

This commit is contained in:
Matteo Pagliazzi
2018-02-04 13:28:05 +01:00
parent 2af99d7c65
commit 4efbbd7bac
15 changed files with 118 additions and 37 deletions

View File

@@ -502,7 +502,7 @@ api.seenChat = {
// Remove from response
user.notifications = user.notifications.filter(n => {
if (n.type === 'NEW_CHAT_MESSAGE' && n.data.group.id === groupId) {
if (n && n.type === 'NEW_CHAT_MESSAGE' && n.data.group.id === groupId) {
return false;
}

View File

@@ -703,7 +703,7 @@ function _removeMessagesFromMember (member, groupId) {
}
member.notifications = member.notifications.filter(n => {
if (n.type === 'NEW_CHAT_MESSAGE' && n.data.group && n.data.group.id === groupId) {
if (n && n.type === 'NEW_CHAT_MESSAGE' && n.date && n.data.group && n.data.group.id === groupId) {
return false;
}
@@ -1277,7 +1277,7 @@ api.removeGroupManager = {
let manager = await User.findById(managerId, 'notifications').exec();
let newNotifications = manager.notifications.filter((notification) => {
const isGroupTaskNotification = notification.type.indexOf('GROUP_TASK_') === 0;
const isGroupTaskNotification = notification && notification.type && notification.type.indexOf('GROUP_TASK_') === 0;
return !isGroupTaskNotification;
});

View File

@@ -92,7 +92,7 @@ api.tellMeLaterNews = {
user.flags.newStuff = false;
const existingNotificationIndex = user.notifications.findIndex(n => {
return n.type === 'NEW_STUFF';
return n && n.type === 'NEW_STUFF';
});
if (existingNotificationIndex !== -1) user.notifications.splice(existingNotificationIndex, 1);
user.addNotification('NEW_STUFF', { title: LAST_ANNOUNCEMENT_TITLE }, true); // seen by default

View File

@@ -5,6 +5,9 @@ import {
import {
model as User,
} from '../../models/user';
import {
model as UserNotification,
} from '../../models/userNotification';
let api = {};
@@ -30,7 +33,7 @@ api.readNotification = {
if (validationErrors) throw validationErrors;
const index = user.notifications.findIndex(n => {
return n.id === req.params.notificationId;
return n && n.id === req.params.notificationId;
});
if (index === -1) {
@@ -48,7 +51,7 @@ api.readNotification = {
$pull: { notifications: { id: req.params.notificationId } },
}).exec();
res.respond(200, user.notifications);
res.respond(200, UserNotification.convertNotificationsToSafeJson(user.notifications));
},
};
@@ -74,7 +77,7 @@ api.readNotifications = {
let notificationsIds = req.body.notificationIds;
for (let notificationId of notificationsIds) {
const index = user.notifications.findIndex(n => {
return n.id === notificationId;
return n && n.id === notificationId;
});
if (index === -1) {
@@ -93,7 +96,7 @@ api.readNotifications = {
// See https://github.com/HabitRPG/habitica/pull/9321#issuecomment-354187666 for more info
user._v++;
res.respond(200, user.notifications);
res.respond(200, UserNotification.convertNotificationsToSafeJson(user.notifications));
},
};
@@ -122,7 +125,7 @@ api.seeNotification = {
const notificationId = req.params.notificationId;
const notification = user.notifications.find(n => {
return n.id === notificationId;
return n && n.id === notificationId;
});
if (!notification) {
@@ -172,7 +175,7 @@ api.seeNotifications = {
for (let notificationId of notificationsIds) {
const notification = user.notifications.find(n => {
return n.id === notificationId;
return n && n.id === notificationId;
});
if (!notification) {
@@ -184,7 +187,7 @@ api.seeNotifications = {
await user.save();
res.respond(200, user.notifications);
res.respond(200, UserNotification.convertNotificationsToSafeJson(user.notifications));
},
};

View File

@@ -1,4 +1,3 @@
import findIndex from 'lodash/findIndex';
import { authWithHeaders } from '../../../middlewares/auth';
import Bluebird from 'bluebird';
import * as Tasks from '../../../models/task';
@@ -316,8 +315,8 @@ api.approveTask = {
// Get task direction
const firstManagerNotifications = managers[0].notifications;
const firstNotificationIndex = findIndex(firstManagerNotifications, (notification) => {
return notification.data.taskId === task._id && notification.type === 'GROUP_TASK_APPROVAL';
const firstNotificationIndex = firstManagerNotifications.findIndex((notification) => {
return notification && notification.data && notification.data.taskId === task._id && notification.type === 'GROUP_TASK_APPROVAL';
});
let direction = 'up';
if (firstManagerNotifications[firstNotificationIndex]) {
@@ -327,8 +326,8 @@ api.approveTask = {
// Remove old notifications
let managerPromises = [];
managers.forEach((manager) => {
let notificationIndex = findIndex(manager.notifications, function findNotification (notification) {
return notification.data.taskId === task._id && notification.type === 'GROUP_TASK_APPROVAL';
let notificationIndex = manager.notifications.findIndex(function findNotification (notification) {
return notification && notification.data && notification.data.taskId === task._id && notification.type === 'GROUP_TASK_APPROVAL';
});
if (notificationIndex !== -1) {
@@ -416,8 +415,8 @@ api.taskNeedsWork = {
// Remove old notifications
managers.forEach((manager) => {
let notificationIndex = findIndex(manager.notifications, function findNotification (notification) {
return notification.data.taskId === task._id && notification.type === 'GROUP_TASK_APPROVAL';
let notificationIndex = manager.notifications.findIndex(function findNotification (notification) {
return notification && notification.data && notification.data.taskId === task._id && notification.type === 'GROUP_TASK_APPROVAL';
});
if (notificationIndex !== -1) {