]>
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 | import { Notifier } from '../../../lib/notifier' | |
20 | import { VideoModel } from '../../../models/video/video' | |
21 | import { sendCreateVideo, sendDeleteVideo, sendUpdateVideo } 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 | 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 addVideoToBlacklist (req: express.Request, res: express.Response) { | |
66 | const videoInstance = res.locals.video | |
67 | const body: VideoBlacklistCreate = req.body | |
68 | ||
69 | const toCreate = { | |
70 | videoId: videoInstance.id, | |
71 | unfederated: body.unfederate === true, | |
72 | reason: body.reason | |
73 | } | |
74 | ||
75 | const blacklist = await VideoBlacklistModel.create(toCreate) | |
76 | blacklist.Video = videoInstance | |
77 | ||
78 | if (body.unfederate === true) { | |
79 | await sendDeleteVideo(videoInstance, undefined) | |
80 | } | |
81 | ||
82 | Notifier.Instance.notifyOnVideoBlacklist(blacklist) | |
83 | ||
84 | logger.info('Video %s blacklisted.', res.locals.video.uuid) | |
85 | ||
86 | return res.type('json').status(204).end() | |
87 | } | |
88 | ||
89 | async function updateVideoBlacklistController (req: express.Request, res: express.Response) { | |
90 | const videoBlacklist = res.locals.videoBlacklist as VideoBlacklistModel | |
91 | ||
92 | if (req.body.reason !== undefined) videoBlacklist.reason = req.body.reason | |
93 | ||
94 | await sequelizeTypescript.transaction(t => { | |
95 | return videoBlacklist.save({ transaction: t }) | |
96 | }) | |
97 | ||
98 | return res.type('json').status(204).end() | |
99 | } | |
100 | ||
101 | async function listBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) { | |
102 | const resultList = await VideoBlacklistModel.listForApi(req.query.start, req.query.count, req.query.sort) | |
103 | ||
104 | return res.json(getFormattedObjects<VideoBlacklist, VideoBlacklistModel>(resultList.data, resultList.total)) | |
105 | } | |
106 | ||
107 | async function removeVideoFromBlacklistController (req: express.Request, res: express.Response, next: express.NextFunction) { | |
108 | const videoBlacklist = res.locals.videoBlacklist as VideoBlacklistModel | |
109 | const video: VideoModel = res.locals.video | |
110 | ||
111 | await sequelizeTypescript.transaction(async t => { | |
112 | const unfederated = videoBlacklist.unfederated | |
113 | await videoBlacklist.destroy({ transaction: t }) | |
114 | ||
115 | // Re federate the video | |
116 | if (unfederated === true) { | |
117 | await federateVideoIfNeeded(video, true, t) | |
118 | } | |
119 | }) | |
120 | ||
121 | Notifier.Instance.notifyOnVideoUnblacklist(video) | |
122 | ||
123 | logger.info('Video %s removed from blacklist.', res.locals.video.uuid) | |
124 | ||
125 | return res.type('json').status(204).end() | |
126 | } |