]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/users/my-history.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / server / controllers / api / users / my-history.ts
CommitLineData
4638cd71 1import { forceNumber } from '@shared/core-utils'
41fb13c3 2import express from 'express'
4c7e60bc
C
3import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
4import { getFormattedObjects } from '../../../helpers/utils'
5import { sequelizeTypescript } from '../../../initializers/database'
8b9a525a
C
6import {
7 asyncMiddleware,
8 asyncRetryTransactionMiddleware,
9 authenticate,
10 paginationValidator,
11 setDefaultPagination,
d8b34ee5 12 userHistoryListValidator,
7177b46c
C
13 userHistoryRemoveAllValidator,
14 userHistoryRemoveElementValidator
8b9a525a 15} from '../../../middlewares'
7d9ba5c0 16import { UserVideoHistoryModel } from '../../../models/user/user-video-history'
8b9a525a
C
17
18const myVideosHistoryRouter = express.Router()
19
20myVideosHistoryRouter.get('/me/history/videos',
21 authenticate,
22 paginationValidator,
23 setDefaultPagination,
d8b34ee5 24 userHistoryListValidator,
8b9a525a
C
25 asyncMiddleware(listMyVideosHistory)
26)
27
7177b46c
C
28myVideosHistoryRouter.delete('/me/history/videos/:videoId',
29 authenticate,
30 userHistoryRemoveElementValidator,
31 asyncMiddleware(removeUserHistoryElement)
32)
33
8b9a525a
C
34myVideosHistoryRouter.post('/me/history/videos/remove',
35 authenticate,
7177b46c
C
36 userHistoryRemoveAllValidator,
37 asyncRetryTransactionMiddleware(removeAllUserHistory)
8b9a525a
C
38)
39
40// ---------------------------------------------------------------------------
41
42export {
43 myVideosHistoryRouter
44}
45
46// ---------------------------------------------------------------------------
47
48async function listMyVideosHistory (req: express.Request, res: express.Response) {
dae86118 49 const user = res.locals.oauth.token.User
8b9a525a 50
d8b34ee5 51 const resultList = await UserVideoHistoryModel.listForApi(user, req.query.start, req.query.count, req.query.search)
8b9a525a
C
52
53 return res.json(getFormattedObjects(resultList.data, resultList.total))
54}
55
7177b46c
C
56async function removeUserHistoryElement (req: express.Request, res: express.Response) {
57 const user = res.locals.oauth.token.User
58
4638cd71 59 await UserVideoHistoryModel.removeUserHistoryElement(user, forceNumber(req.params.videoId))
7177b46c
C
60
61 return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
62}
63
64async function removeAllUserHistory (req: express.Request, res: express.Response) {
dae86118 65 const user = res.locals.oauth.token.User
8b9a525a
C
66 const beforeDate = req.body.beforeDate || null
67
68 await sequelizeTypescript.transaction(t => {
8f0bc73d 69 return UserVideoHistoryModel.removeUserHistoryBefore(user, beforeDate, t)
8b9a525a
C
70 })
71
2d53be02
RK
72 return res.type('json')
73 .status(HttpStatusCode.NO_CONTENT_204)
74 .end()
8b9a525a 75}