From 4ffce011ed304c04ea75d0a6ff1690de542227a1 Mon Sep 17 00:00:00 2001 From: Scott Talley Date: Wed, 2 Aug 2017 12:29:08 -0400 Subject: [PATCH 1/3] Mobile notify always setting added in Admin->General->Notifications --- packages/rocketchat-i18n/i18n/en.i18n.json | 2 ++ .../rocketchat-lib/server/lib/sendNotificationsOnMessage.js | 6 +++++- packages/rocketchat-lib/server/startup/settings.js | 6 ++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/rocketchat-i18n/i18n/en.i18n.json b/packages/rocketchat-i18n/i18n/en.i18n.json index 8f89a01f3d01..cf9d0af14ba6 100644 --- a/packages/rocketchat-i18n/i18n/en.i18n.json +++ b/packages/rocketchat-i18n/i18n/en.i18n.json @@ -1111,6 +1111,8 @@ "Notification_Mobile_Default_For": "Push Mobile Notifications For", "Notification_Duration": "Notification Duration", "Notifications": "Notifications", + "Notifications_Always_Notify_Mobile" : "Always notify mobile", + "Notifications_Always_Notify_Mobile_Description" : "Choose to always notify mobile device regardless of presence status.", "Notifications_Muted_Description": "If you choose to mute everything, you won't see the room highlight in the list when there are new messages, except for mentions. Muting notifications will override notifications settings.", "Notifications_Max_Room_Members": "Max room members before disabling all message notifications", "Notifications_Max_Room_Members_Description": "Max number of members in room when notifications for all messages gets disabled. Users can still change per room setting to receive all notifications on an individual basis. (0 to disable)", diff --git a/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js b/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js index 7f4479d86c7b..afa9b129d0b4 100644 --- a/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js +++ b/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js @@ -189,6 +189,7 @@ RocketChat.callbacks.add('afterSaveMessage', function(message, room, userId) { let userIdsToPushNotify = []; const usersWithHighlights = []; + const alwaysNotifyMobileBoolean = RocketChat.settings.get('Notifications_Always_Notify_Mobile'); const highlights = RocketChat.models.Users.findUsersByUsernamesWithHighlights(room.usernames, { fields: { '_id': 1, 'settings.preferences.highlights': 1 }}).fetch(); highlights.forEach(function(user) { @@ -233,7 +234,7 @@ RocketChat.callbacks.add('afterSaveMessage', function(message, room, userId) { } if ((userOfMention != null) && canBeNotified(userOfMentionId, 'desktop')) { - if (Push.enabled === true && userOfMention.statusConnection !== 'online') { + if (Push.enabled === true && userOfMention.statusConnection !== 'online' || alwaysNotifyMobileBoolean === true) { RocketChat.PushNotification.send({ roomId: message.rid, username: push_username, @@ -325,6 +326,9 @@ RocketChat.callbacks.add('afterSaveMessage', function(message, room, userId) { } userIdsToPushNotify = _.pluck(_.filter(usersOfMobileMentions, function(user) { + if (alwaysNotifyMobileBoolean === true) { + return true; + } return user.statusConnection !== 'online'; }), '_id'); } diff --git a/packages/rocketchat-lib/server/startup/settings.js b/packages/rocketchat-lib/server/startup/settings.js index 420c1db656c2..632f9b81e9af 100644 --- a/packages/rocketchat-lib/server/startup/settings.js +++ b/packages/rocketchat-lib/server/startup/settings.js @@ -455,6 +455,12 @@ RocketChat.settings.addGroup('General', function() { public: true, i18nDescription: 'Notifications_Max_Room_Members_Description' }); + + this.add('Notifications_Always_Notify_Mobile', false, { + type: 'boolean', + public: true, + i18nDescription: 'Notifications_Always_Notify_Mobile_Description' + }); }); this.section('REST API', function() { return this.add('API_User_Limit', 500, { From b69d84d0c746b62178644384f41a5e04c5e13928 Mon Sep 17 00:00:00 2001 From: Scott Talley Date: Wed, 27 Sep 2017 21:15:04 -0400 Subject: [PATCH 2/3] Cleaned up Linting errors --- .../server/lib/sendNotificationsOnMessage.js | 67 ++++++++++--------- 1 file changed, 35 insertions(+), 32 deletions(-) diff --git a/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js b/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js index 9563c70534a8..7e6350a85afa 100644 --- a/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js +++ b/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js @@ -235,6 +235,7 @@ RocketChat.callbacks.add('afterSaveMessage', function(message, room, userId) { let userIdsToNotify = []; let userIdsToPushNotify = []; const mentions = []; + const alwaysNotifyMobileBoolean = RocketChat.settings.get('Notifications_Always_Notify_Mobile'); const usersWithHighlights = RocketChat.models.Users.findUsersByUsernamesWithHighlights(room.usernames, { fields: { '_id': 1, 'settings.preferences.highlights': 1 }}).fetch() .filter(user => messageContainsHighlight(message, user.settings.preferences.highlights)); @@ -275,7 +276,7 @@ RocketChat.callbacks.add('afterSaveMessage', function(message, room, userId) { } if ((userOfMention != null) && canBeNotified(userOfMentionId, 'desktop')) { - if (Push.enabled === true && (userOfMention.statusConnection !== 'online' || alwaysNotifyMobileBoolean === true)) { + if (Push.enabled === true && (userOfMention.statusConnection !== 'online' || alwaysNotifyMobileBoolean === true)) { RocketChat.PushNotification.send({ roomId: message.rid, username: push_username, @@ -300,6 +301,8 @@ RocketChat.callbacks.add('afterSaveMessage', function(message, room, userId) { const mentionIds = (message.mentions || []).map(({_id}) => _id); const toAll = mentionIds.includes('all'); const toHere = mentionIds.includes('here'); + let usersOfMobileMentions = []; + if (mentionIds.length + settings.alwaysNotifyDesktopUsers.length > 0) { let desktopMentionIds = _.union(mentionIds, settings.alwaysNotifyDesktopUsers); desktopMentionIds = _.difference(desktopMentionIds, settings.dontNotifyDesktopUsers); @@ -329,36 +332,36 @@ RocketChat.callbacks.add('afterSaveMessage', function(message, room, userId) { let mobileMentionIds = _.union(mentionIds, settings.alwaysNotifyMobileUsers); mobileMentionIds = _.difference(mobileMentionIds, settings.dontNotifyMobileUsers); - if (alwaysNotifyMobileBoolean === true){ - let usersOfMobileMentions = RocketChat.models.Users.find({ - _id: { - $in: mobileMentionIds - } - }, { - fields: { - _id: 1, - username: 1, - statusConnection: 1, - active: 1 - } - }).fetch(); - }else{ - let usersOfMobileMentions = RocketChat.models.Users.find({ - _id: { - $in: mobileMentionIds - }, - statusConnection: { - $ne: 'online' - } - }, { - fields: { - _id: 1, - username: 1, - statusConnection: 1, - active: 1 - } - }).fetch(); - } + if (alwaysNotifyMobileBoolean === true) { + usersOfMobileMentions = RocketChat.models.Users.find({ + _id: { + $in: mobileMentionIds + } + }, { + fields: { + _id: 1, + username: 1, + statusConnection: 1, + active: 1 + } + }).fetch(); + } else { + usersOfMobileMentions = RocketChat.models.Users.find({ + _id: { + $in: mobileMentionIds + }, + statusConnection: { + $ne: 'online' + } + }, { + fields: { + _id: 1, + username: 1, + statusConnection: 1, + active: 1 + } + }).fetch(); + } mentions.push(...usersOfMobileMentions); if (room.t !== 'c') { usersOfMobileMentions = _.reject(usersOfMobileMentions, usersOfMentionItem => !room.usernames.includes(usersOfMentionItem.username)); @@ -448,4 +451,4 @@ RocketChat.callbacks.add('afterSaveMessage', function(message, room, userId) { return message; -}, RocketChat.callbacks.priority.LOW, 'sendNotificationOnMessage'); \ No newline at end of file +}, RocketChat.callbacks.priority.LOW, 'sendNotificationOnMessage'); From 3b6ca5ec2108f47c2280c01b7c8b9ad53733fb67 Mon Sep 17 00:00:00 2001 From: Rodrigo Nascimento Date: Thu, 7 Dec 2017 15:34:41 -0200 Subject: [PATCH 3/3] Update sendNotificationsOnMessage.js --- .../server/lib/sendNotificationsOnMessage.js | 48 +++++++------------ 1 file changed, 18 insertions(+), 30 deletions(-) diff --git a/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js b/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js index d85d003f4074..70e6dda5eb85 100644 --- a/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js +++ b/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js @@ -316,7 +316,6 @@ RocketChat.callbacks.add('afterSaveMessage', function(message, room, userId) { const mentionIds = (message.mentions || []).map(({_id}) => _id); const toAll = mentionIds.includes('all'); const toHere = mentionIds.includes('here'); - let usersOfMobileMentions = []; if (mentionIds.length + settings.alwaysNotifyDesktopUsers.length > 0) { let desktopMentionIds = _.union(mentionIds, settings.alwaysNotifyDesktopUsers); @@ -347,36 +346,25 @@ RocketChat.callbacks.add('afterSaveMessage', function(message, room, userId) { let mobileMentionIds = _.union(mentionIds, settings.alwaysNotifyMobileUsers); mobileMentionIds = _.difference(mobileMentionIds, settings.dontNotifyMobileUsers); - if (alwaysNotifyMobileBoolean === true) { - usersOfMobileMentions = RocketChat.models.Users.find({ - _id: { - $in: mobileMentionIds - } - }, { - fields: { - _id: 1, - username: 1, - statusConnection: 1, - active: 1 - } - }).fetch(); - } else { - usersOfMobileMentions = RocketChat.models.Users.find({ - _id: { - $in: mobileMentionIds - }, - statusConnection: { - $ne: 'online' - } - }, { - fields: { - _id: 1, - username: 1, - statusConnection: 1, - active: 1 - } - }).fetch(); + const usersOfMobileMentionsQuery = { + _id: { + $in: mobileMentionIds + } + }; + + if (alwaysNotifyMobileBoolean !== true) { + usersOfMobileMentionsQuery.statusConnection = { $ne: 'online' }; } + + let usersOfMobileMentions = RocketChat.models.Users.find(usersOfMobileMentionsQuery, { + fields: { + _id: 1, + username: 1, + statusConnection: 1, + active: 1 + } + }).fetch(); + mentions.push(...usersOfMobileMentions); if (room.t !== 'c') { usersOfMobileMentions = _.reject(usersOfMobileMentions, usersOfMentionItem => !room.usernames.includes(usersOfMentionItem.username));