Add outbox page size parameter
authorRigel Kent <sendmemail@rigelk.eu>
Wed, 8 Jan 2020 23:43:52 +0000 (00:43 +0100)
committerChocobozzz <chocobozzz@cpy.re>
Thu, 9 Jan 2020 08:21:35 +0000 (09:21 +0100)
server/controllers/activitypub/outbox.ts
server/helpers/activitypub.ts
server/middlewares/validators/activitypub/index.ts
server/middlewares/validators/activitypub/pagination.ts [new file with mode: 0644]

index f3dd2ad7d3e5fed8e1be89003813054cde20a24b..916a110a80aa24814e1b02921f1cc6125f3b8cbc 100644 (file)
@@ -9,15 +9,18 @@ import { asyncMiddleware, localAccountValidator, localVideoChannelValidator } fr
 import { VideoModel } from '../../models/video/video'
 import { activityPubResponse } from './utils'
 import { MActorLight } from '@server/typings/models'
+import { apPaginationValidator } from '../../middlewares/validators/activitypub'
 
 const outboxRouter = express.Router()
 
 outboxRouter.get('/accounts/:name/outbox',
+  apPaginationValidator,
   localAccountValidator,
   asyncMiddleware(outboxController)
 )
 
 outboxRouter.get('/video-channels/:name/outbox',
+  apPaginationValidator,
   localVideoChannelValidator,
   asyncMiddleware(outboxController)
 )
@@ -38,7 +41,7 @@ async function outboxController (req: express.Request, res: express.Response) {
   logger.info('Receiving outbox request for %s.', actorOutboxUrl)
 
   const handler = (start: number, count: number) => buildActivities(actor, start, count)
-  const json = await activityPubCollectionPagination(actorOutboxUrl, handler, req.query.page)
+  const json = await activityPubCollectionPagination(actorOutboxUrl, handler, req.query.page, req.query.size)
 
   return activityPubResponse(activityPubContextify(json), res)
 }
index 735f2d73adc8e802d674c3b13c8e8d64438fb142..239d8291d8ef9936b4cb183f020eec0c45f44546 100644 (file)
@@ -100,7 +100,12 @@ function activityPubContextify <T> (data: T) {
 }
 
 type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>>
-async function activityPubCollectionPagination (baseUrl: string, handler: ActivityPubCollectionPaginationHandler, page?: any) {
+async function activityPubCollectionPagination (
+  baseUrl: string,
+  handler: ActivityPubCollectionPaginationHandler,
+  page?: any,
+  size = ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE
+) {
   if (!page || !validator.isInt(page)) {
     // We just display the first page URL, we only need the total items
     const result = await handler(0, 1)
@@ -113,7 +118,7 @@ async function activityPubCollectionPagination (baseUrl: string, handler: Activi
     }
   }
 
-  const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
+  const { start, count } = pageToStartAndCount(page, size)
   const result = await handler(start, count)
 
   let next: string | undefined
@@ -123,7 +128,7 @@ async function activityPubCollectionPagination (baseUrl: string, handler: Activi
   page = parseInt(page, 10)
 
   // There are more results
-  if (result.total > page * ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) {
+  if (result.total > page * size) {
     next = baseUrl + '?page=' + (page + 1)
   }
 
index 84d1107fcd047c4ee3f6e74531717c5da16d3376..159338d2625d40320d9d4a7ca4e402df64fa001a 100644 (file)
@@ -1,2 +1,3 @@
 export * from './activity'
 export * from './signature'
+export * from './pagination'
diff --git a/server/middlewares/validators/activitypub/pagination.ts b/server/middlewares/validators/activitypub/pagination.ts
new file mode 100644 (file)
index 0000000..8b32d34
--- /dev/null
@@ -0,0 +1,23 @@
+import * as express from 'express'
+import { query } from 'express-validator'
+import { logger } from '../../../helpers/logger'
+import { areValidationErrors } from '../utils'
+
+const apPaginationValidator = [
+  query('page').optional().isInt({ min: 1 }).withMessage('Should have a valid page number'),
+  query('size').optional().isInt({ max: 50 }).withMessage('Should have a valid page size (max: 50)'),
+
+  (req: express.Request, res: express.Response, next: express.NextFunction) => {
+    logger.debug('Checking pagination parameters', { parameters: req.query })
+
+    if (areValidationErrors(req, res)) return
+
+    return next()
+  }
+]
+
+// ---------------------------------------------------------------------------
+
+export {
+  apPaginationValidator
+}