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'
8 asyncRetryTransactionMiddleware,
12 userHistoryListValidator,
13 userHistoryRemoveAllValidator,
14 userHistoryRemoveElementValidator
15 } from '../../../middlewares'
16 import { UserVideoHistoryModel } from '../../../models/user/user-video-history'
18 const myVideosHistoryRouter = express.Router()
20 myVideosHistoryRouter.get('/me/history/videos',
24 userHistoryListValidator,
25 asyncMiddleware(listMyVideosHistory)
28 myVideosHistoryRouter.delete('/me/history/videos/:videoId',
30 userHistoryRemoveElementValidator,
31 asyncMiddleware(removeUserHistoryElement)
34 myVideosHistoryRouter.post('/me/history/videos/remove',
36 userHistoryRemoveAllValidator,
37 asyncRetryTransactionMiddleware(removeAllUserHistory)
40 // ---------------------------------------------------------------------------
46 // ---------------------------------------------------------------------------
48 async function listMyVideosHistory (req: express.Request, res: express.Response) {
49 const user = res.locals.oauth.token.User
51 const resultList = await UserVideoHistoryModel.listForApi(user, req.query.start, req.query.count, req.query.search)
53 return res.json(getFormattedObjects(resultList.data, resultList.total))
56 async function removeUserHistoryElement (req: express.Request, res: express.Response) {
57 const user = res.locals.oauth.token.User
59 await UserVideoHistoryModel.removeUserHistoryElement(user, forceNumber(req.params.videoId))
61 return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
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
68 await sequelizeTypescript.transaction(t => {
69 return UserVideoHistoryModel.removeUserHistoryBefore(user, beforeDate, t)
72 return res.type('json')
73 .status(HttpStatusCode.NO_CONTENT_204)