]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/users/my-history.ts
Add banners support
[github/Chocobozzz/PeerTube.git] / server / controllers / api / users / my-history.ts
CommitLineData
8b9a525a
C
1import * as express from 'express'
2import {
3 asyncMiddleware,
4 asyncRetryTransactionMiddleware,
5 authenticate,
6 paginationValidator,
7 setDefaultPagination,
d8b34ee5 8 userHistoryListValidator,
8b9a525a
C
9 userHistoryRemoveValidator
10} from '../../../middlewares'
8b9a525a
C
11import { getFormattedObjects } from '../../../helpers/utils'
12import { UserVideoHistoryModel } from '../../../models/account/user-video-history'
80fdaf06 13import { sequelizeTypescript } from '../../../initializers/database'
2d53be02 14import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
8b9a525a
C
15
16const myVideosHistoryRouter = express.Router()
17
18myVideosHistoryRouter.get('/me/history/videos',
19 authenticate,
20 paginationValidator,
21 setDefaultPagination,
d8b34ee5 22 userHistoryListValidator,
8b9a525a
C
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) {
dae86118 41 const user = res.locals.oauth.token.User
8b9a525a 42
d8b34ee5 43 const resultList = await UserVideoHistoryModel.listForApi(user, req.query.start, req.query.count, req.query.search)
8b9a525a
C
44
45 return res.json(getFormattedObjects(resultList.data, resultList.total))
46}
47
48async function removeUserHistory (req: express.Request, res: express.Response) {
dae86118 49 const user = res.locals.oauth.token.User
8b9a525a
C
50 const beforeDate = req.body.beforeDate || null
51
52 await sequelizeTypescript.transaction(t => {
8f0bc73d 53 return UserVideoHistoryModel.removeUserHistoryBefore(user, beforeDate, t)
8b9a525a
C
54 })
55
2d53be02
RK
56 return res.type('json')
57 .status(HttpStatusCode.NO_CONTENT_204)
58 .end()
8b9a525a 59}