diff --git a/packages/rocketchat-lib/lib/getUserNotificationPreference.js b/packages/rocketchat-lib/lib/getUserNotificationPreference.js index 19a69cb2bda..ef4a2297fd9 100644 --- a/packages/rocketchat-lib/lib/getUserNotificationPreference.js +++ b/packages/rocketchat-lib/lib/getUserNotificationPreference.js @@ -1,28 +1,3 @@ -RocketChat.getUserNotificationPreference = function _getUserNotificationPreference(user, pref) { - if (typeof user === 'string') { - user = RocketChat.models.Users.findOneById(user); - } +import { getUserNotificationPreference } from 'meteor/rocketchat:utils'; - let preferenceKey; - switch (pref) { - case 'desktop': preferenceKey = 'desktopNotifications'; break; - case 'mobile': preferenceKey = 'mobileNotifications'; break; - case 'email': preferenceKey = 'emailNotificationMode'; break; - } - - if (user && user.settings && user.settings.preferences && user.settings.preferences[preferenceKey] !== 'default') { - return { - value: user.settings.preferences[preferenceKey], - origin: 'user', - }; - } - const serverValue = RocketChat.settings.get(`Accounts_Default_User_Preferences_${ preferenceKey }`); - if (serverValue) { - return { - value: serverValue, - origin: 'server', - }; - } - - return null; -}; +RocketChat.getUserNotificationPreference = getUserNotificationPreference; diff --git a/packages/rocketchat-models/server/models/Subscriptions.js b/packages/rocketchat-models/server/models/Subscriptions.js index e1359f2fd7c..f706e3f04dd 100644 --- a/packages/rocketchat-models/server/models/Subscriptions.js +++ b/packages/rocketchat-models/server/models/Subscriptions.js @@ -52,6 +52,282 @@ export class Subscriptions extends Base { return this.find(query, options); } + updateAudioNotificationsById(_id, audioNotifications) { + const query = { + _id, + }; + + const update = {}; + + if (audioNotifications === 'default') { + update.$unset = { audioNotifications: 1 }; + } else { + update.$set = { audioNotifications }; + } + + return this.update(query, update); + } + + updateAudioNotificationValueById(_id, audioNotificationValue) { + const query = { + _id, + }; + + const update = { + $set: { + audioNotificationValue, + }, + }; + + return this.update(query, update); + } + + updateDesktopNotificationsById(_id, desktopNotifications) { + const query = { + _id, + }; + + const update = {}; + + if (desktopNotifications === null) { + update.$unset = { + desktopNotifications: 1, + desktopPrefOrigin: 1, + }; + } else { + update.$set = { + desktopNotifications: desktopNotifications.value, + desktopPrefOrigin: desktopNotifications.origin, + }; + } + + return this.update(query, update); + } + + updateDesktopNotificationDurationById(_id, value) { + const query = { + _id, + }; + + const update = { + $set: { + desktopNotificationDuration: parseInt(value), + }, + }; + + return this.update(query, update); + } + + updateMobilePushNotificationsById(_id, mobilePushNotifications) { + const query = { + _id, + }; + + const update = {}; + + if (mobilePushNotifications === null) { + update.$unset = { + mobilePushNotifications: 1, + mobilePrefOrigin: 1, + }; + } else { + update.$set = { + mobilePushNotifications: mobilePushNotifications.value, + mobilePrefOrigin: mobilePushNotifications.origin, + }; + } + + return this.update(query, update); + } + + updateEmailNotificationsById(_id, emailNotifications) { + const query = { + _id, + }; + + const update = {}; + + if (emailNotifications === null) { + update.$unset = { + emailNotifications: 1, + emailPrefOrigin: 1, + }; + } else { + update.$set = { + emailNotifications: emailNotifications.value, + emailPrefOrigin: emailNotifications.origin, + }; + } + + return this.update(query, update); + } + + updateUnreadAlertById(_id, unreadAlert) { + const query = { + _id, + }; + + const update = { + $set: { + unreadAlert, + }, + }; + + return this.update(query, update); + } + + updateDisableNotificationsById(_id, disableNotifications) { + const query = { + _id, + }; + + const update = { + $set: { + disableNotifications, + }, + }; + + return this.update(query, update); + } + + updateHideUnreadStatusById(_id, hideUnreadStatus) { + const query = { + _id, + }; + + const update = { + $set: { + hideUnreadStatus, + }, + }; + + return this.update(query, update); + } + + updateMuteGroupMentions(_id, muteGroupMentions) { + const query = { + _id, + }; + + const update = { + $set: { + muteGroupMentions, + }, + }; + + return this.update(query, update); + } + + findAlwaysNotifyAudioUsersByRoomId(roomId) { + const query = { + rid: roomId, + audioNotifications: 'all', + }; + + return this.find(query); + } + + findAlwaysNotifyDesktopUsersByRoomId(roomId) { + const query = { + rid: roomId, + desktopNotifications: 'all', + }; + + return this.find(query); + } + + findDontNotifyDesktopUsersByRoomId(roomId) { + const query = { + rid: roomId, + desktopNotifications: 'nothing', + }; + + return this.find(query); + } + + findAlwaysNotifyMobileUsersByRoomId(roomId) { + const query = { + rid: roomId, + mobilePushNotifications: 'all', + }; + + return this.find(query); + } + + findDontNotifyMobileUsersByRoomId(roomId) { + const query = { + rid: roomId, + mobilePushNotifications: 'nothing', + }; + + return this.find(query); + } + + findWithSendEmailByRoomId(roomId) { + const query = { + rid: roomId, + emailNotifications: { + $exists: true, + }, + }; + + return this.find(query, { fields: { emailNotifications: 1, u: 1 } }); + } + + findNotificationPreferencesByRoom(query/* { roomId: rid, desktopFilter: desktopNotifications, mobileFilter: mobilePushNotifications, emailFilter: emailNotifications }*/) { + + return this._db.find(query, { + fields: { + + // fields needed for notifications + rid: 1, + t: 1, + u: 1, + name: 1, + fname: 1, + code: 1, + + // fields to define if should send a notification + ignored: 1, + audioNotifications: 1, + audioNotificationValue: 1, + desktopNotificationDuration: 1, + desktopNotifications: 1, + mobilePushNotifications: 1, + emailNotifications: 1, + disableNotifications: 1, + muteGroupMentions: 1, + userHighlights: 1, + }, + }); + } + + findAllMessagesNotificationPreferencesByRoom(roomId) { + const query = { + rid: roomId, + 'u._id': { $exists: true }, + $or: [ + { desktopNotifications: { $in: ['all', 'mentions'] } }, + { mobilePushNotifications: { $in: ['all', 'mentions'] } }, + { emailNotifications: { $in: ['all', 'mentions'] } }, + ], + }; + + return this._db.find(query, { + fields: { + 'u._id': 1, + audioNotifications: 1, + audioNotificationValue: 1, + desktopNotificationDuration: 1, + desktopNotifications: 1, + mobilePushNotifications: 1, + emailNotifications: 1, + disableNotifications: 1, + muteGroupMentions: 1, + }, + }); + } + resetUserE2EKey(userId) { this.update({ 'u._id': userId }, { $unset: { diff --git a/packages/rocketchat-push-notifications/client/tabBar.js b/packages/rocketchat-push-notifications/client/tabBar.js index b65d6b20fa2..55a3fda3b25 100644 --- a/packages/rocketchat-push-notifications/client/tabBar.js +++ b/packages/rocketchat-push-notifications/client/tabBar.js @@ -1,8 +1,8 @@ import { Meteor } from 'meteor/meteor'; -import { RocketChat } from 'meteor/rocketchat:lib'; +import { TabBar } from 'meteor/rocketchat:ui-utils'; Meteor.startup(function() { - RocketChat.TabBar.addButton({ + TabBar.addButton({ groups: ['channel', 'group', 'direct'], id: 'push-notifications', i18nTitle: 'Notifications_Preferences', diff --git a/packages/rocketchat-push-notifications/client/views/pushNotificationsFlexTab.js b/packages/rocketchat-push-notifications/client/views/pushNotificationsFlexTab.js index 3c7a3dca620..8f89fc0650d 100644 --- a/packages/rocketchat-push-notifications/client/views/pushNotificationsFlexTab.js +++ b/packages/rocketchat-push-notifications/client/views/pushNotificationsFlexTab.js @@ -2,9 +2,11 @@ import { Meteor } from 'meteor/meteor'; import { ReactiveVar } from 'meteor/reactive-var'; import { Session } from 'meteor/session'; import { Template } from 'meteor/templating'; -import { RocketChat, handleError } from 'meteor/rocketchat:lib'; -import { ChatSubscription, popover } from 'meteor/rocketchat:ui'; -import { t } from 'meteor/rocketchat:utils'; +import { settings } from 'meteor/rocketchat:settings'; +import { getUserPreference, handleError, t } from 'meteor/rocketchat:utils'; +import { popover } from 'meteor/rocketchat:ui-utils'; +import { CustomSounds } from 'meteor/rocketchat:custom-sounds'; +import { ChatSubscription } from 'meteor/rocketchat:models'; const notificationLabels = { all: 'All_messages', @@ -79,23 +81,23 @@ Template.pushNotificationsFlexTab.helpers({ } }, defaultAudioNotification() { - let preference = RocketChat.getUserPreference(Meteor.userId(), 'audioNotifications'); + let preference = getUserPreference(Meteor.userId(), 'audioNotifications'); if (preference === 'default') { - preference = RocketChat.settings.get('Accounts_Default_User_Preferences_audioNotifications'); + preference = settings.get('Accounts_Default_User_Preferences_audioNotifications'); } return notificationLabels[preference]; }, defaultDesktopNotification() { - let preference = RocketChat.getUserPreference(Meteor.userId(), 'desktopNotifications'); + let preference = getUserPreference(Meteor.userId(), 'desktopNotifications'); if (preference === 'default') { - preference = RocketChat.settings.get('Accounts_Default_User_Preferences_desktopNotifications'); + preference = settings.get('Accounts_Default_User_Preferences_desktopNotifications'); } return notificationLabels[preference]; }, defaultMobileNotification() { - let preference = RocketChat.getUserPreference(Meteor.userId(), 'mobileNotifications'); + let preference = getUserPreference(Meteor.userId(), 'mobileNotifications'); if (preference === 'default') { - preference = RocketChat.settings.get('Accounts_Default_User_Preferences_mobileNotifications'); + preference = settings.get('Accounts_Default_User_Preferences_mobileNotifications'); } return notificationLabels[preference]; }, @@ -200,11 +202,11 @@ Template.pushNotificationsFlexTab.events({ let value = Template.instance().form.audioNotificationValue.get(); if (value === '0') { - value = RocketChat.getUserPreference(user, 'newMessageNotification'); + value = getUserPreference(user, 'newMessageNotification'); } if (value && value !== 'none') { - const audioVolume = RocketChat.getUserPreference(user, 'notificationsSoundVolume'); + const audioVolume = getUserPreference(user, 'notificationsSoundVolume'); const $audio = $(`audio#${ value }`); if ($audio && $audio[0] && $audio[0].play) { @@ -229,7 +231,7 @@ Template.pushNotificationsFlexTab.events({ switch (key) { case 'audioNotificationValue': - const audioAssets = (RocketChat.CustomSounds && RocketChat.CustomSounds.getList && RocketChat.CustomSounds.getList()) || []; + const audioAssets = (CustomSounds && CustomSounds.getList && CustomSounds.getList()) || []; const audioAssetsArray = audioAssets.map((audio) => ({ id: `audioNotificationValue${ audio.name }`, name: 'audioNotificationValue', @@ -346,9 +348,9 @@ Template.pushNotificationsPopover.helpers({ return Template.instance().data.options; }, defaultDesktopNotification() { - let preference = RocketChat.getUserPreference(Meteor.userId(), 'desktopNotifications'); + let preference = getUserPreference(Meteor.userId(), 'desktopNotifications'); if (preference === 'default') { - preference = RocketChat.settings.get('Accounts_Default_User_Preferences_desktopNotifications'); + preference = settings.get('Accounts_Default_User_Preferences_desktopNotifications'); } return notificationLabels[preference]; }, diff --git a/packages/rocketchat-push-notifications/package.js b/packages/rocketchat-push-notifications/package.js index d130ea8d937..acf4b16dabf 100644 --- a/packages/rocketchat-push-notifications/package.js +++ b/packages/rocketchat-push-notifications/package.js @@ -9,7 +9,11 @@ Package.onUse(function(api) { api.use([ 'ecmascript', 'rocketchat:utils', - 'rocketchat:lib', + 'rocketchat:ui-utils', + 'rocketchat:models', + 'rocketchat:custom-sounds', + 'rocketchat:settings', + 'rocketchat:ui', 'templating', ]); api.addFiles('client/stylesheets/pushNotifications.css', 'client'); diff --git a/packages/rocketchat-push-notifications/server/index.js b/packages/rocketchat-push-notifications/server/index.js index 774683b33c9..7ee7efc55a0 100644 --- a/packages/rocketchat-push-notifications/server/index.js +++ b/packages/rocketchat-push-notifications/server/index.js @@ -1,2 +1 @@ -import './models/Subscriptions'; import './methods/saveNotificationSettings'; diff --git a/packages/rocketchat-push-notifications/server/methods/saveNotificationSettings.js b/packages/rocketchat-push-notifications/server/methods/saveNotificationSettings.js index 699886a47e1..653d74844a4 100644 --- a/packages/rocketchat-push-notifications/server/methods/saveNotificationSettings.js +++ b/packages/rocketchat-push-notifications/server/methods/saveNotificationSettings.js @@ -1,6 +1,7 @@ import { Meteor } from 'meteor/meteor'; import { check } from 'meteor/check'; -import { RocketChat } from 'meteor/rocketchat:lib'; +import { Subscriptions } from 'meteor/rocketchat:models'; +import { getUserNotificationPreference } from 'meteor/rocketchat:utils'; Meteor.methods({ saveNotificationSettings(roomId, field, value) { @@ -13,55 +14,55 @@ Meteor.methods({ const notifications = { audioNotifications: { - updateMethod: (subscription, value) => RocketChat.models.Subscriptions.updateAudioNotificationsById(subscription._id, value), + updateMethod: (subscription, value) => Subscriptions.updateAudioNotificationsById(subscription._id, value), }, desktopNotifications: { updateMethod: (subscription, value) => { if (value === 'default') { - const userPref = RocketChat.getUserNotificationPreference(Meteor.userId(), 'desktop'); - RocketChat.models.Subscriptions.updateDesktopNotificationsById(subscription._id, userPref.origin === 'server' ? null : userPref); + const userPref = getUserNotificationPreference(Meteor.userId(), 'desktop'); + Subscriptions.updateDesktopNotificationsById(subscription._id, userPref.origin === 'server' ? null : userPref); } else { - RocketChat.models.Subscriptions.updateDesktopNotificationsById(subscription._id, { value, origin: 'subscription' }); + Subscriptions.updateDesktopNotificationsById(subscription._id, { value, origin: 'subscription' }); } }, }, mobilePushNotifications: { updateMethod: (subscription, value) => { if (value === 'default') { - const userPref = RocketChat.getUserNotificationPreference(Meteor.userId(), 'mobile'); - RocketChat.models.Subscriptions.updateMobilePushNotificationsById(subscription._id, userPref.origin === 'server' ? null : userPref); + const userPref = getUserNotificationPreference(Meteor.userId(), 'mobile'); + Subscriptions.updateMobilePushNotificationsById(subscription._id, userPref.origin === 'server' ? null : userPref); } else { - RocketChat.models.Subscriptions.updateMobilePushNotificationsById(subscription._id, { value, origin: 'subscription' }); + Subscriptions.updateMobilePushNotificationsById(subscription._id, { value, origin: 'subscription' }); } }, }, emailNotifications: { updateMethod: (subscription, value) => { if (value === 'default') { - const userPref = RocketChat.getUserNotificationPreference(Meteor.userId(), 'email'); - RocketChat.models.Subscriptions.updateEmailNotificationsById(subscription._id, userPref.origin === 'server' ? null : userPref); + const userPref = getUserNotificationPreference(Meteor.userId(), 'email'); + Subscriptions.updateEmailNotificationsById(subscription._id, userPref.origin === 'server' ? null : userPref); } else { - RocketChat.models.Subscriptions.updateEmailNotificationsById(subscription._id, { value, origin: 'subscription' }); + Subscriptions.updateEmailNotificationsById(subscription._id, { value, origin: 'subscription' }); } }, }, unreadAlert: { - updateMethod: (subscription, value) => RocketChat.models.Subscriptions.updateUnreadAlertById(subscription._id, value), + updateMethod: (subscription, value) => Subscriptions.updateUnreadAlertById(subscription._id, value), }, disableNotifications: { - updateMethod: (subscription, value) => RocketChat.models.Subscriptions.updateDisableNotificationsById(subscription._id, value === '1'), + updateMethod: (subscription, value) => Subscriptions.updateDisableNotificationsById(subscription._id, value === '1'), }, hideUnreadStatus: { - updateMethod: (subscription, value) => RocketChat.models.Subscriptions.updateHideUnreadStatusById(subscription._id, value === '1'), + updateMethod: (subscription, value) => Subscriptions.updateHideUnreadStatusById(subscription._id, value === '1'), }, muteGroupMentions: { - updateMethod: (subscription, value) => RocketChat.models.Subscriptions.updateMuteGroupMentions(subscription._id, value === '1'), + updateMethod: (subscription, value) => Subscriptions.updateMuteGroupMentions(subscription._id, value === '1'), }, desktopNotificationDuration: { - updateMethod: (subscription, value) => RocketChat.models.Subscriptions.updateDesktopNotificationDurationById(subscription._id, value), + updateMethod: (subscription, value) => Subscriptions.updateDesktopNotificationDurationById(subscription._id, value), }, audioNotificationValue: { - updateMethod: (subscription, value) => RocketChat.models.Subscriptions.updateAudioNotificationValueById(subscription._id, value), + updateMethod: (subscription, value) => Subscriptions.updateAudioNotificationValueById(subscription._id, value), }, }; const isInvalidNotification = !Object.keys(notifications).includes(field); @@ -76,7 +77,7 @@ Meteor.methods({ throw new Meteor.Error('error-invalid-settings', 'Invalid settings value', { method: 'saveNotificationSettings' }); } - const subscription = RocketChat.models.Subscriptions.findOneByRoomIdAndUserId(roomId, Meteor.userId()); + const subscription = Subscriptions.findOneByRoomIdAndUserId(roomId, Meteor.userId()); if (!subscription) { throw new Meteor.Error('error-invalid-subscription', 'Invalid subscription', { method: 'saveNotificationSettings' }); } @@ -87,20 +88,20 @@ Meteor.methods({ }, saveAudioNotificationValue(rid, value) { - const subscription = RocketChat.models.Subscriptions.findOneByRoomIdAndUserId(rid, Meteor.userId()); + const subscription = Subscriptions.findOneByRoomIdAndUserId(rid, Meteor.userId()); if (!subscription) { throw new Meteor.Error('error-invalid-subscription', 'Invalid subscription', { method: 'saveAudioNotificationValue' }); } - RocketChat.models.Subscriptions.updateAudioNotificationValueById(subscription._id, value); + Subscriptions.updateAudioNotificationValueById(subscription._id, value); return true; }, saveDesktopNotificationDuration(rid, value) { - const subscription = RocketChat.models.Subscriptions.findOneByRoomIdAndUserId(rid, Meteor.userId()); + const subscription = Subscriptions.findOneByRoomIdAndUserId(rid, Meteor.userId()); if (!subscription) { throw new Meteor.Error('error-invalid-subscription', 'Invalid subscription', { method: 'saveDesktopNotificationDuration' }); } - RocketChat.models.Subscriptions.updateDesktopNotificationDurationById(subscription._id, value); + Subscriptions.updateDesktopNotificationDurationById(subscription._id, value); return true; }, }); diff --git a/packages/rocketchat-push-notifications/server/models/Subscriptions.js b/packages/rocketchat-push-notifications/server/models/Subscriptions.js deleted file mode 100644 index fef17a8f8c2..00000000000 --- a/packages/rocketchat-push-notifications/server/models/Subscriptions.js +++ /dev/null @@ -1,278 +0,0 @@ -import { RocketChat } from 'meteor/rocketchat:lib'; - -RocketChat.models.Subscriptions.updateAudioNotificationsById = function(_id, audioNotifications) { - const query = { - _id, - }; - - const update = {}; - - if (audioNotifications === 'default') { - update.$unset = { audioNotifications: 1 }; - } else { - update.$set = { audioNotifications }; - } - - return this.update(query, update); -}; - -RocketChat.models.Subscriptions.updateAudioNotificationValueById = function(_id, audioNotificationValue) { - const query = { - _id, - }; - - const update = { - $set: { - audioNotificationValue, - }, - }; - - return this.update(query, update); -}; - -RocketChat.models.Subscriptions.updateDesktopNotificationsById = function(_id, desktopNotifications) { - const query = { - _id, - }; - - const update = {}; - - if (desktopNotifications === null) { - update.$unset = { - desktopNotifications: 1, - desktopPrefOrigin: 1, - }; - } else { - update.$set = { - desktopNotifications: desktopNotifications.value, - desktopPrefOrigin: desktopNotifications.origin, - }; - } - - return this.update(query, update); -}; - -RocketChat.models.Subscriptions.updateDesktopNotificationDurationById = function(_id, value) { - const query = { - _id, - }; - - const update = { - $set: { - desktopNotificationDuration: parseInt(value), - }, - }; - - return this.update(query, update); -}; - -RocketChat.models.Subscriptions.updateMobilePushNotificationsById = function(_id, mobilePushNotifications) { - const query = { - _id, - }; - - const update = {}; - - if (mobilePushNotifications === null) { - update.$unset = { - mobilePushNotifications: 1, - mobilePrefOrigin: 1, - }; - } else { - update.$set = { - mobilePushNotifications: mobilePushNotifications.value, - mobilePrefOrigin: mobilePushNotifications.origin, - }; - } - - return this.update(query, update); -}; - -RocketChat.models.Subscriptions.updateEmailNotificationsById = function(_id, emailNotifications) { - const query = { - _id, - }; - - const update = {}; - - if (emailNotifications === null) { - update.$unset = { - emailNotifications: 1, - emailPrefOrigin: 1, - }; - } else { - update.$set = { - emailNotifications: emailNotifications.value, - emailPrefOrigin: emailNotifications.origin, - }; - } - - return this.update(query, update); -}; - -RocketChat.models.Subscriptions.updateUnreadAlertById = function(_id, unreadAlert) { - const query = { - _id, - }; - - const update = { - $set: { - unreadAlert, - }, - }; - - return this.update(query, update); -}; - -RocketChat.models.Subscriptions.updateDisableNotificationsById = function(_id, disableNotifications) { - const query = { - _id, - }; - - const update = { - $set: { - disableNotifications, - }, - }; - - return this.update(query, update); -}; - -RocketChat.models.Subscriptions.updateHideUnreadStatusById = function(_id, hideUnreadStatus) { - const query = { - _id, - }; - - const update = { - $set: { - hideUnreadStatus, - }, - }; - - return this.update(query, update); -}; - -RocketChat.models.Subscriptions.updateMuteGroupMentions = function(_id, muteGroupMentions) { - const query = { - _id, - }; - - const update = { - $set: { - muteGroupMentions, - }, - }; - - return this.update(query, update); -}; - -RocketChat.models.Subscriptions.findAlwaysNotifyAudioUsersByRoomId = function(roomId) { - const query = { - rid: roomId, - audioNotifications: 'all', - }; - - return this.find(query); -}; - -RocketChat.models.Subscriptions.findAlwaysNotifyDesktopUsersByRoomId = function(roomId) { - const query = { - rid: roomId, - desktopNotifications: 'all', - }; - - return this.find(query); -}; - -RocketChat.models.Subscriptions.findDontNotifyDesktopUsersByRoomId = function(roomId) { - const query = { - rid: roomId, - desktopNotifications: 'nothing', - }; - - return this.find(query); -}; - -RocketChat.models.Subscriptions.findAlwaysNotifyMobileUsersByRoomId = function(roomId) { - const query = { - rid: roomId, - mobilePushNotifications: 'all', - }; - - return this.find(query); -}; - -RocketChat.models.Subscriptions.findDontNotifyMobileUsersByRoomId = function(roomId) { - const query = { - rid: roomId, - mobilePushNotifications: 'nothing', - }; - - return this.find(query); -}; - -RocketChat.models.Subscriptions.findWithSendEmailByRoomId = function(roomId) { - const query = { - rid: roomId, - emailNotifications: { - $exists: true, - }, - }; - - return this.find(query, { fields: { emailNotifications: 1, u: 1 } }); -}; - - -RocketChat.models.Subscriptions.findNotificationPreferencesByRoom = function(query/* { roomId: rid, desktopFilter: desktopNotifications, mobileFilter: mobilePushNotifications, emailFilter: emailNotifications }*/) { - - return this._db.find(query, { - fields: { - - // fields needed for notifications - rid: 1, - t: 1, - u: 1, - name: 1, - fname: 1, - code: 1, - - // fields to define if should send a notification - ignored: 1, - audioNotifications: 1, - audioNotificationValue: 1, - desktopNotificationDuration: 1, - desktopNotifications: 1, - mobilePushNotifications: 1, - emailNotifications: 1, - disableNotifications: 1, - muteGroupMentions: 1, - userHighlights: 1, - }, - }); -}; - -RocketChat.models.Subscriptions.findAllMessagesNotificationPreferencesByRoom = function(roomId) { - const query = { - rid: roomId, - 'u._id': { $exists: true }, - $or: [ - { desktopNotifications: { $in: ['all', 'mentions'] } }, - { mobilePushNotifications: { $in: ['all', 'mentions'] } }, - { emailNotifications: { $in: ['all', 'mentions'] } }, - ], - }; - - return this._db.find(query, { - fields: { - 'u._id': 1, - audioNotifications: 1, - audioNotificationValue: 1, - desktopNotificationDuration: 1, - desktopNotifications: 1, - mobilePushNotifications: 1, - emailNotifications: 1, - disableNotifications: 1, - muteGroupMentions: 1, - }, - }); -}; diff --git a/packages/rocketchat-utils/client/index.js b/packages/rocketchat-utils/client/index.js index 6db21c18dd3..a18cac241a9 100644 --- a/packages/rocketchat-utils/client/index.js +++ b/packages/rocketchat-utils/client/index.js @@ -10,6 +10,7 @@ import { RoomTypeRouteConfig, RoomTypeConfig, RoomSettingsEnum, UiTextContext } import { RoomTypesCommon } from '../lib/RoomTypesCommon'; import { getAvatarUrlFromUsername } from '../lib/getAvatarUrlFromUsername'; import { slashCommands } from '../lib/slashCommand'; +import { getUserNotificationPreference } from '../lib/getUserNotificationPreference'; export { t, @@ -30,4 +31,5 @@ export { UiTextContext, getAvatarUrlFromUsername, slashCommands, + getUserNotificationPreference, }; diff --git a/packages/rocketchat-utils/lib/getUserNotificationPreference.js b/packages/rocketchat-utils/lib/getUserNotificationPreference.js new file mode 100644 index 00000000000..98d26c86296 --- /dev/null +++ b/packages/rocketchat-utils/lib/getUserNotificationPreference.js @@ -0,0 +1,31 @@ +import { settings } from 'meteor/rocketchat:settings'; +import { Users } from 'meteor/rocketchat:models'; + +export const getUserNotificationPreference = (user, pref) => { + if (typeof user === 'string') { + user = Users.findOneById(user); + } + + let preferenceKey; + switch (pref) { + case 'desktop': preferenceKey = 'desktopNotifications'; break; + case 'mobile': preferenceKey = 'mobileNotifications'; break; + case 'email': preferenceKey = 'emailNotificationMode'; break; + } + + if (user && user.settings && user.settings.preferences && user.settings.preferences[preferenceKey] !== 'default') { + return { + value: user.settings.preferences[preferenceKey], + origin: 'user', + }; + } + const serverValue = settings.get(`Accounts_Default_User_Preferences_${ preferenceKey }`); + if (serverValue) { + return { + value: serverValue, + origin: 'server', + }; + } + + return null; +}; diff --git a/packages/rocketchat-utils/server/index.js b/packages/rocketchat-utils/server/index.js index d205242eb5a..e0585eebb4b 100644 --- a/packages/rocketchat-utils/server/index.js +++ b/packages/rocketchat-utils/server/index.js @@ -9,6 +9,7 @@ import { RoomTypesCommon } from '../lib/RoomTypesCommon'; import { isDocker } from './functions/isDocker'; import { getAvatarUrlFromUsername } from '../lib/getAvatarUrlFromUsername'; import { slashCommands } from '../lib/slashCommand'; +import { getUserNotificationPreference } from '../lib/getUserNotificationPreference'; export { t, @@ -27,4 +28,5 @@ export { isDocker, getAvatarUrlFromUsername, slashCommands, + getUserNotificationPreference, };