]>
Commit | Line | Data |
---|---|---|
8b9a525a C |
1 | import * as express from 'express' |
2 | import { | |
3 | asyncMiddleware, | |
4 | asyncRetryTransactionMiddleware, | |
5 | authenticate, | |
6 | paginationValidator, | |
7 | setDefaultPagination, | |
8 | userHistoryRemoveValidator | |
9 | } from '../../../middlewares' | |
8b9a525a C |
10 | import { getFormattedObjects } from '../../../helpers/utils' |
11 | import { UserVideoHistoryModel } from '../../../models/account/user-video-history' | |
80fdaf06 | 12 | import { sequelizeTypescript } from '../../../initializers/database' |
2d53be02 | 13 | import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' |
8b9a525a C |
14 | |
15 | const myVideosHistoryRouter = express.Router() | |
16 | ||
17 | myVideosHistoryRouter.get('/me/history/videos', | |
18 | authenticate, | |
19 | paginationValidator, | |
20 | setDefaultPagination, | |
21 | asyncMiddleware(listMyVideosHistory) | |
22 | ) | |
23 | ||
24 | myVideosHistoryRouter.post('/me/history/videos/remove', | |
25 | authenticate, | |
26 | userHistoryRemoveValidator, | |
27 | asyncRetryTransactionMiddleware(removeUserHistory) | |
28 | ) | |
29 | ||
30 | // --------------------------------------------------------------------------- | |
31 | ||
32 | export { | |
33 | myVideosHistoryRouter | |
34 | } | |
35 | ||
36 | // --------------------------------------------------------------------------- | |
37 | ||
38 | async function listMyVideosHistory (req: express.Request, res: express.Response) { | |
dae86118 | 39 | const user = res.locals.oauth.token.User |
8b9a525a C |
40 | |
41 | const resultList = await UserVideoHistoryModel.listForApi(user, req.query.start, req.query.count) | |
42 | ||
43 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
44 | } | |
45 | ||
46 | async function removeUserHistory (req: express.Request, res: express.Response) { | |
dae86118 | 47 | const user = res.locals.oauth.token.User |
8b9a525a C |
48 | const beforeDate = req.body.beforeDate || null |
49 | ||
50 | await sequelizeTypescript.transaction(t => { | |
8f0bc73d | 51 | return UserVideoHistoryModel.removeUserHistoryBefore(user, beforeDate, t) |
8b9a525a C |
52 | }) |
53 | ||
2d53be02 RK |
54 | return res.type('json') |
55 | .status(HttpStatusCode.NO_CONTENT_204) | |
56 | .end() | |
8b9a525a | 57 | } |