Repository navigation
Expand file tree
/
Copy pathlistener.ts
More file actions
114 lines (93 loc) · 3.37 KB
/
Copy pathlistener.ts
File metadata and controls
114 lines (93 loc) · 3.37 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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import type { BaseModel } from "../../entity/basemodel.js";
import { Channel } from "../../entity/channel.js";
import { Guild } from "../../entity/guild.js";
import { splitQualifiedMention } from "../../util/activitypub/util.js";
import { channelInGuild } from "../../util/entity/channel.js";
import { listenGatewayEvent, makeGatewayTarget } from "../../util/events.js";
import { createLogger } from "../../util/log.js";
import { handleMemberListRoleAdd } from "../handlers/members.js";
import type { GATEWAY_EVENT } from "./validation/send.js";
import type { Websocket } from "./websocket.js";
const Log = createLogger("GATEWAY:LISTENER");
/**
* Register listeners for each event emitter to a gateway client
* @param socket
* @param emitters UUIDs of all event emitters, i.e. guilds, channels, users
* @param callback Optional callback to use instead of default consume behaviour
*/
export const listenEvents = (socket: Websocket, emitters: BaseModel[], callback = consume) => {
for (const emitter of emitters) {
const id = makeGatewayTarget(emitter);
if (socket.events[id]) Log.warn(`${socket.user_id} is already listening to ${emitter}`);
socket.events[id] = listenGatewayEvent(emitter, callback.bind(null, socket));
}
};
export const removeEventListener = (socket: Websocket, id: string) => {
if (!socket.events[id]) {
Log.warn(`No listener for target ${id} on ${socket.user_id} to remove`);
return;
}
socket.events[id]();
delete socket.events[id];
};
export const consume = async (socket: Websocket, payload: GATEWAY_EVENT) => {
switch (payload.type) {
// TODO: for relationships, see #54
case "CHANNEL_CREATE": {
const { id } = splitQualifiedMention(payload.channel.mention);
listenEvents(socket, [Channel.create({ id })]);
break;
}
case "GUILD_CREATE": {
const { id } = splitQualifiedMention(payload.guild.mention);
listenEvents(socket, [Guild.create({ id })]);
break;
}
case "GUILD_DELETE":
// if we leave a guild that we're subscribed to, remove our subscription
if (
!socket.member_list.channel_id ||
!(await channelInGuild(socket.member_list.channel_id, payload.guild))
)
break;
// remove all our subscriptions to this channel,
// as it's guild was just deleted
for (const id in socket.member_list.events) {
socket.member_list.events[id]();
delete socket.member_list.events[id];
}
socket.member_list.channel_id = undefined;
socket.member_list.range = undefined;
break;
case "CHANNEL_DELETE":
// if we're subscribed to this channel, unsub
if (socket.member_list.channel_id !== payload.channel) break;
for (const id in socket.member_list.events) {
socket.member_list.events[id]();
delete socket.member_list.events[id];
}
socket.member_list.channel_id = undefined;
socket.member_list.range = undefined;
break;
case "ROLE_MEMBER_ADD":
setImmediate(handleMemberListRoleAdd, socket, payload);
break;
case "TYPING":
// don't send our typing indicators to ourselves
if (payload.user === socket.user_mention) return;
break;
case "MESSAGE_CREATE":
// when we send a message, reset the last typing timestamp
// so that we can send typing events again
if (payload.message.author_id === socket.user_mention) {
socket.last_typing = undefined;
}
break;
default:
break;
}
socket.send({
t: payload.type,
d: { ...payload, type: undefined },
});
};