]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/peertube-socket.ts
c91407a5938cc091b428ce0b888c8fc9ae76221e
[github/Chocobozzz/PeerTube.git] / server / lib / peertube-socket.ts
1 import { Server } from 'http'
2 import * as SocketIO from 'socket.io'
3 import { MVideo } from '@server/types/models'
4 import { UserNotificationModelForApi } from '@server/types/models/user'
5 import { LiveVideoEventPayload, LiveVideoEventType } from '@shared/models'
6 import { logger } from '../helpers/logger'
7 import { authenticateSocket } from '../middlewares'
8 import { isIdValid } from '@server/helpers/custom-validators/misc'
9
10 class PeerTubeSocket {
11
12 private static instance: PeerTubeSocket
13
14 private userNotificationSockets: { [ userId: number ]: SocketIO.Socket[] } = {}
15 private liveVideosNamespace: SocketIO.Namespace
16
17 private constructor () {}
18
19 init (server: Server) {
20 const io = new SocketIO.Server(server)
21
22 io.of('/user-notifications')
23 .use(authenticateSocket)
24 .on('connection', socket => {
25 const userId = socket.handshake.auth.user.id
26
27 logger.debug('User %d connected on the notification system.', userId)
28
29 if (!this.userNotificationSockets[userId]) this.userNotificationSockets[userId] = []
30
31 this.userNotificationSockets[userId].push(socket)
32
33 socket.on('disconnect', () => {
34 logger.debug('User %d disconnected from SocketIO notifications.', userId)
35
36 this.userNotificationSockets[userId] = this.userNotificationSockets[userId].filter(s => s !== socket)
37 })
38 })
39
40 this.liveVideosNamespace = io.of('/live-videos')
41 .on('connection', socket => {
42 socket.on('subscribe', ({ videoId }) => {
43 if (!isIdValid(videoId)) return
44
45 socket.join(videoId)
46 })
47
48 socket.on('unsubscribe', ({ videoId }) => {
49 if (!isIdValid(videoId)) return
50
51 socket.leave(videoId)
52 })
53 })
54 }
55
56 sendNotification (userId: number, notification: UserNotificationModelForApi) {
57 const sockets = this.userNotificationSockets[userId]
58 if (!sockets) return
59
60 logger.debug('Sending user notification to user %d.', userId)
61
62 const notificationMessage = notification.toFormattedJSON()
63 for (const socket of sockets) {
64 socket.emit('new-notification', notificationMessage)
65 }
66 }
67
68 sendVideoLiveNewState (video: MVideo) {
69 const data: LiveVideoEventPayload = { state: video.state }
70 const type: LiveVideoEventType = 'state-change'
71
72 logger.debug('Sending video live new state notification of %s.', video.url, { state: video.state })
73
74 this.liveVideosNamespace
75 .in(video.id)
76 .emit(type, data)
77 }
78
79 sendVideoViewsUpdate (video: MVideo) {
80 const data: LiveVideoEventPayload = { views: video.views }
81 const type: LiveVideoEventType = 'views-change'
82
83 logger.debug('Sending video live views update notification of %s.', video.url, { views: video.views })
84
85 this.liveVideosNamespace
86 .in(video.id)
87 .emit(type, data)
88 }
89
90 static get Instance () {
91 return this.instance || (this.instance = new this())
92 }
93 }
94
95 // ---------------------------------------------------------------------------
96
97 export {
98 PeerTubeSocket
99 }