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