Skip to content

Commit 4b6c9c8

Browse files
authored
Merge pull request RocketChat#5733 from ggazzo/xk-task1
(slash commands) invite all users to another room, create a private channel and send a direct message
2 parents d0519dc + 8035186 commit 4b6c9c8

File tree

9 files changed

+181
-3
lines changed

9 files changed

+181
-3
lines changed

.meteor/packages

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,12 @@ rocketchat:slashcommands-archive
9696
rocketchat:slashcommands-asciiarts
9797
rocketchat:slashcommands-create
9898
rocketchat:slashcommands-invite
99+
rocketchat:slashcommands-invite-all
99100
rocketchat:slashcommands-join
100101
rocketchat:slashcommands-kick
101102
rocketchat:slashcommands-leave
102103
rocketchat:slashcommands-me
104+
rocketchat:slashcommands-msg
103105
rocketchat:slashcommands-mute
104106
rocketchat:slashcommands-open
105107
rocketchat:slashcommands-topic

.meteor/versions

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,12 @@ rocketchat:[email protected]
182182
183183
184184
185+
185186
186187
187188
188189
190+
189191
190192
191193

packages/rocketchat-i18n/i18n/en.i18n.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,8 @@
270270
"Change_Room_Type": "Changing the Room Type",
271271
"channel": "channel",
272272
"Channel": "Channel",
273-
"Channel_already_exist": "The channel '#%s' already exists.",
273+
"Channel_already_exist": "The channel `#%s` already exists.",
274+
"Channel_created": "Channel `#%s` created.",
274275
"Channel_already_Unarchived": "Channel with name `#%s` is already in Unarchived state",
275276
"Channel_Archived": "Channel with name `#%s` has been archived successfully",
276277
"Channel_doesnt_exist": "The channel `#%s` does not exist.",
@@ -494,6 +495,7 @@
494495
"error-user-is-not-activated": "User is not activated",
495496
"error-user-not-in-room": "User is not in this room",
496497
"error-user-registration-disabled": "User registration is disabled",
498+
"error-user-limit-exceeded" : "The number of users you are trying to invite to #channel_name exceeds the limit set by the administrator",
497499
"error-user-registration-secret": "User registration is only allowed via Secret URL",
498500
"error-you-are-last-owner": "You are the last owner. Please set new owner before leaving the room.",
499501
"Error_changing_password": "Error changing password",
@@ -660,6 +662,8 @@
660662
"Invitation_Subject": "Invitation Subject",
661663
"Invitation_Subject_Default": "You have been invited to [Site_Name]",
662664
"Invite_user_to_join_channel": "Invite one user to join this channel",
665+
"Invite_user_to_join_channel_all_to": "Invite all users from this channel to join [#channel]",
666+
"Invite_user_to_join_channel_all_from": "Invite all users from [#channel] to join this channel",
663667
"Invite_Users": "Invite Users",
664668
"is_also_typing": "is also typing",
665669
"is_also_typing_female": "is also typing",
@@ -1466,6 +1470,7 @@
14661470
"Username_title": "Register username",
14671471
"Username_wants_to_start_otr_Do_you_want_to_accept": "__username__ wants to start OTR. Do you want to accept?",
14681472
"Users": "Users",
1473+
"Users_added": "The users have been added",
14691474
"Users_in_role": "Users in role",
14701475
"UTF8_Names_Slugify": "UTF8 Names Slugify",
14711476
"UTF8_Names_Validation": "UTF8 Names Validation",

packages/rocketchat-lib/package.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ Package.onUse(function(api) {
121121
api.addFiles('server/methods/addOAuthService.coffee', 'server');
122122
api.addFiles('server/methods/refreshOAuthService.js', 'server');
123123
api.addFiles('server/methods/addUserToRoom.coffee', 'server');
124+
api.addFiles('server/methods/addUsersToRoom.js', 'server');
124125
api.addFiles('server/methods/archiveRoom.coffee', 'server');
125126
api.addFiles('server/methods/blockUser.js', 'server');
126127
api.addFiles('server/methods/checkRegistrationSecretURL.coffee', 'server');
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
Meteor.methods({
2+
addUsersToRoom: function(data) {
3+
var fromId, room;
4+
if (!Meteor.userId()) {
5+
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
6+
method: 'addUserToRoom'
7+
});
8+
}
9+
if (!Match.test(data != null ? data.rid : void 0, String)) {
10+
throw new Meteor.Error('error-invalid-room', 'Invalid room', {
11+
method: 'addUserToRoom'
12+
});
13+
}
14+
15+
room = RocketChat.models.Rooms.findOneById(data.rid);
16+
if (room.usernames.indexOf(Meteor.user().username) === -1) {
17+
throw new Meteor.Error('error-not-allowed', 'Not allowed', {
18+
method: 'addUserToRoom'
19+
});
20+
}
21+
22+
fromId = Meteor.userId();
23+
if (!RocketChat.authz.hasPermission(fromId, 'add-user-to-room', room._id)) {
24+
throw new Meteor.Error('error-not-allowed', 'Not allowed', {
25+
method: 'addUserToRoom'
26+
});
27+
}
28+
if (room.t === 'd') {
29+
throw new Meteor.Error('error-cant-invite-for-direct-room', 'Can\'t invite user to direct rooms', {
30+
method: 'addUserToRoom'
31+
});
32+
}
33+
data.users = Array.isArray(data.users) ? data.users : [];
34+
data.users.forEach(function(username) {
35+
let newUser = RocketChat.models.Users.findOneByUsername(username);
36+
RocketChat.addUserToRoom(data.rid, newUser, Meteor.user());
37+
});
38+
return true;
39+
}
40+
});

