Skip to content
Snippets Groups Projects
Commit 054a2415 authored by Guilherme Gazzo's avatar Guilherme Gazzo
Browse files

slash command invite-all from coffee to pure js

parent 113a891c
No related merge requests found
RocketChat.slashCommands.add 'invite-all', undefined,
description: 'Invite_user_to_join_channel_all'
params: '#room'
RocketChat.slashCommands.add('invite-all', undefined, {
description: 'Invite_user_to_join_channel_all',
params: '#room'
});
\ No newline at end of file
......@@ -16,6 +16,6 @@ Package.onUse(function(api) {
api.use('templating', 'client');
api.addFiles('client.coffee', 'client');
api.addFiles('server.coffee', 'server');
api.addFiles('client.js', 'client');
api.addFiles('server.js', 'server');
});
###
# Invite is a named function that will replace /invite commands
# @param {Object} message - The message object
###
class InviteAll
constructor: (command, params, item) ->
return if command isnt 'invite-all' or not Match.test params, String
regexp = /#([\d-_\w]+)/g
channel = regexp.exec(params.trim())?[1]
return if not channel
room = RocketChat.models.Rooms.findOneByName(channel)
users = (RocketChat.models.Rooms.findOneById(item.rid)?.usernames) || []
return if not room
Meteor.call 'createChannel', channel, users
currentUser = Meteor.users.findOne Meteor.userId()
users.forEach((user) ->
try
RocketChat.Notifications.notifyUser Meteor.userId(), 'message', {
_id: Random.id()
rid: item.rid
ts: new Date
msg: room
}
Meteor.call 'addUserToRoom',
rid: room._id
username: user
catch e
if e.error is 'cant-invite-for-direct-room'
RocketChat.Notifications.notifyUser Meteor.userId(), 'message', {
_id: Random.id()
rid: item.rid
ts: new Date
msg: TAPi18n.__('Cannot_invite_users_to_direct_rooms', null, currentUser.language)
}
else
RocketChat.Notifications.notifyUser Meteor.userId(), 'message', {
_id: Random.id()
rid: item.rid
ts: new Date
msg: TAPi18n.__(e.error, null, currentUser.language)
}
return
)
RocketChat.slashCommands.add 'invite-all', InviteAll
/*
* Invite is a named function that will replace /invite commands
* @param {Object} message - The message object
*/
function InviteAll(command, params, item) {
if (command !== 'invite-all' || !Match.test(params, String)) {
return;
}
let regexp = /#([\d-_\w]+)/g;
let [, channel] = regexp.exec(params.trim());
if (!channel) {
return;
}
let room = RocketChat.models.Rooms.findOneByName(channel);
let users = RocketChat.models.Rooms.findOneById(item.rid).usernames || [];
if (!room) {
return Meteor.call('createChannel', channel, users);
}
let currentUser = Meteor.users.findOne(Meteor.userId());
users.forEach(function(user) {
try {
Meteor.call('addUserToRoom', {
rid: room._id,
username: user
});
} catch (e) {
let msg = e.error === 'cant-invite-for-direct-room' ? 'Cannot_invite_users_to_direct_rooms' : e.error;
RocketChat.Notifications.notifyUser(Meteor.userId(), 'message', {
_id: Random.id(),
rid: item.rid,
ts: new Date(),
msg: TAPi18n.__(msg, null, currentUser.language)
});
}
});
}
RocketChat.slashCommands.add('invite-all', InviteAll);
module.exports = InviteAll;
\ No newline at end of file
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