Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

converted slashcommand-invite coffee to js #6497

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions packages/rocketchat-slashcommands-invite/client.coffee

This file was deleted.

4 changes: 4 additions & 0 deletions packages/rocketchat-slashcommands-invite/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
RocketChat.slashCommands.add('invite', null, {
description: 'Invite_user_to_join_channel',
params: '@username'
});
5 changes: 2 additions & 3 deletions packages/rocketchat-slashcommands-invite/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ Package.onUse(function(api) {

api.use([
'ecmascript',
'coffeescript',
'check',
'rocketchat:lib'
]);

api.use('templating', 'client');

api.addFiles('client.coffee', 'client');
api.addFiles('server.coffee', 'server');
api.addFiles('client.js', 'client');
api.addFiles('server.js', 'server');
});
68 changes: 0 additions & 68 deletions packages/rocketchat-slashcommands-invite/server.coffee

This file was deleted.

82 changes: 82 additions & 0 deletions packages/rocketchat-slashcommands-invite/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@

/*
* 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;
}
let usernames = params.replace(/@/g, '').split(/[\s,]/).filter((a) => a !== '');
if (usernames.length === 0) {
return;
}
const users = Meteor.users.find({
username: {
$in: usernames
}
});
const currentUser = Meteor.users.findOne(Meteor.userId());
if (users.count() === 0) {
RocketChat.Notifications.notifyUser(Meteor.userId(), 'message', {
_id: Random.id(),
rid: item.rid,
ts: new Date,
msg: TAPi18n.__('User_doesnt_exist', {
postProcess: 'sprintf',
sprintf: [usernames.join(' @')]
}, currentUser.language)
});
return;
}
usernames = usernames.filter(function(username) {
if (RocketChat.models.Rooms.findOneByIdContainingUsername(item.rid, username) == null) {
return true;
}
RocketChat.Notifications.notifyUser(Meteor.userId(), 'message', {
_id: Random.id(),
rid: item.rid,
ts: new Date,
msg: TAPi18n.__('Username_is_already_in_here', {
postProcess: 'sprintf',
sprintf: [username]
}, currentUser.language)
});
return false;
});
if (usernames.length === 0) {
return;
}
users.forEach(function(user) {

try {
return Meteor.call('addUserToRoom', {
rid: item.rid,
username: user.username
});
} catch ({error}) {
if (error === '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.__(error, null, currentUser.language)
});
}
}
});
}

RocketChat.slashCommands.add('invite', Invite);

export {Invite};