Cleanup invalid rates/comments/shares
[oweals/peertube.git] / server / controllers / api / videos / watching.ts
1 import * as express from 'express'
2 import { UserWatchingVideo } from '../../../../shared'
3 import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, videoWatchingValidator } from '../../../middlewares'
4 import { UserVideoHistoryModel } from '../../../models/account/user-video-history'
5
6 const watchingRouter = express.Router()
7
8 watchingRouter.put('/:videoId/watching',
9   authenticate,
10   asyncMiddleware(videoWatchingValidator),
11   asyncRetryTransactionMiddleware(userWatchVideo)
12 )
13
14 // ---------------------------------------------------------------------------
15
16 export {
17   watchingRouter
18 }
19
20 // ---------------------------------------------------------------------------
21
22 async function userWatchVideo (req: express.Request, res: express.Response) {
23   const user = res.locals.oauth.token.User
24
25   const body: UserWatchingVideo = req.body
26   const { id: videoId } = res.locals.video as { id: number }
27
28   await UserVideoHistoryModel.upsert({
29     videoId,
30     userId: user.id,
31     currentTime: body.currentTime
32   })
33
34   return res.type('json').status(204).end()
35 }