-
Notifications
You must be signed in to change notification settings - Fork 11.1k
/
Copy patheraseRoom.ts
56 lines (46 loc) · 1.71 KB
/
eraseRoom.ts
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
import { AppEvents, Apps } from '@rocket.chat/apps';
import { Message, Team } from '@rocket.chat/core-services';
import { Rooms } from '@rocket.chat/models';
import { Meteor } from 'meteor/meteor';
import { hasPermissionAsync } from '../../app/authorization/server/functions/hasPermission';
import { deleteRoom } from '../../app/lib/server/functions/deleteRoom';
import { roomCoordinator } from '../lib/rooms/roomCoordinator';
export async function eraseRoom(rid: string, uid: string): Promise<void> {
const room = await Rooms.findOneById(rid);
if (!room) {
throw new Meteor.Error('error-invalid-room', 'Invalid room', {
method: 'eraseRoom',
});
}
if (room.federated) {
throw new Meteor.Error('error-cannot-delete-federated-room', 'Cannot delete federated room', {
method: 'eraseRoom',
});
}
if (
!(await roomCoordinator
.getRoomDirectives(room.t)
?.canBeDeleted((permissionId, rid) => hasPermissionAsync(uid, permissionId, rid), room))
) {
throw new Meteor.Error('error-not-allowed', 'Not allowed', {
method: 'eraseRoom',
});
}
if (Apps.self?.isLoaded()) {
const prevent = await Apps.getBridges()?.getListenerBridge().roomEvent(AppEvents.IPreRoomDeletePrevent, room);
if (prevent) {
throw new Meteor.Error('error-app-prevented-deleting', 'A Rocket.Chat App prevented the room erasing.');
}
}
await deleteRoom(rid);
const team = room.teamId && (await Team.getOneById(room.teamId));
if (team) {
const user = await Meteor.userAsync();
if (user) {
await Message.saveSystemMessage('user-deleted-room-from-team', team.roomId, room.name || '', user);
}
}
if (Apps.self?.isLoaded()) {
void Apps.getBridges()?.getListenerBridge().roomEvent(AppEvents.IPostRoomDeleted, room);
}
}