forked from vegeta999/Rocket.Chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremoveUserFromRoom.js
More file actions
79 lines (61 loc) · 2.21 KB
/
Copy pathremoveUserFromRoom.js
File metadata and controls
79 lines (61 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { Meteor } from 'meteor/meteor';
import { Match, check } from 'meteor/check';
import { hasPermission, hasRole, getUsersInRole, removeUserFromRoles } from '../../app/authorization';
import { Users, Subscriptions, Rooms, Messages } from '../../app/models';
import { callbacks } from '../../app/callbacks';
Meteor.methods({
removeUserFromRoom(data) {
check(data, Match.ObjectIncluding({
rid: String,
username: String,
}));
const fromId = Meteor.userId();
if (!fromId) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
method: 'removeUserFromRoom',
});
}
if (!hasPermission(fromId, 'remove-user', data.rid)) {
throw new Meteor.Error('error-not-allowed', 'Not allowed', {
method: 'removeUserFromRoom',
});
}
const room = Rooms.findOneById(data.rid);
if (!room || room.t === 'd') {
throw new Meteor.Error('error-not-allowed', 'Not allowed', {
method: 'removeUserFromRoom',
});
}
const removedUser = Users.findOneByUsernameIgnoringCase(data.username);
const fromUser = Users.findOneById(fromId);
const subscription = Subscriptions.findOneByRoomIdAndUserId(data.rid, removedUser._id, { fields: { _id: 1 } });
if (!subscription) {
throw new Meteor.Error('error-user-not-in-room', 'User is not in this room', {
method: 'removeUserFromRoom',
});
}
if (hasRole(removedUser._id, 'owner', room._id)) {
const numOwners = getUsersInRole('owner', room._id).fetch().length;
if (numOwners === 1) {
throw new Meteor.Error('error-you-are-last-owner', 'You are the last owner. Please set new owner before leaving the room.', {
method: 'removeUserFromRoom',
});
}
}
callbacks.run('beforeRemoveFromRoom', { removedUser, userWhoRemoved: fromUser }, room);
Subscriptions.removeByRoomIdAndUserId(data.rid, removedUser._id);
if (['c', 'p'].includes(room.t) === true) {
removeUserFromRoles(removedUser._id, ['moderator', 'owner'], data.rid);
}
Messages.createUserRemovedWithRoomIdAndUser(data.rid, removedUser, {
u: {
_id: fromUser._id,
username: fromUser.username,
},
});
Meteor.defer(function() {
callbacks.run('afterRemoveFromRoom', { removedUser, userWhoRemoved: fromUser }, room);
});
return true;
},
});