-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathSlackRoomStore.ts
100 lines (80 loc) · 3.24 KB
/
SlackRoomStore.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { BridgedRoom } from "./BridgedRoom";
import { Logger } from "matrix-appservice-bridge";
import QuickLRU from "@alloc/quick-lru";
import { UserAdminRoom } from "./rooms/UserAdminRoom";
import { Main } from "./Main";
const log = new Logger("SlackRoomStore");
export class SlackRoomStore {
private rooms: Set<BridgedRoom> = new Set();
private userAdminRooms: QuickLRU<string, UserAdminRoom> = new QuickLRU({ maxSize: 50 });
// These are used to optimise the time taken to find a room.
private roomsBySlackChannelId: Map<string, BridgedRoom> = new Map();
private roomsByMatrixId: Map<string, BridgedRoom> = new Map();
private roomsByInboundId: Map<string, BridgedRoom> = new Map();
public get all(): BridgedRoom[] {
return [...this.rooms];
}
public get matrixRoomCount(): number {
return this.roomsByMatrixId.size;
}
public get remoteRoomCount(): number {
return this.roomsByInboundId.size;
}
public upsertRoom(room: BridgedRoom): void {
log.debug(`upsertRoom ${room.MatrixRoomId}`);
this.rooms.add(room);
// Remove if the room already exists in the map.
[...this.roomsByMatrixId.keys()].forEach((k) => {
if (this.roomsByMatrixId.get(k) === room) {
this.roomsByMatrixId.delete(k);
}
});
[...this.roomsByInboundId.keys()].forEach((k) => {
if (this.roomsByInboundId.get(k) === room) {
this.roomsByInboundId.delete(k);
}
});
[...this.roomsBySlackChannelId.keys()].forEach((k) => {
if (this.roomsBySlackChannelId.get(k) === room) {
this.roomsBySlackChannelId.delete(k);
}
});
this.roomsByMatrixId.set(room.MatrixRoomId, room);
this.roomsByInboundId.set(room.InboundId, room);
if (room.SlackChannelId) {
this.roomsBySlackChannelId.set(room.SlackChannelId, room);
}
}
public removeRoom(room: BridgedRoom): void {
log.debug(`removeRoom ${room.MatrixRoomId}`);
this.roomsByMatrixId.delete(room.MatrixRoomId);
if (room.SlackChannelId) {
this.roomsBySlackChannelId.delete(room.SlackChannelId);
}
if (room.InboundId) {
this.roomsByInboundId.delete(room.InboundId);
}
this.rooms.delete(room);
}
public getBySlackChannelId(channelId: string): BridgedRoom|undefined {
return this.roomsBySlackChannelId.get(channelId);
}
public getBySlackTeamId(teamId: string): BridgedRoom[] {
// This is called sufficently infrequently that we can do a filter.
return this.all.filter((r) => r.SlackTeamId === teamId);
}
public getByMatrixRoomId(roomId: string): BridgedRoom|undefined {
return this.roomsByMatrixId.get(roomId);
}
public getByInboundId(inboundId: string): BridgedRoom|undefined {
return this.roomsByInboundId.get(inboundId);
}
public getOrCreateAdminRoom(roomId: string, userId: string, main: Main): UserAdminRoom {
let adminRoom = this.userAdminRooms.get(roomId);
if (adminRoom) {
return adminRoom;
}
adminRoom = new UserAdminRoom(roomId, userId, main);
return adminRoom;
}
}