]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/videos/blacklist.ts
Add ability to unfederate a local video (on blacklist)
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / blacklist.ts
CommitLineData
4d4e5cd4 1import * as express from 'express'
191764f3 2import { VideoBlacklist, UserRight, VideoBlacklistCreate } 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,
15 videosBlacklistUpdateValidator
65fcc311 16} from '../../../middlewares'
3fd3ab2d 17import { VideoBlacklistModel } from '../../../models/video/video-blacklist'
26b7305a 18import { sequelizeTypescript } from '../../../initializers'
cef534ed
C
19import { Notifier } from '../../../lib/notifier'
20import { VideoModel } from '../../../models/video/video'
5abb9fbb
C
21import { sendCreateVideo, sendDeleteVideo, sendUpdateVideo } from '../../../lib/activitypub/send'
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,
eb080476 40 asyncMiddleware(listBlacklist)
35bf0c83
C
41)
42
26b7305a
C
43blacklistRouter.put('/:videoId/blacklist',
44 authenticate,
45 ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
46 asyncMiddleware(videosBlacklistUpdateValidator),
47 asyncMiddleware(updateVideoBlacklistController)
48)
49
35bf0c83
C
50blacklistRouter.delete('/:videoId/blacklist',
51 authenticate,
954605a8 52 ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
a2431b7d 53 asyncMiddleware(videosBlacklistRemoveValidator),
eb080476 54 asyncMiddleware(removeVideoFromBlacklistController)
35bf0c83
C
55)
56
d33242b0
C
57// ---------------------------------------------------------------------------
58
65fcc311
C
59export {
60 blacklistRouter
61}
d33242b0
C
62
63// ---------------------------------------------------------------------------
64
26b7305a 65async function addVideoToBlacklist (req: express.Request, res: express.Response) {
d33242b0 66 const videoInstance = res.locals.video
26b7305a 67 const body: VideoBlacklistCreate = req.body
d33242b0
C
68
69 const toCreate = {
26b7305a 70 videoId: videoInstance.id,
5abb9fbb 71 unfederated: body.unfederate === true,
26b7305a 72 reason: body.reason
d33242b0
C
73 }
74
cef534ed
C
75 const blacklist = await VideoBlacklistModel.create(toCreate)
76 blacklist.Video = videoInstance
77
5abb9fbb
C
78 if (body.unfederate === true) {
79 await sendDeleteVideo(videoInstance, undefined)
80 }
81
cef534ed
C
82 Notifier.Instance.notifyOnVideoBlacklist(blacklist)
83
84 logger.info('Video %s blacklisted.', res.locals.video.uuid)
85
eb080476 86 return res.type('json').status(204).end()
d33242b0 87}
35bf0c83 88
26b7305a
C
89async function updateVideoBlacklistController (req: express.Request, res: express.Response) {
90 const videoBlacklist = res.locals.videoBlacklist as VideoBlacklistModel
26b7305a
C
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
eb080476 101async function listBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) {
3fd3ab2d 102 const resultList = await VideoBlacklistModel.listForApi(req.query.start, req.query.count, req.query.sort)
eb080476 103
191764f3 104 return res.json(getFormattedObjects<VideoBlacklist, VideoBlacklistModel>(resultList.data, resultList.total))
35bf0c83
C
105}
106
eb080476 107async function removeVideoFromBlacklistController (req: express.Request, res: express.Response, next: express.NextFunction) {
26b7305a 108 const videoBlacklist = res.locals.videoBlacklist as VideoBlacklistModel
cef534ed 109 const video: VideoModel = res.locals.video
35bf0c83 110
5abb9fbb
C
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 }
26b7305a 119 })
eb080476 120
cef534ed
C
121 Notifier.Instance.notifyOnVideoUnblacklist(video)
122
26b7305a 123 logger.info('Video %s removed from blacklist.', res.locals.video.uuid)
eb080476 124
26b7305a 125 return res.type('json').status(204).end()
35bf0c83 126}