@eleven-am/pondsocket 0.1.141 → 0.1.143
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -15
- package/abstracts/abstractRequest.js +1 -2
- package/abstracts/abstractRequest.test.js +2 -5
- package/channel/channel.js +118 -97
- package/channel/channel.test.js +86 -61
- package/channel/eventRequest.test.js +3 -3
- package/channel/eventResponse.js +37 -61
- package/channel/eventResponse.test.js +31 -53
- package/endpoint/endpoint.js +182 -100
- package/endpoint/endpoint.test.js +30 -13
- package/endpoint/response.js +48 -14
- package/lobby/JoinRequest.test.js +17 -6
- package/lobby/JoinResponse.test.js +18 -23
- package/lobby/joinResponse.js +70 -31
- package/lobby/lobby.js +18 -53
- package/package.json +8 -8
- package/presence/presence.js +1 -1
- package/presence/presenceEngine.test.js +1 -4
- package/schema.js +0 -1
- package/server/pondSocket.js +8 -5
- package/types.d.ts +296 -227
- package/abstracts/abstractResponse.js +0 -9
package/README.md
CHANGED
|
@@ -175,7 +175,7 @@ const endpoint = pond.createEndpoint('/api/socket', (req, res) => {
|
|
|
175
175
|
const role = getRoleFromToken(token);
|
|
176
176
|
|
|
177
177
|
// Handle socket connection and authentication for valid users
|
|
178
|
-
res.accept({
|
|
178
|
+
res.accept({role}); // Assign the user's role to the socket
|
|
179
179
|
} else {
|
|
180
180
|
// Reject the connection for invalid users or without a token
|
|
181
181
|
res.reject('Invalid token', 401);
|
|
@@ -186,9 +186,9 @@ const endpoint = pond.createEndpoint('/api/socket', (req, res) => {
|
|
|
186
186
|
const profanityChannel = endpoint.createChannel('/channel/:id', async (req, res) => {
|
|
187
187
|
// When joining the channel, any joinParams passed from the client will be available in the request payload
|
|
188
188
|
// Also any previous assigns on the socket will be available in the request payload as well
|
|
189
|
-
const {
|
|
190
|
-
const {
|
|
191
|
-
const {
|
|
189
|
+
const {role} = req.user.assigns;
|
|
190
|
+
const {username} = req.joinParams;
|
|
191
|
+
const {id} = req.event.params;
|
|
192
192
|
|
|
193
193
|
// maybe retrieve the previous messages from the database
|
|
194
194
|
const messages = await getMessagesFromDatabase(id);
|
|
@@ -196,7 +196,7 @@ const profanityChannel = endpoint.createChannel('/channel/:id', async (req, res)
|
|
|
196
196
|
// Check if the user has the required role to join the channel
|
|
197
197
|
if (role === 'admin') {
|
|
198
198
|
// Accept the join request
|
|
199
|
-
res.accept({
|
|
199
|
+
res.accept({username, profanityCount: 0})
|
|
200
200
|
// optionally you can track the presence of the user in the channel
|
|
201
201
|
.trackPresence({
|
|
202
202
|
username,
|
|
@@ -205,7 +205,7 @@ const profanityChannel = endpoint.createChannel('/channel/:id', async (req, res)
|
|
|
205
205
|
onlineSince: Date.now(),
|
|
206
206
|
})
|
|
207
207
|
// and send the user the channel history
|
|
208
|
-
.sendToUsers('history', {
|
|
208
|
+
.sendToUsers('history', {messages}, [req.user.id]);
|
|
209
209
|
|
|
210
210
|
// Alternatively, you can also send messages to the user, NOTE that the user would be automatically subscribed to the channel.
|
|
211
211
|
// res.send('history', { messages }, { username, profanityCount: 0 })
|
|
@@ -217,19 +217,19 @@ const profanityChannel = endpoint.createChannel('/channel/:id', async (req, res)
|
|
|
217
217
|
// });
|
|
218
218
|
} else {
|
|
219
219
|
// Reject the join request
|
|
220
|
-
res.
|
|
220
|
+
res.decline('You do not have the required role to join this channel', 403);
|
|
221
221
|
}
|
|
222
222
|
});
|
|
223
223
|
|
|
224
224
|
// Attach message event listener to the profanityChannel
|
|
225
225
|
profanityChannel.onEvent('message', (req, res) => {
|
|
226
|
-
const {
|
|
226
|
+
const {text} = req.event.payload;
|
|
227
227
|
|
|
228
228
|
// Check for profanity
|
|
229
229
|
if (isTextProfane(text)) {
|
|
230
230
|
// Reject the message if it contains profanity
|
|
231
|
-
res.
|
|
232
|
-
profanityCount:
|
|
231
|
+
res.decline('Profanity is not allowed', 400, {
|
|
232
|
+
profanityCount: req.user.assigns.profanityCount + 1
|
|
233
233
|
});
|
|
234
234
|
|
|
235
235
|
// note that profanityCount is updated so req.user.assigns.profanityCount will be updated
|
|
@@ -238,9 +238,9 @@ profanityChannel.onEvent('message', (req, res) => {
|
|
|
238
238
|
res.evictUser('You have been kicked from the channel for using profanity');
|
|
239
239
|
} else {
|
|
240
240
|
// you can broadcast a message to all users or In the channel that profanity is not allowed
|
|
241
|
-
res.broadcast('profanity-warning', {
|
|
241
|
+
res.broadcast('profanity-warning', {message: 'Profanity is not allowed'})
|
|
242
242
|
// or you can send a message to the user that profanity is not allowed
|
|
243
|
-
.sendToUsers('profanity-warning', {
|
|
243
|
+
.sendToUsers('profanity-warning', {message: `You have used profanity ${profanityCount} times. You will be kicked from the channel if you use profanity more than 3 times.`}, [req.user.id]);
|
|
244
244
|
}
|
|
245
245
|
} else {
|
|
246
246
|
// Accept the message to allow broadcasting to other clients in the channel
|
|
@@ -252,8 +252,8 @@ profanityChannel.onEvent('message', (req, res) => {
|
|
|
252
252
|
});
|
|
253
253
|
|
|
254
254
|
profanityChannel.onEvent('presence/:presence', (req, res) => {
|
|
255
|
-
const {
|
|
256
|
-
const {
|
|
255
|
+
const {presence} = req.event.params;
|
|
256
|
+
const {username} = req.user.assigns;
|
|
257
257
|
|
|
258
258
|
// Handle presence events
|
|
259
259
|
res.updatePresence({
|
|
@@ -265,7 +265,7 @@ profanityChannel.onEvent('presence/:presence', (req, res) => {
|
|
|
265
265
|
});
|
|
266
266
|
|
|
267
267
|
profanityChannel.onLeave((event) => {
|
|
268
|
-
const {
|
|
268
|
+
const {username} = event.assigns;
|
|
269
269
|
|
|
270
270
|
// When a user leaves the channel, PondSocket will automatically remove the user from the presence list and inform other users in the channel
|
|
271
271
|
|
|
@@ -42,8 +42,7 @@ class AbstractRequest {
|
|
|
42
42
|
return this._engine.getAssigns();
|
|
43
43
|
}
|
|
44
44
|
get presence() {
|
|
45
|
-
|
|
46
|
-
return ((_a = this._engine.presenceEngine) === null || _a === void 0 ? void 0 : _a.getPresence()) || {};
|
|
45
|
+
return this._engine.presenceEngine.getPresence();
|
|
47
46
|
}
|
|
48
47
|
/**
|
|
49
48
|
* @desc Parses the event and returns true if the event matches the path
|
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const abstractRequest_1 = require("./abstractRequest");
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
getAssigns: () => ({}),
|
|
7
|
-
getPresence: () => ({}),
|
|
8
|
-
});
|
|
4
|
+
const eventResponse_test_1 = require("../channel/eventResponse.test");
|
|
5
|
+
const createMockChannelEngine = () => (0, eventResponse_test_1.createChannelEngine)().channelEngine;
|
|
9
6
|
describe('AbstractRequest', () => {
|
|
10
7
|
it('should be able to be instantiated', () => {
|
|
11
8
|
const request = new abstractRequest_1.AbstractRequest('/test', createMockChannelEngine(), {});
|
package/channel/channel.js
CHANGED
|
@@ -21,58 +21,14 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
21
21
|
}
|
|
22
22
|
return t;
|
|
23
23
|
};
|
|
24
|
-
var
|
|
24
|
+
var _ChannelEngine_instances, _ChannelEngine_receiver, _ChannelEngine_presenceEngine, _ChannelEngine_users, _ChannelEngine_parentEngine, _ChannelEngine_subscribe, _ChannelEngine_getUsersFromRecipients, _ChannelEngine_buildSubscription, _ChannelEngine_removeUser, _Channel_engine;
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.
|
|
26
|
+
exports.Channel = exports.ChannelEngine = void 0;
|
|
27
27
|
const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
|
|
28
28
|
const eventRequest_1 = require("./eventRequest");
|
|
29
29
|
const eventResponse_1 = require("./eventResponse");
|
|
30
30
|
const pondError_1 = require("../errors/pondError");
|
|
31
31
|
const presence_1 = require("../presence/presence");
|
|
32
|
-
class Channel {
|
|
33
|
-
constructor(engine) {
|
|
34
|
-
_Channel_engine.set(this, void 0);
|
|
35
|
-
__classPrivateFieldSet(this, _Channel_engine, engine, "f");
|
|
36
|
-
}
|
|
37
|
-
get name() {
|
|
38
|
-
return __classPrivateFieldGet(this, _Channel_engine, "f").name;
|
|
39
|
-
}
|
|
40
|
-
getAssigns() {
|
|
41
|
-
return __classPrivateFieldGet(this, _Channel_engine, "f").getAssigns();
|
|
42
|
-
}
|
|
43
|
-
getPresences() {
|
|
44
|
-
return __classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.getPresence();
|
|
45
|
-
}
|
|
46
|
-
getUserData(userId) {
|
|
47
|
-
return __classPrivateFieldGet(this, _Channel_engine, "f").getUserData(userId);
|
|
48
|
-
}
|
|
49
|
-
broadcastMessage(event, payload) {
|
|
50
|
-
__classPrivateFieldGet(this, _Channel_engine, "f").sendMessage((0, pondsocket_common_1.uuid)(), pondsocket_common_1.SystemSender.CHANNEL, pondsocket_common_1.ChannelReceiver.ALL_USERS, pondsocket_common_1.ServerActions.BROADCAST, event, payload);
|
|
51
|
-
}
|
|
52
|
-
broadcastMessageFromUser(userId, event, payload) {
|
|
53
|
-
__classPrivateFieldGet(this, _Channel_engine, "f").sendMessage((0, pondsocket_common_1.uuid)(), userId, pondsocket_common_1.ChannelReceiver.ALL_EXCEPT_SENDER, pondsocket_common_1.ServerActions.BROADCAST, event, payload);
|
|
54
|
-
}
|
|
55
|
-
sendToUser(userId, event, payload) {
|
|
56
|
-
__classPrivateFieldGet(this, _Channel_engine, "f").sendMessage((0, pondsocket_common_1.uuid)(), pondsocket_common_1.SystemSender.CHANNEL, [userId], pondsocket_common_1.ServerActions.BROADCAST, event, payload);
|
|
57
|
-
}
|
|
58
|
-
sendToUsers(userIds, event, payload) {
|
|
59
|
-
__classPrivateFieldGet(this, _Channel_engine, "f").sendMessage((0, pondsocket_common_1.uuid)(), pondsocket_common_1.SystemSender.CHANNEL, userIds, pondsocket_common_1.ServerActions.BROADCAST, event, payload);
|
|
60
|
-
}
|
|
61
|
-
evictUser(userId, reason) {
|
|
62
|
-
__classPrivateFieldGet(this, _Channel_engine, "f").kickUser(userId, reason !== null && reason !== void 0 ? reason : 'You have been banned from the channel');
|
|
63
|
-
}
|
|
64
|
-
trackPresence(userId, presence) {
|
|
65
|
-
__classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.trackPresence(userId, presence);
|
|
66
|
-
}
|
|
67
|
-
removePresence(userId) {
|
|
68
|
-
__classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.removePresence(userId);
|
|
69
|
-
}
|
|
70
|
-
updatePresence(userId, presence) {
|
|
71
|
-
__classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.updatePresence(userId, presence);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
exports.Channel = Channel;
|
|
75
|
-
_Channel_engine = new WeakMap();
|
|
76
32
|
class ChannelEngine {
|
|
77
33
|
constructor(name, parent) {
|
|
78
34
|
_ChannelEngine_instances.add(this);
|
|
@@ -85,6 +41,21 @@ class ChannelEngine {
|
|
|
85
41
|
__classPrivateFieldSet(this, _ChannelEngine_users, new Map(), "f");
|
|
86
42
|
__classPrivateFieldSet(this, _ChannelEngine_parentEngine, parent, "f");
|
|
87
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* @desc Gets the presence engine for the channel
|
|
46
|
+
*/
|
|
47
|
+
get presenceEngine() {
|
|
48
|
+
var _a;
|
|
49
|
+
const presenceEngine = (_a = __classPrivateFieldGet(this, _ChannelEngine_presenceEngine, "f")) !== null && _a !== void 0 ? _a : new presence_1.PresenceEngine(this);
|
|
50
|
+
__classPrivateFieldSet(this, _ChannelEngine_presenceEngine, presenceEngine, "f");
|
|
51
|
+
return presenceEngine;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* @desc Gets the number of users in the channel
|
|
55
|
+
*/
|
|
56
|
+
get size() {
|
|
57
|
+
return __classPrivateFieldGet(this, _ChannelEngine_users, "f").size;
|
|
58
|
+
}
|
|
88
59
|
/**
|
|
89
60
|
* @desc Adds a user to the channel
|
|
90
61
|
* @param userId - The id of the user to add
|
|
@@ -101,48 +72,18 @@ class ChannelEngine {
|
|
|
101
72
|
__classPrivateFieldGet(this, _ChannelEngine_users, "f").set(userId, assigns);
|
|
102
73
|
return __classPrivateFieldGet(this, _ChannelEngine_instances, "m", _ChannelEngine_subscribe).call(this, userId, onMessage);
|
|
103
74
|
}
|
|
104
|
-
/**
|
|
105
|
-
* @desc Removes a user from the channel
|
|
106
|
-
* @param userId - The id of the user to remove
|
|
107
|
-
* @param isGraceful - Whether the user is being removed gracefully
|
|
108
|
-
*/
|
|
109
|
-
removeUser(userId, isGraceful) {
|
|
110
|
-
var _a;
|
|
111
|
-
const user = __classPrivateFieldGet(this, _ChannelEngine_users, "f").get(userId);
|
|
112
|
-
if (user) {
|
|
113
|
-
__classPrivateFieldGet(this, _ChannelEngine_users, "f").delete(userId);
|
|
114
|
-
__classPrivateFieldGet(this, _ChannelEngine_receiver, "f").unsubscribe(userId);
|
|
115
|
-
const userPresence = (_a = __classPrivateFieldGet(this, _ChannelEngine_presenceEngine, "f")) === null || _a === void 0 ? void 0 : _a.removePresence(userId);
|
|
116
|
-
if (__classPrivateFieldGet(this, _ChannelEngine_parentEngine, "f").leaveCallback) {
|
|
117
|
-
__classPrivateFieldGet(this, _ChannelEngine_parentEngine, "f").leaveCallback({
|
|
118
|
-
channel: new Channel(this),
|
|
119
|
-
user: {
|
|
120
|
-
id: userId,
|
|
121
|
-
presence: userPresence !== null && userPresence !== void 0 ? userPresence : {},
|
|
122
|
-
assigns: user,
|
|
123
|
-
},
|
|
124
|
-
});
|
|
125
|
-
}
|
|
126
|
-
if (__classPrivateFieldGet(this, _ChannelEngine_users, "f").size === 0) {
|
|
127
|
-
__classPrivateFieldGet(this, _ChannelEngine_parentEngine, "f").destroyChannel();
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
else if (!isGraceful) {
|
|
131
|
-
throw new pondError_1.ChannelError(`ChannelEngine: User with id ${userId} does not exist in channel ${this.name}`, 404, this.name);
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
75
|
/**
|
|
135
76
|
* @desc Kicks a user from the channel
|
|
136
77
|
* @param userId - The id of the user to kick
|
|
137
78
|
* @param reason - The reason for kicking the user
|
|
138
79
|
*/
|
|
139
80
|
kickUser(userId, reason) {
|
|
140
|
-
this.sendMessage(
|
|
81
|
+
this.sendMessage(pondsocket_common_1.SystemSender.CHANNEL, [userId], pondsocket_common_1.ServerActions.SYSTEM, 'kicked_out', {
|
|
141
82
|
message: reason,
|
|
142
83
|
code: 403,
|
|
143
84
|
});
|
|
144
|
-
this.
|
|
145
|
-
this.sendMessage(
|
|
85
|
+
__classPrivateFieldGet(this, _ChannelEngine_instances, "m", _ChannelEngine_removeUser).call(this, userId);
|
|
86
|
+
this.sendMessage(pondsocket_common_1.SystemSender.CHANNEL, pondsocket_common_1.ChannelReceiver.ALL_USERS, pondsocket_common_1.ServerActions.SYSTEM, 'kicked', {
|
|
146
87
|
userId,
|
|
147
88
|
reason,
|
|
148
89
|
});
|
|
@@ -152,11 +93,11 @@ class ChannelEngine {
|
|
|
152
93
|
* @param reason - The reason for self-destructing the channel
|
|
153
94
|
*/
|
|
154
95
|
destroy(reason) {
|
|
155
|
-
this.sendMessage(
|
|
96
|
+
this.sendMessage(pondsocket_common_1.SystemSender.CHANNEL, pondsocket_common_1.ChannelReceiver.ALL_USERS, pondsocket_common_1.ServerActions.ERROR, 'destroyed', {
|
|
156
97
|
message: reason !== null && reason !== void 0 ? reason : 'Channel has been destroyed',
|
|
157
98
|
});
|
|
158
|
-
__classPrivateFieldGet(this, _ChannelEngine_parentEngine, "f").destroyChannel();
|
|
159
|
-
__classPrivateFieldGet(this, _ChannelEngine_users, "f").forEach((_, userId) => this.
|
|
99
|
+
__classPrivateFieldGet(this, _ChannelEngine_parentEngine, "f").destroyChannel(this.name);
|
|
100
|
+
__classPrivateFieldGet(this, _ChannelEngine_users, "f").forEach((_, userId) => __classPrivateFieldGet(this, _ChannelEngine_instances, "m", _ChannelEngine_removeUser).call(this, userId));
|
|
160
101
|
}
|
|
161
102
|
/**
|
|
162
103
|
* @desc Updates a user's assigns
|
|
@@ -207,7 +148,7 @@ class ChannelEngine {
|
|
|
207
148
|
* @param event - The event name
|
|
208
149
|
* @param payload - The payload of the message
|
|
209
150
|
*/
|
|
210
|
-
sendMessage(
|
|
151
|
+
sendMessage(sender, recipient, action, event, payload, requestId = (0, pondsocket_common_1.uuid)()) {
|
|
211
152
|
if (!__classPrivateFieldGet(this, _ChannelEngine_users, "f").has(sender) && sender !== pondsocket_common_1.SystemSender.CHANNEL) {
|
|
212
153
|
throw new pondError_1.ChannelError(`ChannelEngine: User with id ${sender} does not exist in channel ${this.name}`, 404, this.name);
|
|
213
154
|
}
|
|
@@ -237,41 +178,42 @@ class ChannelEngine {
|
|
|
237
178
|
action: pondsocket_common_1.ServerActions.BROADCAST,
|
|
238
179
|
channelName: this.name,
|
|
239
180
|
requestId: message.requestId,
|
|
240
|
-
recipients: __classPrivateFieldGet(this, _ChannelEngine_instances, "m", _ChannelEngine_getUsersFromRecipients).call(this, message.addresses || pondsocket_common_1.ChannelReceiver.ALL_USERS, userId),
|
|
241
181
|
};
|
|
242
182
|
const request = new eventRequest_1.EventRequest(responseEvent, this);
|
|
243
183
|
const response = new eventResponse_1.EventResponse(responseEvent, this);
|
|
244
|
-
__classPrivateFieldGet(this, _ChannelEngine_parentEngine, "f").
|
|
245
|
-
this.sendMessage(
|
|
184
|
+
__classPrivateFieldGet(this, _ChannelEngine_parentEngine, "f").middleware.run(request, response, () => {
|
|
185
|
+
this.sendMessage(pondsocket_common_1.SystemSender.CHANNEL, [userId], pondsocket_common_1.ServerActions.ERROR, pondsocket_common_1.ErrorTypes.HANDLER_NOT_FOUND, {
|
|
246
186
|
message: 'A handler did not respond to the event',
|
|
247
187
|
code: 404,
|
|
248
|
-
});
|
|
188
|
+
}, message.requestId);
|
|
249
189
|
});
|
|
250
190
|
}
|
|
251
191
|
/**
|
|
252
|
-
* @desc
|
|
192
|
+
* @desc Subscribes a user to a channel, Will join the channel if it exists or add to pending subscriptions
|
|
193
|
+
* @param userId - The id of the user to subscribe
|
|
194
|
+
* @param channel - The name of the channel to subscribe to
|
|
253
195
|
*/
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
const presenceEngine = (_a = __classPrivateFieldGet(this, _ChannelEngine_presenceEngine, "f")) !== null && _a !== void 0 ? _a : new presence_1.PresenceEngine(this);
|
|
257
|
-
__classPrivateFieldSet(this, _ChannelEngine_presenceEngine, presenceEngine, "f");
|
|
258
|
-
return presenceEngine;
|
|
196
|
+
subscribeTo(userId, channel) {
|
|
197
|
+
__classPrivateFieldGet(this, _ChannelEngine_parentEngine, "f").parent.subscribeTo(userId, channel);
|
|
259
198
|
}
|
|
260
199
|
/**
|
|
261
|
-
* @desc
|
|
200
|
+
* @desc Unsubscribes a user from a channel
|
|
201
|
+
* @param userId - The id of the user to unsubscribe
|
|
202
|
+
* @param channel - The name of the channel to unsubscribe from
|
|
262
203
|
*/
|
|
263
|
-
|
|
264
|
-
|
|
204
|
+
unsubscribeFrom(userId, channel) {
|
|
205
|
+
__classPrivateFieldGet(this, _ChannelEngine_parentEngine, "f").parent.unsubscribeFrom(userId, channel);
|
|
265
206
|
}
|
|
266
207
|
}
|
|
267
208
|
exports.ChannelEngine = ChannelEngine;
|
|
268
209
|
_ChannelEngine_receiver = new WeakMap(), _ChannelEngine_presenceEngine = new WeakMap(), _ChannelEngine_users = new WeakMap(), _ChannelEngine_parentEngine = new WeakMap(), _ChannelEngine_instances = new WeakSet(), _ChannelEngine_subscribe = function _ChannelEngine_subscribe(userId, onMessage) {
|
|
269
|
-
|
|
210
|
+
const unsubscribe = __classPrivateFieldGet(this, _ChannelEngine_receiver, "f").subscribe((_a) => {
|
|
270
211
|
var { recipients } = _a, event = __rest(_a, ["recipients"]);
|
|
271
212
|
if (recipients.includes(userId)) {
|
|
272
213
|
onMessage(event);
|
|
273
214
|
}
|
|
274
215
|
});
|
|
216
|
+
return __classPrivateFieldGet(this, _ChannelEngine_instances, "m", _ChannelEngine_buildSubscription).bind(this, userId, unsubscribe);
|
|
275
217
|
}, _ChannelEngine_getUsersFromRecipients = function _ChannelEngine_getUsersFromRecipients(recipients, sender) {
|
|
276
218
|
const allUsers = Array.from(__classPrivateFieldGet(this, _ChannelEngine_users, "f").keys());
|
|
277
219
|
let users;
|
|
@@ -296,4 +238,83 @@ _ChannelEngine_receiver = new WeakMap(), _ChannelEngine_presenceEngine = new Wea
|
|
|
296
238
|
break;
|
|
297
239
|
}
|
|
298
240
|
return users;
|
|
241
|
+
}, _ChannelEngine_buildSubscription = function _ChannelEngine_buildSubscription(userId, unsubscribe) {
|
|
242
|
+
var _a;
|
|
243
|
+
const user = __classPrivateFieldGet(this, _ChannelEngine_users, "f").get(userId);
|
|
244
|
+
if (!user) {
|
|
245
|
+
throw new pondError_1.ChannelError(`ChannelEngine: User with id ${userId} does not exist in channel ${this.name}`, 404, this.name);
|
|
246
|
+
}
|
|
247
|
+
unsubscribe();
|
|
248
|
+
__classPrivateFieldGet(this, _ChannelEngine_users, "f").delete(userId);
|
|
249
|
+
const userPresence = (_a = __classPrivateFieldGet(this, _ChannelEngine_presenceEngine, "f")) === null || _a === void 0 ? void 0 : _a.removePresence(userId);
|
|
250
|
+
if (__classPrivateFieldGet(this, _ChannelEngine_parentEngine, "f").leaveCallback) {
|
|
251
|
+
__classPrivateFieldGet(this, _ChannelEngine_parentEngine, "f").leaveCallback({
|
|
252
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
253
|
+
channel: new Channel(this),
|
|
254
|
+
user: {
|
|
255
|
+
id: userId,
|
|
256
|
+
presence: userPresence !== null && userPresence !== void 0 ? userPresence : {},
|
|
257
|
+
assigns: user,
|
|
258
|
+
},
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
if (__classPrivateFieldGet(this, _ChannelEngine_users, "f").size === 0) {
|
|
262
|
+
__classPrivateFieldGet(this, _ChannelEngine_parentEngine, "f").destroyChannel(this.name);
|
|
263
|
+
}
|
|
264
|
+
}, _ChannelEngine_removeUser = function _ChannelEngine_removeUser(userId) {
|
|
265
|
+
const cachedUser = __classPrivateFieldGet(this, _ChannelEngine_parentEngine, "f").parent.getUser(userId);
|
|
266
|
+
const unsubscribe = cachedUser.subscriptions.get(this.name);
|
|
267
|
+
if (!unsubscribe) {
|
|
268
|
+
throw new pondError_1.ChannelError(`ChannelEngine: User with id ${userId} does not exist in channel ${this.name}`, 404, this.name);
|
|
269
|
+
}
|
|
270
|
+
unsubscribe();
|
|
271
|
+
cachedUser.subscriptions.delete(this.name);
|
|
299
272
|
};
|
|
273
|
+
class Channel {
|
|
274
|
+
constructor(engine) {
|
|
275
|
+
_Channel_engine.set(this, void 0);
|
|
276
|
+
__classPrivateFieldSet(this, _Channel_engine, engine, "f");
|
|
277
|
+
}
|
|
278
|
+
get name() {
|
|
279
|
+
return __classPrivateFieldGet(this, _Channel_engine, "f").name;
|
|
280
|
+
}
|
|
281
|
+
getAssigns() {
|
|
282
|
+
return __classPrivateFieldGet(this, _Channel_engine, "f").getAssigns();
|
|
283
|
+
}
|
|
284
|
+
getPresences() {
|
|
285
|
+
return __classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.getPresence();
|
|
286
|
+
}
|
|
287
|
+
getUserData(userId) {
|
|
288
|
+
return __classPrivateFieldGet(this, _Channel_engine, "f").getUserData(userId);
|
|
289
|
+
}
|
|
290
|
+
broadcast(event, payload) {
|
|
291
|
+
__classPrivateFieldGet(this, _Channel_engine, "f").sendMessage(pondsocket_common_1.SystemSender.CHANNEL, pondsocket_common_1.ChannelReceiver.ALL_USERS, pondsocket_common_1.ServerActions.BROADCAST, event, payload);
|
|
292
|
+
}
|
|
293
|
+
broadcastFrom(userId, event, payload) {
|
|
294
|
+
__classPrivateFieldGet(this, _Channel_engine, "f").sendMessage(userId, pondsocket_common_1.ChannelReceiver.ALL_EXCEPT_SENDER, pondsocket_common_1.ServerActions.BROADCAST, event, payload);
|
|
295
|
+
}
|
|
296
|
+
broadcastTo(userIds, event, payload) {
|
|
297
|
+
const users = Array.isArray(userIds) ? userIds : [userIds];
|
|
298
|
+
__classPrivateFieldGet(this, _Channel_engine, "f").sendMessage(pondsocket_common_1.SystemSender.CHANNEL, users, pondsocket_common_1.ServerActions.BROADCAST, event, payload);
|
|
299
|
+
}
|
|
300
|
+
evictUser(userId, reason) {
|
|
301
|
+
__classPrivateFieldGet(this, _Channel_engine, "f").kickUser(userId, reason !== null && reason !== void 0 ? reason : 'You have been banned from the channel');
|
|
302
|
+
}
|
|
303
|
+
trackPresence(userId, presence) {
|
|
304
|
+
__classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.trackPresence(userId, presence);
|
|
305
|
+
}
|
|
306
|
+
removePresence(userId) {
|
|
307
|
+
__classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.removePresence(userId);
|
|
308
|
+
}
|
|
309
|
+
updatePresence(userId, presence) {
|
|
310
|
+
__classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.updatePresence(userId, presence);
|
|
311
|
+
}
|
|
312
|
+
subscribeTo(userId, channel) {
|
|
313
|
+
__classPrivateFieldGet(this, _Channel_engine, "f").subscribeTo(userId, channel);
|
|
314
|
+
}
|
|
315
|
+
unsubscribeFrom(userId, channel) {
|
|
316
|
+
__classPrivateFieldGet(this, _Channel_engine, "f").unsubscribeFrom(userId, channel);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
exports.Channel = Channel;
|
|
320
|
+
_Channel_engine = new WeakMap();
|