]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/users/my-history.ts
Stronger model typings
[github/Chocobozzz/PeerTube.git] / server / controllers / api / users / my-history.ts
CommitLineData
8b9a525a
C
1import * as express from 'express'
2import {
3 asyncMiddleware,
4 asyncRetryTransactionMiddleware,
5 authenticate,
6 paginationValidator,
7 setDefaultPagination,
8 userHistoryRemoveValidator
9} from '../../../middlewares'
8b9a525a
C
10import { getFormattedObjects } from '../../../helpers/utils'
11import { UserVideoHistoryModel } from '../../../models/account/user-video-history'
12import { sequelizeTypescript } from '../../../initializers'
13
14const myVideosHistoryRouter = express.Router()
15
16myVideosHistoryRouter.get('/me/history/videos',
17 authenticate,
18 paginationValidator,
19 setDefaultPagination,
20 asyncMiddleware(listMyVideosHistory)
21)
22
23myVideosHistoryRouter.post('/me/history/videos/remove',
24 authenticate,
25 userHistoryRemoveValidator,
26 asyncRetryTransactionMiddleware(removeUserHistory)
27)
28
29// ---------------------------------------------------------------------------
30
31export {
32 myVideosHistoryRouter
33}
34
35// ---------------------------------------------------------------------------
36
37async function listMyVideosHistory (req: express.Request, res: express.Response) {
dae86118 38 const user = res.locals.oauth.token.User
8b9a525a
C
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
45async function removeUserHistory (req: express.Request, res: express.Response) {
dae86118 46 const user = res.locals.oauth.token.User
8b9a525a
C
47 const beforeDate = req.body.beforeDate || null
48
49 await sequelizeTypescript.transaction(t => {
8f0bc73d 50 return UserVideoHistoryModel.removeUserHistoryBefore(user, beforeDate, t)
8b9a525a
C
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}