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