From e1ef24b08c7d2c13ba834a384f9f16ab0d2c68f1 Mon Sep 17 00:00:00 2001 From: Diego Sampaio Date: Wed, 23 May 2018 14:30:17 -0300 Subject: [PATCH] Regression: Fix email notification preference not showing correct selected value (#10847) * Fix email notification preference not showing correct selected value Closes #10844 * Save email notification preferences correctly Closes #10787 * Create room with user notification preferences * Add back the uploaded file message on push notifications --- .../lib/getDefaultSubscriptionPref.js | 31 ++++++++++ packages/rocketchat-lib/package.js | 1 + .../server/functions/notifications/index.js | 4 +- .../server/lib/sendNotificationsOnMessage.js | 2 +- .../server/models/Subscriptions.js | 34 +---------- .../methods/saveNotificationSettings.js | 6 -- .../client/accountPreferences.html | 2 +- server/methods/createDirectMessage.js | 10 +++- server/startup/migrations/v121.js | 56 +++++++++++++++++++ 9 files changed, 103 insertions(+), 43 deletions(-) create mode 100644 packages/rocketchat-lib/lib/getDefaultSubscriptionPref.js create mode 100644 server/startup/migrations/v121.js diff --git a/packages/rocketchat-lib/lib/getDefaultSubscriptionPref.js b/packages/rocketchat-lib/lib/getDefaultSubscriptionPref.js new file mode 100644 index 000000000000..0dadc5876584 --- /dev/null +++ b/packages/rocketchat-lib/lib/getDefaultSubscriptionPref.js @@ -0,0 +1,31 @@ +RocketChat.getDefaultSubscriptionPref = function _getDefaultSubscriptionPref(userPref) { + const subscription = {}; + + const { + desktopNotifications, + mobileNotifications, + emailNotificationMode, + highlights + } = (userPref.settings && userPref.settings.preferences) || {}; + + if (Array.isArray(highlights) && highlights.length) { + subscription.userHighlights = highlights; + } + + if (desktopNotifications && desktopNotifications !== 'default') { + subscription.desktopNotifications = desktopNotifications; + subscription.desktopPrefOrigin = 'user'; + } + + if (mobileNotifications && mobileNotifications !== 'default') { + subscription.mobilePushNotifications = mobileNotifications; + subscription.mobilePrefOrigin = 'user'; + } + + if (emailNotificationMode && emailNotificationMode !== 'default') { + subscription.emailNotifications = emailNotificationMode; + subscription.emailPrefOrigin = 'user'; + } + + return subscription; +}; diff --git a/packages/rocketchat-lib/package.js b/packages/rocketchat-lib/package.js index b26777e34ff0..abde3ec803c9 100644 --- a/packages/rocketchat-lib/package.js +++ b/packages/rocketchat-lib/package.js @@ -58,6 +58,7 @@ Package.onUse(function(api) { api.addFiles('lib/callbacks.js'); api.addFiles('lib/fileUploadRestrictions.js'); api.addFiles('lib/getAvatarColor.js'); + api.addFiles('lib/getDefaultSubscriptionPref.js'); api.addFiles('lib/getValidRoomName.js'); api.addFiles('lib/placeholders.js'); api.addFiles('lib/promises.js'); diff --git a/packages/rocketchat-lib/server/functions/notifications/index.js b/packages/rocketchat-lib/server/functions/notifications/index.js index f5974f48e7fc..faa9651052ae 100644 --- a/packages/rocketchat-lib/server/functions/notifications/index.js +++ b/packages/rocketchat-lib/server/functions/notifications/index.js @@ -5,14 +5,14 @@ import s from 'underscore.string'; * * @param {object} message the message to be parsed */ -export function parseMessageTextPerUser(message, receiver) { +export function parseMessageTextPerUser(messageText, message, receiver) { if (!message.msg && message.attachments && message.attachments[0]) { const lng = receiver.language || RocketChat.settings.get('language') || 'en'; return message.attachments[0].image_type ? TAPi18n.__('User_uploaded_image', {lng}) : TAPi18n.__('User_uploaded_file', {lng}); } - return message; + return messageText; } /** diff --git a/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js b/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js index b8823a330416..75d544f487c8 100644 --- a/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js +++ b/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js @@ -46,7 +46,7 @@ const sendNotification = ({ return; } - notificationMessage = parseMessageTextPerUser(notificationMessage, receiver); + notificationMessage = parseMessageTextPerUser(notificationMessage, message, receiver); const isHighlighted = messageContainsHighlight(message, subscription.userHighlights); diff --git a/packages/rocketchat-lib/server/models/Subscriptions.js b/packages/rocketchat-lib/server/models/Subscriptions.js index 0aa8534e1888..6534764164de 100644 --- a/packages/rocketchat-lib/server/models/Subscriptions.js +++ b/packages/rocketchat-lib/server/models/Subscriptions.js @@ -1,5 +1,3 @@ -import _ from 'underscore'; - class ModelSubscriptions extends RocketChat.models._Base { constructor() { super(...arguments); @@ -766,37 +764,11 @@ class ModelSubscriptions extends RocketChat.models._Base { _id: user._id, username: user.username, name: user.name - } + }, + ...RocketChat.getDefaultSubscriptionPref(user), + ...extraData }; - const { - desktopNotifications, - mobileNotifications, - emailNotificationMode, - highlights - } = (user.settings && user.settings.preferences) || {}; - - if (desktopNotifications && desktopNotifications !== 'default') { - subscription.desktopNotifications = desktopNotifications; - subscription.desktopPrefOrigin = 'user'; - } - - if (mobileNotifications && mobileNotifications !== 'default') { - subscription.mobilePushNotifications = mobileNotifications; - subscription.mobilePrefOrigin = 'user'; - } - - if (emailNotificationMode && emailNotificationMode !== 'default') { - subscription.emailNotifications = emailNotificationMode; - subscription.emailPrefOrigin = 'user'; - } - - if (Array.isArray(highlights) && highlights.length) { - subscription.userHighlights = highlights; - } - - _.extend(subscription, extraData); - return this.insert(subscription); } diff --git a/packages/rocketchat-push-notifications/server/methods/saveNotificationSettings.js b/packages/rocketchat-push-notifications/server/methods/saveNotificationSettings.js index faee089e2c12..fa7ebd7e4f09 100644 --- a/packages/rocketchat-push-notifications/server/methods/saveNotificationSettings.js +++ b/packages/rocketchat-push-notifications/server/methods/saveNotificationSettings.js @@ -37,12 +37,6 @@ Meteor.methods({ const userPref = RocketChat.getUserNotificationPreference(Meteor.userId(), 'email'); RocketChat.models.Subscriptions.updateEmailNotificationsById(subscription._id, userPref.origin === 'server' ? null : userPref); } else { - // Keep compatibility with old values - if (value === 'all') { - value = 'mentions'; - } else if (value === 'disabled') { - value = 'nothing'; - } RocketChat.models.Subscriptions.updateEmailNotificationsById(subscription._id, { value, origin: 'subscription' }); } } diff --git a/packages/rocketchat-ui-account/client/accountPreferences.html b/packages/rocketchat-ui-account/client/accountPreferences.html index 0724412ac6b1..8ec4479fda1f 100644 --- a/packages/rocketchat-ui-account/client/accountPreferences.html +++ b/packages/rocketchat-ui-account/client/accountPreferences.html @@ -108,7 +108,7 @@

{{_ "Notifications"}}

{{> icon block="rc-select__arrow" icon="arrow-down" }} diff --git a/server/methods/createDirectMessage.js b/server/methods/createDirectMessage.js index 95b8d634c965..c481160caa4d 100644 --- a/server/methods/createDirectMessage.js +++ b/server/methods/createDirectMessage.js @@ -54,6 +54,8 @@ Meteor.methods({ } }); + const myNotificationPref = RocketChat.getDefaultSubscriptionPref(me); + // Make user I have a subcription to this room const upsertSubscription = { $set: { @@ -72,7 +74,8 @@ Meteor.methods({ u: { _id: me._id, username: me.username - } + }, + ...myNotificationPref } }; @@ -85,6 +88,8 @@ Meteor.methods({ $and: [{'u._id': me._id}] // work around to solve problems with upsert and dot }, upsertSubscription); + const toNotificationPref = RocketChat.getDefaultSubscriptionPref(to); + RocketChat.models.Subscriptions.upsert({ rid, $and: [{'u._id': to._id}] // work around to solve problems with upsert and dot @@ -101,7 +106,8 @@ Meteor.methods({ u: { _id: to._id, username: to.username - } + }, + ...toNotificationPref } }); diff --git a/server/startup/migrations/v121.js b/server/startup/migrations/v121.js new file mode 100644 index 000000000000..237723b2ffe3 --- /dev/null +++ b/server/startup/migrations/v121.js @@ -0,0 +1,56 @@ +RocketChat.Migrations.add({ + version: 121, + up() { + + // set user preferences on subscriptions + RocketChat.models.Users.find({ + $or: [ + { 'settings.preferences.desktopNotifications': { $exists: true } }, + { 'settings.preferences.mobileNotifications': { $exists: true } }, + { 'settings.preferences.emailNotificationMode': { $exists: true } } + ] + }).forEach(user => { + if (user.settings.preferences.desktopNotifications && user.settings.preferences.desktopNotifications !== 'default') { + RocketChat.models.Subscriptions.update({ + 'u._id': user._id, + desktopPrefOrigin: { $exists: false } + }, { + $set: { + desktopNotifications: user.settings.preferences.desktopNotifications, + desktopPrefOrigin: 'user' + } + }, { + multi: true + }); + } + + if (user.settings.preferences.mobileNotifications && user.settings.preferences.mobileNotifications !== 'default') { + RocketChat.models.Subscriptions.update({ + 'u._id': user._id, + mobilePrefOrigin: { $exists: false } + }, { + $set: { + mobileNotifications: user.settings.preferences.mobileNotifications, + mobilePrefOrigin: 'user' + } + }, { + multi: true + }); + } + + if (user.settings.preferences.emailNotificationMode && user.settings.preferences.emailNotificationMode !== 'default') { + RocketChat.models.Subscriptions.update({ + 'u._id': user._id, + emailPrefOrigin: { $exists: false } + }, { + $set: { + emailNotifications: user.settings.preferences.emailNotificationMode === 'disabled' || user.settings.preferences.emailNotificationMode === 'nothing' ? 'nothing' : 'mentions', + emailPrefOrigin: 'user' + } + }, { + multi: true + }); + } + }); + } +});