@cocreate/socket-server 1.19.5 → 1.20.1

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/CHANGELOG.md CHANGED
@@ -1,3 +1,24 @@
1
+ ## [1.20.1](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.20.0...v1.20.1) (2023-10-19)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * do not log updateUserStatus ([f98961b](https://github.com/CoCreate-app/CoCreate-socket-server/commit/f98961b91106ea34b3afeefdafbfcc1dffca095e))
7
+
8
+ # [1.20.0](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.19.5...v1.20.0) (2023-10-19)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * delete data.socket ([4fd38ff](https://github.com/CoCreate-app/CoCreate-socket-server/commit/4fd38ffd9989502955eb73d7d21f90102ea41e69))
14
+ * improved error handling ([04be91f](https://github.com/CoCreate-app/CoCreate-socket-server/commit/04be91ffcc62c7270fc83cbb1c755125485496fb))
15
+
16
+
17
+ ### Features
18
+
19
+ * delay user deletion for potential reconnect ([34e3a14](https://github.com/CoCreate-app/CoCreate-socket-server/commit/34e3a143776a0e67f8cea56ab2f0334a1e349df2))
20
+ * If authinticated emit 'notification.user' ([ac4779f](https://github.com/CoCreate-app/CoCreate-socket-server/commit/ac4779f0773e5f9b906390648c536e654cc1f40a))
21
+
1
22
  ## [1.19.5](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.19.4...v1.19.5) (2023-10-16)
2
23
 
3
24
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/socket-server",
3
- "version": "1.19.5",
3
+ "version": "1.20.1",
4
4
  "description": "CoCreate-socket-server",
5
5
  "keywords": [
6
6
  "cocreate-socket",
package/src/index.js CHANGED
@@ -42,8 +42,6 @@ class SocketServer extends EventEmitter {
42
42
  errors.error = organization.error
43
43
  return socket.send(JSON.stringify({ method: 'Access Denied', error: errors }))
44
44
  }
45
- // if (!organization)
46
- // return socket.send(JSON.stringify({ method: 'Access Denied', error: organization.error }))
47
45
 
48
46
  let options = decodeURIComponent(request.headers['sec-websocket-protocol'])
49
47
  options = JSON.parse(options)
@@ -86,18 +84,21 @@ class SocketServer extends EventEmitter {
86
84
 
87
85
  self.emit('read.object', data);
88
86
 
89
- if (self.authenticate && options.token) {
90
- let { user_id, expires } = self.authenticate.decodeToken(options.token)
91
- // self.authenticate.decodeToken(options.token).then(({ user_id, expires }) => {
87
+ if (self.authenticate) {
88
+ const { user_id, expires } = self.authenticate.decodeToken(options.token)
89
+ const userStatus = { socket, method: 'userStatus', user_id: options.user_id, userStatus: 'off', organization_id }
92
90
  if (user_id) {
91
+ options.user_id = user_id
93
92
  socket.user_id = user_id;
94
93
  socket.expires = expires;
95
- self.emit('userStatus', { socket, method: 'userStatus', user_id, userStatus: 'on', organization_id });
96
- } else
97
- self.emit('userStatus', { socket, user_id, status: 'off', organization_id });
94
+ userStatus.userStatus = 'on'
95
+ self.emit("notification.user", socket)
96
+ }
97
+
98
+ self.emit('userStatus', userStatus);
98
99
 
99
100
  self.onWebSocket(socket);
100
- // })
101
+
101
102
  } else
102
103
  self.onWebSocket(socket);
103
104
  } else {
@@ -174,12 +175,13 @@ class SocketServer extends EventEmitter {
174
175
 
175
176
  if (socket.user_id) {
176
177
  this.emit('userStatus', { socket, user_id: socket.user_id, userStatus: 'on', organization_id });
178
+ let user = this.users.get(socket.user_id)
177
179
 
178
- if (!this.users.has(socket.user_id)) {
180
+ if (!Array.isArray(user)) {
181
+ clearTimeout(user)
179
182
  this.users.set(socket.user_id, [socket])
180
- } else {
181
- this.users.get(socket.user_id).push(socket)
182
- }
183
+ } else
184
+ user.push(socket)
183
185
  }
184
186
 
185
187
  }
@@ -278,13 +280,22 @@ class SocketServer extends EventEmitter {
278
280
  if (socket.user_id) {
279
281
  let sockets = this.users.get(socket.user_id)
280
282
  if (sockets) {
281
- const index = sockets.findIndex(item => item.id === socket.id);
282
- if (index !== -1) {
283
- sockets.splice(index, 1);
284
- }
285
- if (!sockets.length) {
286
- this.users.delete(socket.user_id);
287
- this.emit('userStatus', { socket, user_id, status: 'off', organization_id });
283
+ if (Array.isArray(sockets) && sockets.length) {
284
+ const index = sockets.findIndex(item => item.id === socket.id);
285
+ if (index !== -1) {
286
+ sockets.splice(index, 1);
287
+ }
288
+ } else {
289
+ let userDebounceTimer = sockets
290
+
291
+ clearTimeout(userDebounceTimer);
292
+ userDebounceTimer = setTimeout(() => {
293
+ this.users.delete(socket.user_id);
294
+ this.emit('userStatus', { socket, user_id: socket.user_id, userStatus: 'off', organization_id });
295
+ }, 10000);
296
+
297
+ this.users.set(socket.user_id, userDebounceTimer)
298
+
288
299
  }
289
300
  }
290
301
  }
@@ -301,12 +312,16 @@ class SocketServer extends EventEmitter {
301
312
  organization_id
302
313
  });
303
314
 
304
- const organization = this.organizations.get(organization_id)
305
- if (organization && organization.organizationBalance == false)
306
- return socket.send({ method: 'Access Denied', organizationBalance: false, error: organization.error })
307
315
 
308
316
  let data = JSON.parse(message)
309
317
  if (data.method) {
318
+ const organization = this.organizations.get(organization_id)
319
+ if (organization && organization.organizationBalance == false) {
320
+ data.organizationBalance = false
321
+ data.error = organization.error
322
+ return socket.send(JSON.stringify(data))
323
+ }
324
+
310
325
  if (data.method === 'region.added' || data.method === 'region.removed')
311
326
  console.log('data.method: ', data.method)
312
327
 
@@ -317,8 +332,11 @@ class SocketServer extends EventEmitter {
317
332
 
318
333
  if (this.authorize) {
319
334
  if (!this.sockets.has(socket.id)) {
320
- if (organization && !organization.organizationBalance == false)
321
- return socket.send({ method: 'Access Denied', organizationBalance: false, error: organization.error })
335
+ if (organization && organization.organizationBalance == false) {
336
+ data.organizationBalance = false
337
+ data.error = organization.error
338
+ return socket.send(JSON.stringify(data))
339
+ }
322
340
  }
323
341
 
324
342
  data.socket = socket
@@ -351,11 +369,12 @@ class SocketServer extends EventEmitter {
351
369
 
352
370
  if (organization && organization.status === false) {
353
371
  let errors = {}
354
- errors.serverOrganization = organization.serverOrganization
355
- errors.serverStorage = organization.serverStorage
356
- errors.organizationBalance = organization.organizationBalance
357
- errors.error = organization.error
358
- return socket.send(JSON.stringify({ method: 'Access Denied', ...errors }))
372
+ data.serverOrganization = organization.serverOrganization
373
+ data.serverStorage = organization.serverStorage
374
+ data.organizationBalance = organization.organizationBalance
375
+ data.error = organization.error
376
+ delete data.socket
377
+ return socket.send(JSON.stringify(data))
359
378
  }
360
379
 
361
380
  // dburl is true and db does not have 'keys' array
@@ -402,7 +421,7 @@ class SocketServer extends EventEmitter {
402
421
  if (authorized && authorized.authorized)
403
422
  data = authorized.authorized
404
423
 
405
- if (!data.method.startsWith('read.') || data.log) {
424
+ if (!data.method.startsWith('read.') || data.log || data.method !== 'updateUserStatus' || data.method !== 'userStatus') {
406
425
  let object = { url: socket.socketUrl, data }
407
426
  delete object.socket
408
427
  this.emit('create.object', {