From 56cb1b4570ef28ae1e5c72556ff6ca19ccbe717a Mon Sep 17 00:00:00 2001 From: Rodrigo Nascimento Date: Fri, 3 Feb 2017 02:23:03 -0200 Subject: [PATCH] Code improvement --- .../server/methods/addUsersToRoom.js | 25 +++++++++---------- server/startup/migrations/v084.js | 11 ++++---- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/packages/rocketchat-lib/server/methods/addUsersToRoom.js b/packages/rocketchat-lib/server/methods/addUsersToRoom.js index 6b5b66fe215..b00c42350d3 100644 --- a/packages/rocketchat-lib/server/methods/addUsersToRoom.js +++ b/packages/rocketchat-lib/server/methods/addUsersToRoom.js @@ -1,24 +1,23 @@ Meteor.methods({ - addUsersToRoom: function(data) { - var ref, room, user, userId, userInRoom, canAddUser; - + addUsersToRoom(data = {}) { // Validate user and room if (!Meteor.userId()) { throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'addUsersToRoom' }); } - if (!Match.test(data != null ? data.rid : void 0, String)) { + + if (!Match.test(data.rid, String)) { throw new Meteor.Error('error-invalid-room', 'Invalid room', { method: 'addUsersToRoom' }); } // Get user and room details - room = RocketChat.models.Rooms.findOneById(data.rid); - userId = Meteor.userId(); - user = Meteor.user(); - userInRoom = ((ref = room.usernames) != null ? ref.indexOf(user.username) : void 0) >= 0; + const room = RocketChat.models.Rooms.findOneById(data.rid); + const userId = Meteor.userId(); + const user = Meteor.user(); + const userInRoom = Array.isArray(room.usernames) && room.usernames.includes(user.username); // Can't add to direct room ever if (room.t === 'd') { @@ -28,7 +27,7 @@ Meteor.methods({ } // Can add to any room you're in, with permission, otherwise need specific room type permission - canAddUser = false; + let canAddUser = false; if (userInRoom && RocketChat.authz.hasPermission(userId, 'add-user-to-joined-room', room._id)) { canAddUser = true; } else if (room.t === 'c' && RocketChat.authz.hasPermission(userId, 'add-user-to-any-c-room')) { @@ -52,17 +51,17 @@ Meteor.methods({ } // Validate each user, then add to room - data.users.forEach(function(username) { - let newUser = RocketChat.models.Users.findOneByUsername(username); - if (newUser == null) { + data.users.forEach((username) => { + const newUser = RocketChat.models.Users.findOneByUsername(username); + if (!newUser) { throw new Meteor.Error('error-invalid-username', 'Invalid username', { method: 'addUsersToRoom' }); } + RocketChat.addUserToRoom(data.rid, newUser, user); }); return true; - } }); diff --git a/server/startup/migrations/v084.js b/server/startup/migrations/v084.js index c731e85c714..bd8e4941f1c 100644 --- a/server/startup/migrations/v084.js +++ b/server/startup/migrations/v084.js @@ -1,7 +1,7 @@ RocketChat.Migrations.add({ version: 84, - up: function() { - if (RocketChat && RocketChat.models && RocketChat.models.Permissions) { + up() { + if (RocketChat.models && RocketChat.models.Permissions) { // Update permission name, copy values from old name var oldPermission = RocketChat.models.Permissions.findOne('add-user-to-room'); @@ -9,11 +9,11 @@ RocketChat.Migrations.add({ RocketChat.models.Permissions.upsert({ _id: 'add-user-to-joined-room' }, { $set: { roles: oldPermission.roles } }); RocketChat.models.Permissions.remove({ _id: 'add-user-to-room' }); } - } }, - down: function() { - if (RocketChat && RocketChat.models && RocketChat.models.Permissions) { + + down() { + if (RocketChat.models && RocketChat.models.Permissions) { // Revert permission name, copy values from updated name var newPermission = RocketChat.models.Permissions.findOne('add-user-to-joined-room'); @@ -21,7 +21,6 @@ RocketChat.Migrations.add({ RocketChat.models.Permissions.upsert({ _id: 'add-user-to-room' }, { $set: { roles: newPermission.roles } }); RocketChat.models.Permissions.remove({ _id: 'add-user-to-joined-room' }); } - } } });