Skip to content

Commit 12095db

Browse files
committed
rename isFederatedUsername to sanitizeUsername
1 parent 50cca84 commit 12095db

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

apps/meteor/app/lib/server/methods/addUsersToRoom.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ declare module '@rocket.chat/ddp-client' {
1818
}
1919
}
2020

21+
export const sanitizeUsername = (username: string) => {
22+
const isFederatedUsername = username.includes('@') && username.includes(':');
23+
if (isFederatedUsername) {
24+
return username;
25+
}
26+
27+
return username.replace(/(^@)|( @)/, '');
28+
};
29+
2130
export const addUsersToRoomMethod = async (userId: string, data: { rid: string; users: string[] }, user?: IUser): Promise<boolean> => {
2231
if (!userId) {
2332
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
@@ -79,7 +88,7 @@ export const addUsersToRoomMethod = async (userId: string, data: { rid: string;
7988

8089
await Promise.all(
8190
data.users.map(async (username) => {
82-
const newUser = await Users.findOneByUsernameIgnoringCase(username);
91+
const newUser = await Users.findOneByUsernameIgnoringCase(sanitizeUsername(username));
8392
if (!newUser) {
8493
throw new Meteor.Error('error-user-not-found', 'User not found', {
8594
method: 'addUsersToRoom',

apps/meteor/app/slashcommands-invite/server/server.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,10 @@ import { Subscriptions, Users } from '@rocket.chat/models';
44
import { Meteor } from 'meteor/meteor';
55

66
import { i18n } from '../../../server/lib/i18n';
7-
import { addUsersToRoomMethod } from '../../lib/server/methods/addUsersToRoom';
7+
import { addUsersToRoomMethod, sanitizeUsername } from '../../lib/server/methods/addUsersToRoom';
88
import { settings } from '../../settings/server';
99
import { slashCommands } from '../../utils/server/slashCommand';
1010

11-
const isFederatedUsername = (username: string) => {
12-
return username.includes('@') && username.includes(':');
13-
};
14-
1511
/*
1612
* Invite is a named function that will replace /invite commands
1713
* @param {Object} message - The message object
@@ -21,7 +17,7 @@ slashCommands.add({
2117
callback: async ({ params, message, userId }: SlashCommandCallbackParams<'invite'>): Promise<void> => {
2218
const usernames = params
2319
.split(/[\s,]/)
24-
.map((username) => (isFederatedUsername(username) ? username : username.replace(/(^@)|( @)/, '')))
20+
.map((username) => sanitizeUsername(username))
2521
.filter((a) => a !== '');
2622
if (usernames.length === 0) {
2723
return;

0 commit comments

Comments
 (0)