diff options
Diffstat (limited to 'server/controllers/api/videos/blacklist.ts')
-rw-r--r-- | server/controllers/api/videos/blacklist.ts | 48 |
1 files changed, 23 insertions, 25 deletions
diff --git a/server/controllers/api/videos/blacklist.ts b/server/controllers/api/videos/blacklist.ts index 66311598e..5a2c3fd80 100644 --- a/server/controllers/api/videos/blacklist.ts +++ b/server/controllers/api/videos/blacklist.ts | |||
@@ -10,7 +10,8 @@ import { | |||
10 | paginationValidator, | 10 | paginationValidator, |
11 | blacklistSortValidator, | 11 | blacklistSortValidator, |
12 | setBlacklistSort, | 12 | setBlacklistSort, |
13 | setPagination | 13 | setPagination, |
14 | asyncMiddleware | ||
14 | } from '../../../middlewares' | 15 | } from '../../../middlewares' |
15 | import { BlacklistedVideoInstance } from '../../../models' | 16 | import { BlacklistedVideoInstance } from '../../../models' |
16 | import { BlacklistedVideo } from '../../../../shared' | 17 | import { BlacklistedVideo } from '../../../../shared' |
@@ -21,7 +22,7 @@ blacklistRouter.post('/:videoId/blacklist', | |||
21 | authenticate, | 22 | authenticate, |
22 | ensureIsAdmin, | 23 | ensureIsAdmin, |
23 | videosBlacklistAddValidator, | 24 | videosBlacklistAddValidator, |
24 | addVideoToBlacklist | 25 | asyncMiddleware(addVideoToBlacklist) |
25 | ) | 26 | ) |
26 | 27 | ||
27 | blacklistRouter.get('/blacklist', | 28 | blacklistRouter.get('/blacklist', |
@@ -31,14 +32,14 @@ blacklistRouter.get('/blacklist', | |||
31 | blacklistSortValidator, | 32 | blacklistSortValidator, |
32 | setBlacklistSort, | 33 | setBlacklistSort, |
33 | setPagination, | 34 | setPagination, |
34 | listBlacklist | 35 | asyncMiddleware(listBlacklist) |
35 | ) | 36 | ) |
36 | 37 | ||
37 | blacklistRouter.delete('/:videoId/blacklist', | 38 | blacklistRouter.delete('/:videoId/blacklist', |
38 | authenticate, | 39 | authenticate, |
39 | ensureIsAdmin, | 40 | ensureIsAdmin, |
40 | videosBlacklistRemoveValidator, | 41 | videosBlacklistRemoveValidator, |
41 | removeVideoFromBlacklistController | 42 | asyncMiddleware(removeVideoFromBlacklistController) |
42 | ) | 43 | ) |
43 | 44 | ||
44 | // --------------------------------------------------------------------------- | 45 | // --------------------------------------------------------------------------- |
@@ -49,37 +50,34 @@ export { | |||
49 | 50 | ||
50 | // --------------------------------------------------------------------------- | 51 | // --------------------------------------------------------------------------- |
51 | 52 | ||
52 | function addVideoToBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) { | 53 | async function addVideoToBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) { |
53 | const videoInstance = res.locals.video | 54 | const videoInstance = res.locals.video |
54 | 55 | ||
55 | const toCreate = { | 56 | const toCreate = { |
56 | videoId: videoInstance.id | 57 | videoId: videoInstance.id |
57 | } | 58 | } |
58 | 59 | ||
59 | db.BlacklistedVideo.create(toCreate) | 60 | await db.BlacklistedVideo.create(toCreate) |
60 | .then(() => res.type('json').status(204).end()) | 61 | return res.type('json').status(204).end() |
61 | .catch(err => { | ||
62 | logger.error('Errors when blacklisting video ', err) | ||
63 | return next(err) | ||
64 | }) | ||
65 | } | 62 | } |
66 | 63 | ||
67 | function listBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) { | 64 | async function listBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) { |
68 | db.BlacklistedVideo.listForApi(req.query.start, req.query.count, req.query.sort) | 65 | const resultList = await db.BlacklistedVideo.listForApi(req.query.start, req.query.count, req.query.sort) |
69 | .then(resultList => res.json(getFormattedObjects<BlacklistedVideo, BlacklistedVideoInstance>(resultList.data, resultList.total))) | 66 | |
70 | .catch(err => next(err)) | 67 | return res.json(getFormattedObjects<BlacklistedVideo, BlacklistedVideoInstance>(resultList.data, resultList.total)) |
71 | } | 68 | } |
72 | 69 | ||
73 | function removeVideoFromBlacklistController (req: express.Request, res: express.Response, next: express.NextFunction) { | 70 | async function removeVideoFromBlacklistController (req: express.Request, res: express.Response, next: express.NextFunction) { |
74 | const blacklistedVideo = res.locals.blacklistedVideo as BlacklistedVideoInstance | 71 | const blacklistedVideo = res.locals.blacklistedVideo as BlacklistedVideoInstance |
75 | 72 | ||
76 | blacklistedVideo.destroy() | 73 | try { |
77 | .then(() => { | 74 | await blacklistedVideo.destroy() |
78 | logger.info('Video %s removed from blacklist.', res.locals.video.uuid) | 75 | |
79 | res.sendStatus(204) | 76 | logger.info('Video %s removed from blacklist.', res.locals.video.uuid) |
80 | }) | 77 | |
81 | .catch(err => { | 78 | return res.sendStatus(204) |
82 | logger.error('Some error while removing video %s from blacklist.', res.locals.video.uuid, err) | 79 | } catch (err) { |
83 | next(err) | 80 | logger.error('Some error while removing video %s from blacklist.', res.locals.video.uuid, err) |
84 | }) | 81 | throw err |
82 | } | ||
85 | } | 83 | } |