]>
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 | import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' | |
22 | ||
23 | const blacklistRouter = express.Router() | |
24 | ||
25 | blacklistRouter.post('/:videoId/blacklist', | |
26 | authenticate, | |
27 | ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST), | |
28 | asyncMiddleware(videosBlacklistAddValidator), | |
29 | asyncMiddleware(addVideoToBlacklistController) | |
30 | ) | |
31 | ||
32 | blacklistRouter.get('/blacklist', | |
33 | authenticate, | |
34 | ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST), | |
35 | paginationValidator, | |
36 | blacklistSortValidator, | |
37 | setBlacklistSort, | |
38 | setDefaultPagination, | |
39 | videosBlacklistFiltersValidator, | |
40 | asyncMiddleware(listBlacklist) | |
41 | ) | |
42 | ||
43 | blacklistRouter.put('/:videoId/blacklist', | |
44 | authenticate, | |
45 | ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST), | |
46 | asyncMiddleware(videosBlacklistUpdateValidator), | |
47 | asyncMiddleware(updateVideoBlacklistController) | |
48 | ) | |
49 | ||
50 | blacklistRouter.delete('/:videoId/blacklist', | |
51 | authenticate, | |
52 | ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST), | |
53 | asyncMiddleware(videosBlacklistRemoveValidator), | |
54 | asyncMiddleware(removeVideoFromBlacklistController) | |
55 | ) | |
56 | ||
57 | // --------------------------------------------------------------------------- | |
58 | ||
59 | export { | |
60 | blacklistRouter | |
61 | } | |
62 | ||
63 | // --------------------------------------------------------------------------- | |
64 | ||
65 | async function addVideoToBlacklistController (req: express.Request, res: express.Response) { | |
66 | const videoInstance = res.locals.videoAll | |
67 | const body: VideoBlacklistCreate = req.body | |
68 | ||
69 | await blacklistVideo(videoInstance, body) | |
70 | ||
71 | logger.info('Video %s blacklisted.', videoInstance.uuid) | |
72 | ||
73 | return res.type('json').sendStatus(HttpStatusCode.NO_CONTENT_204) | |
74 | } | |
75 | ||
76 | async function updateVideoBlacklistController (req: express.Request, res: express.Response) { | |
77 | const videoBlacklist = res.locals.videoBlacklist | |
78 | ||
79 | if (req.body.reason !== undefined) videoBlacklist.reason = req.body.reason | |
80 | ||
81 | await sequelizeTypescript.transaction(t => { | |
82 | return videoBlacklist.save({ transaction: t }) | |
83 | }) | |
84 | ||
85 | return res.type('json').sendStatus(HttpStatusCode.NO_CONTENT_204) | |
86 | } | |
87 | ||
88 | async function listBlacklist (req: express.Request, res: express.Response) { | |
89 | const resultList = await VideoBlacklistModel.listForApi({ | |
90 | start: req.query.start, | |
91 | count: req.query.count, | |
92 | sort: req.query.sort, | |
93 | search: req.query.search, | |
94 | type: req.query.type | |
95 | }) | |
96 | ||
97 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
98 | } | |
99 | ||
100 | async function removeVideoFromBlacklistController (req: express.Request, res: express.Response) { | |
101 | const videoBlacklist = res.locals.videoBlacklist | |
102 | const video = res.locals.videoAll | |
103 | ||
104 | await unblacklistVideo(videoBlacklist, video) | |
105 | ||
106 | logger.info('Video %s removed from blacklist.', video.uuid) | |
107 | ||
108 | return res.type('json').sendStatus(HttpStatusCode.NO_CONTENT_204) | |
109 | } |