-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Expand file tree
/
Copy pathaddUserToDefaultChannels.ts
More file actions
47 lines (40 loc) · 1.71 KB
/
addUserToDefaultChannels.ts
File metadata and controls
47 lines (40 loc) · 1.71 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
46
47
import { Message } from '@rocket.chat/core-services';
import type { IUser } from '@rocket.chat/core-typings';
import { Subscriptions } from '@rocket.chat/models';
import { getDefaultChannels } from './getDefaultChannels';
import { callbacks } from '../../../../server/lib/callbacks';
import { getSubscriptionAutotranslateDefaultConfig } from '../../../../server/lib/getSubscriptionAutotranslateDefaultConfig';
import { settings } from '../../../settings/server';
import { getDefaultSubscriptionPref } from '../../../utils/lib/getDefaultSubscriptionPref';
import { notifyOnSubscriptionChangedById } from '../lib/notifyListener';
export const addUserToDefaultChannels = async function (user: IUser, silenced?: boolean): Promise<void> {
await callbacks.run('beforeJoinDefaultChannels', user);
const defaultRooms = await getDefaultChannels();
for await (const room of defaultRooms) {
if (settings.get('ABAC_Enabled') && room?.abacAttributes?.length) {
continue;
}
if (!(await Subscriptions.findOneByRoomIdAndUserId(room._id, user._id, { projection: { _id: 1 } }))) {
const autoTranslateConfig = getSubscriptionAutotranslateDefaultConfig(user);
// Add a subscription to this user
const { insertedId } = await Subscriptions.createWithRoomAndUser(room, user, {
ts: new Date(),
open: true,
alert: true,
unread: 1,
userMentions: 1,
groupMentions: 0,
...(room.favorite && { f: true }),
...autoTranslateConfig,
...getDefaultSubscriptionPref(user),
});
if (insertedId) {
void notifyOnSubscriptionChangedById(insertedId, 'inserted');
}
// Insert user joined message
if (!silenced) {
await Message.saveSystemMessage('uj', room._id, user.username || '', user);
}
}
}
};