Skip to content
Snippets Groups Projects
Unverified Commit 05d5d8e8 authored by eduardofcabrera's avatar eduardofcabrera Committed by GitHub
Browse files

Convert to typescript the slash commands invite files (#24311)

parent 25ab82e3
No related branches found
No related tags found
No related merge requests found
import { slashCommands } from '../../utils';
slashCommands.add('invite', undefined, {
description: 'Invite_user_to_join_channel',
params: '@username',
permission: 'add-user-to-joined-room',
});
import { slashCommands } from '../../utils/lib/slashCommand';
slashCommands.add(
'invite',
undefined,
{
description: 'Invite_user_to_join_channel',
params: '@username',
permission: 'add-user-to-joined-room',
},
undefined,
false,
undefined,
undefined,
);
import { Meteor } from 'meteor/meteor'; import { Meteor } from 'meteor/meteor';
import { Match } from 'meteor/check';
import { TAPi18n } from 'meteor/rocketchat:tap-i18n'; import { TAPi18n } from 'meteor/rocketchat:tap-i18n';
import { slashCommands } from '../../utils'; import { settings } from '../../settings/server';
import { Subscriptions } from '../../models'; import { slashCommands } from '../../utils/lib/slashCommand';
import { Subscriptions } from '../../models/server';
import { api } from '../../../server/sdk/api'; import { api } from '../../../server/sdk/api';
/* /*
...@@ -11,11 +11,7 @@ import { api } from '../../../server/sdk/api'; ...@@ -11,11 +11,7 @@ import { api } from '../../../server/sdk/api';
* @param {Object} message - The message object * @param {Object} message - The message object
*/ */
function Invite(command, params, item) { function Invite(_command: 'invite', params: string, item: Record<string, string>): void {
if (command !== 'invite' || !Match.test(params, String)) {
return;
}
const usernames = params const usernames = params
.split(/[\s,]/) .split(/[\s,]/)
.map((username) => username.replace(/(^@)|( @)/, '')) .map((username) => username.replace(/(^@)|( @)/, ''))
...@@ -23,68 +19,73 @@ function Invite(command, params, item) { ...@@ -23,68 +19,73 @@ function Invite(command, params, item) {
if (usernames.length === 0) { if (usernames.length === 0) {
return; return;
} }
let users = Meteor.users.find({ const users = Meteor.users.find({
username: { username: {
$in: usernames, $in: usernames,
}, },
}); });
const userId = Meteor.userId(); const userId = Meteor.userId() as string;
const currentUser = Meteor.users.findOne(userId);
if (users.count() === 0) { if (users.count() === 0) {
api.broadcast('notify.ephemeralMessage', userId, item.rid, { api.broadcast('notify.ephemeralMessage', userId, item.rid, {
msg: TAPi18n.__( msg: TAPi18n.__('User_doesnt_exist', {
'User_doesnt_exist', postProcess: 'sprintf',
{ sprintf: [usernames.join(' @')],
postProcess: 'sprintf', lng: settings.get('Language') || 'en',
sprintf: [usernames.join(' @')], }),
},
currentUser.language,
),
}); });
return; return;
} }
users = users.fetch().filter(function (user) { const usersFiltered = users.fetch().filter(function (user) {
const subscription = Subscriptions.findOneByRoomIdAndUserId(item.rid, user._id, { const subscription = Subscriptions.findOneByRoomIdAndUserId(item.rid, user._id, {
fields: { _id: 1 }, fields: { _id: 1 },
}); });
if (subscription == null) { if (subscription == null) {
return true; return true;
} }
const usernameStr = user.username as string;
api.broadcast('notify.ephemeralMessage', userId, item.rid, { api.broadcast('notify.ephemeralMessage', userId, item.rid, {
msg: TAPi18n.__( msg: TAPi18n.__('Username_is_already_in_here', {
'Username_is_already_in_here', postProcess: 'sprintf',
{ sprintf: [usernameStr],
postProcess: 'sprintf', lng: settings.get('Language') || 'en',
sprintf: [user.username], }),
},
currentUser.language,
),
}); });
return false; return false;
}); });
users.forEach(function (user) { usersFiltered.forEach(function (user) {
try { try {
return Meteor.call('addUserToRoom', { return Meteor.call('addUserToRoom', {
rid: item.rid, rid: item.rid,
username: user.username, username: user.username,
}); });
} catch ({ error }) { } catch ({ error }) {
if (typeof error !== 'string') {
return;
}
if (error === 'cant-invite-for-direct-room') { if (error === 'cant-invite-for-direct-room') {
api.broadcast('notify.ephemeralMessage', userId, item.rid, { api.broadcast('notify.ephemeralMessage', userId, item.rid, {
msg: TAPi18n.__('Cannot_invite_users_to_direct_rooms', null, currentUser.language), msg: TAPi18n.__('Cannot_invite_users_to_direct_rooms', { lng: settings.get('Language') || 'en' }),
}); });
} else { } else {
api.broadcast('notify.ephemeralMessage', userId, item.rid, { api.broadcast('notify.ephemeralMessage', userId, item.rid, {
msg: TAPi18n.__(error, null, currentUser.language), msg: TAPi18n.__(error, { lng: settings.get('Language') || 'en' }),
}); });
} }
} }
}); });
} }
slashCommands.add('invite', Invite, { slashCommands.add(
description: 'Invite_user_to_join_channel', 'invite',
params: '@username', Invite,
permission: 'add-user-to-joined-room', {
}); description: 'Invite_user_to_join_channel',
params: '@username',
permission: 'add-user-to-joined-room',
},
undefined,
false,
undefined,
undefined,
);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment