Skip to content

fix: status-text update in real time #35378

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

Open
wants to merge 15 commits into
base: develop
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .changeset/hungry-news-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rocket.chat/meteor': major
---

fix realtime user statusText update
52 changes: 27 additions & 25 deletions apps/meteor/app/api/server/v1/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import { generateUsernameSuggestion } from '../../../lib/server/functions/getUse
import { saveCustomFields } from '../../../lib/server/functions/saveCustomFields';
import { saveCustomFieldsWithoutValidation } from '../../../lib/server/functions/saveCustomFieldsWithoutValidation';
import { saveUser } from '../../../lib/server/functions/saveUser';
import { setStatusText } from '../../../lib/server/functions/setStatusText';
import { getNewStatusText } from '../../../lib/server/functions/setStatusText';
import { setUserAvatar } from '../../../lib/server/functions/setUserAvatar';
import { setUsernameWithValidation } from '../../../lib/server/functions/setUsername';
import { validateCustomFields } from '../../../lib/server/functions/validateCustomFields';
Expand Down Expand Up @@ -1313,10 +1313,12 @@ API.v1.addRoute(
return API.v1.forbidden();
}

// TODO refactor to not update the user twice (one inside of `setStatusText` and then later just the status + statusDefault)

const updateFields: Record<string, any> = {};
if (this.bodyParams.message || this.bodyParams.message === '') {
await setStatusText(user._id, this.bodyParams.message);
const statusText = getNewStatusText(user.statusText, this.bodyParams.message);
if (statusText) {
updateFields.statusText = statusText;
}
}
if (this.bodyParams.status) {
const validStatus = ['online', 'away', 'offline', 'busy'];
Expand All @@ -1328,29 +1330,29 @@ API.v1.addRoute(
method: 'users.setStatus',
});
}
updateFields.status = status;
updateFields.statusDefault = status;
}
}
if (Object.keys(updateFields).length > 0) {
await Users.updateOne(
{ _id: user._id },
{
$set: updateFields,
},
);

await Users.updateOne(
{ _id: user._id },
{
$set: {
status,
statusDefault: status,
},
},
);

const { _id, username, statusText, roles, name } = user;
void api.broadcast('presence.status', {
user: { status, _id, username, statusText, roles, name },
previousStatus: user.status,
});
const { _id, username, statusText, roles, name, status } = { ...user, ...updateFields };
void api.broadcast('presence.status', {
user: { status, _id, username, statusText, roles, name },
previousStatus: user.status,
});

void wrapExceptions(() => Calendar.cancelUpcomingStatusChanges(user._id)).suppress();
} else {
throw new Meteor.Error('error-invalid-status', 'Valid status types include online, away, offline, and busy.', {
method: 'users.setStatus',
});
}
void wrapExceptions(() => Calendar.cancelUpcomingStatusChanges(user._id)).suppress();
} else {
throw new Meteor.Error('error-invalid-status', 'Valid status types include online, away, offline, and busy.', {
method: 'users.setStatus',
});
}

return API.v1.success();
Expand Down
11 changes: 8 additions & 3 deletions apps/meteor/app/lib/server/functions/setStatusText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ import { onceTransactionCommitedSuccessfully } from '../../../../server/database
import { hasPermissionAsync } from '../../../authorization/server/functions/hasPermission';
import { RateLimiter } from '../lib';

export const getNewStatusText = (previousStatusText: string | undefined, newStatusText: string) => {
const statusText = newStatusText.trim().substr(0, 120);
return previousStatusText === statusText ? '' : statusText;
};

async function _setStatusText(userId: string, statusText: string, updater?: Updater<IUser>, session?: ClientSession): Promise<boolean> {
if (!userId) {
return false;
}

statusText = statusText.trim().substr(0, 120);

const user = await Users.findOneById<Pick<IUser, '_id' | 'username' | 'name' | 'status' | 'roles' | 'statusText'>>(userId, {
projection: { username: 1, name: 1, status: 1, roles: 1, statusText: 1 },
session,
Expand All @@ -25,7 +28,9 @@ async function _setStatusText(userId: string, statusText: string, updater?: Upda
return false;
}

if (user.statusText === statusText) {
statusText = getNewStatusText(user.statusText, statusText);

if (!statusText) {
return true;
}

Expand Down
Loading