-
Notifications
You must be signed in to change notification settings - Fork 4
/
Dispatcher.js
162 lines (137 loc) · 4.05 KB
/
Dispatcher.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
const OPCODES = require('./OpCodes.json');
const { getServersOfUser } = require('./DatabaseHandler.js');
const logger = require('./Logger.js');
const WebSocket = require('ws');
/**
* Dispatch a HELLO payload to a client
* @param {WebSocket} client The client to dispatch to
*/
async function dispatchHello (client) {
const servers = await getServersOfUser(client.user.id);
const payload = {
op: OPCODES.Hello,
d: servers
};
send([client], payload);
}
/**
* Dispatch a presence update to all connected clients
* @param {Set<WebSocket>} clients The clients to dispatch the payload to
* @param {String} userId The user whose presence was updated
* @param {Boolean} status The status of the user
*/
function dispatchPresenceUpdate (clients, userId, status) {
const payload = {
op: OPCODES.DispatchPresence,
d: {
id: userId,
status
}
};
send(clients, payload);
}
/**
* Dispatches a user message to the provided clients
* @param {Set<WebSocket>|WebSocket[]} clients The clients to dispatch the message to
* @param {Object} message The message to send to the clients
*/
function dispatchMessage (clients, message) {
const payload = {
op: OPCODES.DispatchMessage,
d: message
};
send(clients, payload);
}
/**
* Dispatches a member object to the provided clients
* @param {Set<WebSocket>|WebSocket[]} clients The clients to dispatch the member to
* @param {Number} serverId The serverId that the member was added to
* @param {Object} member The member to send to the clients
*/
function dispatchMember (clients, serverId, member) {
const payload = {
op: OPCODES.DispatchMemberAdd,
d: {
server: serverId,
member
}
};
send(clients, payload);
}
/**
* Dispatches a member leave payload to the provided clients
* @param {Set<WebSocket>|WebSocket[]} clients The clients to dispatch the payload to
* @param {String} username The name of the user who left
* @param {Number} serverId The ID of the server the member left
*/
function dispatchMemberLeave (clients, username, serverId) {
const payload = {
op: OPCODES.DispatchMemberLeave,
d: {
user: username,
server: serverId,
kicked: false // soon
}
};
send(clients, payload);
}
/**
* Dispatches a list of members to the given client
* @param {WebSocket} client The client to dispatch the members to
* @param {Array} members The list of members
*/
function dispatchMembers (client, members) {
const payload = {
op: OPCODES.DispatchMembers,
d: members
};
send([client], payload);
}
/**
* Dispatches a guildJoin op to the given clients
* @param {Set<WebSocket>|WebSocket[]} clients The client to dispatch the event to
* @param {Object} server The server object to be dispatched
*/
function dispatchServerJoin (clients, server) {
const payload = {
op: OPCODES.DispatchServerJoin,
d: server
};
send(clients, payload);
}
/**
* Dispatches a guildLeave op to the given clients
* @param {Set<WebSocket>|WebSocket[]} clients The client to dispatch the event to
* @param {String} serverId The id of the server that was deleted
*/
function dispatchServerLeave (clients, serverId) {
const payload = {
op: OPCODES.DispatchServerLeave,
d: serverId
};
send(clients, payload);
}
/**
* Dispatch a payload to the provided clients
* @param {Set<WebSocket>|WebSocket[]} clients The clients to dispatch the payload to
* @param {Object} payload The payload to send to the clients
*/
function send (clients, payload) {
logger.debug('Dispatching OP {0} to {1} clients', payload.op, clients.size || clients.length);
payload = JSON.stringify(payload);
clients.forEach(ws => {
if (ws.readyState === WebSocket.OPEN) {
ws.send(payload);
}
});
}
module.exports = {
dispatchHello,
dispatchPresenceUpdate,
dispatchMessage,
dispatchMember,
dispatchMemberLeave,
dispatchMembers,
dispatchServerJoin,
dispatchServerLeave
};