]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - 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
1 import * as express from 'express'
2 import {
3 asyncMiddleware,
4 asyncRetryTransactionMiddleware,
5 authenticate,
6 paginationValidator,
7 setDefaultPagination,
8 userHistoryListValidator,
9 userHistoryRemoveValidator
10 } from '../../../middlewares'
11 import { getFormattedObjects } from '../../../helpers/utils'
12 import { UserVideoHistoryModel } from '../../../models/user/user-video-history'
13 import { sequelizeTypescript } from '../../../initializers/database'
14 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
15
16 const myVideosHistoryRouter = express.Router()
17
18 myVideosHistoryRouter.get('/me/history/videos',
19 authenticate,
20 paginationValidator,
21 setDefaultPagination,
22 userHistoryListValidator,
23 asyncMiddleware(listMyVideosHistory)
24 )
25
26 myVideosHistoryRouter.post('/me/history/videos/remove',
27 authenticate,
28 userHistoryRemoveValidator,
29 asyncRetryTransactionMiddleware(removeUserHistory)
30 )
31
32 // ---------------------------------------------------------------------------
33
34 export {
35 myVideosHistoryRouter
36 }
37
38 // ---------------------------------------------------------------------------
39
40 async 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
48 async 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 }