]>
Commit | Line | Data |
---|---|---|
1 | import * as express from 'express' | |
2 | import { VideoBlacklist, UserRight, VideoBlacklistCreate, VideoBlacklistType } 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 | videosBlacklistFiltersValidator | |
17 | } from '../../../middlewares' | |
18 | import { VideoBlacklistModel } from '../../../models/video/video-blacklist' | |
19 | import { sequelizeTypescript } from '../../../initializers' | |
20 | import { Notifier } from '../../../lib/notifier' | |
21 | import { sendDeleteVideo } from '../../../lib/activitypub/send' | |
22 | import { federateVideoIfNeeded } from '../../../lib/activitypub' | |
23 | ||
24 | const blacklistRouter = express.Router() | |
25 | ||
26 | blacklistRouter.post('/:videoId/blacklist', | |
27 | authenticate, | |
28 | ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST), | |
29 | asyncMiddleware(videosBlacklistAddValidator), | |
30 | asyncMiddleware(addVideoToBlacklist) | |
31 | ) | |
32 | ||
33 | blacklistRouter.get('/blacklist', | |
34 | authenticate, | |
35 | ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST), | |
36 | paginationValidator, | |
37 | blacklistSortValidator, | |
38 | setBlacklistSort, | |
39 | setDefaultPagination, | |
40 | videosBlacklistFiltersValidator, | |
41 | asyncMiddleware(listBlacklist) | |
42 | ) | |
43 | ||
44 | blacklistRouter.put('/:videoId/blacklist', | |
45 | authenticate, | |
46 | ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST), | |
47 | asyncMiddleware(videosBlacklistUpdateValidator), | |
48 | asyncMiddleware(updateVideoBlacklistController) | |
49 | ) | |
50 | ||
51 | blacklistRouter.delete('/:videoId/blacklist', | |
52 | authenticate, | |
53 | ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST), | |
54 | asyncMiddleware(videosBlacklistRemoveValidator), | |
55 | asyncMiddleware(removeVideoFromBlacklistController) | |
56 | ) | |
57 | ||
58 | // --------------------------------------------------------------------------- | |
59 | ||
60 | export { | |
61 | blacklistRouter | |
62 | } | |
63 | ||
64 | // --------------------------------------------------------------------------- | |
65 | ||
66 | async function addVideoToBlacklist (req: express.Request, res: express.Response) { | |
67 | const videoInstance = res.locals.video | |
68 | const body: VideoBlacklistCreate = req.body | |
69 | ||
70 | const toCreate = { | |
71 | videoId: videoInstance.id, | |
72 | unfederated: body.unfederate === true, | |
73 | reason: body.reason, | |
74 | type: VideoBlacklistType.MANUAL | |
75 | } | |
76 | ||
77 | const blacklist = await VideoBlacklistModel.create(toCreate) | |
78 | blacklist.Video = videoInstance | |
79 | ||
80 | if (body.unfederate === true) { | |
81 | await sendDeleteVideo(videoInstance, undefined) | |
82 | } | |
83 | ||
84 | Notifier.Instance.notifyOnVideoBlacklist(blacklist) | |
85 | ||
86 | logger.info('Video %s blacklisted.', res.locals.video.uuid) | |
87 | ||
88 | return res.type('json').status(204).end() | |
89 | } | |
90 | ||
91 | async function updateVideoBlacklistController (req: express.Request, res: express.Response) { | |
92 | const videoBlacklist = res.locals.videoBlacklist | |
93 | ||
94 | if (req.body.reason !== undefined) videoBlacklist.reason = req.body.reason | |
95 | ||
96 | await sequelizeTypescript.transaction(t => { | |
97 | return videoBlacklist.save({ transaction: t }) | |
98 | }) | |
99 | ||
100 | return res.type('json').status(204).end() | |
101 | } | |
102 | ||
103 | async function listBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) { | |
104 | const resultList = await VideoBlacklistModel.listForApi(req.query.start, req.query.count, req.query.sort, req.query.type) | |
105 | ||
106 | return res.json(getFormattedObjects<VideoBlacklist, VideoBlacklistModel>(resultList.data, resultList.total)) | |
107 | } | |
108 | ||
109 | async function removeVideoFromBlacklistController (req: express.Request, res: express.Response, next: express.NextFunction) { | |
110 | const videoBlacklist = res.locals.videoBlacklist | |
111 | const video = res.locals.video | |
112 | ||
113 | const videoBlacklistType = await sequelizeTypescript.transaction(async t => { | |
114 | const unfederated = videoBlacklist.unfederated | |
115 | const videoBlacklistType = videoBlacklist.type | |
116 | ||
117 | await videoBlacklist.destroy({ transaction: t }) | |
118 | ||
119 | // Re federate the video | |
120 | if (unfederated === true) { | |
121 | await federateVideoIfNeeded(video, true, t) | |
122 | } | |
123 | ||
124 | return videoBlacklistType | |
125 | }) | |
126 | ||
127 | Notifier.Instance.notifyOnVideoUnblacklist(video) | |
128 | ||
129 | if (videoBlacklistType === VideoBlacklistType.AUTO_BEFORE_PUBLISHED) { | |
130 | Notifier.Instance.notifyOnVideoPublishedAfterRemovedFromAutoBlacklist(video) | |
131 | ||
132 | // Delete on object so new video notifications will send | |
133 | delete video.VideoBlacklist | |
134 | Notifier.Instance.notifyOnNewVideo(video) | |
135 | } | |
136 | ||
137 | logger.info('Video %s removed from blacklist.', res.locals.video.uuid) | |
138 | ||
139 | return res.type('json').status(204).end() | |
140 | } |