diff --git a/packages/rocketchat-i18n/i18n/en.i18n.json b/packages/rocketchat-i18n/i18n/en.i18n.json index 1d18d31ef923..0d057f3a85f1 100644 --- a/packages/rocketchat-i18n/i18n/en.i18n.json +++ b/packages/rocketchat-i18n/i18n/en.i18n.json @@ -413,6 +413,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.", @@ -965,7 +966,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", @@ -1029,6 +1029,7 @@ "Min_length_is": "Min length is %s", "minutes": "minutes", "Mobile": "Mobile", + "Mobile_Notifications_Default_Alert" : "Mobile Notifications Default Alert", "Monday": "Monday", "Monitor_history_for_changes_on": "Monitor history for changes on", "More_channels": "More channels", @@ -1085,9 +1086,13 @@ "Not_found_or_not_allowed": "Not Found or Not Allowed", "Nothing": "Nothing", "Nothing_found": "Nothing found", + "Notification_Desktop_Default_For" : "Show Desktop Notifications For", + "Notification_Mobile_Default_For" : "Push Mobile Notifications For", "Notification_Duration": "Notification Duration", "Notifications": "Notifications", "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)", "Notify_all_in_this_room": "Notify all in this room", "Notify_active_in_this_room": "Notify active users in this room", "Num_Agents": "# Agents", @@ -1719,4 +1724,4 @@ "your_message_optional": "your message (optional)", "Your_password_is_wrong": "Your password is wrong!", "Your_push_was_sent_to_s_devices": "Your push was sent to %s devices" -} \ No newline at end of file +} diff --git a/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js b/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js index ea6ef470118a..362fa7c4f4dc 100644 --- a/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js +++ b/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js @@ -64,20 +64,41 @@ RocketChat.callbacks.add('afterSaveMessage', function(message, room) { settings.dontNotifyMobileUsers = []; settings.desktopNotificationDurations = {}; - const notificationPreferencesByRoom = RocketChat.models.Subscriptions.findNotificationPreferencesByRoom(room._id); - notificationPreferencesByRoom.forEach(function(subscription) { + // Don't fetch all users if room exceeds max members + 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); + }); + 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) => { if (subscription.disableNotifications) { settings.dontNotifyDesktopUsers.push(subscription.u._id); settings.dontNotifyMobileUsers.push(subscription.u._id); } else { - if (subscription.desktopNotifications === 'all') { + 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 + const { + desktopNotifications = userDesktopNotificationPreference || RocketChat.settings.get('Desktop_Notifications_Default_Alert'), + mobilePushNotifications = userMobileNotificationPreference || RocketChat.settings.get('Mobile_Notifications_Default_Alert') + } = subscription; + + if (desktopNotifications === 'all' && !disableAllMessageNotifications) { 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' && !disableAllMessageNotifications) { settings.alwaysNotifyMobileUsers.push(subscription.u._id); - } else if (subscription.mobilePushNotifications === 'nothing') { + } else if (mobilePushNotifications === 'nothing') { settings.dontNotifyMobileUsers.push(subscription.u._id); } } diff --git a/packages/rocketchat-lib/server/models/Users.coffee b/packages/rocketchat-lib/server/models/Users.coffee index 83519874f9fa..6821525ecdba 100644 --- a/packages/rocketchat-lib/server/models/Users.coffee +++ b/packages/rocketchat-lib/server/models/Users.coffee @@ -189,6 +189,13 @@ class ModelUsers extends RocketChat.models._Base return @find query, options + findUsersByIds: (ids, options) -> + query = + _id: + $in: ids + + return @find query, options + # UPDATE addImportIds: (_id, importIds) -> importIds = [].concat(importIds) diff --git a/packages/rocketchat-lib/server/startup/settings.js b/packages/rocketchat-lib/server/startup/settings.js index f222176bb388..a72076ae4f24 100644 --- a/packages/rocketchat-lib/server/startup/settings.js +++ b/packages/rocketchat-lib/server/startup/settings.js @@ -413,11 +413,42 @@ RocketChat.settings.addGroup('General', function() { }); }); this.section('Notifications', function() { - return this.add('Desktop_Notifications_Duration', 0, { + this.add('Desktop_Notifications_Duration', 0, { type: 'int', 'public': true, i18nDescription: 'Desktop_Notification_Durations_Description' }); + this.add('Desktop_Notifications_Default_Alert', 'all', { + type: 'select', + values: [{ + key: 'all', + i18nLabel: 'All_messages' + }, { + key: 'mentions', + i18nLabel: 'Mentions' + }, { + key: 'nothing', + i18nLabel: 'Nothing' + }], + public: true + }); + + this.add('Mobile_Notifications_Default_Alert', 'mentions', { + type: 'select', + values: [{ + key: 'all', + i18nLabel: 'All_messages' + }, { + key: 'mentions', + i18nLabel: 'Mentions' + }, { + key: 'nothing', + i18nLabel: 'Nothing' + }], + public: true + }); + + return this.add('Notifications_Max_Room_Members', 100, { type: 'int', public: true, i18nDescription: 'Notifications_Max_Room_Members_Description' }); }); this.section('REST API', function() { return this.add('API_User_Limit', 500, { diff --git a/packages/rocketchat-push-notifications/client/views/pushNotificationsFlexTab.html b/packages/rocketchat-push-notifications/client/views/pushNotificationsFlexTab.html index 8b051ec36ae0..4abdf969c0be 100644 --- a/packages/rocketchat-push-notifications/client/views/pushNotificationsFlexTab.html +++ b/packages/rocketchat-push-notifications/client/views/pushNotificationsFlexTab.html @@ -39,16 +39,16 @@