2017-06-05 19:53:49 +00:00
|
|
|
import * as express from 'express'
|
2017-12-28 10:16:08 +00:00
|
|
|
import { BlacklistedVideo, UserRight } from '../../../../shared'
|
|
|
|
import { logger } from '../../../helpers/logger'
|
|
|
|
import { getFormattedObjects } from '../../../helpers/utils'
|
2017-05-15 20:22:03 +00:00
|
|
|
import {
|
2018-01-18 09:53:54 +00:00
|
|
|
asyncMiddleware, authenticate, blacklistSortValidator, ensureUserHasRight, paginationValidator, setBlacklistSort, setDefaultPagination,
|
2017-12-28 10:16:08 +00:00
|
|
|
videosBlacklistAddValidator, videosBlacklistRemoveValidator
|
2017-05-15 20:22:03 +00:00
|
|
|
} from '../../../middlewares'
|
2017-12-12 16:53:50 +00:00
|
|
|
import { VideoBlacklistModel } from '../../../models/video/video-blacklist'
|
2017-05-15 20:22:03 +00:00
|
|
|
|
|
|
|
const blacklistRouter = express.Router()
|
|
|
|
|
2017-10-10 08:02:18 +00:00
|
|
|
blacklistRouter.post('/:videoId/blacklist',
|
2017-05-15 20:22:03 +00:00
|
|
|
authenticate,
|
2017-10-27 14:55:03 +00:00
|
|
|
ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
|
2017-11-27 16:30:46 +00:00
|
|
|
asyncMiddleware(videosBlacklistAddValidator),
|
2017-10-25 09:55:06 +00:00
|
|
|
asyncMiddleware(addVideoToBlacklist)
|
2017-05-05 14:53:35 +00:00
|
|
|
)
|
|
|
|
|
2017-10-10 08:02:18 +00:00
|
|
|
blacklistRouter.get('/blacklist',
|
|
|
|
authenticate,
|
2017-10-27 14:55:03 +00:00
|
|
|
ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
|
2017-10-10 08:02:18 +00:00
|
|
|
paginationValidator,
|
|
|
|
blacklistSortValidator,
|
|
|
|
setBlacklistSort,
|
2018-01-18 09:53:54 +00:00
|
|
|
setDefaultPagination,
|
2017-10-25 09:55:06 +00:00
|
|
|
asyncMiddleware(listBlacklist)
|
2017-10-10 08:02:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
blacklistRouter.delete('/:videoId/blacklist',
|
|
|
|
authenticate,
|
2017-10-27 14:55:03 +00:00
|
|
|
ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
|
2017-11-27 16:30:46 +00:00
|
|
|
asyncMiddleware(videosBlacklistRemoveValidator),
|
2017-10-25 09:55:06 +00:00
|
|
|
asyncMiddleware(removeVideoFromBlacklistController)
|
2017-10-10 08:02:18 +00:00
|
|
|
)
|
|
|
|
|
2017-05-05 14:53:35 +00:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-05-15 20:22:03 +00:00
|
|
|
export {
|
|
|
|
blacklistRouter
|
|
|
|
}
|
2017-05-05 14:53:35 +00:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-10-25 09:55:06 +00:00
|
|
|
async function addVideoToBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-05-05 14:53:35 +00:00
|
|
|
const videoInstance = res.locals.video
|
|
|
|
|
|
|
|
const toCreate = {
|
|
|
|
videoId: videoInstance.id
|
|
|
|
}
|
|
|
|
|
2017-12-12 16:53:50 +00:00
|
|
|
await VideoBlacklistModel.create(toCreate)
|
2017-10-25 09:55:06 +00:00
|
|
|
return res.type('json').status(204).end()
|
2017-05-05 14:53:35 +00:00
|
|
|
}
|
2017-10-10 08:02:18 +00:00
|
|
|
|
2017-10-25 09:55:06 +00:00
|
|
|
async function listBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-12-12 16:53:50 +00:00
|
|
|
const resultList = await VideoBlacklistModel.listForApi(req.query.start, req.query.count, req.query.sort)
|
2017-10-25 09:55:06 +00:00
|
|
|
|
2017-12-12 16:53:50 +00:00
|
|
|
return res.json(getFormattedObjects<BlacklistedVideo, VideoBlacklistModel>(resultList.data, resultList.total))
|
2017-10-10 08:02:18 +00:00
|
|
|
}
|
|
|
|
|
2017-10-25 09:55:06 +00:00
|
|
|
async function removeVideoFromBlacklistController (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-12-12 16:53:50 +00:00
|
|
|
const blacklistedVideo = res.locals.blacklistedVideo as VideoBlacklistModel
|
2017-10-10 08:02:18 +00:00
|
|
|
|
2017-10-25 09:55:06 +00:00
|
|
|
try {
|
|
|
|
await blacklistedVideo.destroy()
|
|
|
|
|
|
|
|
logger.info('Video %s removed from blacklist.', res.locals.video.uuid)
|
|
|
|
|
|
|
|
return res.sendStatus(204)
|
|
|
|
} catch (err) {
|
2018-03-26 13:54:13 +00:00
|
|
|
logger.error('Some error while removing video %s from blacklist.', res.locals.video.uuid, { err })
|
2017-10-25 09:55:06 +00:00
|
|
|
throw err
|
|
|
|
}
|
2017-10-10 08:02:18 +00:00
|
|
|
}
|