From b9567f1b80cba8d89982fd3c4fe5c0810e2cec83 Mon Sep 17 00:00:00 2001 From: Alex Brazier Date: Sat, 17 Sep 2016 08:49:58 +0100 Subject: [PATCH 01/12] Add setting to set default notification alert level --- packages/rocketchat-i18n/i18n/en.i18n.json | 3 +- .../server/lib/sendNotificationsOnMessage.js | 14 ++++++--- .../server/startup/settings.coffee | 29 +++++++++++++++++ .../views/pushNotificationsFlexTab.html | 6 ++-- .../client/views/pushNotificationsFlexTab.js | 10 ++---- .../server/models/Subscriptions.js | 31 +++++++++---------- 6 files changed, 63 insertions(+), 30 deletions(-) diff --git a/packages/rocketchat-i18n/i18n/en.i18n.json b/packages/rocketchat-i18n/i18n/en.i18n.json index 4ab277ca795d..9be658da6722 100644 --- a/packages/rocketchat-i18n/i18n/en.i18n.json +++ b/packages/rocketchat-i18n/i18n/en.i18n.json @@ -317,6 +317,7 @@ "Desktop" : "Desktop", "Desktop_Notification_Test" : "Desktop Notification Test", "Desktop_Notifications" : "Desktop Notifications", + "Desktop_Notifications_Default_Alert" : "Desktop Notifications Default Alert", "Desktop_Notifications_Disabled" : "Desktop Notifications are Disabled. Change your browser preferences if you need Notifications enabled.", "Desktop_Notifications_Duration" : "Desktop Notifications Duration", "Desktop_Notifications_Duration_Description" : "Seconds to display desktop notification. This may affect OS X Notification Center. Enter 0 to use default browser settings and not affect OS X Notification Center.", @@ -741,7 +742,6 @@ "Max_length_is" : "Max length is %s", "Members_List" : "Members List", "Mentions" : "Mentions", - "Mentions_default" : "Mentions (default)", "Message" : "Message", "Message_AllowBadWordsFilter" : "Allow Message bad words filtering", "Message_AllowDeleting" : "Allow Message Deleting", @@ -792,6 +792,7 @@ "Min_length_is" : "Min length is %s", "minutes" : "minutes", "Mobile_push" : "Mobile push", + "Mobile_Notifications_Default_Alert" : "Mobile Notifications Default Alert", "Monday" : "Monday", "Monitor_history_for_changes_on" : "Monitor history for changes on", "More_channels" : "More channels", diff --git a/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js b/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js index 0a9724c8e968..61822252739c 100644 --- a/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js +++ b/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js @@ -74,14 +74,20 @@ RocketChat.callbacks.add('afterSaveMessage', function(message, room) { settings.dontNotifyMobileUsers = []; settings.desktopNotificationDurations = {}; RocketChat.models.Subscriptions.findNotificationPreferencesByRoom(room._id).forEach(function(subscription) { - if (subscription.desktopNotifications === 'all') { + // Set defaults if they don't exist + let { + desktopNotifications = RocketChat.settings.get('Desktop_Notifications_Default_Alert'), + mobilePushNotifications = RocketChat.settings.get('Mobile_Notifications_Default_Alert') + } = subscription; + + if (desktopNotifications === 'all') { settings.alwaysNotifyDesktopUsers.push(subscription.u._id); - } else if (subscription.desktopNotifications === 'nothing') { + } else if (desktopNotifications === 'nothing') { settings.dontNotifyDesktopUsers.push(subscription.u._id); } - if (subscription.mobilePushNotifications === 'all') { + if (mobilePushNotifications === 'all') { settings.alwaysNotifyMobileUsers.push(subscription.u._id); - } else if (subscription.mobilePushNotifications === 'nothing') { + } else if (mobilePushNotifications === 'nothing') { settings.dontNotifyMobileUsers.push(subscription.u._id); } settings.desktopNotificationDurations[subscription.u._id] = subscription.desktopNotificationDuration; diff --git a/packages/rocketchat-lib/server/startup/settings.coffee b/packages/rocketchat-lib/server/startup/settings.coffee index 391cf8cf7b3b..b2b187a4bca1 100644 --- a/packages/rocketchat-lib/server/startup/settings.coffee +++ b/packages/rocketchat-lib/server/startup/settings.coffee @@ -109,6 +109,35 @@ RocketChat.settings.addGroup 'General', -> @section 'Notifications', -> @add 'Desktop_Notifications_Duration', 0, { type: 'int', public: true, i18nDescription: 'Desktop_Notification_Durations_Description' } + @add 'Desktop_Notifications_Default_Alert', 'all', { + type: 'select', + values: [{ + key: 'all', + i18nLabel: 'All_messages' + }, { + key: 'mentions', + i18nLabel: 'Mentions' + }, { + key: 'nothing', + i18nLabel: 'Nothing' + }], + public: true + } + + @add 'Mobile_Notifications_Default_Alert', 'mentions', { + type: 'select', + values: [{ + key: 'all', + i18nLabel: 'All_messages' + }, { + key: 'mentions', + i18nLabel: 'Mentions' + }, { + key: 'nothing', + i18nLabel: 'Nothing' + }], + public: true + } @section 'REST API', -> @add 'API_User_Limit', 500, { type: 'int', public: true, i18nDescription: 'API_User_Limit' } diff --git a/packages/rocketchat-push-notifications/client/views/pushNotificationsFlexTab.html b/packages/rocketchat-push-notifications/client/views/pushNotificationsFlexTab.html index ba6bf8ef28be..7bf11ab5c031 100644 --- a/packages/rocketchat-push-notifications/client/views/pushNotificationsFlexTab.html +++ b/packages/rocketchat-push-notifications/client/views/pushNotificationsFlexTab.html @@ -10,8 +10,9 @@

{{_ "Notifications"}}

{{#if editing 'desktopNotifications'}} + - +
{{#if desktopNotificationDuration}} @@ -39,8 +40,9 @@

{{_ "Notifications"}}

{{#if editing 'mobilePushNotifications'}} + - + diff --git a/packages/rocketchat-push-notifications/client/views/pushNotificationsFlexTab.js b/packages/rocketchat-push-notifications/client/views/pushNotificationsFlexTab.js index 3133cb92f2ce..c660d0bca632 100644 --- a/packages/rocketchat-push-notifications/client/views/pushNotificationsFlexTab.js +++ b/packages/rocketchat-push-notifications/client/views/pushNotificationsFlexTab.js @@ -9,7 +9,7 @@ Template.pushNotificationsFlexTab.helpers({ desktopNotifications: 1 } }); - return sub ? sub.desktopNotifications : ''; + return sub ? sub.desktopNotifications || 'default' : 'default'; }, mobilePushNotifications() { var sub = ChatSubscription.findOne({ @@ -19,7 +19,7 @@ Template.pushNotificationsFlexTab.helpers({ mobilePushNotifications: 1 } }); - return sub ? sub.mobilePushNotifications : ''; + return sub ? sub.mobilePushNotifications || 'default' : 'default'; }, emailNotifications() { var sub = ChatSubscription.findOne({ @@ -89,11 +89,7 @@ Template.pushNotificationsFlexTab.helpers({ case 'mentions': return t('Mentions'); default: - if (field === 'emailNotifications') { - return t('Use_account_preference'); - } else { - return t('Mentions'); - } + return t('Use_account_preference'); } } }, diff --git a/packages/rocketchat-push-notifications/server/models/Subscriptions.js b/packages/rocketchat-push-notifications/server/models/Subscriptions.js index 94827344dda3..ea7b7e2f171f 100644 --- a/packages/rocketchat-push-notifications/server/models/Subscriptions.js +++ b/packages/rocketchat-push-notifications/server/models/Subscriptions.js @@ -3,11 +3,13 @@ RocketChat.models.Subscriptions.updateDesktopNotificationsById = function(_id, d _id: _id }; - const update = { - $set: { - desktopNotifications: desktopNotifications - } - }; + const update = {}; + + if (desktopNotifications === 'default') { + update.$unset = { desktopNotifications: 1 } + } else { + update.$set = { desktopNotifications: desktopNotifications } + } return this.update(query, update); }; @@ -31,11 +33,13 @@ RocketChat.models.Subscriptions.updateMobilePushNotificationsById = function(_id _id: _id }; - const update = { - $set: { - mobilePushNotifications: mobilePushNotifications - } - }; + const update = {}; + + if (mobilePushNotifications === 'default') { + update.$unset = { mobilePushNotifications: 1 } + } else { + update.$set = { mobilePushNotifications: mobilePushNotifications } + } return this.update(query, update); }; @@ -107,12 +111,7 @@ RocketChat.models.Subscriptions.findDontNotifyMobileUsersByRoomId = function(roo RocketChat.models.Subscriptions.findNotificationPreferencesByRoom = function(roomId) { const query = { rid: roomId, - 'u._id': {$exists: true}, - $or: [ - {desktopNotifications: {$exists: true}}, - {desktopNotificationDuration: {$exists: true}}, - {mobilePushNotifications: {$exists: true}} - ] + 'u._id': {$exists: true} }; return this.find(query); From a37e0d99c7f25f735e6654c7ba9c72271bf0cc10 Mon Sep 17 00:00:00 2001 From: Alex Brazier Date: Sat, 17 Sep 2016 11:00:09 +0100 Subject: [PATCH 02/12] Add user preference for notifications --- packages/rocketchat-i18n/i18n/en.i18n.json | 1 + .../server/lib/sendNotificationsOnMessage.js | 5 ++++- .../account/accountPreferences.coffee | 1 + .../account/accountPreferences.html | 11 +++++++++++ server/methods/saveUserPreferences.coffee | 3 +++ 5 files changed, 20 insertions(+), 1 deletion(-) diff --git a/packages/rocketchat-i18n/i18n/en.i18n.json b/packages/rocketchat-i18n/i18n/en.i18n.json index 9be658da6722..186a11867582 100644 --- a/packages/rocketchat-i18n/i18n/en.i18n.json +++ b/packages/rocketchat-i18n/i18n/en.i18n.json @@ -844,6 +844,7 @@ "Not_found_or_not_allowed" : "Not Found or Not Allowed", "Nothing" : "Nothing", "Nothing_found" : "Nothing found", + "Notification_Desktop_Default_For" : "Desktop Notifications For", "Notification_Duration" : "Notification Duration", "Notifications" : "Notifications", "Notify_all_in_this_room" : "Notify all in this room", diff --git a/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js b/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js index 61822252739c..092b8630dad1 100644 --- a/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js +++ b/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js @@ -74,9 +74,12 @@ RocketChat.callbacks.add('afterSaveMessage', function(message, room) { settings.dontNotifyMobileUsers = []; settings.desktopNotificationDurations = {}; RocketChat.models.Subscriptions.findNotificationPreferencesByRoom(room._id).forEach(function(subscription) { + let currentUser = RocketChat.models.Users.findOneById(subscription.u._id); + let preferences = currentUser.settings ? currentUser.settings.preferences || {} : {}; + let userDesktopNotificationPreference = preferences.desktopNotifications !== 'default' ? preferences.desktopNotifications : undefined; // Set defaults if they don't exist let { - desktopNotifications = RocketChat.settings.get('Desktop_Notifications_Default_Alert'), + desktopNotifications = userDesktopNotificationPreference || RocketChat.settings.get('Desktop_Notifications_Default_Alert'), mobilePushNotifications = RocketChat.settings.get('Mobile_Notifications_Default_Alert') } = subscription; diff --git a/packages/rocketchat-ui-account/account/accountPreferences.coffee b/packages/rocketchat-ui-account/account/accountPreferences.coffee index 9b1ff3c984e5..a0862d81be4d 100644 --- a/packages/rocketchat-ui-account/account/accountPreferences.coffee +++ b/packages/rocketchat-ui-account/account/accountPreferences.coffee @@ -81,6 +81,7 @@ Template.accountPreferences.onCreated -> data.emailNotificationMode = $('select[name=emailNotificationMode]').val() data.highlights = _.compact(_.map($('[name=highlights]').val().split(','), (e) -> return _.trim(e))) data.desktopNotificationDuration = $('input[name=desktopNotificationDuration]').val() + data.desktopNotifications = $('#desktopNotifications').find('select').val() data.unreadAlert = $('#unreadAlert').find('input:checked').val() Meteor.call 'saveUserPreferences', data, (error, results) -> diff --git a/packages/rocketchat-ui-account/account/accountPreferences.html b/packages/rocketchat-ui-account/account/accountPreferences.html index 64ecdaaed2a7..2a3913c3fad5 100644 --- a/packages/rocketchat-ui-account/account/accountPreferences.html +++ b/packages/rocketchat-ui-account/account/accountPreferences.html @@ -52,6 +52,17 @@

{{_ "Messages"}}

{{/if}}
+
+ +
+ +
+
diff --git a/server/methods/saveUserPreferences.coffee b/server/methods/saveUserPreferences.coffee index c01cd297886f..b19822c07d07 100644 --- a/server/methods/saveUserPreferences.coffee +++ b/server/methods/saveUserPreferences.coffee @@ -44,6 +44,9 @@ Meteor.methods if settings.unreadAlert? preferences.unreadAlert = if settings.unreadAlert is "1" then true else false + if settings.desktopNotifications? + preferences.desktopNotifications = settings.desktopNotifications; + preferences.desktopNotificationDuration = settings.desktopNotificationDuration - 0 preferences.viewMode = settings.viewMode || 0 preferences.hideUsernames = settings.hideUsernames is "1" From ac216b93f31bcef75f9efd84772efe8490f1607a Mon Sep 17 00:00:00 2001 From: Alex Brazier Date: Sat, 17 Sep 2016 11:02:27 +0100 Subject: [PATCH 03/12] Add user preference for mobile notifications --- packages/rocketchat-i18n/i18n/en.i18n.json | 3 ++- .../server/lib/sendNotificationsOnMessage.js | 3 ++- .../account/accountPreferences.coffee | 1 + .../account/accountPreferences.html | 11 +++++++++++ server/methods/saveUserPreferences.coffee | 3 +++ 5 files changed, 19 insertions(+), 2 deletions(-) diff --git a/packages/rocketchat-i18n/i18n/en.i18n.json b/packages/rocketchat-i18n/i18n/en.i18n.json index 186a11867582..7cfbb1a9eab9 100644 --- a/packages/rocketchat-i18n/i18n/en.i18n.json +++ b/packages/rocketchat-i18n/i18n/en.i18n.json @@ -844,7 +844,8 @@ "Not_found_or_not_allowed" : "Not Found or Not Allowed", "Nothing" : "Nothing", "Nothing_found" : "Nothing found", - "Notification_Desktop_Default_For" : "Desktop Notifications For", + "Notification_Desktop_Default_For" : "Show Desktop Notifications For", + "Notification_Mobile_Default_For" : "Push Mobile Notifications For", "Notification_Duration" : "Notification Duration", "Notifications" : "Notifications", "Notify_all_in_this_room" : "Notify all in this room", diff --git a/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js b/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js index 092b8630dad1..48fe3cc729f8 100644 --- a/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js +++ b/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js @@ -77,10 +77,11 @@ RocketChat.callbacks.add('afterSaveMessage', function(message, room) { let currentUser = RocketChat.models.Users.findOneById(subscription.u._id); let preferences = currentUser.settings ? currentUser.settings.preferences || {} : {}; let userDesktopNotificationPreference = preferences.desktopNotifications !== 'default' ? preferences.desktopNotifications : undefined; + let userMobileNotificationPreference = preferences.mobileNotifications !== 'default' ? preferences.mobileNotifications : undefined; // Set defaults if they don't exist let { desktopNotifications = userDesktopNotificationPreference || RocketChat.settings.get('Desktop_Notifications_Default_Alert'), - mobilePushNotifications = RocketChat.settings.get('Mobile_Notifications_Default_Alert') + mobilePushNotifications = userMobileNotificationPreference || RocketChat.settings.get('Mobile_Notifications_Default_Alert') } = subscription; if (desktopNotifications === 'all') { diff --git a/packages/rocketchat-ui-account/account/accountPreferences.coffee b/packages/rocketchat-ui-account/account/accountPreferences.coffee index a0862d81be4d..e276f8711032 100644 --- a/packages/rocketchat-ui-account/account/accountPreferences.coffee +++ b/packages/rocketchat-ui-account/account/accountPreferences.coffee @@ -82,6 +82,7 @@ Template.accountPreferences.onCreated -> data.highlights = _.compact(_.map($('[name=highlights]').val().split(','), (e) -> return _.trim(e))) data.desktopNotificationDuration = $('input[name=desktopNotificationDuration]').val() data.desktopNotifications = $('#desktopNotifications').find('select').val() + data.mobileNotifications = $('#mobileNotifications').find('select').val() data.unreadAlert = $('#unreadAlert').find('input:checked').val() Meteor.call 'saveUserPreferences', data, (error, results) -> diff --git a/packages/rocketchat-ui-account/account/accountPreferences.html b/packages/rocketchat-ui-account/account/accountPreferences.html index 2a3913c3fad5..e627f2f6248a 100644 --- a/packages/rocketchat-ui-account/account/accountPreferences.html +++ b/packages/rocketchat-ui-account/account/accountPreferences.html @@ -63,6 +63,17 @@

{{_ "Messages"}}

+
+ +
+ +
+
diff --git a/server/methods/saveUserPreferences.coffee b/server/methods/saveUserPreferences.coffee index b19822c07d07..1f806f731db6 100644 --- a/server/methods/saveUserPreferences.coffee +++ b/server/methods/saveUserPreferences.coffee @@ -47,6 +47,9 @@ Meteor.methods if settings.desktopNotifications? preferences.desktopNotifications = settings.desktopNotifications; + if settings.mobileNotifications? + preferences.mobileNotifications = settings.mobileNotifications; + preferences.desktopNotificationDuration = settings.desktopNotificationDuration - 0 preferences.viewMode = settings.viewMode || 0 preferences.hideUsernames = settings.hideUsernames is "1" From 997ab780c2636c090a8d96125c7f112c9f529f33 Mon Sep 17 00:00:00 2001 From: Alex Brazier Date: Sat, 17 Sep 2016 11:07:45 +0100 Subject: [PATCH 04/12] Change user notification preference string --- .../rocketchat-ui-account/account/accountPreferences.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/rocketchat-ui-account/account/accountPreferences.html b/packages/rocketchat-ui-account/account/accountPreferences.html index e627f2f6248a..267a7971c397 100644 --- a/packages/rocketchat-ui-account/account/accountPreferences.html +++ b/packages/rocketchat-ui-account/account/accountPreferences.html @@ -57,7 +57,7 @@

{{_ "Messages"}}

@@ -68,7 +68,7 @@

{{_ "Messages"}}

From 3cc4ffb79070d2b476f47c3c320a12d240af8297 Mon Sep 17 00:00:00 2001 From: Alex Brazier Date: Sat, 17 Sep 2016 12:26:36 +0100 Subject: [PATCH 05/12] Fix linting issues --- .../server/models/Subscriptions.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/rocketchat-push-notifications/server/models/Subscriptions.js b/packages/rocketchat-push-notifications/server/models/Subscriptions.js index ea7b7e2f171f..33056349059f 100644 --- a/packages/rocketchat-push-notifications/server/models/Subscriptions.js +++ b/packages/rocketchat-push-notifications/server/models/Subscriptions.js @@ -6,9 +6,9 @@ RocketChat.models.Subscriptions.updateDesktopNotificationsById = function(_id, d const update = {}; if (desktopNotifications === 'default') { - update.$unset = { desktopNotifications: 1 } + update.$unset = { desktopNotifications: 1 }; } else { - update.$set = { desktopNotifications: desktopNotifications } + update.$set = { desktopNotifications: desktopNotifications }; } return this.update(query, update); @@ -36,9 +36,9 @@ RocketChat.models.Subscriptions.updateMobilePushNotificationsById = function(_id const update = {}; if (mobilePushNotifications === 'default') { - update.$unset = { mobilePushNotifications: 1 } + update.$unset = { mobilePushNotifications: 1 }; } else { - update.$set = { mobilePushNotifications: mobilePushNotifications } + update.$set = { mobilePushNotifications: mobilePushNotifications }; } return this.update(query, update); From a82ddbd2bbe2a1920f08b8a09e39f5731aa3ebe8 Mon Sep 17 00:00:00 2001 From: Alex Brazier Date: Sun, 18 Sep 2016 13:41:20 +0100 Subject: [PATCH 06/12] Add setting to disable notifications for all messages in large channels --- packages/rocketchat-i18n/i18n/en.i18n.json | 2 ++ .../server/lib/sendNotificationsOnMessage.js | 9 ++++++--- packages/rocketchat-lib/server/startup/settings.coffee | 2 ++ .../server/models/Subscriptions.js | 10 +++++++++- 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/packages/rocketchat-i18n/i18n/en.i18n.json b/packages/rocketchat-i18n/i18n/en.i18n.json index 6fd85eafcb0b..89dd170fc555 100644 --- a/packages/rocketchat-i18n/i18n/en.i18n.json +++ b/packages/rocketchat-i18n/i18n/en.i18n.json @@ -850,6 +850,8 @@ "Notification_Mobile_Default_For" : "Push Mobile Notifications For", "Notification_Duration" : "Notification Duration", "Notifications" : "Notifications", + "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. (0 to disable)", "Notify_all_in_this_room" : "Notify all in this room", "Num_Agents" : "# Agents", "Number_of_messages" : "Number of messages", diff --git a/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js b/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js index 48fe3cc729f8..14009d036f0d 100644 --- a/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js +++ b/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js @@ -73,7 +73,10 @@ RocketChat.callbacks.add('afterSaveMessage', function(message, room) { settings.alwaysNotifyMobileUsers = []; settings.dontNotifyMobileUsers = []; settings.desktopNotificationDurations = {}; - RocketChat.models.Subscriptions.findNotificationPreferencesByRoom(room._id).forEach(function(subscription) { + // Don't fetch all users if room exceeds max members + let maxMembersForNotification = RocketChat.settings.get('Notifications_Max_Room_Members'); + let disableAllMessageNotifications = room.usernames.length > maxMembersForNotification && maxMembersForNotification !== 0; + RocketChat.models.Subscriptions.findNotificationPreferencesByRoom(room._id, disableAllMessageNotifications).forEach(function(subscription) { let currentUser = RocketChat.models.Users.findOneById(subscription.u._id); let preferences = currentUser.settings ? currentUser.settings.preferences || {} : {}; let userDesktopNotificationPreference = preferences.desktopNotifications !== 'default' ? preferences.desktopNotifications : undefined; @@ -84,12 +87,12 @@ RocketChat.callbacks.add('afterSaveMessage', function(message, room) { mobilePushNotifications = userMobileNotificationPreference || RocketChat.settings.get('Mobile_Notifications_Default_Alert') } = subscription; - if (desktopNotifications === 'all') { + if (desktopNotifications === 'all' && !disableAllMessageNotifications) { settings.alwaysNotifyDesktopUsers.push(subscription.u._id); } else if (desktopNotifications === 'nothing') { settings.dontNotifyDesktopUsers.push(subscription.u._id); } - if (mobilePushNotifications === 'all') { + if (mobilePushNotifications === 'all' && !disableAllMessageNotifications) { settings.alwaysNotifyMobileUsers.push(subscription.u._id); } else if (mobilePushNotifications === 'nothing') { settings.dontNotifyMobileUsers.push(subscription.u._id); diff --git a/packages/rocketchat-lib/server/startup/settings.coffee b/packages/rocketchat-lib/server/startup/settings.coffee index b2b187a4bca1..a8554e9e13c4 100644 --- a/packages/rocketchat-lib/server/startup/settings.coffee +++ b/packages/rocketchat-lib/server/startup/settings.coffee @@ -139,6 +139,8 @@ RocketChat.settings.addGroup 'General', -> public: true } + @add 'Notifications_Max_Room_Members', 100, { type: 'int', public: true, i18nDescription: 'Notifications_Max_Room_Members_Description' } + @section 'REST API', -> @add 'API_User_Limit', 500, { type: 'int', public: true, i18nDescription: 'API_User_Limit' } diff --git a/packages/rocketchat-push-notifications/server/models/Subscriptions.js b/packages/rocketchat-push-notifications/server/models/Subscriptions.js index 33056349059f..fe274c85122c 100644 --- a/packages/rocketchat-push-notifications/server/models/Subscriptions.js +++ b/packages/rocketchat-push-notifications/server/models/Subscriptions.js @@ -108,12 +108,20 @@ RocketChat.models.Subscriptions.findDontNotifyMobileUsersByRoomId = function(roo return this.find(query); }; -RocketChat.models.Subscriptions.findNotificationPreferencesByRoom = function(roomId) { +RocketChat.models.Subscriptions.findNotificationPreferencesByRoom = function(roomId, explicit) { const query = { rid: roomId, 'u._id': {$exists: true} }; + if (explicit) { + query.$or = [ + {desktopNotifications: {$exists: true}}, + {desktopNotificationDuration: {$exists: true}}, + {mobilePushNotifications: {$exists: true}} + ]; + } + return this.find(query); }; From 81ffe239fc84442d864c00196c237953a8dc1fe9 Mon Sep 17 00:00:00 2001 From: Alex Brazier Date: Sun, 18 Sep 2016 14:09:25 +0100 Subject: [PATCH 07/12] Optimise queries for notifications --- .../rocketchat-lib/server/lib/sendNotificationsOnMessage.js | 3 ++- .../server/models/Subscriptions.js | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js b/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js index 14009d036f0d..e377cd894134 100644 --- a/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js +++ b/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js @@ -77,7 +77,8 @@ RocketChat.callbacks.add('afterSaveMessage', function(message, room) { let maxMembersForNotification = RocketChat.settings.get('Notifications_Max_Room_Members'); let disableAllMessageNotifications = room.usernames.length > maxMembersForNotification && maxMembersForNotification !== 0; RocketChat.models.Subscriptions.findNotificationPreferencesByRoom(room._id, disableAllMessageNotifications).forEach(function(subscription) { - let currentUser = RocketChat.models.Users.findOneById(subscription.u._id); + let currentUser = RocketChat.models.Users.findOneById(subscription.u._id, + { fields: {'settings.preferences.desktopNotifications': 1, 'settings.preferences.mobileNotifications': 1} }); let preferences = currentUser.settings ? currentUser.settings.preferences || {} : {}; let userDesktopNotificationPreference = preferences.desktopNotifications !== 'default' ? preferences.desktopNotifications : undefined; let userMobileNotificationPreference = preferences.mobileNotifications !== 'default' ? preferences.mobileNotifications : undefined; diff --git a/packages/rocketchat-push-notifications/server/models/Subscriptions.js b/packages/rocketchat-push-notifications/server/models/Subscriptions.js index fe274c85122c..14169c92226e 100644 --- a/packages/rocketchat-push-notifications/server/models/Subscriptions.js +++ b/packages/rocketchat-push-notifications/server/models/Subscriptions.js @@ -122,7 +122,7 @@ RocketChat.models.Subscriptions.findNotificationPreferencesByRoom = function(roo ]; } - return this.find(query); + return this.find(query, { fields: { 'u._id': 1, desktopNotificationDuration: 1, desktopNotifications: 1, mobilePushNotifications: 1 } }); }; RocketChat.models.Subscriptions.findWithSendEmailByRoomId = function(roomId) { From 3f0af311cd365fcd200812f3f1f7824c0faa31bb Mon Sep 17 00:00:00 2001 From: Alex Brazier Date: Sun, 18 Sep 2016 15:53:03 +0100 Subject: [PATCH 08/12] Optimise mongo query for large databases --- .../server/lib/sendNotificationsOnMessage.js | 13 +++++++++---- packages/rocketchat-lib/server/models/Users.coffee | 7 +++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js b/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js index e377cd894134..53728437d393 100644 --- a/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js +++ b/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js @@ -76,10 +76,15 @@ RocketChat.callbacks.add('afterSaveMessage', function(message, room) { // Don't fetch all users if room exceeds max members let maxMembersForNotification = RocketChat.settings.get('Notifications_Max_Room_Members'); let disableAllMessageNotifications = room.usernames.length > maxMembersForNotification && maxMembersForNotification !== 0; - RocketChat.models.Subscriptions.findNotificationPreferencesByRoom(room._id, disableAllMessageNotifications).forEach(function(subscription) { - let currentUser = RocketChat.models.Users.findOneById(subscription.u._id, - { fields: {'settings.preferences.desktopNotifications': 1, 'settings.preferences.mobileNotifications': 1} }); - let preferences = currentUser.settings ? currentUser.settings.preferences || {} : {}; + let subscriptions = RocketChat.models.Subscriptions.findNotificationPreferencesByRoom(room._id, disableAllMessageNotifications); + let userIds = subscriptions.map((s) => s.u._id); + let userSettings = {}; + RocketChat.models.Users.findUsersByIds(userIds, { fields: { 'settings.preferences.desktopNotifications': 1, 'settings.preferences.mobileNotifications': 1 } }).forEach((user) => { + userSettings[user._id] = user.settings; + }); + + subscriptions.forEach((subscription) => { + let preferences = userSettings[subscription.u._id] ? userSettings[subscription.u._id].preferences || {} : {}; let userDesktopNotificationPreference = preferences.desktopNotifications !== 'default' ? preferences.desktopNotifications : undefined; let userMobileNotificationPreference = preferences.mobileNotifications !== 'default' ? preferences.mobileNotifications : undefined; // Set defaults if they don't exist diff --git a/packages/rocketchat-lib/server/models/Users.coffee b/packages/rocketchat-lib/server/models/Users.coffee index 7f8b2051105f..0b5d98e721e8 100644 --- a/packages/rocketchat-lib/server/models/Users.coffee +++ b/packages/rocketchat-lib/server/models/Users.coffee @@ -166,6 +166,13 @@ RocketChat.models.Users = new class extends RocketChat.models._Base return @find query, options + findUsersByIds: (ids, options) -> + query = + _id: + $in: ids + + return @find query, options + # UPDATE updateLastLoginById: (_id) -> update = From 80dcfef85ee1c60f6ffbc5f33ea27f06fcdbb499 Mon Sep 17 00:00:00 2001 From: Alex Brazier Date: Wed, 19 Oct 2016 22:07:56 +0100 Subject: [PATCH 09/12] Update admin setting notification description to make it more clear --- packages/rocketchat-i18n/i18n/en.i18n.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/rocketchat-i18n/i18n/en.i18n.json b/packages/rocketchat-i18n/i18n/en.i18n.json index ff511db732d2..e43403a71d0e 100644 --- a/packages/rocketchat-i18n/i18n/en.i18n.json +++ b/packages/rocketchat-i18n/i18n/en.i18n.json @@ -898,7 +898,7 @@ "Notification_Duration": "Notification Duration", "Notifications": "Notifications", "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. (0 to disable)", + "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)", "Notify_all_in_this_room": "Notify all in this room", "Notify_active_in_this_room": "Notify active users in this room", "Num_Agents": "# Agents", From ec71055577ee1411da9465ba6ec4371e9ee8b7a3 Mon Sep 17 00:00:00 2001 From: Alex Brazier Date: Tue, 20 Dec 2016 08:49:35 +0000 Subject: [PATCH 10/12] Fix issue with notifications --- .../rocketchat-lib/server/lib/sendNotificationsOnMessage.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js b/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js index 48a747fdfa69..9cbb0193d4f2 100644 --- a/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js +++ b/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js @@ -78,7 +78,10 @@ RocketChat.callbacks.add('afterSaveMessage', function(message, room) { let maxMembersForNotification = RocketChat.settings.get('Notifications_Max_Room_Members'); let disableAllMessageNotifications = room.usernames.length > maxMembersForNotification && maxMembersForNotification !== 0; let subscriptions = RocketChat.models.Subscriptions.findNotificationPreferencesByRoom(room._id, disableAllMessageNotifications); - let userIds = subscriptions.map((s) => s.u._id); + let userIds = []; + subscriptions.forEach((s) => { + userIds.push(s.u._id); + }); let userSettings = {}; RocketChat.models.Users.findUsersByIds(userIds, { fields: { 'settings.preferences.desktopNotifications': 1, 'settings.preferences.mobileNotifications': 1 } }).forEach((user) => { userSettings[user._id] = user.settings; From d57c9071a6b66f32d4612f331de12a9f6dada83e Mon Sep 17 00:00:00 2001 From: Alex Brazier Date: Thu, 9 Feb 2017 19:52:34 +0000 Subject: [PATCH 11/12] Change lets to consts --- .../server/lib/sendNotificationsOnMessage.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js b/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js index 16bed7c783c3..9afd356678cc 100644 --- a/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js +++ b/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js @@ -75,24 +75,24 @@ RocketChat.callbacks.add('afterSaveMessage', function(message, room) { settings.dontNotifyMobileUsers = []; settings.desktopNotificationDurations = {}; // Don't fetch all users if room exceeds max members - let maxMembersForNotification = RocketChat.settings.get('Notifications_Max_Room_Members'); - let disableAllMessageNotifications = room.usernames.length > maxMembersForNotification && maxMembersForNotification !== 0; - let subscriptions = RocketChat.models.Subscriptions.findNotificationPreferencesByRoom(room._id, disableAllMessageNotifications); - let userIds = []; + const maxMembersForNotification = RocketChat.settings.get('Notifications_Max_Room_Members'); + const disableAllMessageNotifications = room.usernames.length > maxMembersForNotification && maxMembersForNotification !== 0; + const subscriptions = RocketChat.models.Subscriptions.findNotificationPreferencesByRoom(room._id, disableAllMessageNotifications); + const userIds = []; subscriptions.forEach((s) => { userIds.push(s.u._id); }); - let userSettings = {}; + const userSettings = {}; RocketChat.models.Users.findUsersByIds(userIds, { fields: { 'settings.preferences.desktopNotifications': 1, 'settings.preferences.mobileNotifications': 1 } }).forEach((user) => { userSettings[user._id] = user.settings; }); subscriptions.forEach((subscription) => { - let preferences = userSettings[subscription.u._id] ? userSettings[subscription.u._id].preferences || {} : {}; - let userDesktopNotificationPreference = preferences.desktopNotifications !== 'default' ? preferences.desktopNotifications : undefined; - let userMobileNotificationPreference = preferences.mobileNotifications !== 'default' ? preferences.mobileNotifications : undefined; + const preferences = userSettings[subscription.u._id] ? userSettings[subscription.u._id].preferences || {} : {}; + const userDesktopNotificationPreference = preferences.desktopNotifications !== 'default' ? preferences.desktopNotifications : undefined; + const userMobileNotificationPreference = preferences.mobileNotifications !== 'default' ? preferences.mobileNotifications : undefined; // Set defaults if they don't exist - let { + const { desktopNotifications = userDesktopNotificationPreference || RocketChat.settings.get('Desktop_Notifications_Default_Alert'), mobilePushNotifications = userMobileNotificationPreference || RocketChat.settings.get('Mobile_Notifications_Default_Alert') } = subscription; From 02541bb79a1b34406f069a55bcba0982853c8606 Mon Sep 17 00:00:00 2001 From: Alex Brazier Date: Sun, 14 May 2017 11:45:48 +0100 Subject: [PATCH 12/12] Fix linting issues --- .../server/models/Subscriptions.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/rocketchat-push-notifications/server/models/Subscriptions.js b/packages/rocketchat-push-notifications/server/models/Subscriptions.js index 0acc816f1876..e55c965afd10 100644 --- a/packages/rocketchat-push-notifications/server/models/Subscriptions.js +++ b/packages/rocketchat-push-notifications/server/models/Subscriptions.js @@ -163,8 +163,8 @@ RocketChat.models.Subscriptions.findNotificationPreferencesByRoom = function(roo {desktopNotificationDuration: {$exists: true}}, {mobilePushNotifications: {$exists: true}}, {disableNotifications: {$exists: true}} - ] - }; + ]; + } return this.find(query, { fields: { 'u._id': 1, desktopNotificationDuration: 1, desktopNotifications: 1, mobilePushNotifications: 1 } }); };