X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fcontrollers%2Fapi%2Fvideos%2Fblacklist.ts;h=4103bb0630793b7f5edf3c521f46c821b004cb22;hb=44e702ded455c118f9908b70d25e7c7e5512abe9;hp=c9087fd97ed744caece8c2ae835d9e02ce017eda;hpb=da854ddd502cd70685ef779c673b9e63757b8aa0;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/controllers/api/videos/blacklist.ts b/server/controllers/api/videos/blacklist.ts index c9087fd97..4103bb063 100644 --- a/server/controllers/api/videos/blacklist.ts +++ b/server/controllers/api/videos/blacklist.ts @@ -1,33 +1,56 @@ -import * as express from 'express' -import { BlacklistedVideo, UserRight } from '../../../../shared' +import express from 'express' +import { blacklistVideo, unblacklistVideo } from '@server/lib/video-blacklist' +import { HttpStatusCode, UserRight, VideoBlacklistCreate } from '@shared/models' import { logger } from '../../../helpers/logger' import { getFormattedObjects } from '../../../helpers/utils' +import { sequelizeTypescript } from '../../../initializers/database' import { - asyncMiddleware, authenticate, blacklistSortValidator, ensureUserHasRight, paginationValidator, setBlacklistSort, setPagination, - videosBlacklistAddValidator, videosBlacklistRemoveValidator + asyncMiddleware, + authenticate, + blacklistSortValidator, + ensureUserHasRight, + openapiOperationDoc, + paginationValidator, + setBlacklistSort, + setDefaultPagination, + videosBlacklistAddValidator, + videosBlacklistFiltersValidator, + videosBlacklistRemoveValidator, + videosBlacklistUpdateValidator } from '../../../middlewares' import { VideoBlacklistModel } from '../../../models/video/video-blacklist' const blacklistRouter = express.Router() blacklistRouter.post('/:videoId/blacklist', + openapiOperationDoc({ operationId: 'addVideoBlock' }), authenticate, ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST), asyncMiddleware(videosBlacklistAddValidator), - asyncMiddleware(addVideoToBlacklist) + asyncMiddleware(addVideoToBlacklistController) ) blacklistRouter.get('/blacklist', + openapiOperationDoc({ operationId: 'getVideoBlocks' }), authenticate, ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST), paginationValidator, blacklistSortValidator, setBlacklistSort, - setPagination, + setDefaultPagination, + videosBlacklistFiltersValidator, asyncMiddleware(listBlacklist) ) +blacklistRouter.put('/:videoId/blacklist', + authenticate, + ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST), + asyncMiddleware(videosBlacklistUpdateValidator), + asyncMiddleware(updateVideoBlacklistController) +) + blacklistRouter.delete('/:videoId/blacklist', + openapiOperationDoc({ operationId: 'delVideoBlock' }), authenticate, ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST), asyncMiddleware(videosBlacklistRemoveValidator), @@ -42,34 +65,48 @@ export { // --------------------------------------------------------------------------- -async function addVideoToBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) { - const videoInstance = res.locals.video +async function addVideoToBlacklistController (req: express.Request, res: express.Response) { + const videoInstance = res.locals.videoAll + const body: VideoBlacklistCreate = req.body + + await blacklistVideo(videoInstance, body) + + logger.info('Video %s blacklisted.', videoInstance.uuid) + + return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end() +} + +async function updateVideoBlacklistController (req: express.Request, res: express.Response) { + const videoBlacklist = res.locals.videoBlacklist + + if (req.body.reason !== undefined) videoBlacklist.reason = req.body.reason - const toCreate = { - videoId: videoInstance.id - } + await sequelizeTypescript.transaction(t => { + return videoBlacklist.save({ transaction: t }) + }) - await VideoBlacklistModel.create(toCreate) - return res.type('json').status(204).end() + return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end() } -async function listBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) { - const resultList = await VideoBlacklistModel.listForApi(req.query.start, req.query.count, req.query.sort) +async function listBlacklist (req: express.Request, res: express.Response) { + const resultList = await VideoBlacklistModel.listForApi({ + start: req.query.start, + count: req.query.count, + sort: req.query.sort, + search: req.query.search, + type: req.query.type + }) - return res.json(getFormattedObjects(resultList.data, resultList.total)) + return res.json(getFormattedObjects(resultList.data, resultList.total)) } -async function removeVideoFromBlacklistController (req: express.Request, res: express.Response, next: express.NextFunction) { - const blacklistedVideo = res.locals.blacklistedVideo as VideoBlacklistModel +async function removeVideoFromBlacklistController (req: express.Request, res: express.Response) { + const videoBlacklist = res.locals.videoBlacklist + const video = res.locals.videoAll - try { - await blacklistedVideo.destroy() + await unblacklistVideo(videoBlacklist, video) - logger.info('Video %s removed from blacklist.', res.locals.video.uuid) + logger.info('Video %s removed from blacklist.', video.uuid) - return res.sendStatus(204) - } catch (err) { - logger.error('Some error while removing video %s from blacklist.', res.locals.video.uuid, err) - throw err - } + return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end() }