]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/peertube-socket.ts
Logging ip requesting unknown infoHash
[github/Chocobozzz/PeerTube.git] / server / lib / peertube-socket.ts
index eb84ecd4bbb134a3216f2ca62576b9459f0a7274..26ced351f3333cf1dfe756dc333ad95653c9ff88 100644 (file)
@@ -1,14 +1,14 @@
 import * as SocketIO from 'socket.io'
 import { authenticateSocket } from '../middlewares'
-import { UserNotificationModel } from '../models/account/user-notification'
 import { logger } from '../helpers/logger'
 import { Server } from 'http'
+import { UserNotificationModelForApi } from '@server/typings/models/user'
 
 class PeerTubeSocket {
 
   private static instance: PeerTubeSocket
 
-  private userNotificationSockets: { [ userId: number ]: SocketIO.Socket } = {}
+  private userNotificationSockets: { [ userId: number ]: SocketIO.Socket[] } = {}
 
   private constructor () {}
 
@@ -22,22 +22,27 @@ class PeerTubeSocket {
 
         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', () => {
           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) {
-    const socket = this.userNotificationSockets[userId]
+  sendNotification (userId: number, notification: UserNotificationModelForApi) {
+    const sockets = this.userNotificationSockets[userId]
 
-    if (!socket) return
+    if (!sockets) return
 
-    socket.emit('new-notification', notification.toFormattedJSON())
+    const notificationMessage = notification.toFormattedJSON()
+    for (const socket of sockets) {
+      socket.emit('new-notification', notificationMessage)
+    }
   }
 
   static get Instance () {