]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/videos/blacklist.ts
8112b59b804464e6eb0fc3653e81d8d6f5876fcc
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / blacklist.ts
1 import * as express from 'express'
2 import { BlacklistedVideo, UserRight } from '../../../../shared'
3 import { logger } from '../../../helpers/logger'
4 import { getFormattedObjects } from '../../../helpers/utils'
5 import {
6 asyncMiddleware, authenticate, blacklistSortValidator, ensureUserHasRight, paginationValidator, setBlacklistSort, setDefaultPagination,
7 videosBlacklistAddValidator, videosBlacklistRemoveValidator
8 } from '../../../middlewares'
9 import { VideoBlacklistModel } from '../../../models/video/video-blacklist'
10
11 const blacklistRouter = express.Router()
12
13 blacklistRouter.post('/:videoId/blacklist',
14 authenticate,
15 ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
16 asyncMiddleware(videosBlacklistAddValidator),
17 asyncMiddleware(addVideoToBlacklist)
18 )
19
20 blacklistRouter.get('/blacklist',
21 authenticate,
22 ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
23 paginationValidator,
24 blacklistSortValidator,
25 setBlacklistSort,
26 setDefaultPagination,
27 asyncMiddleware(listBlacklist)
28 )
29
30 blacklistRouter.delete('/:videoId/blacklist',
31 authenticate,
32 ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
33 asyncMiddleware(videosBlacklistRemoveValidator),
34 asyncMiddleware(removeVideoFromBlacklistController)
35 )
36
37 // ---------------------------------------------------------------------------
38
39 export {
40 blacklistRouter
41 }
42
43 // ---------------------------------------------------------------------------
44
45 async function addVideoToBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) {
46 const videoInstance = res.locals.video
47
48 const toCreate = {
49 videoId: videoInstance.id
50 }
51
52 await VideoBlacklistModel.create(toCreate)
53 return res.type('json').status(204).end()
54 }
55
56 async function listBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) {
57 const resultList = await VideoBlacklistModel.listForApi(req.query.start, req.query.count, req.query.sort)
58
59 return res.json(getFormattedObjects<BlacklistedVideo, VideoBlacklistModel>(resultList.data, resultList.total))
60 }
61
62 async function removeVideoFromBlacklistController (req: express.Request, res: express.Response, next: express.NextFunction) {
63 const blacklistedVideo = res.locals.blacklistedVideo as VideoBlacklistModel
64
65 try {
66 await blacklistedVideo.destroy()
67
68 logger.info('Video %s removed from blacklist.', res.locals.video.uuid)
69
70 return res.sendStatus(204)
71 } catch (err) {
72 logger.error('Some error while removing video %s from blacklist.', res.locals.video.uuid, { err })
73 throw err
74 }
75 }