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