Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NEW] REST API endpoint /me now returns all the settings, including the default values #10662

Merged
merged 4 commits into from
May 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions packages/rocketchat-api/server/v1/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ RocketChat.API.v1.addRoute('info', { authRequired: false }, {

RocketChat.API.v1.addRoute('me', { authRequired: true }, {
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, [
'_id',
'name',
Expand All @@ -30,17 +40,15 @@ RocketChat.API.v1.addRoute('me', { authRequired: true }, {
'utcOffset',
'active',
'language',
'roles',
'settings'
'roles'
]);

const verifiedEmail = me.emails.find((email) => email.verified);
const userHasNotSetPreferencesYet = !me.settings || !me.settings.preferences;

me.email = verifiedEmail ? verifiedEmail.address : undefined;
if (userHasNotSetPreferencesYet) {
me.settings = { preferences: {} };
}
me.settings = {
preferences: getUserPreferences()
};

return RocketChat.API.v1.success(me);
}
Expand Down
7 changes: 7 additions & 0 deletions tests/end-to-end/api/00-miscellaneous.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ describe('miscellaneous', function() {
.expect('Content-Type', 'application/json')
.expect(200)
.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('_id', credentials['X-User-Id']);
expect(res.body).to.have.property('username', login.user);
Expand All @@ -69,6 +75,7 @@ describe('miscellaneous', function() {
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('settings.preferences').and.to.be.an('object');
expect(res.body.settings.preferences).to.have.all.keys(allUserPreferencesKeys);
})
.end(done);
});
Expand Down