]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as express from 'express' |
da854ddd C |
2 | import { BlacklistedVideo, UserRight } from '../../../../shared' |
3 | import { logger } from '../../../helpers/logger' | |
4 | import { getFormattedObjects } from '../../../helpers/utils' | |
65fcc311 | 5 | import { |
f05a1c30 | 6 | asyncMiddleware, authenticate, blacklistSortValidator, ensureUserHasRight, paginationValidator, setBlacklistSort, setDefaultPagination, |
da854ddd | 7 | videosBlacklistAddValidator, videosBlacklistRemoveValidator |
65fcc311 | 8 | } from '../../../middlewares' |
3fd3ab2d | 9 | import { VideoBlacklistModel } from '../../../models/video/video-blacklist' |
65fcc311 C |
10 | |
11 | const blacklistRouter = express.Router() | |
12 | ||
35bf0c83 | 13 | blacklistRouter.post('/:videoId/blacklist', |
65fcc311 | 14 | authenticate, |
954605a8 | 15 | ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST), |
a2431b7d | 16 | asyncMiddleware(videosBlacklistAddValidator), |
eb080476 | 17 | asyncMiddleware(addVideoToBlacklist) |
d33242b0 C |
18 | ) |
19 | ||
35bf0c83 C |
20 | blacklistRouter.get('/blacklist', |
21 | authenticate, | |
954605a8 | 22 | ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST), |
35bf0c83 C |
23 | paginationValidator, |
24 | blacklistSortValidator, | |
25 | setBlacklistSort, | |
f05a1c30 | 26 | setDefaultPagination, |
eb080476 | 27 | asyncMiddleware(listBlacklist) |
35bf0c83 C |
28 | ) |
29 | ||
30 | blacklistRouter.delete('/:videoId/blacklist', | |
31 | authenticate, | |
954605a8 | 32 | ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST), |
a2431b7d | 33 | asyncMiddleware(videosBlacklistRemoveValidator), |
eb080476 | 34 | asyncMiddleware(removeVideoFromBlacklistController) |
35bf0c83 C |
35 | ) |
36 | ||
d33242b0 C |
37 | // --------------------------------------------------------------------------- |
38 | ||
65fcc311 C |
39 | export { |
40 | blacklistRouter | |
41 | } | |
d33242b0 C |
42 | |
43 | // --------------------------------------------------------------------------- | |
44 | ||
eb080476 | 45 | async function addVideoToBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) { |
d33242b0 C |
46 | const videoInstance = res.locals.video |
47 | ||
48 | const toCreate = { | |
49 | videoId: videoInstance.id | |
50 | } | |
51 | ||
3fd3ab2d | 52 | await VideoBlacklistModel.create(toCreate) |
eb080476 | 53 | return res.type('json').status(204).end() |
d33242b0 | 54 | } |
35bf0c83 | 55 | |
eb080476 | 56 | async function listBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 57 | const resultList = await VideoBlacklistModel.listForApi(req.query.start, req.query.count, req.query.sort) |
eb080476 | 58 | |
3fd3ab2d | 59 | return res.json(getFormattedObjects<BlacklistedVideo, VideoBlacklistModel>(resultList.data, resultList.total)) |
35bf0c83 C |
60 | } |
61 | ||
eb080476 | 62 | async function removeVideoFromBlacklistController (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 63 | const blacklistedVideo = res.locals.blacklistedVideo as VideoBlacklistModel |
35bf0c83 | 64 | |
eb080476 C |
65 | try { |
66 | await blacklistedVideo.destroy() | |
67 | ||
68 | logger.info('Video %s removed from blacklist.', res.locals.video.uuid) | |
69 | ||
70 | return res.sendStatus(204) | |
71 | } catch (err) { | |
d5b7d911 | 72 | logger.error('Some error while removing video %s from blacklist.', res.locals.video.uuid, { err }) |
eb080476 C |
73 | throw err |
74 | } | |
35bf0c83 | 75 | } |