aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/videos
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2020-11-06 16:42:23 +0100
committerChocobozzz <chocobozzz@cpy.re>2020-11-09 15:33:04 +0100
commite4bf78561763cd84d22ebceb6f371cccf9a356d8 (patch)
tree7d8ea6ed53810d1dfcc2cfa5e3150da8e87e4645 /server/controllers/api/videos
parent529f037294d9917a62235f8162887a8edc04c32f (diff)
downloadPeerTube-e4bf78561763cd84d22ebceb6f371cccf9a356d8.tar.gz
PeerTube-e4bf78561763cd84d22ebceb6f371cccf9a356d8.tar.zst
PeerTube-e4bf78561763cd84d22ebceb6f371cccf9a356d8.zip
Handle views for live videos
Diffstat (limited to 'server/controllers/api/videos')
-rw-r--r--server/controllers/api/videos/index.ts45
1 files changed, 33 insertions, 12 deletions
diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts
index ff29e584b..3f96f142c 100644
--- a/server/controllers/api/videos/index.ts
+++ b/server/controllers/api/videos/index.ts
@@ -66,6 +66,7 @@ import { liveRouter } from './live'
66import { ownershipVideoRouter } from './ownership' 66import { ownershipVideoRouter } from './ownership'
67import { rateVideoRouter } from './rate' 67import { rateVideoRouter } from './rate'
68import { watchingRouter } from './watching' 68import { watchingRouter } from './watching'
69import { LiveManager } from '@server/lib/live-manager'
69 70
70const auditLogger = auditLoggerFactory('videos') 71const auditLogger = auditLoggerFactory('videos')
71const videosRouter = express.Router() 72const videosRouter = express.Router()
@@ -416,26 +417,46 @@ async function getVideo (req: express.Request, res: express.Response) {
416} 417}
417 418
418async function viewVideo (req: express.Request, res: express.Response) { 419async function viewVideo (req: express.Request, res: express.Response) {
419 const videoInstance = res.locals.onlyImmutableVideo 420 const immutableVideoAttrs = res.locals.onlyImmutableVideo
420 421
421 const ip = req.ip 422 const ip = req.ip
422 const exists = await Redis.Instance.doesVideoIPViewExist(ip, videoInstance.uuid) 423 const exists = await Redis.Instance.doesVideoIPViewExist(ip, immutableVideoAttrs.uuid)
423 if (exists) { 424 if (exists) {
424 logger.debug('View for ip %s and video %s already exists.', ip, videoInstance.uuid) 425 logger.debug('View for ip %s and video %s already exists.', ip, immutableVideoAttrs.uuid)
425 return res.status(204).end() 426 return res.sendStatus(204)
426 } 427 }
427 428
428 await Promise.all([ 429 const video = await VideoModel.load(immutableVideoAttrs.id)
429 Redis.Instance.addVideoView(videoInstance.id),
430 Redis.Instance.setIPVideoView(ip, videoInstance.uuid)
431 ])
432 430
433 const serverActor = await getServerActor() 431 const promises: Promise<any>[] = [
434 await sendView(serverActor, videoInstance, undefined) 432 Redis.Instance.setIPVideoView(ip, video.uuid, video.isLive)
433 ]
435 434
436 Hooks.runAction('action:api.video.viewed', { video: videoInstance, ip }) 435 let federateView = true
437 436
438 return res.status(204).end() 437 // Increment our live manager
438 if (video.isLive && video.isOwned()) {
439 LiveManager.Instance.addViewTo(video.id)
440
441 // Views of our local live will be sent by our live manager
442 federateView = false
443 }
444
445 // Increment our video views cache counter
446 if (!video.isLive) {
447 promises.push(Redis.Instance.addVideoView(video.id))
448 }
449
450 if (federateView) {
451 const serverActor = await getServerActor()
452 promises.push(sendView(serverActor, video, undefined))
453 }
454
455 await Promise.all(promises)
456
457 Hooks.runAction('action:api.video.viewed', { video, ip })
458
459 return res.sendStatus(204)
439} 460}
440 461
441async function getVideoDescription (req: express.Request, res: express.Response) { 462async function getVideoDescription (req: express.Request, res: express.Response) {