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