]>
Commit | Line | Data |
---|---|---|
cef534ed | 1 | import { Server } from 'http' |
a5cf76af C |
2 | import * as SocketIO from 'socket.io' |
3 | import { MVideo } from '@server/types/models' | |
26d6bf65 | 4 | import { UserNotificationModelForApi } from '@server/types/models/user' |
a5cf76af C |
5 | import { LiveVideoEventPayload, LiveVideoEventType } from '@shared/models' |
6 | import { logger } from '../helpers/logger' | |
7 | import { authenticateSocket } from '../middlewares' | |
bd54ad19 | 8 | import { isIdValid } from '@server/helpers/custom-validators/misc' |
cef534ed C |
9 | |
10 | class 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 => { | |
25 | const userId = socket.handshake.query.user.id | |
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 | ||
45 | socket.join(videoId) | |
46 | }) | |
47 | ||
48 | socket.on('unsubscribe', ({ videoId }) => { | |
49 | if (!isIdValid(videoId)) return | |
50 | ||
51 | socket.leave(videoId) | |
52 | }) | |
a5cf76af | 53 | }) |
cef534ed C |
54 | } |
55 | ||
453e83ea | 56 | sendNotification (userId: number, notification: UserNotificationModelForApi) { |
1b42d73f | 57 | const sockets = this.userNotificationSockets[userId] |
1b42d73f | 58 | if (!sockets) return |
cef534ed | 59 | |
a5cf76af C |
60 | logger.debug('Sending user notification to user %d.', userId) |
61 | ||
20ec0384 | 62 | const notificationMessage = notification.toFormattedJSON() |
1b42d73f | 63 | for (const socket of sockets) { |
20ec0384 | 64 | socket.emit('new-notification', notificationMessage) |
1b42d73f | 65 | } |
cef534ed C |
66 | } |
67 | ||
a5cf76af C |
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) | |
73 | ||
74 | this.liveVideosNamespace | |
75 | .in(video.id) | |
76 | .emit(type, data) | |
77 | } | |
78 | ||
cef534ed C |
79 | static get Instance () { |
80 | return this.instance || (this.instance = new this()) | |
81 | } | |
82 | } | |
83 | ||
84 | // --------------------------------------------------------------------------- | |
85 | ||
86 | export { | |
87 | PeerTubeSocket | |
88 | } |