Skip to content

Commit

Permalink
Regression: Fix email notification preference not showing correct sel…
Browse files Browse the repository at this point in the history
…ected 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
  • Loading branch information
sampaiodiego authored and rodrigok committed May 23, 2018
1 parent 888e2d9 commit e1ef24b
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 43 deletions.
31 changes: 31 additions & 0 deletions packages/rocketchat-lib/lib/getDefaultSubscriptionPref.js
Original file line number Diff line number Diff line change
@@ -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;
};
1 change: 1 addition & 0 deletions packages/rocketchat-lib/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const sendNotification = ({
return;
}

notificationMessage = parseMessageTextPerUser(notificationMessage, receiver);
notificationMessage = parseMessageTextPerUser(notificationMessage, message, receiver);

const isHighlighted = messageContainsHighlight(message, subscription.userHighlights);

Expand Down
34 changes: 3 additions & 31 deletions packages/rocketchat-lib/server/models/Subscriptions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import _ from 'underscore';

class ModelSubscriptions extends RocketChat.models._Base {
constructor() {
super(...arguments);
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ <h1>{{_ "Notifications"}}</h1>
<div class="rc-select">
<select class="input-monitor rc-select__element" name="emailNotificationMode">
<option value="default" selected="{{selected 'emailNotificationMode' 'default' 'default'}}">{{_ "Default"}} ({{_ defaultEmailNotification}})</option>
<option value="nothing" selected="{{selected 'emailNotificationMode' 'disabled'}}">{{_ "Email_Notification_Mode_Disabled"}}</option>
<option value="nothing" selected="{{selected 'emailNotificationMode' 'nothing'}}">{{_ "Email_Notification_Mode_Disabled"}}</option>
<option value="mentions" selected="{{selected 'emailNotificationMode' 'mentions'}}">{{_ "Email_Notification_Mode_All"}}</option>
</select>
{{> icon block="rc-select__arrow" icon="arrow-down" }}
Expand Down
10 changes: 8 additions & 2 deletions server/methods/createDirectMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ Meteor.methods({
}
});

const myNotificationPref = RocketChat.getDefaultSubscriptionPref(me);

// Make user I have a subcription to this room
const upsertSubscription = {
$set: {
Expand All @@ -72,7 +74,8 @@ Meteor.methods({
u: {
_id: me._id,
username: me.username
}
},
...myNotificationPref
}
};

Expand All @@ -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
Expand All @@ -101,7 +106,8 @@ Meteor.methods({
u: {
_id: to._id,
username: to.username
}
},
...toNotificationPref
}
});

Expand Down
56 changes: 56 additions & 0 deletions server/startup/migrations/v121.js
Original file line number Diff line number Diff line change
@@ -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
});
}
});
}
});

0 comments on commit e1ef24b

Please sign in to comment.