Skip to content

Commit

Permalink
Convert to typescript the slash commands invite files (#24311)
Browse files Browse the repository at this point in the history
Co-authored-by: Leonardo Ostjen Couto <leonardoostjen@gmail.com>
  • Loading branch information
eduardofcabrera and ostjen authored Feb 15, 2022
1 parent 25ab82e commit 05d5d8e
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 97 deletions.
7 changes: 0 additions & 7 deletions app/slashcommands-invite/client/client.js

This file was deleted.

15 changes: 15 additions & 0 deletions app/slashcommands-invite/client/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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,
);
File renamed without changes.
File renamed without changes.
90 changes: 0 additions & 90 deletions app/slashcommands-invite/server/server.js

This file was deleted.

91 changes: 91 additions & 0 deletions app/slashcommands-invite/server/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { Meteor } from 'meteor/meteor';
import { TAPi18n } from 'meteor/rocketchat:tap-i18n';

import { settings } from '../../settings/server';
import { slashCommands } from '../../utils/lib/slashCommand';
import { Subscriptions } from '../../models/server';
import { api } from '../../../server/sdk/api';

/*
* Invite is a named function that will replace /invite commands
* @param {Object} message - The message object
*/

function Invite(_command: 'invite', params: string, item: Record<string, string>): void {
const usernames = params
.split(/[\s,]/)
.map((username) => username.replace(/(^@)|( @)/, ''))
.filter((a) => a !== '');
if (usernames.length === 0) {
return;
}
const users = Meteor.users.find({
username: {
$in: usernames,
},
});
const userId = Meteor.userId() as string;
if (users.count() === 0) {
api.broadcast('notify.ephemeralMessage', userId, item.rid, {
msg: TAPi18n.__('User_doesnt_exist', {
postProcess: 'sprintf',
sprintf: [usernames.join(' @')],
lng: settings.get('Language') || 'en',
}),
});
return;
}
const usersFiltered = users.fetch().filter(function (user) {
const subscription = Subscriptions.findOneByRoomIdAndUserId(item.rid, user._id, {
fields: { _id: 1 },
});
if (subscription == null) {
return true;
}
const usernameStr = user.username as string;
api.broadcast('notify.ephemeralMessage', userId, item.rid, {
msg: TAPi18n.__('Username_is_already_in_here', {
postProcess: 'sprintf',
sprintf: [usernameStr],
lng: settings.get('Language') || 'en',
}),
});
return false;
});

usersFiltered.forEach(function (user) {
try {
return Meteor.call('addUserToRoom', {
rid: item.rid,
username: user.username,
});
} catch ({ error }) {
if (typeof error !== 'string') {
return;
}
if (error === 'cant-invite-for-direct-room') {
api.broadcast('notify.ephemeralMessage', userId, item.rid, {
msg: TAPi18n.__('Cannot_invite_users_to_direct_rooms', { lng: settings.get('Language') || 'en' }),
});
} else {
api.broadcast('notify.ephemeralMessage', userId, item.rid, {
msg: TAPi18n.__(error, { lng: settings.get('Language') || 'en' }),
});
}
}
});
}

slashCommands.add(
'invite',
Invite,
{
description: 'Invite_user_to_join_channel',
params: '@username',
permission: 'add-user-to-joined-room',
},
undefined,
false,
undefined,
undefined,
);

0 comments on commit 05d5d8e

Please sign in to comment.