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] Add user settings / preferences API endpoint #9457

Merged
merged 30 commits into from
Feb 21, 2018
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
1b8483f
Add user settings / preferences API endpoint #8694
jgtoriginal Jan 21, 2018
f30d581
Thx coday bot: missing semicolons
jgtoriginal Jan 21, 2018
28b2993
Thx coday bot: property shorthand expected
jgtoriginal Jan 21, 2018
c8f843b
unneded ;
jgtoriginal Jan 21, 2018
b2c75b4
making tests happy
jgtoriginal Jan 21, 2018
c5c2042
updating mongo
jgtoriginal Jan 22, 2018
01f499f
missing semicolon
jgtoriginal Jan 22, 2018
e0c2b8f
formatting
jgtoriginal Jan 22, 2018
c99bfcc
formatting
jgtoriginal Jan 22, 2018
c797214
formatting
jgtoriginal Jan 22, 2018
4cc4c48
formatting
jgtoriginal Jan 22, 2018
3c5077a
Merge branch 'develop' of github.com:RocketChat/Rocket.Chat into user…
jgtoriginal Jan 22, 2018
d0d7f59
users.getPreferences always requires authentication
jgtoriginal Jan 23, 2018
1df340e
Merge branch 'develop' of github.com:RocketChat/Rocket.Chat into user…
jgtoriginal Feb 15, 2018
58d744f
fixing conflicts
jgtoriginal Feb 15, 2018
e336ddb
going by single quotes
jgtoriginal Feb 15, 2018
cee2461
default to the user registered if none is passed
jgtoriginal Feb 16, 2018
b3295a5
adding new params requested by @karlprieb
jgtoriginal Feb 16, 2018
f203b27
get own preferences only
jgtoriginal Feb 16, 2018
9adf9fc
adding muteFocusedConversations param on @karlprieb request
jgtoriginal Feb 16, 2018
2db5a94
giving a more sensible error message
jgtoriginal Feb 16, 2018
e36f95d
replacing this.getUserFromParams() for this.userId, due to legit secu…
jgtoriginal Feb 17, 2018
c964116
fixing typo on i18n
jgtoriginal Feb 17, 2018
3132664
Merge branch 'develop' of github.com:RocketChat/Rocket.Chat into user…
jgtoriginal Feb 17, 2018
82e9c42
Merge branch 'DeprecationWarning' into userPrefsEndPountRestAPI
jgtoriginal Feb 17, 2018
5871eb8
Merge branch 'develop' of github.com:RocketChat/Rocket.Chat into user…
jgtoriginal Feb 17, 2018
19a8ad6
added findOne call to retrieve the user, and remove verification whic…
MarcosSpessatto Feb 20, 2018
d81d1f7
added tests for this endpoint
MarcosSpessatto Feb 20, 2018
68ceaf7
fix test about getPreferences endpoint
MarcosSpessatto Feb 20, 2018
6e47d51
Merge branch 'develop' into userPrefsEndPountRestAPI
rodrigok Feb 20, 2018
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
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 = this.userId;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jgtoriginal did you tested this? You will need to execute a find to get the user's object when you have the user's id

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rodrigok no worries, will get to it at some point today and let you know.
Thanks very much!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this has been solved by @MarcosSpessatto on 19a8ad6

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
12 changes: 11 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,15 @@ RocketChat.saveUser = function(userId, userData) {
updateUser.$set.roles = userData.roles;
}

if (userData.settings) {
updateUser.$set.settings = { preferences: {} };
for (const key in userData.settings.preferences) {
if (userData.settings.preferences[key]) {
updateUser.$set.settings.preferences[key] = userData.settings.preferences[key];
}
}
}

if (typeof userData.requirePasswordChange !== 'undefined') {
updateUser.$set.requirePasswordChange = userData.requirePasswordChange;
}
Expand Down