]>
Commit | Line | Data |
---|---|---|
1 | import { Server as HTTPServer } from 'http' | |
2 | import { Namespace, Server as SocketServer, Socket } from 'socket.io' | |
3 | import { isIdValid } from '@server/helpers/custom-validators/misc' | |
4 | import { MVideo } from '@server/types/models' | |
5 | import { UserNotificationModelForApi } from '@server/types/models/user' | |
6 | import { LiveVideoEventPayload, LiveVideoEventType } from '@shared/models' | |
7 | import { logger } from '../helpers/logger' | |
8 | import { authenticateSocket } from '../middlewares' | |
9 | ||
10 | class PeerTubeSocket { | |
11 | ||
12 | private static instance: PeerTubeSocket | |
13 | ||
14 | private userNotificationSockets: { [ userId: number ]: Socket[] } = {} | |
15 | private liveVideosNamespace: Namespace | |
16 | ||
17 | private constructor () {} | |
18 | ||
19 | init (server: HTTPServer) { | |
20 | const io = new SocketServer(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 | /* eslint-disable @typescript-eslint/no-floating-promises */ | |
46 | socket.join(videoId) | |
47 | }) | |
48 | ||
49 | socket.on('unsubscribe', ({ videoId }) => { | |
50 | if (!isIdValid(videoId)) return | |
51 | ||
52 | /* eslint-disable @typescript-eslint/no-floating-promises */ | |
53 | socket.leave(videoId) | |
54 | }) | |
55 | }) | |
56 | } | |
57 | ||
58 | sendNotification (userId: number, notification: UserNotificationModelForApi) { | |
59 | const sockets = this.userNotificationSockets[userId] | |
60 | if (!sockets) return | |
61 | ||
62 | logger.debug('Sending user notification to user %d.', userId) | |
63 | ||
64 | const notificationMessage = notification.toFormattedJSON() | |
65 | for (const socket of sockets) { | |
66 | socket.emit('new-notification', notificationMessage) | |
67 | } | |
68 | } | |
69 | ||
70 | sendVideoLiveNewState (video: MVideo) { | |
71 | const data: LiveVideoEventPayload = { state: video.state } | |
72 | const type: LiveVideoEventType = 'state-change' | |
73 | ||
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 }) | |
86 | ||
87 | this.liveVideosNamespace | |
88 | .in(video.id) | |
89 | .emit(type, data) | |
90 | } | |
91 | ||
92 | static get Instance () { | |
93 | return this.instance || (this.instance = new this()) | |
94 | } | |
95 | } | |
96 | ||
97 | // --------------------------------------------------------------------------- | |
98 | ||
99 | export { | |
100 | PeerTubeSocket | |
101 | } |