diff options
author | Chocobozzz <me@florianbigard.com> | 2019-08-22 10:33:22 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2019-08-22 10:33:58 +0200 |
commit | 1b42d73f44811eec1b7ddd72dd0d640a57c3376c (patch) | |
tree | 1d4c19191d602ff58bbb883375e54c5d35b5e837 | |
parent | 916937d7daf386e4e2d37b2ca22db07b644b02df (diff) | |
download | PeerTube-1b42d73f44811eec1b7ddd72dd0d640a57c3376c.tar.gz PeerTube-1b42d73f44811eec1b7ddd72dd0d640a57c3376c.tar.zst PeerTube-1b42d73f44811eec1b7ddd72dd0d640a57c3376c.zip |
Fix socket notification with multiple user tabs
-rw-r--r-- | server/lib/peertube-socket.ts | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/server/lib/peertube-socket.ts b/server/lib/peertube-socket.ts index eb84ecd4b..17748fd18 100644 --- a/server/lib/peertube-socket.ts +++ b/server/lib/peertube-socket.ts | |||
@@ -8,7 +8,7 @@ class PeerTubeSocket { | |||
8 | 8 | ||
9 | private static instance: PeerTubeSocket | 9 | private static instance: PeerTubeSocket |
10 | 10 | ||
11 | private userNotificationSockets: { [ userId: number ]: SocketIO.Socket } = {} | 11 | private userNotificationSockets: { [ userId: number ]: SocketIO.Socket[] } = {} |
12 | 12 | ||
13 | private constructor () {} | 13 | private constructor () {} |
14 | 14 | ||
@@ -22,22 +22,26 @@ class PeerTubeSocket { | |||
22 | 22 | ||
23 | logger.debug('User %d connected on the notification system.', userId) | 23 | logger.debug('User %d connected on the notification system.', userId) |
24 | 24 | ||
25 | this.userNotificationSockets[userId] = socket | 25 | if (!this.userNotificationSockets[userId]) this.userNotificationSockets[userId] = [] |
26 | |||
27 | this.userNotificationSockets[userId].push(socket) | ||
26 | 28 | ||
27 | socket.on('disconnect', () => { | 29 | socket.on('disconnect', () => { |
28 | logger.debug('User %d disconnected from SocketIO notifications.', userId) | 30 | logger.debug('User %d disconnected from SocketIO notifications.', userId) |
29 | 31 | ||
30 | delete this.userNotificationSockets[userId] | 32 | this.userNotificationSockets[userId] = this.userNotificationSockets[userId].filter(s => s !== socket) |
31 | }) | 33 | }) |
32 | }) | 34 | }) |
33 | } | 35 | } |
34 | 36 | ||
35 | sendNotification (userId: number, notification: UserNotificationModel) { | 37 | sendNotification (userId: number, notification: UserNotificationModel) { |
36 | const socket = this.userNotificationSockets[userId] | 38 | const sockets = this.userNotificationSockets[userId] |
37 | 39 | ||
38 | if (!socket) return | 40 | if (!sockets) return |
39 | 41 | ||
40 | socket.emit('new-notification', notification.toFormattedJSON()) | 42 | for (const socket of sockets) { |
43 | socket.emit('new-notification', notification.toFormattedJSON()) | ||
44 | } | ||
41 | } | 45 | } |
42 | 46 | ||
43 | static get Instance () { | 47 | static get Instance () { |