1 import express from 'express'
2 import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
3 import { getFormattedObjects } from '../../../helpers/utils'
4 import { sequelizeTypescript } from '../../../initializers/database'
7 asyncRetryTransactionMiddleware,
11 userHistoryListValidator,
12 userHistoryRemoveAllValidator,
13 userHistoryRemoveElementValidator
14 } from '../../../middlewares'
15 import { UserVideoHistoryModel } from '../../../models/user/user-video-history'
17 const myVideosHistoryRouter = express.Router()
19 myVideosHistoryRouter.get('/me/history/videos',
23 userHistoryListValidator,
24 asyncMiddleware(listMyVideosHistory)
27 myVideosHistoryRouter.delete('/me/history/videos/:videoId',
29 userHistoryRemoveElementValidator,
30 asyncMiddleware(removeUserHistoryElement)
33 myVideosHistoryRouter.post('/me/history/videos/remove',
35 userHistoryRemoveAllValidator,
36 asyncRetryTransactionMiddleware(removeAllUserHistory)
39 // ---------------------------------------------------------------------------
45 // ---------------------------------------------------------------------------
47 async function listMyVideosHistory (req: express.Request, res: express.Response) {
48 const user = res.locals.oauth.token.User
50 const resultList = await UserVideoHistoryModel.listForApi(user, req.query.start, req.query.count, req.query.search)
52 return res.json(getFormattedObjects(resultList.data, resultList.total))
55 async function removeUserHistoryElement (req: express.Request, res: express.Response) {
56 const user = res.locals.oauth.token.User
58 await UserVideoHistoryModel.removeUserHistoryElement(user, parseInt(req.params.videoId + ''))
60 return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
63 async function removeAllUserHistory (req: express.Request, res: express.Response) {
64 const user = res.locals.oauth.token.User
65 const beforeDate = req.body.beforeDate || null
67 await sequelizeTypescript.transaction(t => {
68 return UserVideoHistoryModel.removeUserHistoryBefore(user, beforeDate, t)
71 return res.type('json')
72 .status(HttpStatusCode.NO_CONTENT_204)