-
Notifications
You must be signed in to change notification settings - Fork 11k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert to typescript the slash commands invite files (#24311)
Co-authored-by: Leonardo Ostjen Couto <leonardoostjen@gmail.com>
- Loading branch information
1 parent
25ab82e
commit 05d5d8e
Showing
6 changed files
with
106 additions
and
97 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); |