forked from RocketChat/Rocket.Chat
/
emitter.js
45 lines (40 loc) · 1.24 KB
/
emitter.js
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
import Settings from '../../../../models/server/models/Settings';
import { Notifications } from '../../../../notifications/server';
import { CONSTANTS } from '../../../lib';
import Permissions from '../../../../models/server/models/Permissions';
import { clearCache } from '../../functions/hasPermission';
Permissions.on('change', ({ clientAction, id, data, diff }) => {
if (diff && Object.keys(diff).length === 1 && diff._updatedAt) {
// avoid useless changes
return;
}
switch (clientAction) {
case 'updated':
case 'inserted':
data = data || Permissions.findOneById(id);
break;
case 'removed':
data = { _id: id };
break;
}
clearCache();
Notifications.notifyLoggedInThisInstance(
'permissions-changed',
clientAction,
data,
);
if (data.level && data.level === CONSTANTS.SETTINGS_LEVEL) {
// if the permission changes, the effect on the visible settings depends on the role affected.
// The selected-settings-based consumers have to react accordingly and either add or remove the
// setting from the user's collection
const setting = Settings.findOneNotHiddenById(data.settingId);
if (!setting) {
return;
}
Notifications.notifyLoggedInThisInstance(
'private-settings-changed',
'updated',
setting,
);
}
});