From 35bf0c83c80f59ca79f4b84fac8700f17adeb22d Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 10 Oct 2017 10:02:18 +0200 Subject: Video blacklist refractoring --- server/controllers/api/videos/blacklist.ts | 54 +++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 5 deletions(-) (limited to 'server/controllers/api/videos') diff --git a/server/controllers/api/videos/blacklist.ts b/server/controllers/api/videos/blacklist.ts index d8f2068ec..66311598e 100644 --- a/server/controllers/api/videos/blacklist.ts +++ b/server/controllers/api/videos/blacklist.ts @@ -1,22 +1,46 @@ import * as express from 'express' -import { database as db } from '../../../initializers/database' -import { logger } from '../../../helpers' +import { database as db } from '../../../initializers' +import { logger, getFormattedObjects } from '../../../helpers' import { authenticate, ensureIsAdmin, - videosBlacklistValidator + videosBlacklistAddValidator, + videosBlacklistRemoveValidator, + paginationValidator, + blacklistSortValidator, + setBlacklistSort, + setPagination } from '../../../middlewares' +import { BlacklistedVideoInstance } from '../../../models' +import { BlacklistedVideo } from '../../../../shared' const blacklistRouter = express.Router() -blacklistRouter.post('/:id/blacklist', +blacklistRouter.post('/:videoId/blacklist', authenticate, ensureIsAdmin, - videosBlacklistValidator, + videosBlacklistAddValidator, addVideoToBlacklist ) +blacklistRouter.get('/blacklist', + authenticate, + ensureIsAdmin, + paginationValidator, + blacklistSortValidator, + setBlacklistSort, + setPagination, + listBlacklist +) + +blacklistRouter.delete('/:videoId/blacklist', + authenticate, + ensureIsAdmin, + videosBlacklistRemoveValidator, + removeVideoFromBlacklistController +) + // --------------------------------------------------------------------------- export { @@ -39,3 +63,23 @@ function addVideoToBlacklist (req: express.Request, res: express.Response, next: return next(err) }) } + +function listBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) { + db.BlacklistedVideo.listForApi(req.query.start, req.query.count, req.query.sort) + .then(resultList => res.json(getFormattedObjects(resultList.data, resultList.total))) + .catch(err => next(err)) +} + +function removeVideoFromBlacklistController (req: express.Request, res: express.Response, next: express.NextFunction) { + const blacklistedVideo = res.locals.blacklistedVideo as BlacklistedVideoInstance + + blacklistedVideo.destroy() + .then(() => { + logger.info('Video %s removed from blacklist.', res.locals.video.uuid) + res.sendStatus(204) + }) + .catch(err => { + logger.error('Some error while removing video %s from blacklist.', res.locals.video.uuid, err) + next(err) + }) +} -- cgit v1.2.3