X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fcontrollers%2Fapi%2Fvideos%2Fblacklist.ts;h=4103bb0630793b7f5edf3c521f46c821b004cb22;hb=bd911b54b555b11df7e9849cf92d358bccfecf6e;hp=58960798bd503cfd2978f411a9352f3d9a795686;hpb=4d4e5cd4dca78480ec7f40e747f424cd107376a4;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/controllers/api/videos/blacklist.ts b/server/controllers/api/videos/blacklist.ts index 58960798b..4103bb063 100644 --- a/server/controllers/api/videos/blacklist.ts +++ b/server/controllers/api/videos/blacklist.ts @@ -1,20 +1,60 @@ -import * as express from 'express' - -import { database as db } from '../../../initializers/database' -import { logger } from '../../../helpers' +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, - ensureIsAdmin, - videosBlacklistValidator + 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('/:id/blacklist', +blacklistRouter.post('/:videoId/blacklist', + openapiOperationDoc({ operationId: 'addVideoBlock' }), + authenticate, + ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST), + asyncMiddleware(videosBlacklistAddValidator), + asyncMiddleware(addVideoToBlacklistController) +) + +blacklistRouter.get('/blacklist', + openapiOperationDoc({ operationId: 'getVideoBlocks' }), + authenticate, + ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST), + paginationValidator, + blacklistSortValidator, + setBlacklistSort, + 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, - ensureIsAdmin, - videosBlacklistValidator, - addVideoToBlacklist + ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST), + asyncMiddleware(videosBlacklistRemoveValidator), + asyncMiddleware(removeVideoFromBlacklistController) ) // --------------------------------------------------------------------------- @@ -25,19 +65,48 @@ export { // --------------------------------------------------------------------------- -function addVideoToBlacklist (req, res, next) { - 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) - const toCreate = { - videoId: videoInstance.id - } + 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 - db.BlacklistedVideo.create(toCreate).asCallback(function (err) { - if (err) { - logger.error('Errors when blacklisting video ', { error: err }) - return next(err) - } + await sequelizeTypescript.transaction(t => { + return videoBlacklist.save({ transaction: t }) + }) + + return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end() +} - return res.type('json').status(204).end() +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)) +} + +async function removeVideoFromBlacklistController (req: express.Request, res: express.Response) { + const videoBlacklist = res.locals.videoBlacklist + const video = res.locals.videoAll + + await unblacklistVideo(videoBlacklist, video) + + logger.info('Video %s removed from blacklist.', video.uuid) + + return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end() }