]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/videos/blacklist.ts
rename blacklist to block/blocklist, merge block and auto-block views
[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'
65fcc311
C
21
22const blacklistRouter = express.Router()
23
35bf0c83 24blacklistRouter.post('/:videoId/blacklist',
65fcc311 25 authenticate,
5baee5fc 26 ensureUserHasRight(UserRight.MANAGE_VIDEO_BLOCKS),
a2431b7d 27 asyncMiddleware(videosBlacklistAddValidator),
80fdaf06 28 asyncMiddleware(addVideoToBlacklistController)
d33242b0
C
29)
30
35bf0c83
C
31blacklistRouter.get('/blacklist',
32 authenticate,
5baee5fc 33 ensureUserHasRight(UserRight.MANAGE_VIDEO_BLOCKS),
35bf0c83
C
34 paginationValidator,
35 blacklistSortValidator,
36 setBlacklistSort,
f05a1c30 37 setDefaultPagination,
7ccddd7b 38 videosBlacklistFiltersValidator,
eb080476 39 asyncMiddleware(listBlacklist)
35bf0c83
C
40)
41
26b7305a
C
42blacklistRouter.put('/:videoId/blacklist',
43 authenticate,
5baee5fc 44 ensureUserHasRight(UserRight.MANAGE_VIDEO_BLOCKS),
26b7305a
C
45 asyncMiddleware(videosBlacklistUpdateValidator),
46 asyncMiddleware(updateVideoBlacklistController)
47)
48
35bf0c83
C
49blacklistRouter.delete('/:videoId/blacklist',
50 authenticate,
5baee5fc 51 ensureUserHasRight(UserRight.MANAGE_VIDEO_BLOCKS),
a2431b7d 52 asyncMiddleware(videosBlacklistRemoveValidator),
eb080476 53 asyncMiddleware(removeVideoFromBlacklistController)
35bf0c83
C
54)
55
d33242b0
C
56// ---------------------------------------------------------------------------
57
65fcc311
C
58export {
59 blacklistRouter
60}
d33242b0
C
61
62// ---------------------------------------------------------------------------
63
80fdaf06 64async function addVideoToBlacklistController (req: express.Request, res: express.Response) {
453e83ea 65 const videoInstance = res.locals.videoAll
26b7305a 66 const body: VideoBlacklistCreate = req.body
d33242b0 67
80fdaf06 68 await blacklistVideo(videoInstance, body)
cef534ed 69
453e83ea 70 logger.info('Video %s blacklisted.', videoInstance.uuid)
cef534ed 71
80fdaf06 72 return res.type('json').sendStatus(204)
d33242b0 73}
35bf0c83 74
26b7305a 75async function updateVideoBlacklistController (req: express.Request, res: express.Response) {
dae86118 76 const videoBlacklist = res.locals.videoBlacklist
26b7305a
C
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
80fdaf06 84 return res.type('json').sendStatus(204)
26b7305a
C
85}
86
a8b666e9 87async function listBlacklist (req: express.Request, res: express.Response) {
e0a92917
RK
88 const resultList = await VideoBlacklistModel.listForApi({
89 start: req.query.start,
90 count: req.query.count,
91 sort: req.query.sort,
92 search: req.query.search,
93 type: req.query.type
94 })
eb080476 95
a8b666e9 96 return res.json(getFormattedObjects(resultList.data, resultList.total))
35bf0c83
C
97}
98
a8b666e9 99async function removeVideoFromBlacklistController (req: express.Request, res: express.Response) {
dae86118 100 const videoBlacklist = res.locals.videoBlacklist
453e83ea 101 const video = res.locals.videoAll
35bf0c83 102
80fdaf06 103 await unblacklistVideo(videoBlacklist, video)
7ccddd7b 104
453e83ea 105 logger.info('Video %s removed from blacklist.', video.uuid)
eb080476 106
80fdaf06 107 return res.type('json').sendStatus(204)
35bf0c83 108}