]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/videos/blacklist.ts
Add users search filter
[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'
65fcc311
C
19
20const blacklistRouter = express.Router()
21
35bf0c83 22blacklistRouter.post('/:videoId/blacklist',
65fcc311 23 authenticate,
954605a8 24 ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
a2431b7d 25 asyncMiddleware(videosBlacklistAddValidator),
eb080476 26 asyncMiddleware(addVideoToBlacklist)
d33242b0
C
27)
28
35bf0c83
C
29blacklistRouter.get('/blacklist',
30 authenticate,
954605a8 31 ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
35bf0c83
C
32 paginationValidator,
33 blacklistSortValidator,
34 setBlacklistSort,
f05a1c30 35 setDefaultPagination,
eb080476 36 asyncMiddleware(listBlacklist)
35bf0c83
C
37)
38
26b7305a
C
39blacklistRouter.put('/:videoId/blacklist',
40 authenticate,
41 ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
42 asyncMiddleware(videosBlacklistUpdateValidator),
43 asyncMiddleware(updateVideoBlacklistController)
44)
45
35bf0c83
C
46blacklistRouter.delete('/:videoId/blacklist',
47 authenticate,
954605a8 48 ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
a2431b7d 49 asyncMiddleware(videosBlacklistRemoveValidator),
eb080476 50 asyncMiddleware(removeVideoFromBlacklistController)
35bf0c83
C
51)
52
d33242b0
C
53// ---------------------------------------------------------------------------
54
65fcc311
C
55export {
56 blacklistRouter
57}
d33242b0
C
58
59// ---------------------------------------------------------------------------
60
26b7305a 61async function addVideoToBlacklist (req: express.Request, res: express.Response) {
d33242b0 62 const videoInstance = res.locals.video
26b7305a 63 const body: VideoBlacklistCreate = req.body
d33242b0
C
64
65 const toCreate = {
26b7305a
C
66 videoId: videoInstance.id,
67 reason: body.reason
d33242b0
C
68 }
69
3fd3ab2d 70 await VideoBlacklistModel.create(toCreate)
eb080476 71 return res.type('json').status(204).end()
d33242b0 72}
35bf0c83 73
26b7305a
C
74async function updateVideoBlacklistController (req: express.Request, res: express.Response) {
75 const videoBlacklist = res.locals.videoBlacklist as VideoBlacklistModel
76 logger.info(videoBlacklist)
77
78 if (req.body.reason !== undefined) videoBlacklist.reason = req.body.reason
79
80 await sequelizeTypescript.transaction(t => {
81 return videoBlacklist.save({ transaction: t })
82 })
83
84 return res.type('json').status(204).end()
85}
86
eb080476 87async function listBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) {
3fd3ab2d 88 const resultList = await VideoBlacklistModel.listForApi(req.query.start, req.query.count, req.query.sort)
eb080476 89
191764f3 90 return res.json(getFormattedObjects<VideoBlacklist, VideoBlacklistModel>(resultList.data, resultList.total))
35bf0c83
C
91}
92
eb080476 93async function removeVideoFromBlacklistController (req: express.Request, res: express.Response, next: express.NextFunction) {
26b7305a 94 const videoBlacklist = res.locals.videoBlacklist as VideoBlacklistModel
35bf0c83 95
26b7305a
C
96 await sequelizeTypescript.transaction(t => {
97 return videoBlacklist.destroy({ transaction: t })
98 })
eb080476 99
26b7305a 100 logger.info('Video %s removed from blacklist.', res.locals.video.uuid)
eb080476 101
26b7305a 102 return res.type('json').status(204).end()
35bf0c83 103}