]>
Commit | Line | Data |
---|---|---|
1 | import * as express from 'express' | |
2 | import { VideoBlacklist, UserRight, VideoBlacklistCreate } from '../../../../shared' | |
3 | import { logger } from '../../../helpers/logger' | |
4 | import { getFormattedObjects } from '../../../helpers/utils' | |
5 | import { | |
6 | asyncMiddleware, | |
7 | authenticate, | |
8 | blacklistSortValidator, | |
9 | ensureUserHasRight, | |
10 | paginationValidator, | |
11 | setBlacklistSort, | |
12 | setDefaultPagination, | |
13 | videosBlacklistAddValidator, | |
14 | videosBlacklistRemoveValidator, | |
15 | videosBlacklistUpdateValidator | |
16 | } from '../../../middlewares' | |
17 | import { VideoBlacklistModel } from '../../../models/video/video-blacklist' | |
18 | import { sequelizeTypescript } from '../../../initializers' | |
19 | ||
20 | const blacklistRouter = express.Router() | |
21 | ||
22 | blacklistRouter.post('/:videoId/blacklist', | |
23 | authenticate, | |
24 | ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST), | |
25 | asyncMiddleware(videosBlacklistAddValidator), | |
26 | asyncMiddleware(addVideoToBlacklist) | |
27 | ) | |
28 | ||
29 | blacklistRouter.get('/blacklist', | |
30 | authenticate, | |
31 | ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST), | |
32 | paginationValidator, | |
33 | blacklistSortValidator, | |
34 | setBlacklistSort, | |
35 | setDefaultPagination, | |
36 | asyncMiddleware(listBlacklist) | |
37 | ) | |
38 | ||
39 | blacklistRouter.put('/:videoId/blacklist', | |
40 | authenticate, | |
41 | ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST), | |
42 | asyncMiddleware(videosBlacklistUpdateValidator), | |
43 | asyncMiddleware(updateVideoBlacklistController) | |
44 | ) | |
45 | ||
46 | blacklistRouter.delete('/:videoId/blacklist', | |
47 | authenticate, | |
48 | ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST), | |
49 | asyncMiddleware(videosBlacklistRemoveValidator), | |
50 | asyncMiddleware(removeVideoFromBlacklistController) | |
51 | ) | |
52 | ||
53 | // --------------------------------------------------------------------------- | |
54 | ||
55 | export { | |
56 | blacklistRouter | |
57 | } | |
58 | ||
59 | // --------------------------------------------------------------------------- | |
60 | ||
61 | async function addVideoToBlacklist (req: express.Request, res: express.Response) { | |
62 | const videoInstance = res.locals.video | |
63 | const body: VideoBlacklistCreate = req.body | |
64 | ||
65 | const toCreate = { | |
66 | videoId: videoInstance.id, | |
67 | reason: body.reason | |
68 | } | |
69 | ||
70 | await VideoBlacklistModel.create(toCreate) | |
71 | return res.type('json').status(204).end() | |
72 | } | |
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 | ||
87 | async function listBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) { | |
88 | const resultList = await VideoBlacklistModel.listForApi(req.query.start, req.query.count, req.query.sort) | |
89 | ||
90 | return res.json(getFormattedObjects<VideoBlacklist, VideoBlacklistModel>(resultList.data, resultList.total)) | |
91 | } | |
92 | ||
93 | async function removeVideoFromBlacklistController (req: express.Request, res: express.Response, next: express.NextFunction) { | |
94 | const videoBlacklist = res.locals.videoBlacklist as VideoBlacklistModel | |
95 | ||
96 | await sequelizeTypescript.transaction(t => { | |
97 | return videoBlacklist.destroy({ transaction: t }) | |
98 | }) | |
99 | ||
100 | logger.info('Video %s removed from blacklist.', res.locals.video.uuid) | |
101 | ||
102 | return res.type('json').status(204).end() | |
103 | } |