aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
Diffstat (limited to 'server')
-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
-rw-r--r--server/helpers/custom-validators/videos.ts34
-rw-r--r--server/lib/blacklist.ts20
-rw-r--r--server/lib/index.ts1
-rw-r--r--server/middlewares/validators/blacklist.ts35
-rw-r--r--server/middlewares/validators/index.ts2
-rw-r--r--server/middlewares/validators/video-blacklist.ts67
-rw-r--r--server/middlewares/validators/videos.ts58
-rw-r--r--server/tests/api/check-params/video-blacklist.ts12
-rw-r--r--server/tests/utils/video-blacklist.ts6
12 files changed, 161 insertions, 190 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}
diff --git a/server/helpers/custom-validators/videos.ts b/server/helpers/custom-validators/videos.ts
index a31aca019..05d1dc607 100644
--- a/server/helpers/custom-validators/videos.ts
+++ b/server/helpers/custom-validators/videos.ts
@@ -1,5 +1,7 @@
1import { values } from 'lodash' 1import { values } from 'lodash'
2import * as validator from 'validator' 2import * as validator from 'validator'
3import * as Promise from 'bluebird'
4import * as express from 'express'
3import 'express-validator' 5import 'express-validator'
4import 'multer' 6import 'multer'
5 7
@@ -8,10 +10,13 @@ import {
8 VIDEO_CATEGORIES, 10 VIDEO_CATEGORIES,
9 VIDEO_LICENCES, 11 VIDEO_LICENCES,
10 VIDEO_LANGUAGES, 12 VIDEO_LANGUAGES,
11 VIDEO_RATE_TYPES 13 VIDEO_RATE_TYPES,
14 database as db
12} from '../../initializers' 15} from '../../initializers'
13import { isUserUsernameValid } from './users' 16import { isUserUsernameValid } from './users'
14import { isArray, exists } from './misc' 17import { isArray, exists } from './misc'
18import { VideoInstance } from '../../models'
19import { logger } from '../../helpers'
15import { VideoRateType } from '../../../shared' 20import { VideoRateType } from '../../../shared'
16 21
17const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS 22const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS
@@ -138,6 +143,30 @@ function isVideoFileInfoHashValid (value: string) {
138 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH) 143 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH)
139} 144}
140 145
146function checkVideoExists (id: string, res: express.Response, callback: () => void) {
147 let promise: Promise<VideoInstance>
148 if (validator.isInt(id)) {
149 promise = db.Video.loadAndPopulateAuthorAndPodAndTags(+id)
150 } else { // UUID
151 promise = db.Video.loadByUUIDAndPopulateAuthorAndPodAndTags(id)
152 }
153
154 promise.then(video => {
155 if (!video) {
156 return res.status(404)
157 .json({ error: 'Video not found' })
158 .end()
159 }
160
161 res.locals.video = video
162 callback()
163 })
164 .catch(err => {
165 logger.error('Error in video request validator.', err)
166 return res.sendStatus(500)
167 })
168}
169
141// --------------------------------------------------------------------------- 170// ---------------------------------------------------------------------------
142 171
143export { 172export {
@@ -166,5 +195,6 @@ export {
166 isVideoDislikesValid, 195 isVideoDislikesValid,
167 isVideoEventCountValid, 196 isVideoEventCountValid,
168 isVideoFileSizeValid, 197 isVideoFileSizeValid,
169 isVideoFileResolutionValid 198 isVideoFileResolutionValid,
199 checkVideoExists
170} 200}
diff --git a/server/lib/blacklist.ts b/server/lib/blacklist.ts
deleted file mode 100644
index dcf8aa03c..000000000
--- a/server/lib/blacklist.ts
+++ /dev/null
@@ -1,20 +0,0 @@
1import { logger } from '../helpers'
2import { BlacklistedVideoInstance } from '../models'
3
4function removeVideoFromBlacklist (entry: BlacklistedVideoInstance) {
5 return entry.destroy()
6 .then(() => {
7 logger.info('Video removed from the blacklist')
8 })
9 .catch(err => {
10 logger.error('Some error while removing video from the blacklist.', err)
11 })
12}
13
14// ---------------------------------------------------------------------------
15
16export {
17 removeVideoFromBlacklist
18}
19
20// ---------------------------------------------------------------------------
diff --git a/server/lib/index.ts b/server/lib/index.ts
index df781f29f..8628da4dd 100644
--- a/server/lib/index.ts
+++ b/server/lib/index.ts
@@ -3,4 +3,3 @@ export * from './jobs'
3export * from './request' 3export * from './request'
4export * from './friends' 4export * from './friends'
5export * from './oauth-model' 5export * from './oauth-model'
6export * from './blacklist'
diff --git a/server/middlewares/validators/blacklist.ts b/server/middlewares/validators/blacklist.ts
deleted file mode 100644
index fe8fa40a4..000000000
--- a/server/middlewares/validators/blacklist.ts
+++ /dev/null
@@ -1,35 +0,0 @@
1import { param } from 'express-validator/check'
2import * as express from 'express'
3
4import { database as db } from '../../initializers/database'
5import { checkErrors } from './utils'
6import { logger } from '../../helpers'
7
8const blacklistRemoveValidator = [
9 param('id').isNumeric().not().isEmpty().withMessage('Should have a valid id'),
10
11 (req: express.Request, res: express.Response, next: express.NextFunction) => {
12 logger.debug('Checking blacklistRemove parameters.', { parameters: req.params })
13
14 checkErrors(req, res, () => {
15 db.BlacklistedVideo.loadById(req.params.id)
16 .then(entry => {
17 if (!entry) return res.status(404).send('Blacklisted video not found')
18
19 res.locals.blacklistEntryToRemove = entry
20
21 next()
22 })
23 .catch(err => {
24 logger.error('Error in blacklistRemove request validator', { error: err })
25 return res.sendStatus(500)
26 })
27 })
28 }
29]
30
31// ---------------------------------------------------------------------------
32
33export {
34 blacklistRemoveValidator
35}
diff --git a/server/middlewares/validators/index.ts b/server/middlewares/validators/index.ts
index a6198e22c..418fa5f1d 100644
--- a/server/middlewares/validators/index.ts
+++ b/server/middlewares/validators/index.ts
@@ -4,4 +4,4 @@ export * from './pods'
4export * from './sort' 4export * from './sort'
5export * from './users' 5export * from './users'
6export * from './videos' 6export * from './videos'
7export * from './blacklist' 7export * from './video-blacklist'
diff --git a/server/middlewares/validators/video-blacklist.ts b/server/middlewares/validators/video-blacklist.ts
new file mode 100644
index 000000000..30c6d4bd9
--- /dev/null
+++ b/server/middlewares/validators/video-blacklist.ts
@@ -0,0 +1,67 @@
1import { param } from 'express-validator/check'
2import * as express from 'express'
3
4import { database as db } from '../../initializers/database'
5import { checkErrors } from './utils'
6import { logger, isVideoIdOrUUIDValid, checkVideoExists } from '../../helpers'
7
8const videosBlacklistRemoveValidator = [
9 param('videoId').custom(isVideoIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
10
11 (req: express.Request, res: express.Response, next: express.NextFunction) => {
12 logger.debug('Checking blacklistRemove parameters.', { parameters: req.params })
13
14 checkErrors(req, res, () => {
15 checkVideoExists(req.params.videoId, res, () => {
16 checkVideoIsBlacklisted(req, res, next)
17 })
18 })
19 }
20]
21
22const videosBlacklistAddValidator = [
23 param('videoId').custom(isVideoIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
24
25 (req: express.Request, res: express.Response, next: express.NextFunction) => {
26 logger.debug('Checking videosBlacklist parameters', { parameters: req.params })
27
28 checkErrors(req, res, () => {
29 checkVideoExists(req.params.videoId, res, () => {
30 checkVideoIsBlacklistable(req, res, next)
31 })
32 })
33 }
34]
35
36// ---------------------------------------------------------------------------
37
38export {
39 videosBlacklistAddValidator,
40 videosBlacklistRemoveValidator
41}
42// ---------------------------------------------------------------------------
43
44function checkVideoIsBlacklistable (req: express.Request, res: express.Response, callback: () => void) {
45 if (res.locals.video.isOwned() === true) {
46 return res.status(403)
47 .json({ error: 'Cannot blacklist a local video' })
48 .end()
49 }
50
51 callback()
52}
53
54function checkVideoIsBlacklisted (req: express.Request, res: express.Response, callback: () => void) {
55 db.BlacklistedVideo.loadByVideoId(res.locals.video.id)
56 .then(blacklistedVideo => {
57 if (!blacklistedVideo) return res.status(404).send('Blacklisted video not found')
58
59 res.locals.blacklistedVideo = blacklistedVideo
60
61 callback()
62 })
63 .catch(err => {
64 logger.error('Error in blacklistRemove request validator', { error: err })
65 return res.sendStatus(500)
66 })
67}
diff --git a/server/middlewares/validators/videos.ts b/server/middlewares/validators/videos.ts
index 5f213f974..deed07524 100644
--- a/server/middlewares/validators/videos.ts
+++ b/server/middlewares/validators/videos.ts
@@ -1,7 +1,5 @@
1import { body, param, query } from 'express-validator/check' 1import { body, param, query } from 'express-validator/check'
2import * as express from 'express' 2import * as express from 'express'
3import * as Promise from 'bluebird'
4import * as validator from 'validator'
5 3
6import { database as db } from '../../initializers/database' 4import { database as db } from '../../initializers/database'
7import { checkErrors } from './utils' 5import { checkErrors } from './utils'
@@ -20,9 +18,9 @@ import {
20 isVideoIdOrUUIDValid, 18 isVideoIdOrUUIDValid,
21 isVideoAbuseReasonValid, 19 isVideoAbuseReasonValid,
22 isVideoRatingTypeValid, 20 isVideoRatingTypeValid,
23 getDurationFromVideoFile 21 getDurationFromVideoFile,
22 checkVideoExists
24} from '../../helpers' 23} from '../../helpers'
25import { VideoInstance } from '../../models'
26 24
27const videosAddValidator = [ 25const videosAddValidator = [
28 body('videofile').custom((value, { req }) => isVideoFile(req.files)).withMessage('Should have a valid file'), 26 body('videofile').custom((value, { req }) => isVideoFile(req.files)).withMessage('Should have a valid file'),
@@ -186,20 +184,6 @@ const videoRateValidator = [
186 } 184 }
187] 185]
188 186
189const videosBlacklistValidator = [
190 param('id').custom(isVideoIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
191
192 (req: express.Request, res: express.Response, next: express.NextFunction) => {
193 logger.debug('Checking videosBlacklist parameters', { parameters: req.params })
194
195 checkErrors(req, res, () => {
196 checkVideoExists(req.params.id, res, () => {
197 checkVideoIsBlacklistable(req, res, next)
198 })
199 })
200 }
201]
202
203// --------------------------------------------------------------------------- 187// ---------------------------------------------------------------------------
204 188
205export { 189export {
@@ -211,37 +195,11 @@ export {
211 195
212 videoAbuseReportValidator, 196 videoAbuseReportValidator,
213 197
214 videoRateValidator, 198 videoRateValidator
215
216 videosBlacklistValidator
217} 199}
218 200
219// --------------------------------------------------------------------------- 201// ---------------------------------------------------------------------------
220 202
221function checkVideoExists (id: string, res: express.Response, callback: () => void) {
222 let promise: Promise<VideoInstance>
223 if (validator.isInt(id)) {
224 promise = db.Video.loadAndPopulateAuthorAndPodAndTags(+id)
225 } else { // UUID
226 promise = db.Video.loadByUUIDAndPopulateAuthorAndPodAndTags(id)
227 }
228
229 promise.then(video => {
230 if (!video) {
231 return res.status(404)
232 .json({ error: 'Video not found' })
233 .end()
234 }
235
236 res.locals.video = video
237 callback()
238 })
239 .catch(err => {
240 logger.error('Error in video request validator.', err)
241 return res.sendStatus(500)
242 })
243}
244
245function checkUserCanDeleteVideo (userId: number, res: express.Response, callback: () => void) { 203function checkUserCanDeleteVideo (userId: number, res: express.Response, callback: () => void) {
246 // Retrieve the user who did the request 204 // Retrieve the user who did the request
247 db.User.loadById(userId) 205 db.User.loadById(userId)
@@ -269,13 +227,3 @@ function checkUserCanDeleteVideo (userId: number, res: express.Response, callbac
269 return res.sendStatus(500) 227 return res.sendStatus(500)
270 }) 228 })
271} 229}
272
273function checkVideoIsBlacklistable (req: express.Request, res: express.Response, callback: () => void) {
274 if (res.locals.video.isOwned() === true) {
275 return res.status(403)
276 .json({ error: 'Cannot blacklist a local video' })
277 .end()
278 }
279
280 callback()
281}
diff --git a/server/tests/api/check-params/video-blacklist.ts b/server/tests/api/check-params/video-blacklist.ts
index 80e6f8011..eb16b3af0 100644
--- a/server/tests/api/check-params/video-blacklist.ts
+++ b/server/tests/api/check-params/video-blacklist.ts
@@ -81,10 +81,10 @@ describe('Test video blacklist API validators', function () {
81 }) 81 })
82 82
83 describe('When removing a video in blacklist', function () { 83 describe('When removing a video in blacklist', function () {
84 const basePath = '/api/v1/blacklist/' 84 const basePath = '/api/v1/videos/'
85 85
86 it('Should fail with a non authenticated user', async function () { 86 it('Should fail with a non authenticated user', async function () {
87 const path = basePath + server.video.id 87 const path = basePath + server.video.id + '/blacklist'
88 88
89 await request(server.url) 89 await request(server.url)
90 .delete(path) 90 .delete(path)
@@ -94,7 +94,7 @@ describe('Test video blacklist API validators', function () {
94 }) 94 })
95 95
96 it('Should fail with a non admin user', async function () { 96 it('Should fail with a non admin user', async function () {
97 const path = basePath + server.video.id 97 const path = basePath + server.video.id + '/blacklist'
98 98
99 await request(server.url) 99 await request(server.url)
100 .delete(path) 100 .delete(path)
@@ -104,7 +104,7 @@ describe('Test video blacklist API validators', function () {
104 }) 104 })
105 105
106 it('Should fail with an incorrect id', async function () { 106 it('Should fail with an incorrect id', async function () {
107 const path = basePath + 'foobar' 107 const path = basePath + 'foobar/blacklist'
108 108
109 await request(server.url) 109 await request(server.url)
110 .delete(path) 110 .delete(path)
@@ -115,7 +115,7 @@ describe('Test video blacklist API validators', function () {
115 115
116 it('Should fail with a not blacklisted video', async function () { 116 it('Should fail with a not blacklisted video', async function () {
117 // The video was not added to the blacklist so it should fail 117 // The video was not added to the blacklist so it should fail
118 const path = basePath + server.video.id 118 const path = basePath + server.video.id + '/blacklist'
119 119
120 await request(server.url) 120 await request(server.url)
121 .delete(path) 121 .delete(path)
@@ -126,7 +126,7 @@ describe('Test video blacklist API validators', function () {
126 }) 126 })
127 127
128 describe('When listing videos in blacklist', function () { 128 describe('When listing videos in blacklist', function () {
129 const basePath = '/api/v1/blacklist/' 129 const basePath = '/api/v1/videos/blacklist/'
130 130
131 it('Should fail with a non authenticated user', async function () { 131 it('Should fail with a non authenticated user', async function () {
132 const path = basePath 132 const path = basePath
diff --git a/server/tests/utils/video-blacklist.ts b/server/tests/utils/video-blacklist.ts
index 5729d13d8..3a499f46a 100644
--- a/server/tests/utils/video-blacklist.ts
+++ b/server/tests/utils/video-blacklist.ts
@@ -11,7 +11,7 @@ function addVideoToBlacklist (url: string, token: string, videoId: number, speci
11} 11}
12 12
13function removeVideoFromBlacklist (url: string, token: string, videoId: number, specialStatus = 204) { 13function removeVideoFromBlacklist (url: string, token: string, videoId: number, specialStatus = 204) {
14 const path = '/api/v1/blacklist/' + videoId 14 const path = '/api/v1/videos/' + videoId + '/blacklist'
15 15
16 return request(url) 16 return request(url)
17 .delete(path) 17 .delete(path)
@@ -21,7 +21,7 @@ function removeVideoFromBlacklist (url: string, token: string, videoId: number,
21} 21}
22 22
23function getBlacklistedVideosList (url: string, token: string, specialStatus = 200) { 23function getBlacklistedVideosList (url: string, token: string, specialStatus = 200) {
24 const path = '/api/v1/blacklist/' 24 const path = '/api/v1/videos/blacklist/'
25 25
26 return request(url) 26 return request(url)
27 .get(path) 27 .get(path)
@@ -33,7 +33,7 @@ function getBlacklistedVideosList (url: string, token: string, specialStatus = 2
33} 33}
34 34
35function getSortedBlacklistedVideosList (url: string, token: string, sort: string, specialStatus = 200) { 35function getSortedBlacklistedVideosList (url: string, token: string, sort: string, specialStatus = 200) {
36 const path = '/api/v1/blacklist/' 36 const path = '/api/v1/videos/blacklist/'
37 37
38 return request(url) 38 return request(url)
39 .get(path) 39 .get(path)