aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
authorRigel Kent <sendmemail@rigelk.eu>2020-01-09 00:43:52 +0100
committerChocobozzz <chocobozzz@cpy.re>2020-01-09 09:21:35 +0100
commitfbc77eb648bda9add4634c08dbb6af48c3670b5d (patch)
treecb2c32e33031490027210cf7e29cc98b519b2f83 /server
parentc08579e14fe712cb2264a25daea956e682574335 (diff)
downloadPeerTube-fbc77eb648bda9add4634c08dbb6af48c3670b5d.tar.gz
PeerTube-fbc77eb648bda9add4634c08dbb6af48c3670b5d.tar.zst
PeerTube-fbc77eb648bda9add4634c08dbb6af48c3670b5d.zip
Add outbox page size parameter
Diffstat (limited to 'server')
-rw-r--r--server/controllers/activitypub/outbox.ts5
-rw-r--r--server/helpers/activitypub.ts11
-rw-r--r--server/middlewares/validators/activitypub/index.ts1
-rw-r--r--server/middlewares/validators/activitypub/pagination.ts23
4 files changed, 36 insertions, 4 deletions
diff --git a/server/controllers/activitypub/outbox.ts b/server/controllers/activitypub/outbox.ts
index f3dd2ad7d..916a110a8 100644
--- a/server/controllers/activitypub/outbox.ts
+++ b/server/controllers/activitypub/outbox.ts
@@ -9,15 +9,18 @@ import { asyncMiddleware, localAccountValidator, localVideoChannelValidator } fr
9import { VideoModel } from '../../models/video/video' 9import { VideoModel } from '../../models/video/video'
10import { activityPubResponse } from './utils' 10import { activityPubResponse } from './utils'
11import { MActorLight } from '@server/typings/models' 11import { MActorLight } from '@server/typings/models'
12import { apPaginationValidator } from '../../middlewares/validators/activitypub'
12 13
13const outboxRouter = express.Router() 14const outboxRouter = express.Router()
14 15
15outboxRouter.get('/accounts/:name/outbox', 16outboxRouter.get('/accounts/:name/outbox',
17 apPaginationValidator,
16 localAccountValidator, 18 localAccountValidator,
17 asyncMiddleware(outboxController) 19 asyncMiddleware(outboxController)
18) 20)
19 21
20outboxRouter.get('/video-channels/:name/outbox', 22outboxRouter.get('/video-channels/:name/outbox',
23 apPaginationValidator,
21 localVideoChannelValidator, 24 localVideoChannelValidator,
22 asyncMiddleware(outboxController) 25 asyncMiddleware(outboxController)
23) 26)
@@ -38,7 +41,7 @@ async function outboxController (req: express.Request, res: express.Response) {
38 logger.info('Receiving outbox request for %s.', actorOutboxUrl) 41 logger.info('Receiving outbox request for %s.', actorOutboxUrl)
39 42
40 const handler = (start: number, count: number) => buildActivities(actor, start, count) 43 const handler = (start: number, count: number) => buildActivities(actor, start, count)
41 const json = await activityPubCollectionPagination(actorOutboxUrl, handler, req.query.page) 44 const json = await activityPubCollectionPagination(actorOutboxUrl, handler, req.query.page, req.query.size)
42 45
43 return activityPubResponse(activityPubContextify(json), res) 46 return activityPubResponse(activityPubContextify(json), res)
44} 47}
diff --git a/server/helpers/activitypub.ts b/server/helpers/activitypub.ts
index 735f2d73a..239d8291d 100644
--- a/server/helpers/activitypub.ts
+++ b/server/helpers/activitypub.ts
@@ -100,7 +100,12 @@ function activityPubContextify <T> (data: T) {
100} 100}
101 101
102type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>> 102type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>>
103async function activityPubCollectionPagination (baseUrl: string, handler: ActivityPubCollectionPaginationHandler, page?: any) { 103async function activityPubCollectionPagination (
104 baseUrl: string,
105 handler: ActivityPubCollectionPaginationHandler,
106 page?: any,
107 size = ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE
108) {
104 if (!page || !validator.isInt(page)) { 109 if (!page || !validator.isInt(page)) {
105 // We just display the first page URL, we only need the total items 110 // We just display the first page URL, we only need the total items
106 const result = await handler(0, 1) 111 const result = await handler(0, 1)
@@ -113,7 +118,7 @@ async function activityPubCollectionPagination (baseUrl: string, handler: Activi
113 } 118 }
114 } 119 }
115 120
116 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) 121 const { start, count } = pageToStartAndCount(page, size)
117 const result = await handler(start, count) 122 const result = await handler(start, count)
118 123
119 let next: string | undefined 124 let next: string | undefined
@@ -123,7 +128,7 @@ async function activityPubCollectionPagination (baseUrl: string, handler: Activi
123 page = parseInt(page, 10) 128 page = parseInt(page, 10)
124 129
125 // There are more results 130 // There are more results
126 if (result.total > page * ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) { 131 if (result.total > page * size) {
127 next = baseUrl + '?page=' + (page + 1) 132 next = baseUrl + '?page=' + (page + 1)
128 } 133 }
129 134
diff --git a/server/middlewares/validators/activitypub/index.ts b/server/middlewares/validators/activitypub/index.ts
index 84d1107fc..159338d26 100644
--- a/server/middlewares/validators/activitypub/index.ts
+++ b/server/middlewares/validators/activitypub/index.ts
@@ -1,2 +1,3 @@
1export * from './activity' 1export * from './activity'
2export * from './signature' 2export * from './signature'
3export * from './pagination'
diff --git a/server/middlewares/validators/activitypub/pagination.ts b/server/middlewares/validators/activitypub/pagination.ts
new file mode 100644
index 000000000..8b32d3415
--- /dev/null
+++ b/server/middlewares/validators/activitypub/pagination.ts
@@ -0,0 +1,23 @@
1import * as express from 'express'
2import { query } from 'express-validator'
3import { logger } from '../../../helpers/logger'
4import { areValidationErrors } from '../utils'
5
6const apPaginationValidator = [
7 query('page').optional().isInt({ min: 1 }).withMessage('Should have a valid page number'),
8 query('size').optional().isInt({ max: 50 }).withMessage('Should have a valid page size (max: 50)'),
9
10 (req: express.Request, res: express.Response, next: express.NextFunction) => {
11 logger.debug('Checking pagination parameters', { parameters: req.query })
12
13 if (areValidationErrors(req, res)) return
14
15 return next()
16 }
17]
18
19// ---------------------------------------------------------------------------
20
21export {
22 apPaginationValidator
23}