Skip to content
Snippets Groups Projects
Commit 8160a5f0 authored by Marcos Spessatto Defendi's avatar Marcos Spessatto Defendi Committed by Rodrigo Nascimento
Browse files

[FIX] Fix rest /me endpoint (#10662)

[NEW] REST API endpoint `/me` now returns all the settings, including the default values
parent d6ff269b
No related merge requests found
...@@ -20,6 +20,16 @@ RocketChat.API.v1.addRoute('info', { authRequired: false }, { ...@@ -20,6 +20,16 @@ RocketChat.API.v1.addRoute('info', { authRequired: false }, {
RocketChat.API.v1.addRoute('me', { authRequired: true }, { RocketChat.API.v1.addRoute('me', { authRequired: true }, {
get() { get() {
const getUserPreferences = () => {
const defaultUserSettingPrefix = 'Accounts_Default_User_Preferences_';
const allDefaultUserSettings = RocketChat.settings.get(new RegExp(`^${ defaultUserSettingPrefix }.*$`));
return allDefaultUserSettings.reduce((accumulator, setting) => {
const settingWithoutPrefix = setting.key.replace(defaultUserSettingPrefix, ' ').trim();
accumulator[settingWithoutPrefix] = RocketChat.getUserPreference(this.getLoggedInUser(), settingWithoutPrefix);
return accumulator;
}, {});
};
const me = _.pick(this.user, [ const me = _.pick(this.user, [
'_id', '_id',
'name', 'name',
...@@ -30,17 +40,15 @@ RocketChat.API.v1.addRoute('me', { authRequired: true }, { ...@@ -30,17 +40,15 @@ RocketChat.API.v1.addRoute('me', { authRequired: true }, {
'utcOffset', 'utcOffset',
'active', 'active',
'language', 'language',
'roles', 'roles'
'settings'
]); ]);
const verifiedEmail = me.emails.find((email) => email.verified); const verifiedEmail = me.emails.find((email) => email.verified);
const userHasNotSetPreferencesYet = !me.settings || !me.settings.preferences;
me.email = verifiedEmail ? verifiedEmail.address : undefined; me.email = verifiedEmail ? verifiedEmail.address : undefined;
if (userHasNotSetPreferencesYet) { me.settings = {
me.settings = { preferences: {} }; preferences: getUserPreferences()
} };
return RocketChat.API.v1.success(me); return RocketChat.API.v1.success(me);
} }
......
...@@ -61,6 +61,12 @@ describe('miscellaneous', function() { ...@@ -61,6 +61,12 @@ describe('miscellaneous', function() {
.expect('Content-Type', 'application/json') .expect('Content-Type', 'application/json')
.expect(200) .expect(200)
.expect((res) => { .expect((res) => {
const allUserPreferencesKeys = ['enableAutoAway', 'idleTimeoutLimit', 'desktopNotificationDuration', 'audioNotifications',
'desktopNotifications', 'mobileNotifications', 'unreadAlert', 'useEmojis', 'convertAsciiEmoji', 'autoImageLoad',
'saveMobileBandwidth', 'collapseMediaByDefault', 'hideUsernames', 'hideRoles', 'hideFlexTab', 'hideAvatars',
'roomsListExhibitionMode', 'sidebarViewMode', 'sidebarHideAvatar', 'sidebarShowUnread', 'sidebarShowFavorites',
'sendOnEnter', 'messageViewMode', 'emailNotificationMode', 'roomCounterSidebar', 'newRoomNotification', 'newMessageNotification',
'muteFocusedConversations', 'notificationsSoundVolume'];
expect(res.body).to.have.property('success', true); expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('_id', credentials['X-User-Id']); expect(res.body).to.have.property('_id', credentials['X-User-Id']);
expect(res.body).to.have.property('username', login.user); expect(res.body).to.have.property('username', login.user);
...@@ -69,6 +75,7 @@ describe('miscellaneous', function() { ...@@ -69,6 +75,7 @@ describe('miscellaneous', function() {
expect(res.body).to.have.property('roles').and.to.be.an('array'); expect(res.body).to.have.property('roles').and.to.be.an('array');
expect(res.body).to.have.nested.property('emails[0].address', adminEmail); expect(res.body).to.have.nested.property('emails[0].address', adminEmail);
expect(res.body).to.have.nested.property('settings.preferences').and.to.be.an('object'); expect(res.body).to.have.nested.property('settings.preferences').and.to.be.an('object');
expect(res.body.settings.preferences).to.have.all.keys(allUserPreferencesKeys);
}) })
.end(done); .end(done);
}); });
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment