]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/videos/blacklist.ts
Add plugin static files cache
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / blacklist.ts
CommitLineData
4d4e5cd4 1import * as express from 'express'
7ccddd7b 2import { VideoBlacklist, UserRight, VideoBlacklistCreate, VideoBlacklistType } from '../../../../shared'
da854ddd
C
3import { logger } from '../../../helpers/logger'
4import { getFormattedObjects } from '../../../helpers/utils'
65fcc311 5import {
26b7305a
C
6 asyncMiddleware,
7 authenticate,
8 blacklistSortValidator,
9 ensureUserHasRight,
10 paginationValidator,
11 setBlacklistSort,
12 setDefaultPagination,
13 videosBlacklistAddValidator,
14 videosBlacklistRemoveValidator,
7ccddd7b
JM
15 videosBlacklistUpdateValidator,
16 videosBlacklistFiltersValidator
65fcc311 17} from '../../../middlewares'
3fd3ab2d 18import { VideoBlacklistModel } from '../../../models/video/video-blacklist'
26b7305a 19import { sequelizeTypescript } from '../../../initializers'
cef534ed 20import { Notifier } from '../../../lib/notifier'
418d092a 21import { sendDeleteVideo } from '../../../lib/activitypub/send'
5abb9fbb 22import { federateVideoIfNeeded } from '../../../lib/activitypub'
65fcc311
C
23
24const blacklistRouter = express.Router()
25
35bf0c83 26blacklistRouter.post('/:videoId/blacklist',
65fcc311 27 authenticate,
954605a8 28 ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
a2431b7d 29 asyncMiddleware(videosBlacklistAddValidator),
eb080476 30 asyncMiddleware(addVideoToBlacklist)
d33242b0
C
31)
32
35bf0c83
C
33blacklistRouter.get('/blacklist',
34 authenticate,
954605a8 35 ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
35bf0c83
C
36 paginationValidator,
37 blacklistSortValidator,
38 setBlacklistSort,
f05a1c30 39 setDefaultPagination,
7ccddd7b 40 videosBlacklistFiltersValidator,
eb080476 41 asyncMiddleware(listBlacklist)
35bf0c83
C
42)
43
26b7305a
C
44blacklistRouter.put('/:videoId/blacklist',
45 authenticate,
46 ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
47 asyncMiddleware(videosBlacklistUpdateValidator),
48 asyncMiddleware(updateVideoBlacklistController)
49)
50
35bf0c83
C
51blacklistRouter.delete('/:videoId/blacklist',
52 authenticate,
954605a8 53 ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
a2431b7d 54 asyncMiddleware(videosBlacklistRemoveValidator),
eb080476 55 asyncMiddleware(removeVideoFromBlacklistController)
35bf0c83
C
56)
57
d33242b0
C
58// ---------------------------------------------------------------------------
59
65fcc311
C
60export {
61 blacklistRouter
62}
d33242b0
C
63
64// ---------------------------------------------------------------------------
65
26b7305a 66async function addVideoToBlacklist (req: express.Request, res: express.Response) {
d33242b0 67 const videoInstance = res.locals.video
26b7305a 68 const body: VideoBlacklistCreate = req.body
d33242b0
C
69
70 const toCreate = {
26b7305a 71 videoId: videoInstance.id,
5abb9fbb 72 unfederated: body.unfederate === true,
7ccddd7b
JM
73 reason: body.reason,
74 type: VideoBlacklistType.MANUAL
d33242b0
C
75 }
76
cef534ed
C
77 const blacklist = await VideoBlacklistModel.create(toCreate)
78 blacklist.Video = videoInstance
79
5abb9fbb
C
80 if (body.unfederate === true) {
81 await sendDeleteVideo(videoInstance, undefined)
82 }
83
cef534ed
C
84 Notifier.Instance.notifyOnVideoBlacklist(blacklist)
85
86 logger.info('Video %s blacklisted.', res.locals.video.uuid)
87
eb080476 88 return res.type('json').status(204).end()
d33242b0 89}
35bf0c83 90
26b7305a 91async function updateVideoBlacklistController (req: express.Request, res: express.Response) {
dae86118 92 const videoBlacklist = res.locals.videoBlacklist
26b7305a
C
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
a8b666e9 103async function listBlacklist (req: express.Request, res: express.Response) {
7ccddd7b 104 const resultList = await VideoBlacklistModel.listForApi(req.query.start, req.query.count, req.query.sort, req.query.type)
eb080476 105
a8b666e9 106 return res.json(getFormattedObjects(resultList.data, resultList.total))
35bf0c83
C
107}
108
a8b666e9 109async function removeVideoFromBlacklistController (req: express.Request, res: express.Response) {
dae86118
C
110 const videoBlacklist = res.locals.videoBlacklist
111 const video = res.locals.video
35bf0c83 112
7ccddd7b 113 const videoBlacklistType = await sequelizeTypescript.transaction(async t => {
5abb9fbb 114 const unfederated = videoBlacklist.unfederated
7ccddd7b
JM
115 const videoBlacklistType = videoBlacklist.type
116
5abb9fbb
C
117 await videoBlacklist.destroy({ transaction: t })
118
119 // Re federate the video
120 if (unfederated === true) {
121 await federateVideoIfNeeded(video, true, t)
122 }
7ccddd7b
JM
123
124 return videoBlacklistType
26b7305a 125 })
eb080476 126
cef534ed
C
127 Notifier.Instance.notifyOnVideoUnblacklist(video)
128
7ccddd7b
JM
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
26b7305a 137 logger.info('Video %s removed from blacklist.', res.locals.video.uuid)
eb080476 138
26b7305a 139 return res.type('json').status(204).end()
35bf0c83 140}