]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as express from 'express' |
191764f3 | 2 | import { VideoBlacklist, UserRight, VideoBlacklistCreate } from '../../../../shared' |
da854ddd C |
3 | import { logger } from '../../../helpers/logger' |
4 | import { getFormattedObjects } from '../../../helpers/utils' | |
65fcc311 | 5 | import { |
26b7305a C |
6 | asyncMiddleware, |
7 | authenticate, | |
8 | blacklistSortValidator, | |
9 | ensureUserHasRight, | |
10 | paginationValidator, | |
11 | setBlacklistSort, | |
12 | setDefaultPagination, | |
13 | videosBlacklistAddValidator, | |
14 | videosBlacklistRemoveValidator, | |
15 | videosBlacklistUpdateValidator | |
65fcc311 | 16 | } from '../../../middlewares' |
3fd3ab2d | 17 | import { VideoBlacklistModel } from '../../../models/video/video-blacklist' |
26b7305a | 18 | import { sequelizeTypescript } from '../../../initializers' |
cef534ed C |
19 | import { Notifier } from '../../../lib/notifier' |
20 | import { VideoModel } from '../../../models/video/video' | |
65fcc311 C |
21 | |
22 | const blacklistRouter = express.Router() | |
23 | ||
35bf0c83 | 24 | blacklistRouter.post('/:videoId/blacklist', |
65fcc311 | 25 | authenticate, |
954605a8 | 26 | ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST), |
a2431b7d | 27 | asyncMiddleware(videosBlacklistAddValidator), |
eb080476 | 28 | asyncMiddleware(addVideoToBlacklist) |
d33242b0 C |
29 | ) |
30 | ||
35bf0c83 C |
31 | blacklistRouter.get('/blacklist', |
32 | authenticate, | |
954605a8 | 33 | ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST), |
35bf0c83 C |
34 | paginationValidator, |
35 | blacklistSortValidator, | |
36 | setBlacklistSort, | |
f05a1c30 | 37 | setDefaultPagination, |
eb080476 | 38 | asyncMiddleware(listBlacklist) |
35bf0c83 C |
39 | ) |
40 | ||
26b7305a C |
41 | blacklistRouter.put('/:videoId/blacklist', |
42 | authenticate, | |
43 | ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST), | |
44 | asyncMiddleware(videosBlacklistUpdateValidator), | |
45 | asyncMiddleware(updateVideoBlacklistController) | |
46 | ) | |
47 | ||
35bf0c83 C |
48 | blacklistRouter.delete('/:videoId/blacklist', |
49 | authenticate, | |
954605a8 | 50 | ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST), |
a2431b7d | 51 | asyncMiddleware(videosBlacklistRemoveValidator), |
eb080476 | 52 | asyncMiddleware(removeVideoFromBlacklistController) |
35bf0c83 C |
53 | ) |
54 | ||
d33242b0 C |
55 | // --------------------------------------------------------------------------- |
56 | ||
65fcc311 C |
57 | export { |
58 | blacklistRouter | |
59 | } | |
d33242b0 C |
60 | |
61 | // --------------------------------------------------------------------------- | |
62 | ||
26b7305a | 63 | async function addVideoToBlacklist (req: express.Request, res: express.Response) { |
d33242b0 | 64 | const videoInstance = res.locals.video |
26b7305a | 65 | const body: VideoBlacklistCreate = req.body |
d33242b0 C |
66 | |
67 | const toCreate = { | |
26b7305a C |
68 | videoId: videoInstance.id, |
69 | reason: body.reason | |
d33242b0 C |
70 | } |
71 | ||
cef534ed C |
72 | const blacklist = await VideoBlacklistModel.create(toCreate) |
73 | blacklist.Video = videoInstance | |
74 | ||
75 | Notifier.Instance.notifyOnVideoBlacklist(blacklist) | |
76 | ||
77 | logger.info('Video %s blacklisted.', res.locals.video.uuid) | |
78 | ||
eb080476 | 79 | return res.type('json').status(204).end() |
d33242b0 | 80 | } |
35bf0c83 | 81 | |
26b7305a C |
82 | async function updateVideoBlacklistController (req: express.Request, res: express.Response) { |
83 | const videoBlacklist = res.locals.videoBlacklist as VideoBlacklistModel | |
26b7305a C |
84 | |
85 | if (req.body.reason !== undefined) videoBlacklist.reason = req.body.reason | |
86 | ||
87 | await sequelizeTypescript.transaction(t => { | |
88 | return videoBlacklist.save({ transaction: t }) | |
89 | }) | |
90 | ||
91 | return res.type('json').status(204).end() | |
92 | } | |
93 | ||
eb080476 | 94 | async function listBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 95 | const resultList = await VideoBlacklistModel.listForApi(req.query.start, req.query.count, req.query.sort) |
eb080476 | 96 | |
191764f3 | 97 | return res.json(getFormattedObjects<VideoBlacklist, VideoBlacklistModel>(resultList.data, resultList.total)) |
35bf0c83 C |
98 | } |
99 | ||
eb080476 | 100 | async function removeVideoFromBlacklistController (req: express.Request, res: express.Response, next: express.NextFunction) { |
26b7305a | 101 | const videoBlacklist = res.locals.videoBlacklist as VideoBlacklistModel |
cef534ed | 102 | const video: VideoModel = res.locals.video |
35bf0c83 | 103 | |
26b7305a C |
104 | await sequelizeTypescript.transaction(t => { |
105 | return videoBlacklist.destroy({ transaction: t }) | |
106 | }) | |
eb080476 | 107 | |
cef534ed C |
108 | Notifier.Instance.notifyOnVideoUnblacklist(video) |
109 | ||
26b7305a | 110 | logger.info('Video %s removed from blacklist.', res.locals.video.uuid) |
eb080476 | 111 | |
26b7305a | 112 | return res.type('json').status(204).end() |
35bf0c83 | 113 | } |