packages/rocketchat-slashcommands-create/server.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
function Create(command, params, item) {
2+
function getParams(str) {
3+
const regex = /(--(\w+))+/g;
4+
let m, result = [];
5+
while ((m = regex.exec(str)) !== null) {
6+
if (m.index === regex.lastIndex) {
7+
regex.lastIndex++;
8+
}
9+
result.push(m[2]);
10+
}
11+
return result;
12+
}
213
var channel, room, user;
14+
var regexp = /#?([\d-_\w]+)/g;
315
if (command !== 'create' || !Match.test(params, String)) {
416
return;
517
}
6-
channel = params.trim();
18+
channel = regexp.exec(params.trim());
19+
channel = channel ? channel[1] : '';
720
if (channel === '') {
821
return;
922
}
@@ -22,7 +35,12 @@ function Create(command, params, item) {
2235
});
2336
return;
2437
}
38+
39+
if (getParams(params).indexOf('private') > -1) {
40+
return Meteor.call('createPrivateGroup', channel, []);
41+
}
42+
2543
Meteor.call('createChannel', channel, []);
2644
}
2745

28-
RocketChat.slashCommands.add('create', Create);
46+
RocketChat.slashCommands.add('create', Create);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
RocketChat.slashCommands.add('invite-all-to', undefined, {
2+
description: 'Invite_user_to_join_channel_all_to',
3+
params: '#room'
4+
});
5+
RocketChat.slashCommands.add('invite-all-from', undefined, {
6+
description: 'Invite_user_to_join_channel_all_from',
7+
params: '#room'
8+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Package.describe({
2+
name: 'rocketchat:slashcommands-invite-all',
3+
version: '0.0.1',
4+
summary: 'Message pre-processor that will translate /invite-all commands',
5+
git: ''
6+
});
7+
8+
Package.onUse(function(api) {
9+
10+
api.use([
11+
'ecmascript',
12+
'coffeescript',
13+
'check',
14+
'rocketchat:lib'
15+
]);
16+
17+
api.use('templating', 'client');
18+
19+
api.addFiles('client.js', 'client');
20+
api.addFiles('server.js', 'server');
21+
});
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Invite is a named function that will replace /invite commands
3+
* @param {Object} message - The message object
4+
*/
5+
6+
function inviteAll(type) {
7+
8+
return function inviteAll(command, params, item) {
9+
10+
if (!/invite\-all-(to|from)/.test(command) || !Match.test(params, String)) {
11+
return;
12+
}
13+
14+
let regexp = /#?([\d-_\w]+)/g,
15+
[, channel] = regexp.exec(params.trim());
16+
17+
if (!channel) {
18+
return;
19+
}
20+
21+
let currentUser = Meteor.users.findOne(Meteor.userId());
22+
let baseChannel = type === 'to' ? RocketChat.models.Rooms.findOneById(item.rid) : RocketChat.models.Rooms.findOneByName(channel);
23+
let targetChannel = type === 'from' ? RocketChat.models.Rooms.findOneById(item.rid) : RocketChat.models.Rooms.findOneByName(channel);
24+
25+
if (!baseChannel) {
26+
return RocketChat.Notifications.notifyUser(Meteor.userId(), 'message', {
27+
_id: Random.id(),
28+
rid: item.rid,
29+
ts: new Date(),
30+
msg: TAPi18n.__('Channel_doesnt_exist', {
31+
postProcess: 'sprintf',
32+
sprintf: [channel]
33+
}, currentUser.language)
34+
});
35+
}
36+
let users = baseChannel.usernames || [];
37+
38+
try {
39+
if (users.length > RocketChat.settings.get('API_User_Limit')) {
40+
throw new Meteor.Error('error-user-limit-exceeded', 'User Limit Exceeded', {
41+
method: 'addAllToRoom'
42+
});
43+
}
44+
45+
if (!targetChannel && ['c', 'p'].indexOf(baseChannel.t) > -1) {
46+
Meteor.call(baseChannel.t === 'c' ? 'createChannel' : 'createPrivateGroup', channel, users);
47+
RocketChat.Notifications.notifyUser(Meteor.userId(), 'message', {
48+
_id: Random.id(),
49+
rid: item.rid,
50+
ts: new Date(),
51+
msg: TAPi18n.__('Channel_created', {
52+
postProcess: 'sprintf',
53+
sprintf: [channel]
54+
}, currentUser.language)
55+
});
56+
} else {
57+
Meteor.call('addUsersToRoom', {
58+
rid: targetChannel._id,
59+
users: users
60+
});
61+
}
62+
return RocketChat.Notifications.notifyUser(Meteor.userId(), 'message', {
63+
_id: Random.id(),
64+
rid: item.rid,
65+
ts: new Date(),
66+
msg: TAPi18n.__('Users_added', null, currentUser.language)
67+
});
68+
} catch (e) {
69+
let msg = e.error === 'cant-invite-for-direct-room' ? 'Cannot_invite_users_to_direct_rooms' : e.error;
70+
RocketChat.Notifications.notifyUser(Meteor.userId(), 'message', {
71+
_id: Random.id(),
72+
rid: item.rid,
73+
ts: new Date(),
74+
msg: TAPi18n.__(msg, null, currentUser.language)
75+
});
76+
}
77+
};
78+
}
79+
RocketChat.slashCommands.add('invite-all-to', inviteAll('to'));
80+
RocketChat.slashCommands.add('invite-all-from', inviteAll('from'));
81+
module.exports = inviteAll;

0 commit comments

Comments
 (0)