Remove dependency of RocketChat namespace and push-notifications (#13137)

* Move rocketchat settings to specific package

* WIP: Move models from rocketchat-lib to a specific package (server)

* Move function from rocketchat:lib to rocketchat:utils to use it in rocketchat:models

* Move client models from rocketchat:lib to rocketchat:models

* Fix lint

* Move rocketchat.info from lib to utils

* Remove directly dependency between lib and migrations

* Move statistics Model to rocketchat:models

* Create rocketchat:metrics to be able to depacking rocketchat callbacks

* Move  callbacks to specific package

* Remove unused dependency

* Move rocketchat-notifications to a specific package

* Move rocketchat-promises to a specific package

* remove directly dependency from metrics and models

* Move CachedCollection from lib to models

* Move ui models/collections from ui to models

* Move authorization client/ui models to rocketchat:models to be able to remove lib dependency

* Creation of rocketchat:ui-utils to help decouple rocketchat:lib and rocketchat:authz

* Move some common functions to rocketchat:utils

* Change imports to dynamic imports to avoid directly dependency between some packages

* Move authz models to rocketchat:models

* Remove directly dependency between rocketchat:authz and rocketchat:lib

* Move some functions from rocketchat:lib to rocketchat:utils

* Add functions to settings package

* Convert rocketchat:file-upload to main module structure

* Import FileUpload where it is being used

* Remove FileUpload and fileUploadHandler from globals eslintrc

* Move some  functions to rocketchat:ui-utils

* Remove directly dependency between rocketchat:authorization and rocketchat:ui-utils

* Remove dependency between lazy-load and lib

* Change imports of renderMessageBody from ui-message to ui-utils

* Add import of main ready from ui-utils

* Convert rocketchat-ui-sidenav to main module structure

* Add imports of toolbarSearch from ui-sidenav

* Remove toolbarSearch from eslintrc globals

* Move CachedCollection to a specific package

* Change imports of CachedCollection to new package

* Move some functions to rocketchat:ui-utils

* Remove directly dependency between tooltip and lib

*  Remove directly dependency between settings and metrics

* Move some settings client function from lib to settings

* Convert rocketchat-ui-master to main module structure

* Remove directly dependency between rocketchat:e2e and rocketchat:lib

* Fix wrong import and lint

* Convert rocketchat-webrtc to main module structure

* Fix missing export

* Remove directly dependency between rocketchat:emoji and lib

* Add emoji dependencies inside RocketChat namespace

* Merge branch 'develop' into globals/move-rocketchat-callbacks

* Move some functions to utils

* Fix lint

* Move some ui functions to ui-utils

* Fix import missed objects inside RocketChat namespace

* Fix lint

* Remove rocketchat:ui package dependency of RocketChat namespace

* Remove lib dependency in rocketchat:ui-sidenav

* Remove dependency between lib and ui-vrecord

* Add logger dependency in file-upload

* Convert rocketchat:ui to main module structure

* import variables that was broken due to conversion of rocketchat:ui

* Remove globals variables from eslintrc and add some to the eslintrc of livechat app

* Remove dependency between RocketChat namespace and migrations

* Revert commented test file

* Remove dependency of RocketChat namespace and logger

* Move CustomSounds Model to rocketchat:models

* Remove dependency of RocketChat namespace and custom-sounds

* MOve function to utils

* Remove dependency of RocketChat namespace and push-notifications
pull/12540/head^2
Marcos Spessatto Defendi 6 years ago committed by Rodrigo Nascimento
parent c3c2f5a324
commit 06a523f865
  1. 29
      packages/rocketchat-lib/lib/getUserNotificationPreference.js
  2. 276
      packages/rocketchat-models/server/models/Subscriptions.js
  3. 4
      packages/rocketchat-push-notifications/client/tabBar.js
  4. 30
      packages/rocketchat-push-notifications/client/views/pushNotificationsFlexTab.js
  5. 6
      packages/rocketchat-push-notifications/package.js
  6. 1
      packages/rocketchat-push-notifications/server/index.js
  7. 45
      packages/rocketchat-push-notifications/server/methods/saveNotificationSettings.js
  8. 278
      packages/rocketchat-push-notifications/server/models/Subscriptions.js
  9. 2
      packages/rocketchat-utils/client/index.js
  10. 31
      packages/rocketchat-utils/lib/getUserNotificationPreference.js
  11. 2
      packages/rocketchat-utils/server/index.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;

@ -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: {

@ -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',

@ -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];
},

@ -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');

@ -1,2 +1 @@
import './models/Subscriptions';
import './methods/saveNotificationSettings';

@ -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;
},
});

@ -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,
},
});
};

@ -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,
};

@ -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;
};

@ -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,
};

Loading…
Cancel
Save