Skip to content

Commit

Permalink
Fix strange behavior at users.setPreferences REST API
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigok committed Jul 6, 2018
1 parent 39e9daa commit 1bf3327
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions packages/rocketchat-api/server/v1/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,26 +352,23 @@ RocketChat.API.v1.addRoute('users.setPreferences', { authRequired: true }, {
})
});

let preferences;
const userId = this.bodyParams.userId ? this.bodyParams.userId : this.userId;
const userData = {
_id: userId,
settings: {
preferences: this.bodyParams.data
}
};

if (this.bodyParams.data.language) {
const language = this.bodyParams.data.language;
delete this.bodyParams.data.language;
preferences = _.extend({ _id: userId, settings: { preferences: this.bodyParams.data }, language });
} else {
preferences = _.extend({ _id: userId, settings: { preferences: this.bodyParams.data } });
}

// Keep compatibility with old values
if (preferences.emailNotificationMode === 'all') {
preferences.emailNotificationMode = 'mentions';
} else if (preferences.emailNotificationMode === 'disabled') {
preferences.emailNotificationMode = 'nothing';
userData.language = language;
}

Meteor.runAsUser(this.userId, () => RocketChat.saveUser(this.userId, preferences));
Meteor.runAsUser(this.userId, () => RocketChat.saveUser(this.userId, userData));

return RocketChat.API.v1.success({ user: RocketChat.models.Users.findOneById(this.bodyParams.userId, { fields: preferences }) });
return RocketChat.API.v1.success({ user: RocketChat.models.Users.findOneById(this.bodyParams.userId, { fields: 'settings.preferences' }) });
}
});

Expand Down

0 comments on commit 1bf3327

Please sign in to comment.