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

[FIX] Custom status fixes #14853

Merged
merged 15 commits into from
Jun 27, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion app/api/server/v1/channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ API.v1.addRoute('channels.members', { authRequired: true }, {
const members = subscriptions.fetch().map((s) => s.u && s.u._id);

const users = Users.find({ _id: { $in: members } }, {
fields: { _id: 1, username: 1, name: 1, status: 1, utcOffset: 1 },
fields: { _id: 1, username: 1, name: 1, status: 1, statusText: 1, utcOffset: 1 },
sort: { username: sort.username != null ? sort.username : 1 },
}).fetch();

Expand Down
2 changes: 1 addition & 1 deletion app/api/server/v1/groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ API.v1.addRoute('groups.members', { authRequired: true }, {
const members = subscriptions.fetch().map((s) => s.u && s.u._id);

const users = Users.find({ _id: { $in: members } }, {
fields: { _id: 1, username: 1, name: 1, status: 1, utcOffset: 1 },
fields: { _id: 1, username: 1, name: 1, status: 1, statusText: 1, utcOffset: 1 },
sort: { username: sort.username != null ? sort.username : 1 },
}).fetch();

Expand Down
2 changes: 1 addition & 1 deletion app/api/server/v1/im.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ API.v1.addRoute(['dm.members', 'im.members'], { authRequired: true }, {
const members = cursor.fetch().map((s) => s.u && s.u.username);

const users = Users.find({ username: { $in: members } }, {
fields: { _id: 1, username: 1, name: 1, status: 1, utcOffset: 1 },
fields: { _id: 1, username: 1, name: 1, status: 1, statusText: 1, utcOffset: 1 },
sort: { username: sort && sort.username ? sort.username : 1 },
}).fetch();

Expand Down
4 changes: 2 additions & 2 deletions app/api/server/v1/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
} from '../../../lib';
import { getFullUserData } from '../../../lib/server/functions/getFullUserData';
import { API } from '../api';
import { setStatusMessage } from '../../../lib/server';
import { setStatusText } from '../../../lib/server';

API.v1.addRoute('users.create', { authRequired: true }, {
post() {
Expand Down Expand Up @@ -370,7 +370,7 @@ API.v1.addRoute('users.setStatus', { authRequired: true }, {

Meteor.runAsUser(user._id, () => {
if (this.bodyParams.message) {
setStatusMessage(user._id, this.bodyParams.message);
setStatusText(user._id, this.bodyParams.message);
}
if (this.bodyParams.status) {
const validStatus = ['online', 'away', 'offline', 'busy'];
Expand Down
9 changes: 5 additions & 4 deletions app/lib/lib/roomTypes/direct.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,12 @@ export class DirectMessageRoomType extends RoomTypeConfig {
}

getUserStatusText(roomId) {
const userId = roomId.replace(Meteor.userId(), '');
const userData = Users.findOne({ _id: userId });
if (userData && userData.statusText) {
return userData.statusText;
const subscription = Subscriptions.findOne({ rid: roomId });
if (subscription == null) {
return;
}

return Session.get(`user_${ subscription.name }_status_text`);
}

getDisplayName(room) {
Expand Down
2 changes: 1 addition & 1 deletion app/lib/server/functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export { saveUser } from './saveUser';
export { sendMessage } from './sendMessage';
export { setEmail } from './setEmail';
export { setRealName, _setRealName } from './setRealName';
export { setStatusMessage, _setStatusMessage } from './setStatusMessage';
export { setStatusText, _setStatusText } from './setStatusText';
export { setUserAvatar } from './setUserAvatar';
export { _setUsername, setUsername } from './setUsername';
export { unarchiveRoom } from './unarchiveRoom';
Expand Down
4 changes: 2 additions & 2 deletions app/lib/server/functions/saveUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { settings } from '../../../settings';
import PasswordPolicy from '../lib/PasswordPolicyClass';
import { validateEmailDomain } from '../lib';

import { checkEmailAvailability, checkUsernameAvailability, setUserAvatar, setEmail, setRealName, setUsername, setStatusMessage } from '.';
import { checkEmailAvailability, checkUsernameAvailability, setUserAvatar, setEmail, setRealName, setUsername, setStatusText } from '.';

const passwordPolicy = new PasswordPolicy();

Expand Down Expand Up @@ -256,7 +256,7 @@ export const saveUser = function(userId, userData) {
}

if (typeof userData.statusText === 'string') {
setStatusMessage(userData._id, userData.statusText);
setStatusText(userData._id, userData.statusText);
}

if (userData.email) {
Expand Down
45 changes: 0 additions & 45 deletions app/lib/server/functions/setStatusMessage.js

This file was deleted.

53 changes: 53 additions & 0 deletions app/lib/server/functions/setStatusText.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Meteor } from 'meteor/meteor';
import s from 'underscore.string';

import { Users } from '../../../models';
import { Notifications } from '../../../notifications';
import { hasPermission } from '../../../authorization';
import { RateLimiter } from '../lib';

// mirror of object in /imports/startup/client/listenActiveUsers.js - keep updated
const STATUS_MAP = {
offline: 0,
online: 1,
away: 2,
busy: 3,
};

export const _setStatusText = function(userId, statusText) {
statusText = s.trim(statusText);
if (statusText.length > 120) {
statusText = statusText.substr(0, 120);
}

if (!userId) {
return false;
}

const user = Users.findOneById(userId);

// User already has desired statusText, return
if (user.statusText === statusText) {
return user;
}

// Set new statusText
Users.updateStatusText(user._id, statusText);
user.statusText = statusText;

Notifications.notifyLogged('user-status', [
user._id,
user.username,
STATUS_MAP[user.status],
statusText,
]);

return true;
};

export const setStatusText = RateLimiter.limitFunction(_setStatusText, 1, 60000, {
0() {
// Administrators have permission to change others status, so don't limit those
return !Meteor.userId() || !hasPermission(Meteor.userId(), 'edit-other-user-info');
},
});
5 changes: 3 additions & 2 deletions app/ui-flextab/client/tabs/userInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ Template.userInfo.helpers({
userStatus() {
const user = Template.instance().user.get();
const userStatus = Session.get(`user_${ user.username }_status`);
return userStatus || 'offline';
return userStatus || TAPi18n.__('offline');
},

userStatusText() {
Expand All @@ -92,7 +92,8 @@ Template.userInfo.helpers({
}

const user = Template.instance().user.get();
return t(Session.get(`user_${ user.username }_status`));
const userStatus = Session.get(`user_${ user.username }_status`);
return userStatus || TAPi18n.__('offline');
},

email() {
Expand Down
9 changes: 4 additions & 5 deletions app/ui-sidenav/client/sidebarHeader.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ Template.sidebarHeader.helpers({
};
}
return id && Meteor.users.findOne(id, { fields: {
username: 1, status: 1,
username: 1, status: 1, statusText: 1,
} });
},
toolbarButtons() {
Expand All @@ -317,22 +317,21 @@ Template.sidebarHeader.events({
if (!(Meteor.userId() == null && settings.get('Accounts_AllowAnonymousRead'))) {
const user = Meteor.user();

const statusText = user.statusText || t(user.status);

const userStatusList = Object.keys(userStatus.list).map((key) => {
const status = userStatus.list[key];
const customName = status.localizeName ? null : status.name;
const name = status.localizeName ? t(status.name) : status.name;
const modifier = status.statusType || user.status;

return {
icon: 'circle',
name,
modifier,
action: () => setStatus(status.statusType, customName),
action: () => setStatus(status.statusType, statusText),
};
});

const statusText = user.statusText || t(user.status);

userStatusList.push({
icon: 'edit',
name: t('Edit_Status'),
Expand Down
14 changes: 6 additions & 8 deletions app/ui/client/components/header/headerRoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ const getUserStatus = (id) => {
return roomTypes.getUserStatus(roomData.t, id) || 'offline';
};

const getUserStatusText = (id) => {
const roomData = Session.get(`roomData${ id }`);
return roomTypes.getUserStatusText(roomData.t, id) || 'offline';
Hudell marked this conversation as resolved.
Show resolved Hide resolved
};

Template.headerRoom.helpers({
back() {
return Template.instance().data.back;
Expand Down Expand Up @@ -115,14 +120,7 @@ Template.headerRoom.helpers({
},

userStatusText() {
const roomData = Session.get(`roomData${ this._id }`);
const statusText = roomTypes.getUserStatusText(roomData.t, this._id);

if (s.trim(statusText)) {
return statusText;
}

return t(getUserStatus(this._id));
return getUserStatusText(this._id);
},

showToggleFavorite() {
Expand Down
4 changes: 2 additions & 2 deletions app/user-status/server/methods/setUserStatus.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Meteor } from 'meteor/meteor';
import { check } from 'meteor/check';

import { settings } from '../../../settings';
import { RateLimiter, setStatusMessage } from '../../../lib';
import { RateLimiter, setStatusText } from '../../../lib';

Meteor.methods({
setUserStatus(statusType, statusText) {
Expand All @@ -20,7 +20,7 @@ Meteor.methods({
}

const userId = Meteor.userId();
setStatusMessage(userId, statusText);
setStatusText(userId, statusText);
}
},
});
Expand Down
4 changes: 2 additions & 2 deletions imports/startup/client/listenActiveUsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ Tracker.autorun(() => {
});

Meteor.startup(function() {
Notifications.onLogged('user-status', ([_id, username, status]) => {
Notifications.onLogged('user-status', ([_id, username, status, statusText]) => {
// only set after first request completed
if (lastStatusChange) {
lastStatusChange = new Date();
}

saveUser({ _id, username, status: STATUS_MAP[status] }, true);
saveUser({ _id, username, status: STATUS_MAP[status], statusText }, true);
});

Notifications.onLogged('Users:NameChanged', ({ _id, username }) => {
Expand Down
2 changes: 2 additions & 0 deletions imports/users-presence/server/activeUsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ UserPresenceEvents.on('setUserStatus', (user, status/* , statusConnection*/) =>
const {
_id,
username,
statusText,
} = user;

// since this callback can be called by only one instance in the cluster
Expand All @@ -22,5 +23,6 @@ UserPresenceEvents.on('setUserStatus', (user, status/* , statusConnection*/) =>
_id,
username,
STATUS_MAP[status],
statusText,
]);
});
1 change: 1 addition & 0 deletions server/publications/spotlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Meteor.methods({
username: 1,
name: 1,
status: 1,
statusText: 1,
},
sort: {},
};
Expand Down