]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/videos/blacklist.ts
Guess if we need to generate the thumbnail for imports
[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'
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,
12 paginationValidator,
13 setBlacklistSort,
14 setDefaultPagination,
15 videosBlacklistAddValidator,
453e83ea 16 videosBlacklistFiltersValidator,
26b7305a 17 videosBlacklistRemoveValidator,
453e83ea 18 videosBlacklistUpdateValidator
65fcc311 19} from '../../../middlewares'
3fd3ab2d 20import { VideoBlacklistModel } from '../../../models/video/video-blacklist'
2d53be02 21import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
65fcc311
C
22
23const blacklistRouter = express.Router()
24
35bf0c83 25blacklistRouter.post('/:videoId/blacklist',
65fcc311 26 authenticate,
954605a8 27 ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
a2431b7d 28 asyncMiddleware(videosBlacklistAddValidator),
80fdaf06 29 asyncMiddleware(addVideoToBlacklistController)
d33242b0
C
30)
31
35bf0c83
C
32blacklistRouter.get('/blacklist',
33 authenticate,
954605a8 34 ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
35bf0c83
C
35 paginationValidator,
36 blacklistSortValidator,
37 setBlacklistSort,
f05a1c30 38 setDefaultPagination,
7ccddd7b 39 videosBlacklistFiltersValidator,
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
80fdaf06 65async function addVideoToBlacklistController (req: express.Request, res: express.Response) {
453e83ea 66 const videoInstance = res.locals.videoAll
26b7305a 67 const body: VideoBlacklistCreate = req.body
d33242b0 68
80fdaf06 69 await blacklistVideo(videoInstance, body)
cef534ed 70
453e83ea 71 logger.info('Video %s blacklisted.', videoInstance.uuid)
cef534ed 72
2d53be02 73 return res.type('json').sendStatus(HttpStatusCode.NO_CONTENT_204)
d33242b0 74}
35bf0c83 75
26b7305a 76async function updateVideoBlacklistController (req: express.Request, res: express.Response) {
dae86118 77 const videoBlacklist = res.locals.videoBlacklist
26b7305a
C
78
79 if (req.body.reason !== undefined) videoBlacklist.reason = req.body.reason
80
81 await sequelizeTypescript.transaction(t => {
82 return videoBlacklist.save({ transaction: t })
83 })
84
2d53be02 85 return res.type('json').sendStatus(HttpStatusCode.NO_CONTENT_204)
26b7305a
C
86}
87
a8b666e9 88async function listBlacklist (req: express.Request, res: express.Response) {
e0a92917
RK
89 const resultList = await VideoBlacklistModel.listForApi({
90 start: req.query.start,
91 count: req.query.count,
92 sort: req.query.sort,
93 search: req.query.search,
94 type: req.query.type
95 })
eb080476 96
a8b666e9 97 return res.json(getFormattedObjects(resultList.data, resultList.total))
35bf0c83
C
98}
99
a8b666e9 100async function removeVideoFromBlacklistController (req: express.Request, res: express.Response) {
dae86118 101 const videoBlacklist = res.locals.videoBlacklist
453e83ea 102 const video = res.locals.videoAll
35bf0c83 103
80fdaf06 104 await unblacklistVideo(videoBlacklist, video)
7ccddd7b 105
453e83ea 106 logger.info('Video %s removed from blacklist.', video.uuid)
eb080476 107
2d53be02 108 return res.type('json').sendStatus(HttpStatusCode.NO_CONTENT_204)
35bf0c83 109}