Fix socket notification with multiple user tabs

pull/2073/head
Chocobozzz 2019-08-22 10:33:22 +02:00
parent 7c58378a57
commit 015d9dec91
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
1 changed files with 10 additions and 6 deletions

View File

@ -8,7 +8,7 @@ class PeerTubeSocket {
private static instance: PeerTubeSocket private static instance: PeerTubeSocket
private userNotificationSockets: { [ userId: number ]: SocketIO.Socket } = {} private userNotificationSockets: { [ userId: number ]: SocketIO.Socket[] } = {}
private constructor () {} private constructor () {}
@ -22,22 +22,26 @@ class PeerTubeSocket {
logger.debug('User %d connected on the notification system.', userId) logger.debug('User %d connected on the notification system.', userId)
this.userNotificationSockets[userId] = socket if (!this.userNotificationSockets[userId]) this.userNotificationSockets[userId] = []
this.userNotificationSockets[userId].push(socket)
socket.on('disconnect', () => { socket.on('disconnect', () => {
logger.debug('User %d disconnected from SocketIO notifications.', userId) logger.debug('User %d disconnected from SocketIO notifications.', userId)
delete this.userNotificationSockets[userId] this.userNotificationSockets[userId] = this.userNotificationSockets[userId].filter(s => s !== socket)
}) })
}) })
} }
sendNotification (userId: number, notification: UserNotificationModel) { sendNotification (userId: number, notification: UserNotificationModel) {
const socket = this.userNotificationSockets[userId] const sockets = this.userNotificationSockets[userId]
if (!socket) return if (!sockets) return
socket.emit('new-notification', notification.toFormattedJSON()) for (const socket of sockets) {
socket.emit('new-notification', notification.toFormattedJSON())
}
} }
static get Instance () { static get Instance () {