]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/controllers/api/users/my-history.ts
Merge branch 'release/3.2.0' into develop
[github/Chocobozzz/PeerTube.git] / server / controllers / api / users / my-history.ts
... / ...
CommitLineData
1import * as express from 'express'
2import {
3 asyncMiddleware,
4 asyncRetryTransactionMiddleware,
5 authenticate,
6 paginationValidator,
7 setDefaultPagination,
8 userHistoryListValidator,
9 userHistoryRemoveValidator
10} from '../../../middlewares'
11import { getFormattedObjects } from '../../../helpers/utils'
12import { UserVideoHistoryModel } from '../../../models/user/user-video-history'
13import { sequelizeTypescript } from '../../../initializers/database'
14import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
15
16const myVideosHistoryRouter = express.Router()
17
18myVideosHistoryRouter.get('/me/history/videos',
19 authenticate,
20 paginationValidator,
21 setDefaultPagination,
22 userHistoryListValidator,
23 asyncMiddleware(listMyVideosHistory)
24)
25
26myVideosHistoryRouter.post('/me/history/videos/remove',
27 authenticate,
28 userHistoryRemoveValidator,
29 asyncRetryTransactionMiddleware(removeUserHistory)
30)
31
32// ---------------------------------------------------------------------------
33
34export {
35 myVideosHistoryRouter
36}
37
38// ---------------------------------------------------------------------------
39
40async function listMyVideosHistory (req: express.Request, res: express.Response) {
41 const user = res.locals.oauth.token.User
42
43 const resultList = await UserVideoHistoryModel.listForApi(user, req.query.start, req.query.count, req.query.search)
44
45 return res.json(getFormattedObjects(resultList.data, resultList.total))
46}
47
48async function removeUserHistory (req: express.Request, res: express.Response) {
49 const user = res.locals.oauth.token.User
50 const beforeDate = req.body.beforeDate || null
51
52 await sequelizeTypescript.transaction(t => {
53 return UserVideoHistoryModel.removeUserHistoryBefore(user, beforeDate, t)
54 })
55
56 return res.type('json')
57 .status(HttpStatusCode.NO_CONTENT_204)
58 .end()
59}