Code improvement

pull/5895/head
Rodrigo Nascimento 9 years ago
parent 467c5a0140
commit 56cb1b4570
  1. 25
      packages/rocketchat-lib/server/methods/addUsersToRoom.js
  2. 11
      server/startup/migrations/v084.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;
}
});

@ -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' });
}
}
}
});

Loading…
Cancel
Save