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)
)
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)
}
}
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)
}
}
- 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
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)
}
export * from './activity'
export * from './signature'
+export * from './pagination'
--- /dev/null
+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
+}