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