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