]>
Commit | Line | Data |
---|---|---|
1 | import express from 'express' | |
2 | import { HttpStatusCode, UserWatchingVideo } from '@shared/models' | |
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 | ||
12 | const watchingRouter = express.Router() | |
13 | ||
14 | watchingRouter.put('/:videoId/watching', | |
15 | openapiOperationDoc({ operationId: 'setProgress' }), | |
16 | authenticate, | |
17 | asyncMiddleware(videoWatchingValidator), | |
18 | asyncRetryTransactionMiddleware(userWatchVideo) | |
19 | ) | |
20 | ||
21 | // --------------------------------------------------------------------------- | |
22 | ||
23 | export { | |
24 | watchingRouter | |
25 | } | |
26 | ||
27 | // --------------------------------------------------------------------------- | |
28 | ||
29 | async function userWatchVideo (req: express.Request, res: express.Response) { | |
30 | const user = res.locals.oauth.token.User | |
31 | ||
32 | const body: UserWatchingVideo = req.body | |
33 | const { id: videoId } = res.locals.videoId | |
34 | ||
35 | await UserVideoHistoryModel.upsert({ | |
36 | videoId, | |
37 | userId: user.id, | |
38 | currentTime: body.currentTime | |
39 | }) | |
40 | ||
41 | return res.type('json') | |
42 | .status(HttpStatusCode.NO_CONTENT_204) | |
43 | .end() | |
44 | } |