X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fcontrollers%2Fapi%2Fvideos%2Fblacklist.ts;h=3b25ceea28bbbfe04e3104999024180a0209eb76;hb=80fdaf064562aff968f4c9cea1cf220bc12a70da;hp=9ef08812b98de48e2f265ff690f45d323e1bfcc0;hpb=cef534ed53e4518fe0acf581bfe880788d42fc36;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/controllers/api/videos/blacklist.ts b/server/controllers/api/videos/blacklist.ts index 9ef08812b..3b25ceea2 100644 --- a/server/controllers/api/videos/blacklist.ts +++ b/server/controllers/api/videos/blacklist.ts @@ -1,7 +1,9 @@ import * as express from 'express' -import { VideoBlacklist, UserRight, VideoBlacklistCreate } from '../../../../shared' +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, @@ -11,13 +13,11 @@ import { setBlacklistSort, setDefaultPagination, videosBlacklistAddValidator, + videosBlacklistFiltersValidator, videosBlacklistRemoveValidator, videosBlacklistUpdateValidator } from '../../../middlewares' import { VideoBlacklistModel } from '../../../models/video/video-blacklist' -import { sequelizeTypescript } from '../../../initializers' -import { Notifier } from '../../../lib/notifier' -import { VideoModel } from '../../../models/video/video' const blacklistRouter = express.Router() @@ -25,7 +25,7 @@ blacklistRouter.post('/:videoId/blacklist', authenticate, ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST), asyncMiddleware(videosBlacklistAddValidator), - asyncMiddleware(addVideoToBlacklist) + asyncMiddleware(addVideoToBlacklistController) ) blacklistRouter.get('/blacklist', @@ -35,6 +35,7 @@ blacklistRouter.get('/blacklist', blacklistSortValidator, setBlacklistSort, setDefaultPagination, + videosBlacklistFiltersValidator, asyncMiddleware(listBlacklist) ) @@ -60,27 +61,19 @@ export { // --------------------------------------------------------------------------- -async function addVideoToBlacklist (req: express.Request, res: express.Response) { - const videoInstance = res.locals.video +async function addVideoToBlacklistController (req: express.Request, res: express.Response) { + const videoInstance = res.locals.videoAll const body: VideoBlacklistCreate = req.body - const toCreate = { - videoId: videoInstance.id, - reason: body.reason - } + await blacklistVideo(videoInstance, body) - const blacklist = await VideoBlacklistModel.create(toCreate) - blacklist.Video = videoInstance + logger.info('Video %s blacklisted.', videoInstance.uuid) - Notifier.Instance.notifyOnVideoBlacklist(blacklist) - - logger.info('Video %s blacklisted.', res.locals.video.uuid) - - return res.type('json').status(204).end() + return res.type('json').sendStatus(204) } async function updateVideoBlacklistController (req: express.Request, res: express.Response) { - const videoBlacklist = res.locals.videoBlacklist as VideoBlacklistModel + const videoBlacklist = res.locals.videoBlacklist if (req.body.reason !== undefined) videoBlacklist.reason = req.body.reason @@ -88,26 +81,28 @@ async function updateVideoBlacklistController (req: express.Request, res: expres return videoBlacklist.save({ transaction: t }) }) - 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 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 videoBlacklist = res.locals.videoBlacklist as VideoBlacklistModel - const video: VideoModel = res.locals.video - - await sequelizeTypescript.transaction(t => { - return videoBlacklist.destroy({ transaction: t }) - }) +async function removeVideoFromBlacklistController (req: express.Request, res: express.Response) { + const videoBlacklist = res.locals.videoBlacklist + const video = res.locals.videoAll - Notifier.Instance.notifyOnVideoUnblacklist(video) + 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.type('json').status(204).end() + return res.type('json').sendStatus(204) }