1 import * as express from 'express'
2 import { logger, getFormattedObjects } from '../../../helpers'
6 videosBlacklistAddValidator,
7 videosBlacklistRemoveValidator,
9 blacklistSortValidator,
13 } from '../../../middlewares'
14 import { BlacklistedVideo, UserRight } from '../../../../shared'
15 import { VideoBlacklistModel } from '../../../models/video/video-blacklist'
17 const blacklistRouter = express.Router()
19 blacklistRouter.post('/:videoId/blacklist',
21 ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
22 asyncMiddleware(videosBlacklistAddValidator),
23 asyncMiddleware(addVideoToBlacklist)
26 blacklistRouter.get('/blacklist',
28 ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
30 blacklistSortValidator,
33 asyncMiddleware(listBlacklist)
36 blacklistRouter.delete('/:videoId/blacklist',
38 ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
39 asyncMiddleware(videosBlacklistRemoveValidator),
40 asyncMiddleware(removeVideoFromBlacklistController)
43 // ---------------------------------------------------------------------------
49 // ---------------------------------------------------------------------------
51 async function addVideoToBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) {
52 const videoInstance = res.locals.video
55 videoId: videoInstance.id
58 await VideoBlacklistModel.create(toCreate)
59 return res.type('json').status(204).end()
62 async function listBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) {
63 const resultList = await VideoBlacklistModel.listForApi(req.query.start, req.query.count, req.query.sort)
65 return res.json(getFormattedObjects<BlacklistedVideo, VideoBlacklistModel>(resultList.data, resultList.total))
68 async function removeVideoFromBlacklistController (req: express.Request, res: express.Response, next: express.NextFunction) {
69 const blacklistedVideo = res.locals.blacklistedVideo as VideoBlacklistModel
72 await blacklistedVideo.destroy()
74 logger.info('Video %s removed from blacklist.', res.locals.video.uuid)
76 return res.sendStatus(204)
78 logger.error('Some error while removing video %s from blacklist.', res.locals.video.uuid, err)