mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-16 22:27:26 +01:00
prevent multiple notifications (#10524)
* WIP - prevent multiple notifications * merge promises to one * update test, iterate each user * revert changes in `groups.js` - filter duplicate notifications in `convertNotificationsToSafeJson`
This commit is contained in:
@@ -0,0 +1,39 @@
|
|||||||
|
import {
|
||||||
|
createAndPopulateGroup,
|
||||||
|
} from '../../../../helpers/api-integration/v3';
|
||||||
|
|
||||||
|
describe('Prevent multiple notifications', () => {
|
||||||
|
let partyLeader, partyMembers, party;
|
||||||
|
|
||||||
|
before(async () => {
|
||||||
|
let { group, groupLeader, members } = await createAndPopulateGroup({
|
||||||
|
groupDetails: {
|
||||||
|
type: 'party',
|
||||||
|
privacy: 'private',
|
||||||
|
},
|
||||||
|
members: 4,
|
||||||
|
});
|
||||||
|
|
||||||
|
party = group;
|
||||||
|
partyLeader = groupLeader;
|
||||||
|
partyMembers = members;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not add the same notification twice', async () => {
|
||||||
|
const multipleChatMessages = [];
|
||||||
|
|
||||||
|
for (let i = 0; i < 4; i++) {
|
||||||
|
for (let memberIndex = 0; memberIndex < partyMembers.length; memberIndex++) {
|
||||||
|
multipleChatMessages.push(
|
||||||
|
partyMembers[memberIndex].post(`/groups/${party._id}/chat`, { message: `Message ${i}_${memberIndex}`}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await Promise.all(multipleChatMessages);
|
||||||
|
|
||||||
|
const userWithNotification = await partyLeader.get('/user');
|
||||||
|
|
||||||
|
expect(userWithNotification.notifications.length).to.be.eq(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -7,8 +7,8 @@ import {
|
|||||||
model as Group,
|
model as Group,
|
||||||
} from '../group';
|
} from '../group';
|
||||||
|
|
||||||
import { defaults, map, flatten, flow, compact, uniq, partialRight } from 'lodash';
|
import {defaults, map, flatten, flow, compact, uniq, partialRight} from 'lodash';
|
||||||
import { model as UserNotification } from '../userNotification';
|
import {model as UserNotification} from '../userNotification';
|
||||||
import schema from './schema';
|
import schema from './schema';
|
||||||
import payments from '../../libs/payments/payments';
|
import payments from '../../libs/payments/payments';
|
||||||
import amazonPayments from '../../libs/payments/amazon';
|
import amazonPayments from '../../libs/payments/amazon';
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import mongoose from 'mongoose';
|
|||||||
import baseModel from '../libs/baseModel';
|
import baseModel from '../libs/baseModel';
|
||||||
import { v4 as uuid } from 'uuid';
|
import { v4 as uuid } from 'uuid';
|
||||||
import validator from 'validator';
|
import validator from 'validator';
|
||||||
|
import _ from 'lodash';
|
||||||
|
|
||||||
const NOTIFICATION_TYPES = [
|
const NOTIFICATION_TYPES = [
|
||||||
'DROPS_ENABLED',
|
'DROPS_ENABLED',
|
||||||
@@ -74,13 +75,22 @@ export let schema = new Schema({
|
|||||||
schema.statics.convertNotificationsToSafeJson = function convertNotificationsToSafeJson (notifications) {
|
schema.statics.convertNotificationsToSafeJson = function convertNotificationsToSafeJson (notifications) {
|
||||||
if (!notifications) return notifications;
|
if (!notifications) return notifications;
|
||||||
|
|
||||||
return notifications.filter(n => {
|
let filteredNotifications = notifications.filter(n => {
|
||||||
// Exclude notifications with a nullish value
|
// Exclude notifications with a nullish value
|
||||||
if (!n) return false;
|
if (!n) return false;
|
||||||
// Exclude notifications without an id or a type
|
// Exclude notifications without an id or a type
|
||||||
if (!n.id || !n.type) return false;
|
if (!n.id || !n.type) return false;
|
||||||
return true;
|
return true;
|
||||||
}).map(n => {
|
});
|
||||||
|
|
||||||
|
filteredNotifications = _.uniqWith(filteredNotifications, (val, otherVal) => {
|
||||||
|
if (val.type === otherVal.type && val.type === 'NEW_CHAT_MESSAGE') {
|
||||||
|
return val.data.group.id === otherVal.data.group.id;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
|
||||||
|
return filteredNotifications.map(n => {
|
||||||
return n.toJSON();
|
return n.toJSON();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user