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