]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/videos/blacklist.ts
Merge branch 'release/3.3.0' into develop
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / blacklist.ts
CommitLineData
4d4e5cd4 1import * as express from 'express'
80fdaf06
C
2import { blacklistVideo, unblacklistVideo } from '@server/lib/video-blacklist'
3import { UserRight, VideoBlacklistCreate } from '../../../../shared'
4c7e60bc 4import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
da854ddd
C
5import { logger } from '../../../helpers/logger'
6import { getFormattedObjects } from '../../../helpers/utils'
80fdaf06 7import { sequelizeTypescript } from '../../../initializers/database'
65fcc311 8import {
26b7305a
C
9 asyncMiddleware,
10 authenticate,
11 blacklistSortValidator,
12 ensureUserHasRight,
1333ab1f 13 openapiOperationDoc,
26b7305a
C
14 paginationValidator,
15 setBlacklistSort,
16 setDefaultPagination,
17 videosBlacklistAddValidator,
453e83ea 18 videosBlacklistFiltersValidator,
26b7305a 19 videosBlacklistRemoveValidator,
453e83ea 20 videosBlacklistUpdateValidator
65fcc311 21} from '../../../middlewares'
3fd3ab2d 22import { VideoBlacklistModel } from '../../../models/video/video-blacklist'
65fcc311
C
23
24const blacklistRouter = express.Router()
25
35bf0c83 26blacklistRouter.post('/:videoId/blacklist',
1333ab1f 27 openapiOperationDoc({ operationId: 'addVideoBlock' }),
65fcc311 28 authenticate,
954605a8 29 ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
a2431b7d 30 asyncMiddleware(videosBlacklistAddValidator),
80fdaf06 31 asyncMiddleware(addVideoToBlacklistController)
d33242b0
C
32)
33
35bf0c83 34blacklistRouter.get('/blacklist',
1333ab1f 35 openapiOperationDoc({ operationId: 'getVideoBlocks' }),
35bf0c83 36 authenticate,
954605a8 37 ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
35bf0c83
C
38 paginationValidator,
39 blacklistSortValidator,
40 setBlacklistSort,
f05a1c30 41 setDefaultPagination,
7ccddd7b 42 videosBlacklistFiltersValidator,
eb080476 43 asyncMiddleware(listBlacklist)
35bf0c83
C
44)
45
26b7305a
C
46blacklistRouter.put('/:videoId/blacklist',
47 authenticate,
48 ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
49 asyncMiddleware(videosBlacklistUpdateValidator),
50 asyncMiddleware(updateVideoBlacklistController)
51)
52
35bf0c83 53blacklistRouter.delete('/:videoId/blacklist',
1333ab1f 54 openapiOperationDoc({ operationId: 'delVideoBlock' }),
35bf0c83 55 authenticate,
954605a8 56 ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
a2431b7d 57 asyncMiddleware(videosBlacklistRemoveValidator),
eb080476 58 asyncMiddleware(removeVideoFromBlacklistController)
35bf0c83
C
59)
60
d33242b0
C
61// ---------------------------------------------------------------------------
62
65fcc311
C
63export {
64 blacklistRouter
65}
d33242b0
C
66
67// ---------------------------------------------------------------------------
68
80fdaf06 69async function addVideoToBlacklistController (req: express.Request, res: express.Response) {
453e83ea 70 const videoInstance = res.locals.videoAll
26b7305a 71 const body: VideoBlacklistCreate = req.body
d33242b0 72
80fdaf06 73 await blacklistVideo(videoInstance, body)
cef534ed 74
453e83ea 75 logger.info('Video %s blacklisted.', videoInstance.uuid)
cef534ed 76
76148b27 77 return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end()
d33242b0 78}
35bf0c83 79
26b7305a 80async function updateVideoBlacklistController (req: express.Request, res: express.Response) {
dae86118 81 const videoBlacklist = res.locals.videoBlacklist
26b7305a
C
82
83 if (req.body.reason !== undefined) videoBlacklist.reason = req.body.reason
84
85 await sequelizeTypescript.transaction(t => {
86 return videoBlacklist.save({ transaction: t })
87 })
88
76148b27 89 return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end()
26b7305a
C
90}
91
a8b666e9 92async function listBlacklist (req: express.Request, res: express.Response) {
e0a92917
RK
93 const resultList = await VideoBlacklistModel.listForApi({
94 start: req.query.start,
95 count: req.query.count,
96 sort: req.query.sort,
97 search: req.query.search,
98 type: req.query.type
99 })
eb080476 100
a8b666e9 101 return res.json(getFormattedObjects(resultList.data, resultList.total))
35bf0c83
C
102}
103
a8b666e9 104async function removeVideoFromBlacklistController (req: express.Request, res: express.Response) {
dae86118 105 const videoBlacklist = res.locals.videoBlacklist
453e83ea 106 const video = res.locals.videoAll
35bf0c83 107
80fdaf06 108 await unblacklistVideo(videoBlacklist, video)
7ccddd7b 109
453e83ea 110 logger.info('Video %s removed from blacklist.', video.uuid)
eb080476 111
76148b27 112 return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end()
35bf0c83 113}