Skip to content
Snippets Groups Projects
Commit 56cb1b45 authored by Rodrigo Nascimento's avatar Rodrigo Nascimento
Browse files

Code improvement

parent 467c5a01
No related branches found
No related tags found
No related merge requests found
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;
}
});
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' });
}
}
}
});
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