From 1f3e9feca2caf68024168b0ea9ed39d8438fa235 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 30 Nov 2017 09:21:11 +0100 Subject: Better view counter --- server/controllers/api/videos/index.ts | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) (limited to 'server/controllers/api/videos') diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts index 244d91914..e2798830e 100644 --- a/server/controllers/api/videos/index.ts +++ b/server/controllers/api/videos/index.ts @@ -104,6 +104,10 @@ videosRouter.get('/:id', asyncMiddleware(videosGetValidator), getVideo ) +videosRouter.post('/:id/views', + asyncMiddleware(videosGetValidator), + asyncMiddleware(viewVideo) +) videosRouter.delete('/:id', authenticate, @@ -311,25 +315,25 @@ async function updateVideo (req: express.Request, res: express.Response) { } } -async function getVideo (req: express.Request, res: express.Response) { +function getVideo (req: express.Request, res: express.Response) { + const videoInstance = res.locals.video + + return res.json(videoInstance.toFormattedDetailsJSON()) +} + +async function viewVideo (req: express.Request, res: express.Response) { const videoInstance = res.locals.video - const baseIncrementPromise = videoInstance.increment('views') - .then(() => getServerAccount()) + await videoInstance.increment('views') + const serverAccount = await getServerAccount() if (videoInstance.isOwned()) { - // The increment is done directly in the database, not using the instance value - baseIncrementPromise - .then(serverAccount => sendCreateViewToVideoFollowers(serverAccount, videoInstance, undefined)) - .catch(err => logger.error('Cannot add view to video/send view to followers for %s.', videoInstance.uuid, err)) + await sendCreateViewToVideoFollowers(serverAccount, videoInstance, undefined) } else { - baseIncrementPromise - .then(serverAccount => sendCreateViewToOrigin(serverAccount, videoInstance, undefined)) - .catch(err => logger.error('Cannot send view to origin server for %s.', videoInstance.uuid, err)) + await sendCreateViewToOrigin(serverAccount, videoInstance, undefined) } - // Do not wait the view system - return res.json(videoInstance.toFormattedDetailsJSON()) + return res.status(204).end() } async function getVideoDescription (req: express.Request, res: express.Response) { -- cgit v1.2.3