From 2d3f2500e8a9f5588c43e9006e7f0f3c2724acd3 Mon Sep 17 00:00:00 2001 From: Phillip Thelen Date: Tue, 1 Oct 2019 13:26:38 +0200 Subject: [PATCH] Improve handling for sending mention notifications --- website/server/controllers/api-v3/chat.js | 31 ++++++----------------- website/server/libs/chat.js | 7 +++-- website/server/models/group.js | 25 ++++++++++++++++-- 3 files changed, 36 insertions(+), 27 deletions(-) diff --git a/website/server/controllers/api-v3/chat.js b/website/server/controllers/api-v3/chat.js index 5f38dadee5..ba2312bae3 100644 --- a/website/server/controllers/api-v3/chat.js +++ b/website/server/controllers/api-v3/chat.js @@ -224,7 +224,14 @@ api.postChat = { }); } - const newChatMessage = group.sendChat({message: req.body.message, user, flagCount, metaData: null, client, translate: res.t}); + const newChatMessage = group.sendChat({message: req.body.message, + user, + flagCount, + metaData: null, + client, + translate: res.t, + mentions, + mentionedMembers}); let toSave = [newChatMessage.save()]; if (group.type === 'party') { @@ -232,28 +239,6 @@ api.postChat = { toSave.push(user.save()); } - mentionedMembers.forEach((member) => { - if (member._id === user._id) return; - const pushNotifPrefs = member.preferences.pushNotifications; - if (group.type === 'party') { - if (pushNotifPrefs.mentionParty !== true) { - return; - } - } else if (member.guilds.contains(group._id)) { - if (pushNotifPrefs.mentionJoinedGuild !== true) { - return; - } - } else { - if (group.privacy !== 'public') { - return; - } - if (pushNotifPrefs.mentionUnjoinedGuild !== true) { - return; - } - } - sendNotification(member, {identifier: 'chatMention', title: `${user.profile.name} mentioned you in ${group.name}`, message: req.body.message}); - }); - await Promise.all(toSave); diff --git a/website/server/libs/chat.js b/website/server/libs/chat.js index a19e9b260c..0e27535099 100644 --- a/website/server/libs/chat.js +++ b/website/server/libs/chat.js @@ -18,15 +18,18 @@ export async function getAuthorEmailFromMessage (message) { } } -export async function sendChatPushNotifications (user, group, message, translate) { +export async function sendChatPushNotifications (user, group, message, mentions, translate) { let members = await User.find({ 'party._id': group._id, _id: {$ne: user._id}, }) - .select('preferences.pushNotifications preferences.language profile.name pushDevices') + .select('preferences.pushNotifications preferences.language profile.name pushDevices auth.local.username') .exec(); members.forEach(member => { if (member.preferences.pushNotifications.partyActivity !== false) { + if (mentions && mentions.includes(`@${member.auth.local.username}`) && member.preferences.pushNotifications.mentionParty !== false) { + return; + } sendPushNotification( member, { diff --git a/website/server/models/group.js b/website/server/models/group.js index f7325bc636..f577218a7a 100644 --- a/website/server/models/group.js +++ b/website/server/models/group.js @@ -513,7 +513,7 @@ schema.methods.getMemberCount = async function getMemberCount () { }; schema.methods.sendChat = function sendChat (options = {}) { - const {message, user, metaData, client, flagCount = 0, info = {}, translate} = options; + const {message, user, metaData, client, flagCount = 0, info = {}, translate, mentions, mentionedMembers} = options; let newMessage = messageDefaults(message, user, client, flagCount, info); let newChatMessage = new Chat(); newChatMessage = Object.assign(newChatMessage, newMessage); @@ -576,8 +576,29 @@ schema.methods.sendChat = function sendChat (options = {}) { }); if (this.type === 'party' && user) { - sendChatPushNotifications(user, this, newChatMessage, translate); + sendChatPushNotifications(user, this, newChatMessage, mentions, translate); } + mentionedMembers.forEach((member) => { + if (member._id === user._id) return; + const pushNotifPrefs = member.preferences.pushNotifications; + if (this.type === 'party') { + if (pushNotifPrefs.mentionParty !== true) { + return; + } + } else if (member.guilds && member.guilds.includes(this._id)) { + if (pushNotifPrefs.mentionJoinedGuild !== true) { + return; + } + } else { + if (this.privacy !== 'public') { + return; + } + if (pushNotifPrefs.mentionUnjoinedGuild !== true) { + return; + } + } + sendPushNotification(member, {identifier: 'chatMention', title: `${user.profile.name} mentioned you in ${this.name}`, message}); + }); return newChatMessage; };