-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Expand file tree
/
Copy pathsetStatusText.ts
More file actions
45 lines (39 loc) · 1.08 KB
/
setStatusText.ts
File metadata and controls
45 lines (39 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { api } from '@rocket.chat/core-services';
import type { IUser } from '@rocket.chat/core-typings';
import type { Updater } from '@rocket.chat/models';
import { Users } from '@rocket.chat/models';
import type { ClientSession } from 'mongodb';
import { onceTransactionCommitedSuccessfully } from '../../../../server/database/utils';
export async function setStatusText(
user: IUser,
statusText: string,
{
updater,
session,
emit = true,
}: {
updater?: Updater<IUser>;
session?: ClientSession;
emit?: boolean;
} = {},
): Promise<boolean> {
statusText = statusText.trim().substr(0, 120);
if (user.statusText === statusText) {
return true;
}
if (updater) {
updater.set('statusText', statusText);
} else {
await Users.updateStatusText(user._id, statusText, { session });
}
if (emit) {
const { _id, username, status, name, roles } = user;
await onceTransactionCommitedSuccessfully(() => {
void api.broadcast('presence.status', {
user: { _id, username, status, statusText, name, roles },
previousStatus: status,
});
}, session);
}
return true;
}