Skip to content

Commit

Permalink
Fix viewers federation
Browse files Browse the repository at this point in the history
  • Loading branch information
Chocobozzz committed Jan 27, 2025
1 parent d16d2e6 commit 962d340
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
16 changes: 8 additions & 8 deletions packages/tests/src/api/live/live-socket-messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ describe('Test live socket messages', function () {
it('Should correctly send views change notification', async function () {
this.timeout(60000)

let localLastVideoViews = 0
let remoteLastVideoViews = 0
let localLastVideoViewers = 0
let remoteLastVideoViewers = 0

const liveVideoUUID = await createLiveWrapper()
await waitJobs(servers)
Expand All @@ -114,15 +114,15 @@ describe('Test live socket messages', function () {
const videoId = await servers[0].videos.getId({ uuid: liveVideoUUID })

const localSocket = servers[0].socketIO.getLiveNotificationSocket()
localSocket.on('views-change', (data: LiveVideoEventPayload) => { localLastVideoViews = data.viewers })
localSocket.on('views-change', (data: LiveVideoEventPayload) => { localLastVideoViewers = data.viewers })
localSocket.emit('subscribe', { videoId })
}

{
const videoId = await servers[1].videos.getId({ uuid: liveVideoUUID })

const remoteSocket = servers[1].socketIO.getLiveNotificationSocket()
remoteSocket.on('views-change', (data: LiveVideoEventPayload) => { remoteLastVideoViews = data.viewers })
remoteSocket.on('views-change', (data: LiveVideoEventPayload) => { remoteLastVideoViewers = data.viewers })
remoteSocket.emit('subscribe', { videoId })
}

Expand All @@ -131,8 +131,8 @@ describe('Test live socket messages', function () {
await waitUntilLivePublishedOnAllServers(servers, liveVideoUUID)
await waitJobs(servers)

expect(localLastVideoViews).to.equal(0)
expect(remoteLastVideoViews).to.equal(0)
expect(localLastVideoViewers).to.equal(0)
expect(remoteLastVideoViewers).to.equal(0)

const interval = setInterval(async () => {
try {
Expand All @@ -146,8 +146,8 @@ describe('Test live socket messages', function () {
await wait(6000)
await waitJobs(servers)

expect(localLastVideoViews).to.equal(2)
expect(remoteLastVideoViews).to.equal(2)
expect(localLastVideoViewers).to.equal(2)
expect(remoteLastVideoViewers).to.equal(2)

clearInterval(interval)
await stopFfmpeg(ffmpegCommand)
Expand Down
9 changes: 9 additions & 0 deletions server/core/lib/views/shared/video-viewer-counters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ export class VideoViewerCounters {

logger.debug('Adding remote viewer to local video %s.', video.uuid, { viewerId, viewerExpires, ...lTags(video.uuid) })

const viewer = this.idToViewer.get(viewerId)
if (viewer) {
viewer.expires = viewerExpires.getTime()

return false
}

this.addViewerToVideo({ video, viewerExpires, viewerId, viewerScope: 'remote', viewerCount: 1 })

return true
Expand Down Expand Up @@ -119,6 +126,8 @@ export class VideoViewerCounters {
getTotalViewersOf (video: MVideoImmutable) {
const viewers = this.viewersPerVideo.get(video.id)

logger.error('toto', { viewers })

return viewers?.reduce((p, c) => p + c.viewerCount, 0) || 0
}

Expand Down
2 changes: 1 addition & 1 deletion server/core/middlewares/validators/videos/video-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const videoViewValidator = [

body('sessionId')
.optional()
.isAlphanumeric(),
.isAlphanumeric(undefined, { ignore: '-' }),

async (req: express.Request, res: express.Response, next: express.NextFunction) => {
if (areValidationErrors(req, res, { tags })) return
Expand Down

0 comments on commit 962d340

Please sign in to comment.