X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fcontrollers%2Fapi%2Fvideos%2Fblacklist.ts;h=4103bb0630793b7f5edf3c521f46c821b004cb22;hb=44e702ded455c118f9908b70d25e7c7e5512abe9;hp=e4be6f0f9cb8d11d7763dfbd4a7d1633e50ba946;hpb=6fcd19ba737f1f5614a56c6925adb882dea43b8d;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/controllers/api/videos/blacklist.ts b/server/controllers/api/videos/blacklist.ts index e4be6f0f9..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,17 +65,48 @@ export { // --------------------------------------------------------------------------- -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 + + await sequelizeTypescript.transaction(t => { + return videoBlacklist.save({ transaction: t }) + }) + + return res.type('json').status(HttpStatusCode.NO_CONTENT_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) - const toCreate = { - videoId: videoInstance.id - } + logger.info('Video %s removed from blacklist.', video.uuid) - db.BlacklistedVideo.create(toCreate) - .then(() => res.type('json').status(204).end()) - .catch(err => { - logger.error('Errors when blacklisting video ', { error: err }) - return next(err) - }) + return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end() }