diff options
-rw-r--r-- | server/controllers/activitypub/outbox.ts | 5 | ||||
-rw-r--r-- | server/helpers/activitypub.ts | 11 | ||||
-rw-r--r-- | server/middlewares/validators/activitypub/index.ts | 1 | ||||
-rw-r--r-- | server/middlewares/validators/activitypub/pagination.ts | 23 |
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 | |||
9 | import { VideoModel } from '../../models/video/video' | 9 | import { VideoModel } from '../../models/video/video' |
10 | import { activityPubResponse } from './utils' | 10 | import { activityPubResponse } from './utils' |
11 | import { MActorLight } from '@server/typings/models' | 11 | import { MActorLight } from '@server/typings/models' |
12 | import { apPaginationValidator } from '../../middlewares/validators/activitypub' | ||
12 | 13 | ||
13 | const outboxRouter = express.Router() | 14 | const outboxRouter = express.Router() |
14 | 15 | ||
15 | outboxRouter.get('/accounts/:name/outbox', | 16 | outboxRouter.get('/accounts/:name/outbox', |
17 | apPaginationValidator, | ||
16 | localAccountValidator, | 18 | localAccountValidator, |
17 | asyncMiddleware(outboxController) | 19 | asyncMiddleware(outboxController) |
18 | ) | 20 | ) |
19 | 21 | ||
20 | outboxRouter.get('/video-channels/:name/outbox', | 22 | outboxRouter.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 | ||
102 | type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>> | 102 | type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>> |
103 | async function activityPubCollectionPagination (baseUrl: string, handler: ActivityPubCollectionPaginationHandler, page?: any) { | 103 | async 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 @@ | |||
1 | export * from './activity' | 1 | export * from './activity' |
2 | export * from './signature' | 2 | export * from './signature' |
3 | export * 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 @@ | |||
1 | import * as express from 'express' | ||
2 | import { query } from 'express-validator' | ||
3 | import { logger } from '../../../helpers/logger' | ||
4 | import { areValidationErrors } from '../utils' | ||
5 | |||
6 | const 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 | |||
21 | export { | ||
22 | apPaginationValidator | ||
23 | } | ||