From 57b81bd6796ca4540351785affb7f19a632e195a Mon Sep 17 00:00:00 2001 From: Diego Sampaio Date: Wed, 26 Sep 2018 11:06:25 -0300 Subject: [PATCH] Fix duplicate emails on mentions --- .../server/functions/notifications/index.js | 4 +- .../server/lib/sendNotificationsOnMessage.js | 50 ++++++++++++------- .../server/models/Subscriptions.js | 4 +- 3 files changed, 37 insertions(+), 21 deletions(-) diff --git a/packages/rocketchat-lib/server/functions/notifications/index.js b/packages/rocketchat-lib/server/functions/notifications/index.js index c948bdbc4435..40fd2f03bb9b 100644 --- a/packages/rocketchat-lib/server/functions/notifications/index.js +++ b/packages/rocketchat-lib/server/functions/notifications/index.js @@ -58,9 +58,9 @@ export function messageContainsHighlight(message, highlights) { }); } -export function callJoinRoom(user, rid) { +export function callJoinRoom(userId, rid) { return new Promise((resolve, reject) => { - Meteor.runAsUser(user._id, () => Meteor.call('joinRoom', rid, (error, result) => { + Meteor.runAsUser(userId, () => Meteor.call('joinRoom', rid, (error, result) => { if (error) { return reject(error); } diff --git a/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js b/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js index 398207532816..345dc856c54a 100644 --- a/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js +++ b/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js @@ -243,23 +243,39 @@ function sendAllNotifications(message, room) { // on public channels, if a mentioned user is not member of the channel yet, he will first join the channel and then be notified based on his preferences. if (room.t === 'c') { - const mentions = message.mentions.filter(({ _id }) => _id !== 'here' && _id !== 'all').map(({ _id }) => _id); - Promise.all(RocketChat.models.Subscriptions.findByRoomIdAndUserIds(room._id, mentions) - .fetch() - .map(async(subscription) => { - await callJoinRoom(subscription.u, room._id); - return subscription; - })).then((subscriptions) => subscriptions.forEach((subscription) => - sendNotification({ - subscription, - sender, - hasMentionToAll, - hasMentionToHere, - message, - notificationMessage, - room, - mentionIds, - }))); + // get subscriptions from users already in room (to not send them a notification) + const mentions = [...mentionIdsWithoutGroups]; + RocketChat.models.Subscriptions.findByRoomIdAndUserIds(room._id, mentionIdsWithoutGroups, { fields: { 'u._id': 1 } }).forEach((subscription) => { + const index = mentions.indexOf(subscription.u._id); + if (index !== -1) { + mentions.splice(index, 1); + } + }); + + Promise.all(mentions + .map(async(userId) => { + await callJoinRoom(userId, room._id); + + return userId; + }) + ).then((users) => { + users.forEach((userId) => { + const subscription = RocketChat.models.Subscriptions.findOneByRoomIdAndUserId(room._id, userId); + + sendNotification({ + subscription, + sender, + hasMentionToAll, + hasMentionToHere, + message, + notificationMessage, + room, + mentionIds, + }); + }); + }).catch((error) => { + throw new Meteor.Error(error); + }); } return message; diff --git a/packages/rocketchat-lib/server/models/Subscriptions.js b/packages/rocketchat-lib/server/models/Subscriptions.js index 1315c1df25c0..0c413020da87 100644 --- a/packages/rocketchat-lib/server/models/Subscriptions.js +++ b/packages/rocketchat-lib/server/models/Subscriptions.js @@ -158,7 +158,7 @@ class ModelSubscriptions extends RocketChat.models._Base { return subscription && subscription.ls; } - findByRoomIdAndUserIds(roomId, userIds) { + findByRoomIdAndUserIds(roomId, userIds, options) { const query = { rid: roomId, 'u._id': { @@ -166,7 +166,7 @@ class ModelSubscriptions extends RocketChat.models._Base { }, }; - return this.find(query); + return this.find(query, options); } findByRoomIdAndUserIdsOrAllMessages(roomId, userIds) {