forked from RocketChat/Rocket.Chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmuteUserInRoom.js
More file actions
58 lines (46 loc) · 1.45 KB
/
Copy pathmuteUserInRoom.js
File metadata and controls
58 lines (46 loc) · 1.45 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
Meteor.methods({
muteUserInRoom(data) {
check(data, Match.ObjectIncluding({
rid: String,
username: String
}));
if (!Meteor.userId()) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
method: 'muteUserInRoom'
});
}
const fromId = Meteor.userId();
if (!RocketChat.authz.hasPermission(fromId, 'mute-user', data.rid)) {
throw new Meteor.Error('error-not-allowed', 'Not allowed', {
method: 'muteUserInRoom'
});
}
const room = RocketChat.models.Rooms.findOneById(data.rid);
if (!room) {
throw new Meteor.Error('error-invalid-room', 'Invalid room', {
method: 'muteUserInRoom'
});
}
if (['c', 'p'].includes(room.t) === false) {
throw new Meteor.Error('error-invalid-room-type', `${ room.t } is not a valid room type`, {
method: 'muteUserInRoom',
type: room.t
});
}
if (Array.isArray(room.usernames) === false || room.usernames.includes(data.username) === false) {
throw new Meteor.Error('error-user-not-in-room', 'User is not in this room', {
method: 'muteUserInRoom'
});
}
const mutedUser = RocketChat.models.Users.findOneByUsername(data.username);
RocketChat.models.Rooms.muteUsernameByRoomId(data.rid, mutedUser.username);
const fromUser = RocketChat.models.Users.findOneById(fromId);
RocketChat.models.Messages.createUserMutedWithRoomIdAndUser(data.rid, mutedUser, {
u: {
_id: fromUser._id,
username: fromUser.username
}
});
return true;
}
});