]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/videos/watching.ts
add video upload types, add doc middleware to more routes
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / watching.ts
1 import * as express from 'express'
2 import { UserWatchingVideo } from '../../../../shared'
3 import {
4 asyncMiddleware,
5 asyncRetryTransactionMiddleware,
6 authenticate,
7 openapiOperationDoc,
8 videoWatchingValidator
9 } from '../../../middlewares'
10 import { UserVideoHistoryModel } from '../../../models/user/user-video-history'
11 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
12
13 const watchingRouter = express.Router()
14
15 watchingRouter.put('/:videoId/watching',
16 openapiOperationDoc({ operationId: 'setProgress' }),
17 authenticate,
18 asyncMiddleware(videoWatchingValidator),
19 asyncRetryTransactionMiddleware(userWatchVideo)
20 )
21
22 // ---------------------------------------------------------------------------
23
24 export {
25 watchingRouter
26 }
27
28 // ---------------------------------------------------------------------------
29
30 async function userWatchVideo (req: express.Request, res: express.Response) {
31 const user = res.locals.oauth.token.User
32
33 const body: UserWatchingVideo = req.body
34 const { id: videoId } = res.locals.videoId
35
36 await UserVideoHistoryModel.upsert({
37 videoId,
38 userId: user.id,
39 currentTime: body.currentTime
40 })
41
42 return res.type('json')
43 .status(HttpStatusCode.NO_CONTENT_204)
44 .end()
45 }