From 8fffe21a7bc96d08b229293d66ddba576e609790 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 25 May 2018 16:21:16 +0200 Subject: Refractor and optimize AP collections Only display urls in general object, and paginate video comments, shares, likes and dislikes --- server/helpers/activitypub.ts | 44 +++++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 23 deletions(-) (limited to 'server/helpers') diff --git a/server/helpers/activitypub.ts b/server/helpers/activitypub.ts index 1934fa0f0..d1f3ec02d 100644 --- a/server/helpers/activitypub.ts +++ b/server/helpers/activitypub.ts @@ -1,8 +1,11 @@ +import * as Bluebird from 'bluebird' +import * as validator from 'validator' import { ResultList } from '../../shared/models' import { Activity, ActivityPubActor } from '../../shared/models/activitypub' import { ACTIVITY_PUB } from '../initializers' import { ActorModel } from '../models/activitypub/actor' import { signObject } from './peertube-crypto' +import { pageToStartAndCount } from './core-utils' function activityPubContextify (data: T) { return Object.assign(data,{ @@ -44,16 +47,23 @@ function activityPubContextify (data: T) { }) } -function activityPubCollection (url: string, results: any[]) { - return { - id: url, - type: 'OrderedCollection', - totalItems: results.length, - orderedItems: results +type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird> | Promise> +async function activityPubCollectionPagination (url: string, handler: ActivityPubCollectionPaginationHandler, page?: any) { + if (!page || !validator.isInt(page)) { + // We just display the first page URL, we only need the total items + const result = await handler(0, 1) + + return { + id: url, + type: 'OrderedCollection', + totalItems: result.total, + first: url + '?page=1' + } } -} -function activityPubCollectionPagination (url: string, page: any, result: ResultList) { + const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) + const result = await handler(start, count) + let next: string let prev: string @@ -69,27 +79,16 @@ function activityPubCollectionPagination (url: string, page: any, result: Result prev = url + '?page=' + (page - 1) } - const orderedCollectionPagination = { + return { id: url + '?page=' + page, type: 'OrderedCollectionPage', prev, next, partOf: url, - orderedItems: result.data - } - - if (page === 1) { - return activityPubContextify({ - id: url, - type: 'OrderedCollection', - totalItems: result.total, - first: orderedCollectionPagination - }) - } else { - orderedCollectionPagination['totalItems'] = result.total + orderedItems: result.data, + totalItems: result.total } - return orderedCollectionPagination } function buildSignedActivity (byActor: ActorModel, data: Object) { @@ -110,6 +109,5 @@ export { getActorUrl, activityPubContextify, activityPubCollectionPagination, - activityPubCollection, buildSignedActivity } -- cgit v1.2.3