]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/users/my-history.ts
Merge branch 'feature/strong-model-types' 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 userHistoryRemoveValidator
9 } from '../../../middlewares'
10 import { getFormattedObjects } from '../../../helpers/utils'
11 import { UserVideoHistoryModel } from '../../../models/account/user-video-history'
12 import { sequelizeTypescript } from '../../../initializers'
13
14 const myVideosHistoryRouter = express.Router()
15
16 myVideosHistoryRouter.get('/me/history/videos',
17 authenticate,
18 paginationValidator,
19 setDefaultPagination,
20 asyncMiddleware(listMyVideosHistory)
21 )
22
23 myVideosHistoryRouter.post('/me/history/videos/remove',
24 authenticate,
25 userHistoryRemoveValidator,
26 asyncRetryTransactionMiddleware(removeUserHistory)
27 )
28
29 // ---------------------------------------------------------------------------
30
31 export {
32 myVideosHistoryRouter
33 }
34
35 // ---------------------------------------------------------------------------
36
37 async function listMyVideosHistory (req: express.Request, res: express.Response) {
38 const user = res.locals.oauth.token.User
39
40 const resultList = await UserVideoHistoryModel.listForApi(user, req.query.start, req.query.count)
41
42 return res.json(getFormattedObjects(resultList.data, resultList.total))
43 }
44
45 async function removeUserHistory (req: express.Request, res: express.Response) {
46 const user = res.locals.oauth.token.User
47 const beforeDate = req.body.beforeDate || null
48
49 await sequelizeTypescript.transaction(t => {
50 return UserVideoHistoryModel.removeUserHistoryBefore(user, beforeDate, t)
51 })
52
53 // Do not send the delete to other instances, we delete OUR copy of this video abuse
54
55 return res.type('json').status(204).end()
56 }