Skip to content

Commit

Permalink
Merge pull request #9457 from jgtoriginal/userPrefsEndPountRestAPI
Browse files Browse the repository at this point in the history
[NEW] Add user settings / preferences API endpoint
  • Loading branch information
rodrigok authored Feb 21, 2018
2 parents 41bffea + 6e47d51 commit 34340ca
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 4 deletions.
72 changes: 71 additions & 1 deletion packages/rocketchat-api/server/v1/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,77 @@ RocketChat.API.v1.addRoute('users.createToken', { authRequired: true }, {
}
});

RocketChat.API.v1.addRoute('users.getPreferences', { authRequired: true }, {
get() {
const user = RocketChat.models.Users.findOneById(this.userId);
if (user.settings) {
const preferences = user.settings.preferences;
preferences['language'] = user.language;

return RocketChat.API.v1.success({
preferences
});
} else {
return RocketChat.API.v1.failure(TAPi18n.__('Accounts_Default_User_Preferences_not_available').toUpperCase());
}
}
});

RocketChat.API.v1.addRoute('users.setPreferences', { authRequired: true }, {
post() {
check(this.bodyParams, {
userId: Match.Maybe(String),
data: Match.ObjectIncluding({
newRoomNotification: Match.Maybe(String),
newMessageNotification: Match.Maybe(String),
useEmojis: Match.Maybe(Boolean),
convertAsciiEmoji: Match.Maybe(Boolean),
saveMobileBandwidth: Match.Maybe(Boolean),
collapseMediaByDefault: Match.Maybe(Boolean),
autoImageLoad: Match.Maybe(Boolean),
emailNotificationMode: Match.Maybe(String),
roomsListExhibitionMode: Match.Maybe(String),
unreadAlert: Match.Maybe(Boolean),
notificationsSoundVolume: Match.Maybe(Number),
desktopNotifications: Match.Maybe(String),
mobileNotifications: Match.Maybe(String),
enableAutoAway: Match.Maybe(Boolean),
highlights: Match.Maybe(Array),
desktopNotificationDuration: Match.Maybe(Number),
viewMode: Match.Maybe(Number),
hideUsernames: Match.Maybe(Boolean),
hideRoles: Match.Maybe(Boolean),
hideAvatars: Match.Maybe(Boolean),
hideFlexTab: Match.Maybe(Boolean),
sendOnEnter: Match.Maybe(String),
roomCounterSidebar: Match.Maybe(Boolean),
language: Match.Maybe(String),
sidebarShowFavorites: Match.Optional(Boolean),
sidebarShowUnread: Match.Optional(Boolean),
sidebarSortby: Match.Optional(String),
sidebarViewMode: Match.Optional(String),
sidebarHideAvatar: Match.Optional(Boolean),
mergeChannels: Match.Optional(Boolean),
muteFocusedConversations: Match.Optional(Boolean)
})
});

let preferences;
const userId = this.bodyParams.userId ? this.bodyParams.userId : this.userId;
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 }});
}

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

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

/**
This API returns the logged user roles.
Expand All @@ -290,4 +361,3 @@ RocketChat.API.v1.addRoute('user.roles', { authRequired: true }, {
return RocketChat.API.v1.success(currentUserRoles);
}
});

1 change: 1 addition & 0 deletions packages/rocketchat-i18n/i18n/en.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"Accounts_Default_User_Preferences_audioNotifications": "Audio Notifications Default Alert",
"Accounts_Default_User_Preferences_desktopNotifications": "Desktop Notifications Default Alert",
"Accounts_Default_User_Preferences_mobileNotifications": "Mobile Notifications Default Alert",
"Accounts_Default_User_Preferences_not_available": "Failed to retrieve User Preferences because they haven't been set up by the user yet",
"Accounts_denyUnverifiedEmail": "Deny unverified email",
"Accounts_EmailVerification": "Email Verification",
"Accounts_EmailVerification_Description": "Make sure you have correct SMTP settings to use this feature",
Expand Down
7 changes: 6 additions & 1 deletion packages/rocketchat-lib/server/functions/saveUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ RocketChat.saveUser = function(userId, userData) {
const updateUser = {
$set: {
name: userData.name,
roles: userData.roles || ['user']
roles: userData.roles || ['user'],
settings: userData.settings
}
};

Expand Down Expand Up @@ -163,6 +164,10 @@ RocketChat.saveUser = function(userId, userData) {
updateUser.$set.roles = userData.roles;
}

if (userData.settings) {
updateUser.$set.settings = { preferences: userData.settings.preferences };
}

if (typeof userData.requirePasswordChange !== 'undefined') {
updateUser.$set.requirePasswordChange = userData.requirePasswordChange;
}
Expand Down
29 changes: 28 additions & 1 deletion tests/data/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,34 @@ export const username = `user.test.${ Date.now() }`;
export const email = `${ username }@rocket.chat`;
export const password = 'rocket.chat';
export const reason = 'rocket.chat.reason';

export const adminUsername = 'rocketchat.internal.admin.test';
export const adminEmail = `${ adminUsername }@rocket.chat`;
export const adminPassword = adminUsername;
export const preferences = {
data: {
newRoomNotification: 'door',
newMessageNotification: 'chime',
muteFocusedConversations: true,
useEmojis: true,
convertAsciiEmoji: true,
saveMobileBandwidth: true,
collapseMediaByDefault: false,
autoImageLoad: true,
emailNotificationMode: 'all',
roomsListExhibitionMode: 'category',
unreadAlert: true,
notificationsSoundVolume: 100,
desktopNotifications: 'default',
mobileNotifications: 'default',
enableAutoAway: true,
highlights: [],
desktopNotificationDuration: 0,
viewMode: 0,
hideUsernames: false,
hideRoles: false,
hideAvatars: false,
hideFlexTab: false,
sendOnEnter: 'normal',
roomCounterSidebar: false
}
};
32 changes: 31 additions & 1 deletion tests/end-to-end/api/01-users.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/* eslint no-unused-vars: 0 */

import {getCredentials, api, login, request, credentials, apiEmail, apiUsername, targetUser, log} from '../../data/api-data.js';
import {adminEmail, password} from '../../data/user.js';
import {adminEmail, password, preferences} from '../../data/user.js';
import {imgURL} from '../../data/interactions.js';
import {customFieldText, clearCustomFields, setCustomFields} from '../../data/custom-fields.js';

Expand Down Expand Up @@ -377,4 +377,34 @@ describe('[Users]', function() {
.end(done);
});
});

describe('[/users.setPreferences]', () => {
it('should set some preferences by user when execute successfully', (done) => {
preferences.userId = credentials['X-User-Id'];
request.post(api('users.setPreferences'))
.set(credentials)
.send(preferences)
.expect(200)
.expect('Content-Type', 'application/json')
.expect((res) => {
expect(res.body.user.settings.preferences).to.be.eql(preferences.data);
expect(res.body).to.have.property('success', true);
})
.end(done);
});
});

describe('[/users.getPreferences]', () => {
it('should return all preferences when execute successfully', (done) => {
request.get(api('users.getPreferences'))
.set(credentials)
.expect(200)
.expect('Content-Type', 'application/json')
.expect((res) => {
expect(res.body.preferences).to.be.eql(preferences.data);
expect(res.body).to.have.property('success', true);
})
.end(done);
});
});
});

0 comments on commit 34340ca

Please sign in to comment.