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