diff options
Diffstat (limited to 'server/controllers/api/videos/blacklist.ts')
-rw-r--r-- | server/controllers/api/videos/blacklist.ts | 56 |
1 files changed, 42 insertions, 14 deletions
diff --git a/server/controllers/api/videos/blacklist.ts b/server/controllers/api/videos/blacklist.ts index 8112b59b8..358f339ed 100644 --- a/server/controllers/api/videos/blacklist.ts +++ b/server/controllers/api/videos/blacklist.ts | |||
@@ -1,12 +1,21 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { BlacklistedVideo, UserRight } from '../../../../shared' | 2 | import { BlacklistedVideo, UserRight, VideoBlacklistCreate } from '../../../../shared' |
3 | import { logger } from '../../../helpers/logger' | 3 | import { logger } from '../../../helpers/logger' |
4 | import { getFormattedObjects } from '../../../helpers/utils' | 4 | import { getFormattedObjects } from '../../../helpers/utils' |
5 | import { | 5 | import { |
6 | asyncMiddleware, authenticate, blacklistSortValidator, ensureUserHasRight, paginationValidator, setBlacklistSort, setDefaultPagination, | 6 | asyncMiddleware, |
7 | videosBlacklistAddValidator, videosBlacklistRemoveValidator | 7 | authenticate, |
8 | blacklistSortValidator, | ||
9 | ensureUserHasRight, | ||
10 | paginationValidator, | ||
11 | setBlacklistSort, | ||
12 | setDefaultPagination, | ||
13 | videosBlacklistAddValidator, | ||
14 | videosBlacklistRemoveValidator, | ||
15 | videosBlacklistUpdateValidator | ||
8 | } from '../../../middlewares' | 16 | } from '../../../middlewares' |
9 | import { VideoBlacklistModel } from '../../../models/video/video-blacklist' | 17 | import { VideoBlacklistModel } from '../../../models/video/video-blacklist' |
18 | import { sequelizeTypescript } from '../../../initializers' | ||
10 | 19 | ||
11 | const blacklistRouter = express.Router() | 20 | const blacklistRouter = express.Router() |
12 | 21 | ||
@@ -27,6 +36,13 @@ blacklistRouter.get('/blacklist', | |||
27 | asyncMiddleware(listBlacklist) | 36 | asyncMiddleware(listBlacklist) |
28 | ) | 37 | ) |
29 | 38 | ||
39 | blacklistRouter.put('/:videoId/blacklist', | ||
40 | authenticate, | ||
41 | ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST), | ||
42 | asyncMiddleware(videosBlacklistUpdateValidator), | ||
43 | asyncMiddleware(updateVideoBlacklistController) | ||
44 | ) | ||
45 | |||
30 | blacklistRouter.delete('/:videoId/blacklist', | 46 | blacklistRouter.delete('/:videoId/blacklist', |
31 | authenticate, | 47 | authenticate, |
32 | ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST), | 48 | ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST), |
@@ -42,17 +58,32 @@ export { | |||
42 | 58 | ||
43 | // --------------------------------------------------------------------------- | 59 | // --------------------------------------------------------------------------- |
44 | 60 | ||
45 | async function addVideoToBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) { | 61 | async function addVideoToBlacklist (req: express.Request, res: express.Response) { |
46 | const videoInstance = res.locals.video | 62 | const videoInstance = res.locals.video |
63 | const body: VideoBlacklistCreate = req.body | ||
47 | 64 | ||
48 | const toCreate = { | 65 | const toCreate = { |
49 | videoId: videoInstance.id | 66 | videoId: videoInstance.id, |
67 | reason: body.reason | ||
50 | } | 68 | } |
51 | 69 | ||
52 | await VideoBlacklistModel.create(toCreate) | 70 | await VideoBlacklistModel.create(toCreate) |
53 | return res.type('json').status(204).end() | 71 | return res.type('json').status(204).end() |
54 | } | 72 | } |
55 | 73 | ||
74 | async function updateVideoBlacklistController (req: express.Request, res: express.Response) { | ||
75 | const videoBlacklist = res.locals.videoBlacklist as VideoBlacklistModel | ||
76 | logger.info(videoBlacklist) | ||
77 | |||
78 | if (req.body.reason !== undefined) videoBlacklist.reason = req.body.reason | ||
79 | |||
80 | await sequelizeTypescript.transaction(t => { | ||
81 | return videoBlacklist.save({ transaction: t }) | ||
82 | }) | ||
83 | |||
84 | return res.type('json').status(204).end() | ||
85 | } | ||
86 | |||
56 | async function listBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) { | 87 | async function listBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) { |
57 | const resultList = await VideoBlacklistModel.listForApi(req.query.start, req.query.count, req.query.sort) | 88 | const resultList = await VideoBlacklistModel.listForApi(req.query.start, req.query.count, req.query.sort) |
58 | 89 | ||
@@ -60,16 +91,13 @@ async function listBlacklist (req: express.Request, res: express.Response, next: | |||
60 | } | 91 | } |
61 | 92 | ||
62 | async function removeVideoFromBlacklistController (req: express.Request, res: express.Response, next: express.NextFunction) { | 93 | async function removeVideoFromBlacklistController (req: express.Request, res: express.Response, next: express.NextFunction) { |
63 | const blacklistedVideo = res.locals.blacklistedVideo as VideoBlacklistModel | 94 | const videoBlacklist = res.locals.videoBlacklist as VideoBlacklistModel |
64 | 95 | ||
65 | try { | 96 | await sequelizeTypescript.transaction(t => { |
66 | await blacklistedVideo.destroy() | 97 | return videoBlacklist.destroy({ transaction: t }) |
98 | }) | ||
67 | 99 | ||
68 | logger.info('Video %s removed from blacklist.', res.locals.video.uuid) | 100 | logger.info('Video %s removed from blacklist.', res.locals.video.uuid) |
69 | 101 | ||
70 | return res.sendStatus(204) | 102 | return res.type('json').status(204).end() |
71 | } catch (err) { | ||
72 | logger.error('Some error while removing video %s from blacklist.', res.locals.video.uuid, { err }) | ||
73 | throw err | ||
74 | } | ||
75 | } | 103 | } |