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