forked from vegeta999/Rocket.Chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunmuteUserInRoom.js
More file actions
66 lines (51 loc) · 1.72 KB
/
Copy pathunmuteUserInRoom.js
File metadata and controls
66 lines (51 loc) · 1.72 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
import { Meteor } from 'meteor/meteor';
import { Match, check } from 'meteor/check';
import { hasPermission } from '../../app/authorization';
import { callbacks } from '../../app/callbacks';
import { Rooms, Subscriptions, Users, Messages } from '../../app/models';
Meteor.methods({
unmuteUserInRoom(data) {
const fromId = Meteor.userId();
check(data, Match.ObjectIncluding({
rid: String,
username: String,
}));
if (!hasPermission(fromId, 'mute-user', data.rid)) {
throw new Meteor.Error('error-not-allowed', 'Not allowed', {
method: 'unmuteUserInRoom',
});
}
const room = Rooms.findOneById(data.rid);
if (!room) {
throw new Meteor.Error('error-invalid-room', 'Invalid room', {
method: 'unmuteUserInRoom',
});
}
if (['c', 'p'].includes(room.t) === false) {
throw new Meteor.Error('error-invalid-room-type', `${ room.t } is not a valid room type`, {
method: 'unmuteUserInRoom',
type: room.t,
});
}
const subscription = Subscriptions.findOneByRoomIdAndUsername(data.rid, data.username, { fields: { _id: 1 } });
if (!subscription) {
throw new Meteor.Error('error-user-not-in-room', 'User is not in this room', {
method: 'unmuteUserInRoom',
});
}
const unmutedUser = Users.findOneByUsernameIgnoringCase(data.username);
const fromUser = Users.findOneById(fromId);
callbacks.run('beforeUnmuteUser', { unmutedUser, fromUser }, room);
Rooms.unmuteUsernameByRoomId(data.rid, unmutedUser.username);
Messages.createUserUnmutedWithRoomIdAndUser(data.rid, unmutedUser, {
u: {
_id: fromUser._id,
username: fromUser.username,
},
});
Meteor.defer(function() {
callbacks.run('afterUnmuteUser', { unmutedUser, fromUser }, room);
});
return true;
},
});