-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Expand file tree
/
Copy pathserver.ts
More file actions
135 lines (124 loc) · 4.5 KB
/
server.ts
File metadata and controls
135 lines (124 loc) · 4.5 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { api, FederationMatrix } from '@rocket.chat/core-services';
import type { IUser, SlashCommandCallbackParams } from '@rocket.chat/core-typings';
import { validateFederatedUsername } from '@rocket.chat/federation-matrix';
import { Subscriptions, Users, Rooms } from '@rocket.chat/models';
import { Meteor } from 'meteor/meteor';
import { i18n } from '../../../server/lib/i18n';
import { FederationActions } from '../../../server/services/room/hooks/BeforeFederationActions';
import { addUsersToRoomMethod, sanitizeUsername } from '../../lib/server/methods/addUsersToRoom';
import { settings } from '../../settings/server';
import { slashCommands } from '../../utils/server/slashCommand';
/*
* Invite is a named function that will replace /invite commands
* @param {Object} message - The message object
*/
slashCommands.add({
command: 'invite',
callback: async ({ params, message, userId }: SlashCommandCallbackParams<'invite'>): Promise<void> => {
let usernames = params
.split(/[\s,]/)
.map((username) => sanitizeUsername(username))
.filter((a) => a !== '');
if (usernames.length === 0) {
return;
}
// Get room information for federation check
const room = await Rooms.findOneById(message.rid);
if (!room) {
void api.broadcast('notify.ephemeralMessage', userId, message.rid, {
msg: i18n.t('error-invalid-room', { lng: settings.get('Language') || 'en' }),
});
return;
}
// Ensure federated users exist locally before looking them up
const federatedUsernames = usernames.filter((u) => validateFederatedUsername(u)) as string[];
if (federatedUsernames.length > 0) {
if (FederationActions.shouldPerformFederationAction(room)) {
await FederationMatrix.ensureFederatedUsersExistLocally(federatedUsernames);
} else {
void api.broadcast('notify.ephemeralMessage', userId, message.rid, {
msg: i18n.t('You_cannot_add_external_users_to_non_federated_room', { lng: settings.get('Language') || 'en' }),
});
// These federated users shouldn't be invited and we already broadcasted the error message
usernames = usernames.filter((username) => {
return !federatedUsernames.includes(username);
});
if (usernames.length === 0) {
return;
}
}
}
const users = await Users.findByUsernames(usernames).toArray();
if (users.length === 0) {
void api.broadcast('notify.ephemeralMessage', userId, message.rid, {
msg: i18n.t('User_doesnt_exist', {
postProcess: 'sprintf',
sprintf: [usernames.join(' @')],
lng: settings.get('Language') || 'en',
}),
});
return;
}
const usersFiltered: IUser[] = [];
for await (const user of users) {
const subscription = await Subscriptions.findOneByRoomIdAndUserId(message.rid, user._id, {
projection: { _id: 1 },
});
if (subscription == null) {
usersFiltered.push(user as IUser);
continue;
}
const usernameStr = user.username as string;
void api.broadcast('notify.ephemeralMessage', userId, message.rid, {
msg: i18n.t('Username_is_already_in_here', {
postProcess: 'sprintf',
sprintf: [usernameStr],
lng: settings.get('Language') || 'en',
}),
});
}
const inviter = await Users.findOneById(userId);
if (!inviter) {
throw new Meteor.Error('error-user-not-found', 'Inviter not found', {
method: 'slashcommand-invite',
});
}
await Promise.all(
usersFiltered.map(async (user) => {
try {
// TODO: Refactor this to return an error if some user fails to be added
return await addUsersToRoomMethod(
userId,
{
rid: message.rid,
users: [user.username || ''],
},
inviter,
);
} catch ({ error }: any) {
if (typeof error !== 'string') {
return;
}
if (error === 'error-federated-users-in-non-federated-rooms') {
void api.broadcast('notify.ephemeralMessage', userId, message.rid, {
msg: i18n.t('You_cannot_add_external_users_to_non_federated_room', { lng: settings.get('Language') || 'en' }),
});
} else if (error === 'cant-invite-for-direct-room') {
void api.broadcast('notify.ephemeralMessage', userId, message.rid, {
msg: i18n.t('Cannot_invite_users_to_direct_rooms', { lng: settings.get('Language') || 'en' }),
});
} else {
void api.broadcast('notify.ephemeralMessage', userId, message.rid, {
msg: i18n.t(error, { lng: settings.get('Language') || 'en' }),
});
}
}
}),
);
},
options: {
description: 'Invite_user_to_join_channel',
params: '@username',
permission: 'add-user-to-joined-room',
},
});