forked from vegeta999/Rocket.Chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremoveRoomModerator.js
More file actions
75 lines (61 loc) · 1.88 KB
/
Copy pathremoveRoomModerator.js
File metadata and controls
75 lines (61 loc) · 1.88 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
import { Meteor } from 'meteor/meteor';
import { check } from 'meteor/check';
import { hasPermission } from '../../app/authorization';
import { Users, Subscriptions, Messages } from '../../app/models';
import { settings } from '../../app/settings';
import { Notifications } from '../../app/notifications';
Meteor.methods({
removeRoomModerator(rid, userId) {
check(rid, String);
check(userId, String);
if (!Meteor.userId()) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
method: 'removeRoomModerator',
});
}
if (!hasPermission(Meteor.userId(), 'set-moderator', rid)) {
throw new Meteor.Error('error-not-allowed', 'Not allowed', {
method: 'removeRoomModerator',
});
}
const user = Users.findOneById(userId);
if (!user || !user.username) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
method: 'removeRoomModerator',
});
}
const subscription = Subscriptions.findOneByRoomIdAndUserId(rid, user._id);
if (!subscription) {
throw new Meteor.Error('error-invalid-room', 'Invalid room', {
method: 'removeRoomModerator',
});
}
if (Array.isArray(subscription.roles) === false || subscription.roles.includes('moderator') === false) {
throw new Meteor.Error('error-user-not-moderator', 'User is not a moderator', {
method: 'removeRoomModerator',
});
}
Subscriptions.removeRoleById(subscription._id, 'moderator');
const fromUser = Users.findOneById(Meteor.userId());
Messages.createSubscriptionRoleRemovedWithRoomIdAndUser(rid, user, {
u: {
_id: fromUser._id,
username: fromUser.username,
},
role: 'moderator',
});
if (settings.get('UI_DisplayRoles')) {
Notifications.notifyLogged('roles-change', {
type: 'removed',
_id: 'moderator',
u: {
_id: user._id,
username: user.username,
name: user.name,
},
scope: rid,
});
}
return true;
},
});