Skip to content

Commit

Permalink
[NEW] Directory page refactored, new user's bio field (RocketChat#17043)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggazzo authored Apr 1, 2020
1 parent fd09822 commit f0fc738
Show file tree
Hide file tree
Showing 43 changed files with 860 additions and 562 deletions.
1 change: 1 addition & 0 deletions app/api/server/v1/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ API.v1.addRoute('directory', { authRequired: true }, {
const { sort, query } = this.parseJsonQuery();

const { text, type, workspace = 'local' } = query;

if (sort && Object.keys(sort).length > 1) {
return API.v1.failure('This method support only one "sort" parameter');
}
Expand Down
1 change: 1 addition & 0 deletions app/api/server/v1/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ API.v1.addRoute('users.info', { authRequired: true }, {
user.rooms = Subscriptions.findByUserId(user._id, {
fields: {
rid: 1,
bio: 1,
name: 1,
t: 1,
roles: 1,
Expand Down
11 changes: 10 additions & 1 deletion app/channel-settings/server/methods/saveRoomSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { saveRoomTokenpass } from '../functions/saveRoomTokens';
import { saveStreamingOptions } from '../functions/saveStreamingOptions';
import { RoomSettingsEnum, roomTypes } from '../../../utils';

const fields = ['roomName', 'roomTopic', 'roomAnnouncement', 'roomCustomFields', 'roomDescription', 'roomType', 'readOnly', 'reactWhenReadOnly', 'systemMessages', 'default', 'joinCode', 'tokenpass', 'streamingOptions', 'retentionEnabled', 'retentionMaxAge', 'retentionExcludePinned', 'retentionFilesOnly', 'retentionOverrideGlobal', 'encrypted'];
const fields = ['featured', 'roomName', 'roomTopic', 'roomAnnouncement', 'roomCustomFields', 'roomDescription', 'roomType', 'readOnly', 'reactWhenReadOnly', 'systemMessages', 'default', 'joinCode', 'tokenpass', 'streamingOptions', 'retentionEnabled', 'retentionMaxAge', 'retentionExcludePinned', 'retentionFilesOnly', 'retentionOverrideGlobal', 'encrypted'];
Meteor.methods({
saveRoomSettings(rid, settings, value) {
const userId = Meteor.userId();
Expand Down Expand Up @@ -79,6 +79,12 @@ Meteor.methods({
action: 'Viewing_room_administration',
});
}
if (settings === 'featured' && !hasPermission(userId, 'view-room-administration')) {
throw new Meteor.Error('error-action-not-allowed', 'Viewing room administration is not allowed', {
method: 'saveRoomSettings',
action: 'Viewing_room_administration',
});
}
if (setting === 'roomType' && value !== room.t && value === 'c' && !hasPermission(userId, 'create-c')) {
throw new Meteor.Error('error-action-not-allowed', 'Changing a private group to a public channel is not allowed', {
method: 'saveRoomSettings',
Expand Down Expand Up @@ -194,6 +200,9 @@ Meteor.methods({
case 'default':
Rooms.saveDefaultById(rid, value);
break;
case 'featured':
Rooms.saveFeaturedById(rid, value);
break;
case 'retentionEnabled':
Rooms.saveRetentionEnabledById(rid, value);
break;
Expand Down
1 change: 1 addition & 0 deletions app/lib/server/functions/getFullUserData.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const fullFields = {
emails: 1,
phone: 1,
statusConnection: 1,
bio: 1,
createdAt: 1,
lastLogin: 1,
services: 1,
Expand Down
26 changes: 22 additions & 4 deletions app/lib/server/functions/saveUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,23 @@ function validateUserEditing(userId, userData) {
}
}

const handleBio = (updateUser, bio) => {
if (bio) {
if (bio.trim()) {
if (typeof bio !== 'string' || bio.length > 260) {
throw new Meteor.Error('error-invalid-field', 'bio', {
method: 'saveUserProfile',
});
}
updateUser.$set = updateUser.$set || {};
updateUser.$set.bio = bio;
} else {
updateUser.$unset = updateUser.$unset || {};
updateUser.$unset.bio = 1;
}
}
};

export const saveUser = function(userId, userData) {
validateUserData(userId, userData);
let sendPassword = false;
Expand Down Expand Up @@ -225,14 +242,11 @@ export const saveUser = function(userId, userData) {
const updateUser = {
$set: {
roles: userData.roles || ['user'],
...typeof userData.name !== 'undefined' && { name: userData.name },
settings: userData.settings || {},
},
};

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

if (typeof userData.requirePasswordChange !== 'undefined') {
updateUser.$set.requirePasswordChange = userData.requirePasswordChange;
}
Expand All @@ -241,6 +255,8 @@ export const saveUser = function(userId, userData) {
updateUser.$set['emails.0.verified'] = userData.verified;
}

handleBio(updateUser, userData.bio);

Meteor.users.update({ _id }, updateUser);

if (userData.sendWelcomeEmail) {
Expand Down Expand Up @@ -294,6 +310,8 @@ export const saveUser = function(userId, userData) {
$set: {},
};

handleBio(updateUser, userData.bio);

if (userData.roles) {
updateUser.$set.roles = userData.roles;
}
Expand Down
2 changes: 1 addition & 1 deletion app/lib/server/startup/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ settings.addGroup('Accounts', function() {
type: 'boolean',
public: true,
});
this.add('Accounts_SearchFields', 'username, name', {
this.add('Accounts_SearchFields', '', {
type: 'string',
public: true,
});
Expand Down
29 changes: 29 additions & 0 deletions app/models/server/models/Rooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ export class Rooms extends Base {

this.tryEnsureIndex({ name: 1 }, { unique: true, sparse: true });
this.tryEnsureIndex({ default: 1 });
this.tryEnsureIndex({ featured: 1 });
this.tryEnsureIndex({ t: 1 });
this.tryEnsureIndex({ 'u._id': 1 });
this.tryEnsureIndex({ 'tokenpass.tokens.token': 1 });
this.tryEnsureIndex({ ts: 1 });
// discussions
this.tryEnsureIndex({ prid: 1 }, { sparse: true });
this.tryEnsureIndex({ fname: 1 }, { sparse: true });
// Livechat - statistics
this.tryEnsureIndex({ closedAt: 1 }, { sparse: true });

Expand Down Expand Up @@ -414,6 +416,20 @@ export class Rooms extends Base {
return this._db.find(query, options);
}

findByNameOrFNameAndType(name, type, options) {
const query = {
t: type,
$or: [{
name,
}, {
fname: name,
}],
};

// do not use cache
return this._db.find(query, options);
}

findByNameAndTypeNotDefault(name, type, options) {
const query = {
t: type,
Expand Down Expand Up @@ -873,6 +889,19 @@ export class Rooms extends Base {
return this.update(query, update);
}

saveFeaturedById(_id, featured) {
const query = { _id };
const set = ['true', true].includes(featured);

const update = {
[set ? '$set' : '$unset']: {
featured: true,
},
};

return this.update(query, update);
}

saveDefaultById(_id, defaultValue) {
const query = { _id };

Expand Down
28 changes: 24 additions & 4 deletions app/models/server/models/Users.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class Users extends Base {

this.tryEnsureIndex({ roles: 1 }, { sparse: 1 });
this.tryEnsureIndex({ name: 1 });
this.tryEnsureIndex({ name: 'text', username: 'text', bio: 'text' }, { default_language: 'none', language_override: 'documentLanguage' });
this.tryEnsureIndex({ createdAt: 1 });
this.tryEnsureIndex({ lastLogin: 1 });
this.tryEnsureIndex({ status: 1 });
Expand Down Expand Up @@ -688,16 +689,20 @@ export class Users extends Base {
const searchFields = forcedSearchFields || settings.get('Accounts_SearchFields').trim().split(',');

const orStmt = _.reduce(searchFields, function(acc, el) {
acc.push({ [el.trim()]: termRegex });
el = el.trim();
if (el && !['name', 'username', 'bio'].includes(el)) {
acc.push({ [el]: termRegex });
}
return acc;
}, []);

const query = {
$and: [
{
active: true,
$or: orStmt,
},
{
$or: [{
$text: { $search: searchTerm },
}, ...orStmt],
username: { $exists: true, $nin: exceptions },
},
...extraQuery,
Expand Down Expand Up @@ -1117,6 +1122,21 @@ export class Users extends Base {
return this.update(_id, update);
}

setBio(_id, bio = '') {
const update = {
...bio.trim() ? {
$set: {
bio,
},
} : {
$unset: {
bio: 1,
},
},
};
return this.update(_id, update);
}

clearSettings(_id) {
const update = {
$set: {
Expand Down
49 changes: 29 additions & 20 deletions app/ui-account/client/accountProfile.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,26 +70,6 @@
{{/if}}
</div>
</div>
<div class="rc-form-group rc-grid">
{{# with canChange=allowStatusMessageChange}}

<div class="rc-input rc-w100 padded {{#if statusMessageInvalid}}rc-input--error{{/if}}">
<label class="rc-input__label">
<div class="rc-input__title">{{_ "StatusMessage"}}</div>
<div class="rc-input__wrapper">
<div class="rc-input__icon">
{{> icon block="rc-input__icon-svg" icon="edit"}}
</div>

<input type="text" class="rc-input__element" maxlength="120" name="statusText" id="statusText" placeholder="{{_ "StatusMessage_Placeholder" }}" value="{{statusText}}" {{ifThenElse canChange '' 'disabled'}}>
</div>
</label>
{{# unless canChange}}
<div class="rc-input__description">{{_ 'StatusMessage_Change_Disabled'}}</div>
{{/unless}}
</div>
{{/with}}
</div>
<div class="rc-form-group rc-grid">
{{# with canChange=allowRealNameChange}}
<div class="rc-input rc-w50 padded {{#if nameInvalid}}rc-input--error{{/if}}">
Expand Down Expand Up @@ -129,6 +109,35 @@
</div>
{{/with}}
</div>
<div class="rc-form-group rc-grid">
{{# with canChange=allowStatusMessageChange}}
<div class="rc-input rc-w100 padded {{#if statusMessageInvalid}}rc-input--error{{/if}}">
<label class="rc-input__label">
<div class="rc-input__title">{{_ "StatusMessage"}}</div>
<div class="rc-input__wrapper">
<div class="rc-input__icon">
{{> icon block="rc-input__icon-svg" icon="edit"}}
</div>

<input type="text" class="rc-input__element" maxlength="120" name="statusText" id="statusText" placeholder="{{_ "StatusMessage_Placeholder" }}" value="{{get 'statusText'}}" {{ifThenElse canChange '' 'disabled'}}>
</div>
</label>
{{# unless canChange}}
<div class="rc-input__description">{{_ 'StatusMessage_Change_Disabled'}}</div>
{{/unless}}
</div>
{{/with}}
</div>
<div class="rc-form-group">
<div class="rc-input rc-w100 padded">
<label class="rc-input__label">
<div class="rc-input__title">{{_ "Bio"}}</div>
<div class="rc-input__wrapper">
<textarea type="text" class="rc-input__element" name="bio" id="bio" maxlength="260" rows="4" style="height: auto" placeholder="{{_ "Bio_Placeholder" }}" value="{{get 'bio'}}"></textarea>
</div>
</label>
</div>
</div>
<div class="rc-form-group rc-grid">
{{#with canChange=allowEmailChange}}
<div class="rc-input rc-w50 padded {{#if emailInvalid}}rc-input--error{{/if}}">
Expand Down
Loading

0 comments on commit f0fc738

Please sign in to comment.