X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;ds=sidebyside;f=server%2Flib%2Fpeertube-socket.ts;h=e27963e606b52d6ea6c4ade1272000aec5f50441;hb=fae6e4da8f516a9d6c3bad9bf6f35811ccacbad8;hp=c918a8685d05856627c1b58b13471a2381d30309;hpb=a5cf76afa378aae81af2a9b0ce548e5d2582f832;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/peertube-socket.ts b/server/lib/peertube-socket.ts index c918a8685..e27963e60 100644 --- a/server/lib/peertube-socket.ts +++ b/server/lib/peertube-socket.ts @@ -1,4 +1,3 @@ -import { Socket } from 'dgram' import { Server } from 'http' import * as SocketIO from 'socket.io' import { MVideo } from '@server/types/models' @@ -6,6 +5,7 @@ import { UserNotificationModelForApi } from '@server/types/models/user' import { LiveVideoEventPayload, LiveVideoEventType } from '@shared/models' import { logger } from '../helpers/logger' import { authenticateSocket } from '../middlewares' +import { isIdValid } from '@server/helpers/custom-validators/misc' class PeerTubeSocket { @@ -17,7 +17,7 @@ class PeerTubeSocket { private constructor () {} init (server: Server) { - const io = SocketIO(server) + const io = new SocketIO.Server(server) io.of('/user-notifications') .use(authenticateSocket) @@ -39,8 +39,17 @@ class PeerTubeSocket { this.liveVideosNamespace = io.of('/live-videos') .on('connection', socket => { - socket.on('subscribe', ({ videoId }) => socket.join(videoId)) - socket.on('unsubscribe', ({ videoId }) => socket.leave(videoId)) + socket.on('subscribe', ({ videoId }) => { + if (!isIdValid(videoId)) return + + socket.join(videoId) + }) + + socket.on('unsubscribe', ({ videoId }) => { + if (!isIdValid(videoId)) return + + socket.leave(videoId) + }) }) } @@ -60,7 +69,18 @@ class PeerTubeSocket { const data: LiveVideoEventPayload = { state: video.state } const type: LiveVideoEventType = 'state-change' - logger.debug('Sending video live new state notification of %s.', video.url) + logger.debug('Sending video live new state notification of %s.', video.url, { state: video.state }) + + this.liveVideosNamespace + .in(video.id) + .emit(type, data) + } + + sendVideoViewsUpdate (video: MVideo) { + const data: LiveVideoEventPayload = { views: video.views } + const type: LiveVideoEventType = 'views-change' + + logger.debug('Sending video live views update notification of %s.', video.url, { views: video.views }) this.liveVideosNamespace .in(video.id)