diff --git a/app/slashcommands-invite/client/client.js b/app/slashcommands-invite/client/client.js deleted file mode 100644 index f18c8d21c344b6ca538dbd789a9f652ceedb08ec..0000000000000000000000000000000000000000 --- a/app/slashcommands-invite/client/client.js +++ /dev/null @@ -1,7 +0,0 @@ -import { slashCommands } from '../../utils'; - -slashCommands.add('invite', undefined, { - description: 'Invite_user_to_join_channel', - params: '@username', - permission: 'add-user-to-joined-room', -}); diff --git a/app/slashcommands-invite/client/client.ts b/app/slashcommands-invite/client/client.ts new file mode 100644 index 0000000000000000000000000000000000000000..4a494287d0ae8ee735b5154d93dc603d2e1e94d7 --- /dev/null +++ b/app/slashcommands-invite/client/client.ts @@ -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, +); diff --git a/app/slashcommands-invite/client/index.js b/app/slashcommands-invite/client/index.ts similarity index 100% rename from app/slashcommands-invite/client/index.js rename to app/slashcommands-invite/client/index.ts diff --git a/app/slashcommands-invite/server/index.js b/app/slashcommands-invite/server/index.ts similarity index 100% rename from app/slashcommands-invite/server/index.js rename to app/slashcommands-invite/server/index.ts diff --git a/app/slashcommands-invite/server/server.js b/app/slashcommands-invite/server/server.js deleted file mode 100644 index eca11742a34f688dc772f1e224ce1f55f00d3fcc..0000000000000000000000000000000000000000 --- a/app/slashcommands-invite/server/server.js +++ /dev/null @@ -1,90 +0,0 @@ -import { Meteor } from 'meteor/meteor'; -import { Match } from 'meteor/check'; -import { TAPi18n } from 'meteor/rocketchat:tap-i18n'; - -import { slashCommands } from '../../utils'; -import { Subscriptions } from '../../models'; -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, params, item) { - if (command !== 'invite' || !Match.test(params, String)) { - return; - } - - const usernames = params - .split(/[\s,]/) - .map((username) => username.replace(/(^@)|( @)/, '')) - .filter((a) => a !== ''); - if (usernames.length === 0) { - return; - } - let users = Meteor.users.find({ - username: { - $in: usernames, - }, - }); - const userId = Meteor.userId(); - const currentUser = Meteor.users.findOne(userId); - if (users.count() === 0) { - api.broadcast('notify.ephemeralMessage', userId, item.rid, { - msg: TAPi18n.__( - 'User_doesnt_exist', - { - postProcess: 'sprintf', - sprintf: [usernames.join(' @')], - }, - currentUser.language, - ), - }); - return; - } - users = users.fetch().filter(function (user) { - const subscription = Subscriptions.findOneByRoomIdAndUserId(item.rid, user._id, { - fields: { _id: 1 }, - }); - if (subscription == null) { - return true; - } - api.broadcast('notify.ephemeralMessage', userId, item.rid, { - msg: TAPi18n.__( - 'Username_is_already_in_here', - { - postProcess: 'sprintf', - sprintf: [user.username], - }, - currentUser.language, - ), - }); - return false; - }); - - users.forEach(function (user) { - try { - return Meteor.call('addUserToRoom', { - rid: item.rid, - username: user.username, - }); - } catch ({ error }) { - if (error === 'cant-invite-for-direct-room') { - api.broadcast('notify.ephemeralMessage', userId, item.rid, { - msg: TAPi18n.__('Cannot_invite_users_to_direct_rooms', null, currentUser.language), - }); - } else { - api.broadcast('notify.ephemeralMessage', userId, item.rid, { - msg: TAPi18n.__(error, null, currentUser.language), - }); - } - } - }); -} - -slashCommands.add('invite', Invite, { - description: 'Invite_user_to_join_channel', - params: '@username', - permission: 'add-user-to-joined-room', -}); diff --git a/app/slashcommands-invite/server/server.ts b/app/slashcommands-invite/server/server.ts new file mode 100644 index 0000000000000000000000000000000000000000..afe86755360227d2658ebfce254ee43ec09c363c --- /dev/null +++ b/app/slashcommands-invite/server/server.ts @@ -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, +);