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