X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fcontrollers%2Fapi%2Fusers%2Fmy-history.ts;h=e6d3e86ac42466939c53692c3130df62aee7a8e0;hb=HEAD;hp=7025c0ff15e88b1aa15c29aa721b5e07737aa1c9;hpb=8f0bc73d7d5f4c88cbc5588a0ece12b3855c8f98;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/controllers/api/users/my-history.ts b/server/controllers/api/users/my-history.ts index 7025c0ff1..e6d3e86ac 100644 --- a/server/controllers/api/users/my-history.ts +++ b/server/controllers/api/users/my-history.ts @@ -1,16 +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 { UserModel } from '../../../models/account/user' -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() @@ -18,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) ) // --------------------------------------------------------------------------- @@ -38,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 @@ -51,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() }