peertube-socket.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. class PeerTubeSocket {
  10. private static instance: PeerTubeSocket
  11. private userNotificationSockets: { [ userId: number ]: SocketIO.Socket[] } = {}
  12. private liveVideosNamespace: SocketIO.Namespace
  13. private constructor () {}
  14. init (server: Server) {
  15. const io = new SocketIO.Server(server)
  16. io.of('/user-notifications')
  17. .use(authenticateSocket)
  18. .on('connection', socket => {
  19. const userId = socket.handshake.query.user.id
  20. logger.debug('User %d connected on the notification system.', userId)
  21. if (!this.userNotificationSockets[userId]) this.userNotificationSockets[userId] = []
  22. this.userNotificationSockets[userId].push(socket)
  23. socket.on('disconnect', () => {
  24. logger.debug('User %d disconnected from SocketIO notifications.', userId)
  25. this.userNotificationSockets[userId] = this.userNotificationSockets[userId].filter(s => s !== socket)
  26. })
  27. })
  28. this.liveVideosNamespace = io.of('/live-videos')
  29. .on('connection', socket => {
  30. socket.on('subscribe', ({ videoId }) => {
  31. if (!isIdValid(videoId)) return
  32. socket.join(videoId)
  33. })
  34. socket.on('unsubscribe', ({ videoId }) => {
  35. if (!isIdValid(videoId)) return
  36. socket.leave(videoId)
  37. })
  38. })
  39. }
  40. sendNotification (userId: number, notification: UserNotificationModelForApi) {
  41. const sockets = this.userNotificationSockets[userId]
  42. if (!sockets) return
  43. logger.debug('Sending user notification to user %d.', userId)
  44. const notificationMessage = notification.toFormattedJSON()
  45. for (const socket of sockets) {
  46. socket.emit('new-notification', notificationMessage)
  47. }
  48. }
  49. sendVideoLiveNewState (video: MVideo) {
  50. const data: LiveVideoEventPayload = { state: video.state }
  51. const type: LiveVideoEventType = 'state-change'
  52. logger.debug('Sending video live new state notification of %s.', video.url, { state: video.state })
  53. this.liveVideosNamespace
  54. .in(video.id)
  55. .emit(type, data)
  56. }
  57. sendVideoViewsUpdate (video: MVideo) {
  58. const data: LiveVideoEventPayload = { views: video.views }
  59. const type: LiveVideoEventType = 'views-change'
  60. logger.debug('Sending video live views update notification of %s.', video.url, { views: video.views })
  61. this.liveVideosNamespace
  62. .in(video.id)
  63. .emit(type, data)
  64. }
  65. static get Instance () {
  66. return this.instance || (this.instance = new this())
  67. }
  68. }
  69. // ---------------------------------------------------------------------------
  70. export {
  71. PeerTubeSocket
  72. }