aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-10-10 10:02:18 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-10-10 10:18:16 +0200
commit35bf0c83c80f59ca79f4b84fac8700f17adeb22d (patch)
treea9b2106096d6ba04d7219051b17fd32cfbe6a885 /server/controllers
parent769d332177a5b02d5c2ffc134687d3b4ed65bae9 (diff)
downloadPeerTube-35bf0c83c80f59ca79f4b84fac8700f17adeb22d.tar.gz
PeerTube-35bf0c83c80f59ca79f4b84fac8700f17adeb22d.tar.zst
PeerTube-35bf0c83c80f59ca79f4b84fac8700f17adeb22d.zip
Video blacklist refractoring
Diffstat (limited to 'server/controllers')
-rw-r--r--server/controllers/api/blacklist.ts60
-rw-r--r--server/controllers/api/index.ts2
-rw-r--r--server/controllers/api/videos/blacklist.ts54
3 files changed, 49 insertions, 67 deletions
diff --git a/server/controllers/api/blacklist.ts b/server/controllers/api/blacklist.ts
deleted file mode 100644
index 9b2d8017e..000000000
--- a/server/controllers/api/blacklist.ts
+++ /dev/null
@@ -1,60 +0,0 @@
1import * as express from 'express'
2
3import { database } from '../../initializers'
4import { getFormattedObjects } from '../../helpers'
5import { BlacklistedVideo } from '../../../shared'
6import { BlacklistedVideoInstance } from '../../models'
7
8import {
9 removeVideoFromBlacklist
10} from '../../lib'
11import {
12 authenticate,
13 ensureIsAdmin,
14 paginationValidator,
15 blacklistSortValidator,
16 setBlacklistSort,
17 setPagination,
18 blacklistRemoveValidator
19} from '../../middlewares'
20
21const blacklistRouter = express.Router()
22
23blacklistRouter.get('/',
24 authenticate,
25 ensureIsAdmin,
26 paginationValidator,
27 blacklistSortValidator,
28 setBlacklistSort,
29 setPagination,
30 listBlacklist
31)
32
33blacklistRouter.delete('/:id',
34 authenticate,
35 ensureIsAdmin,
36 blacklistRemoveValidator,
37 removeVideoFromBlacklistController
38)
39
40// ---------------------------------------------------------------------------
41
42export {
43 blacklistRouter
44}
45
46// ---------------------------------------------------------------------------
47
48function listBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) {
49 database.BlacklistedVideo.listForApi(req.query.start, req.query.count, req.query.sort)
50 .then(resultList => res.json(getFormattedObjects<BlacklistedVideo, BlacklistedVideoInstance>(resultList.data, resultList.total)))
51 .catch(err => next(err))
52}
53
54function removeVideoFromBlacklistController (req: express.Request, res: express.Response, next: express.NextFunction) {
55 const entry = res.locals.blacklistEntryToRemove as BlacklistedVideoInstance
56
57 removeVideoFromBlacklist(entry)
58 .then(() => res.sendStatus(204))
59 .catch(err => next(err))
60}
diff --git a/server/controllers/api/index.ts b/server/controllers/api/index.ts
index fdc887915..a9205b33c 100644
--- a/server/controllers/api/index.ts
+++ b/server/controllers/api/index.ts
@@ -9,7 +9,6 @@ import { remoteRouter } from './remote'
9import { requestSchedulerRouter } from './request-schedulers' 9import { requestSchedulerRouter } from './request-schedulers'
10import { usersRouter } from './users' 10import { usersRouter } from './users'
11import { videosRouter } from './videos' 11import { videosRouter } from './videos'
12import { blacklistRouter } from './blacklist'
13 12
14const apiRouter = express.Router() 13const apiRouter = express.Router()
15 14
@@ -20,7 +19,6 @@ apiRouter.use('/remote', remoteRouter)
20apiRouter.use('/request-schedulers', requestSchedulerRouter) 19apiRouter.use('/request-schedulers', requestSchedulerRouter)
21apiRouter.use('/users', usersRouter) 20apiRouter.use('/users', usersRouter)
22apiRouter.use('/videos', videosRouter) 21apiRouter.use('/videos', videosRouter)
23apiRouter.use('/blacklist', blacklistRouter)
24apiRouter.use('/ping', pong) 22apiRouter.use('/ping', pong)
25apiRouter.use('/*', badRequest) 23apiRouter.use('/*', badRequest)
26 24
diff --git a/server/controllers/api/videos/blacklist.ts b/server/controllers/api/videos/blacklist.ts
index d8f2068ec..66311598e 100644
--- a/server/controllers/api/videos/blacklist.ts
+++ b/server/controllers/api/videos/blacklist.ts
@@ -1,22 +1,46 @@
1import * as express from 'express' 1import * as express from 'express'
2 2
3import { database as db } from '../../../initializers/database' 3import { database as db } from '../../../initializers'
4import { logger } from '../../../helpers' 4import { logger, getFormattedObjects } from '../../../helpers'
5import { 5import {
6 authenticate, 6 authenticate,
7 ensureIsAdmin, 7 ensureIsAdmin,
8 videosBlacklistValidator 8 videosBlacklistAddValidator,
9 videosBlacklistRemoveValidator,
10 paginationValidator,
11 blacklistSortValidator,
12 setBlacklistSort,
13 setPagination
9} from '../../../middlewares' 14} from '../../../middlewares'
15import { BlacklistedVideoInstance } from '../../../models'
16import { BlacklistedVideo } from '../../../../shared'
10 17
11const blacklistRouter = express.Router() 18const blacklistRouter = express.Router()
12 19
13blacklistRouter.post('/:id/blacklist', 20blacklistRouter.post('/:videoId/blacklist',
14 authenticate, 21 authenticate,
15 ensureIsAdmin, 22 ensureIsAdmin,
16 videosBlacklistValidator, 23 videosBlacklistAddValidator,
17 addVideoToBlacklist 24 addVideoToBlacklist
18) 25)
19 26
27blacklistRouter.get('/blacklist',
28 authenticate,
29 ensureIsAdmin,
30 paginationValidator,
31 blacklistSortValidator,
32 setBlacklistSort,
33 setPagination,
34 listBlacklist
35)
36
37blacklistRouter.delete('/:videoId/blacklist',
38 authenticate,
39 ensureIsAdmin,
40 videosBlacklistRemoveValidator,
41 removeVideoFromBlacklistController
42)
43
20// --------------------------------------------------------------------------- 44// ---------------------------------------------------------------------------
21 45
22export { 46export {
@@ -39,3 +63,23 @@ function addVideoToBlacklist (req: express.Request, res: express.Response, next:
39 return next(err) 63 return next(err)
40 }) 64 })
41} 65}
66
67function 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
73function 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}