X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fcontrollers%2Fapi%2Fusers%2Fmy-history.ts;h=e6d3e86ac42466939c53692c3130df62aee7a8e0;hb=HEAD;hp=4da1f34963e01638947c3a9565ea0ee808ee906a;hpb=d5c8932a601c1854db0a2e399ccaf26e17385f1a;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/controllers/api/users/my-history.ts b/server/controllers/api/users/my-history.ts index 4da1f3496..e6d3e86ac 100644 --- a/server/controllers/api/users/my-history.ts +++ b/server/controllers/api/users/my-history.ts @@ -1,15 +1,19 @@ -import * as express from 'express' +import { forceNumber } from '@shared/core-utils' +import express from 'express' +import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes' +import { getFormattedObjects } from '../../../helpers/utils' +import { sequelizeTypescript } from '../../../initializers/database' import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, paginationValidator, setDefaultPagination, - userHistoryRemoveValidator + userHistoryListValidator, + userHistoryRemoveAllValidator, + userHistoryRemoveElementValidator } from '../../../middlewares' -import { getFormattedObjects } from '../../../helpers/utils' -import { UserVideoHistoryModel } from '../../../models/account/user-video-history' -import { sequelizeTypescript } from '../../../initializers' +import { UserVideoHistoryModel } from '../../../models/user/user-video-history' const myVideosHistoryRouter = express.Router() @@ -17,13 +21,20 @@ myVideosHistoryRouter.get('/me/history/videos', authenticate, paginationValidator, setDefaultPagination, + userHistoryListValidator, asyncMiddleware(listMyVideosHistory) ) +myVideosHistoryRouter.delete('/me/history/videos/:videoId', + authenticate, + userHistoryRemoveElementValidator, + asyncMiddleware(removeUserHistoryElement) +) + myVideosHistoryRouter.post('/me/history/videos/remove', authenticate, - userHistoryRemoveValidator, - asyncRetryTransactionMiddleware(removeUserHistory) + userHistoryRemoveAllValidator, + asyncRetryTransactionMiddleware(removeAllUserHistory) ) // --------------------------------------------------------------------------- @@ -37,12 +48,20 @@ export { async function listMyVideosHistory (req: express.Request, res: express.Response) { const user = res.locals.oauth.token.User - const resultList = await UserVideoHistoryModel.listForApi(user, req.query.start, req.query.count) + const resultList = await UserVideoHistoryModel.listForApi(user, req.query.start, req.query.count, req.query.search) return res.json(getFormattedObjects(resultList.data, resultList.total)) } -async function removeUserHistory (req: express.Request, res: express.Response) { +async function removeUserHistoryElement (req: express.Request, res: express.Response) { + const user = res.locals.oauth.token.User + + await UserVideoHistoryModel.removeUserHistoryElement(user, forceNumber(req.params.videoId)) + + return res.sendStatus(HttpStatusCode.NO_CONTENT_204) +} + +async function removeAllUserHistory (req: express.Request, res: express.Response) { const user = res.locals.oauth.token.User const beforeDate = req.body.beforeDate || null @@ -50,7 +69,7 @@ async function removeUserHistory (req: express.Request, res: express.Response) { return UserVideoHistoryModel.removeUserHistoryBefore(user, beforeDate, t) }) - // Do not send the delete to other instances, we delete OUR copy of this video abuse - - return res.type('json').status(204).end() + return res.type('json') + .status(HttpStatusCode.NO_CONTENT_204) + .end() }