diff options
Diffstat (limited to 'server/controllers/api/videos/index.ts')
-rw-r--r-- | server/controllers/api/videos/index.ts | 45 |
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' | |||
66 | import { ownershipVideoRouter } from './ownership' | 66 | import { ownershipVideoRouter } from './ownership' |
67 | import { rateVideoRouter } from './rate' | 67 | import { rateVideoRouter } from './rate' |
68 | import { watchingRouter } from './watching' | 68 | import { watchingRouter } from './watching' |
69 | import { LiveManager } from '@server/lib/live-manager' | ||
69 | 70 | ||
70 | const auditLogger = auditLoggerFactory('videos') | 71 | const auditLogger = auditLoggerFactory('videos') |
71 | const videosRouter = express.Router() | 72 | const videosRouter = express.Router() |
@@ -416,26 +417,46 @@ async function getVideo (req: express.Request, res: express.Response) { | |||
416 | } | 417 | } |
417 | 418 | ||
418 | async function viewVideo (req: express.Request, res: express.Response) { | 419 | async 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 | ||
441 | async function getVideoDescription (req: express.Request, res: express.Response) { | 462 | async function getVideoDescription (req: express.Request, res: express.Response) { |