X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fcontrollers%2Fapi%2Fvideos%2Fblacklist.ts;h=3b25ceea28bbbfe04e3104999024180a0209eb76;hb=80fdaf064562aff968f4c9cea1cf220bc12a70da;hp=5a2c3fd8005478e8c1b4b71ab6f11816998df898;hpb=eb08047657e739bcd9e592d76307befa3998482b;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/controllers/api/videos/blacklist.ts b/server/controllers/api/videos/blacklist.ts index 5a2c3fd80..3b25ceea2 100644 --- a/server/controllers/api/videos/blacklist.ts +++ b/server/controllers/api/videos/blacklist.ts @@ -1,44 +1,55 @@ import * as express from 'express' - -import { database as db } from '../../../initializers' -import { logger, getFormattedObjects } from '../../../helpers' +import { blacklistVideo, unblacklistVideo } from '@server/lib/video-blacklist' +import { UserRight, VideoBlacklistCreate } from '../../../../shared' +import { logger } from '../../../helpers/logger' +import { getFormattedObjects } from '../../../helpers/utils' +import { sequelizeTypescript } from '../../../initializers/database' import { + asyncMiddleware, authenticate, - ensureIsAdmin, - videosBlacklistAddValidator, - videosBlacklistRemoveValidator, - paginationValidator, blacklistSortValidator, + ensureUserHasRight, + paginationValidator, setBlacklistSort, - setPagination, - asyncMiddleware + setDefaultPagination, + videosBlacklistAddValidator, + videosBlacklistFiltersValidator, + videosBlacklistRemoveValidator, + videosBlacklistUpdateValidator } from '../../../middlewares' -import { BlacklistedVideoInstance } from '../../../models' -import { BlacklistedVideo } from '../../../../shared' +import { VideoBlacklistModel } from '../../../models/video/video-blacklist' const blacklistRouter = express.Router() blacklistRouter.post('/:videoId/blacklist', authenticate, - ensureIsAdmin, - videosBlacklistAddValidator, - asyncMiddleware(addVideoToBlacklist) + ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST), + asyncMiddleware(videosBlacklistAddValidator), + asyncMiddleware(addVideoToBlacklistController) ) blacklistRouter.get('/blacklist', authenticate, - ensureIsAdmin, + 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', authenticate, - ensureIsAdmin, - videosBlacklistRemoveValidator, + ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST), + asyncMiddleware(videosBlacklistRemoveValidator), asyncMiddleware(removeVideoFromBlacklistController) ) @@ -50,34 +61,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').sendStatus(204) +} + +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 db.BlacklistedVideo.create(toCreate) - return res.type('json').status(204).end() + return res.type('json').sendStatus(204) } -async function listBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) { - const resultList = await db.BlacklistedVideo.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 BlacklistedVideoInstance +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').sendStatus(204) }