]>
Commit | Line | Data |
---|---|---|
1 | import express from 'express' | |
2 | import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes' | |
3 | import { getFormattedObjects } from '../../../helpers/utils' | |
4 | import { sequelizeTypescript } from '../../../initializers/database' | |
5 | import { | |
6 | asyncMiddleware, | |
7 | asyncRetryTransactionMiddleware, | |
8 | authenticate, | |
9 | paginationValidator, | |
10 | setDefaultPagination, | |
11 | userHistoryListValidator, | |
12 | userHistoryRemoveAllValidator, | |
13 | userHistoryRemoveElementValidator | |
14 | } from '../../../middlewares' | |
15 | import { UserVideoHistoryModel } from '../../../models/user/user-video-history' | |
16 | ||
17 | const myVideosHistoryRouter = express.Router() | |
18 | ||
19 | myVideosHistoryRouter.get('/me/history/videos', | |
20 | authenticate, | |
21 | paginationValidator, | |
22 | setDefaultPagination, | |
23 | userHistoryListValidator, | |
24 | asyncMiddleware(listMyVideosHistory) | |
25 | ) | |
26 | ||
27 | myVideosHistoryRouter.delete('/me/history/videos/:videoId', | |
28 | authenticate, | |
29 | userHistoryRemoveElementValidator, | |
30 | asyncMiddleware(removeUserHistoryElement) | |
31 | ) | |
32 | ||
33 | myVideosHistoryRouter.post('/me/history/videos/remove', | |
34 | authenticate, | |
35 | userHistoryRemoveAllValidator, | |
36 | asyncRetryTransactionMiddleware(removeAllUserHistory) | |
37 | ) | |
38 | ||
39 | // --------------------------------------------------------------------------- | |
40 | ||
41 | export { | |
42 | myVideosHistoryRouter | |
43 | } | |
44 | ||
45 | // --------------------------------------------------------------------------- | |
46 | ||
47 | async function listMyVideosHistory (req: express.Request, res: express.Response) { | |
48 | const user = res.locals.oauth.token.User | |
49 | ||
50 | const resultList = await UserVideoHistoryModel.listForApi(user, req.query.start, req.query.count, req.query.search) | |
51 | ||
52 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
53 | } | |
54 | ||
55 | async function removeUserHistoryElement (req: express.Request, res: express.Response) { | |
56 | const user = res.locals.oauth.token.User | |
57 | ||
58 | await UserVideoHistoryModel.removeUserHistoryElement(user, parseInt(req.params.videoId + '')) | |
59 | ||
60 | return res.sendStatus(HttpStatusCode.NO_CONTENT_204) | |
61 | } | |
62 | ||
63 | async function removeAllUserHistory (req: express.Request, res: express.Response) { | |
64 | const user = res.locals.oauth.token.User | |
65 | const beforeDate = req.body.beforeDate || null | |
66 | ||
67 | await sequelizeTypescript.transaction(t => { | |
68 | return UserVideoHistoryModel.removeUserHistoryBefore(user, beforeDate, t) | |
69 | }) | |
70 | ||
71 | return res.type('json') | |
72 | .status(HttpStatusCode.NO_CONTENT_204) | |
73 | .end() | |
74 | } |