2022-11-15 14:41:55 +01:00
|
|
|
import { forceNumber } from '@shared/core-utils'
|
2021-08-27 14:32:44 +02:00
|
|
|
import express from 'express'
|
2021-07-16 14:27:30 +02:00
|
|
|
import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
|
|
|
|
import { getFormattedObjects } from '../../../helpers/utils'
|
|
|
|
import { sequelizeTypescript } from '../../../initializers/database'
|
2018-12-17 15:52:38 +01:00
|
|
|
import {
|
|
|
|
asyncMiddleware,
|
|
|
|
asyncRetryTransactionMiddleware,
|
|
|
|
authenticate,
|
|
|
|
paginationValidator,
|
|
|
|
setDefaultPagination,
|
2021-01-13 09:16:15 +01:00
|
|
|
userHistoryListValidator,
|
2022-01-18 11:23:41 +01:00
|
|
|
userHistoryRemoveAllValidator,
|
|
|
|
userHistoryRemoveElementValidator
|
2018-12-17 15:52:38 +01:00
|
|
|
} from '../../../middlewares'
|
2021-05-11 11:15:29 +02:00
|
|
|
import { UserVideoHistoryModel } from '../../../models/user/user-video-history'
|
2018-12-17 15:52:38 +01:00
|
|
|
|
|
|
|
const myVideosHistoryRouter = express.Router()
|
|
|
|
|
|
|
|
myVideosHistoryRouter.get('/me/history/videos',
|
|
|
|
authenticate,
|
|
|
|
paginationValidator,
|
|
|
|
setDefaultPagination,
|
2021-01-13 09:16:15 +01:00
|
|
|
userHistoryListValidator,
|
2018-12-17 15:52:38 +01:00
|
|
|
asyncMiddleware(listMyVideosHistory)
|
|
|
|
)
|
|
|
|
|
2022-01-18 11:23:41 +01:00
|
|
|
myVideosHistoryRouter.delete('/me/history/videos/:videoId',
|
|
|
|
authenticate,
|
|
|
|
userHistoryRemoveElementValidator,
|
|
|
|
asyncMiddleware(removeUserHistoryElement)
|
|
|
|
)
|
|
|
|
|
2018-12-17 15:52:38 +01:00
|
|
|
myVideosHistoryRouter.post('/me/history/videos/remove',
|
|
|
|
authenticate,
|
2022-01-18 11:23:41 +01:00
|
|
|
userHistoryRemoveAllValidator,
|
|
|
|
asyncRetryTransactionMiddleware(removeAllUserHistory)
|
2018-12-17 15:52:38 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
myVideosHistoryRouter
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
async function listMyVideosHistory (req: express.Request, res: express.Response) {
|
2019-03-19 10:35:15 +01:00
|
|
|
const user = res.locals.oauth.token.User
|
2018-12-17 15:52:38 +01:00
|
|
|
|
2021-01-13 09:16:15 +01:00
|
|
|
const resultList = await UserVideoHistoryModel.listForApi(user, req.query.start, req.query.count, req.query.search)
|
2018-12-17 15:52:38 +01:00
|
|
|
|
|
|
|
return res.json(getFormattedObjects(resultList.data, resultList.total))
|
|
|
|
}
|
|
|
|
|
2022-01-18 11:23:41 +01:00
|
|
|
async function removeUserHistoryElement (req: express.Request, res: express.Response) {
|
|
|
|
const user = res.locals.oauth.token.User
|
|
|
|
|
2022-11-15 14:41:55 +01:00
|
|
|
await UserVideoHistoryModel.removeUserHistoryElement(user, forceNumber(req.params.videoId))
|
2022-01-18 11:23:41 +01:00
|
|
|
|
|
|
|
return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function removeAllUserHistory (req: express.Request, res: express.Response) {
|
2019-03-19 10:35:15 +01:00
|
|
|
const user = res.locals.oauth.token.User
|
2018-12-17 15:52:38 +01:00
|
|
|
const beforeDate = req.body.beforeDate || null
|
|
|
|
|
|
|
|
await sequelizeTypescript.transaction(t => {
|
2019-04-11 15:38:53 +02:00
|
|
|
return UserVideoHistoryModel.removeUserHistoryBefore(user, beforeDate, t)
|
2018-12-17 15:52:38 +01:00
|
|
|
})
|
|
|
|
|
2020-12-07 14:32:36 +01:00
|
|
|
return res.type('json')
|
|
|
|
.status(HttpStatusCode.NO_CONTENT_204)
|
|
|
|
.end()
|
2018-12-17 15:52:38 +01:00
|
|
|
}
|