From babecc3c09cd4ed06fe643a97fff4bcc31c5a9be Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 16 Nov 2018 15:38:09 +0100 Subject: [PATCH] Fix AP collections pagination --- server/controllers/activitypub/client.ts | 4 ++-- server/helpers/activitypub.ts | 14 +++++++------- server/models/activitypub/actor-follow.ts | 4 ++-- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/server/controllers/activitypub/client.ts b/server/controllers/activitypub/client.ts index a342a48d4..d9d385460 100644 --- a/server/controllers/activitypub/client.ts +++ b/server/controllers/activitypub/client.ts @@ -298,7 +298,7 @@ async function actorFollowing (req: express.Request, actor: ActorModel) { return ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count) } - return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, handler, req.query.page) + return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.path, handler, req.query.page) } async function actorFollowers (req: express.Request, actor: ActorModel) { @@ -306,7 +306,7 @@ async function actorFollowers (req: express.Request, actor: ActorModel) { return ActorFollowModel.listAcceptedFollowerUrlsForApi([ actor.id ], undefined, start, count) } - return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, handler, req.query.page) + return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.path, handler, req.query.page) } function videoRates (req: express.Request, rateType: VideoRateType, video: VideoModel, url: string) { diff --git a/server/helpers/activitypub.ts b/server/helpers/activitypub.ts index 4bf6e387d..bcbd9be59 100644 --- a/server/helpers/activitypub.ts +++ b/server/helpers/activitypub.ts @@ -57,16 +57,16 @@ function activityPubContextify (data: T) { } type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird> | Promise> -async function activityPubCollectionPagination (url: string, handler: ActivityPubCollectionPaginationHandler, page?: any) { +async function activityPubCollectionPagination (baseUrl: 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, + id: baseUrl, type: 'OrderedCollection', totalItems: result.total, - first: url + '?page=1' + first: baseUrl + '?page=1' } } @@ -81,19 +81,19 @@ async function activityPubCollectionPagination (url: string, handler: ActivityPu // There are more results if (result.total > page * ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) { - next = url + '?page=' + (page + 1) + next = baseUrl + '?page=' + (page + 1) } if (page > 1) { - prev = url + '?page=' + (page - 1) + prev = baseUrl + '?page=' + (page - 1) } return { - id: url + '?page=' + page, + id: baseUrl + '?page=' + page, type: 'OrderedCollectionPage', prev, next, - partOf: url, + partOf: baseUrl, orderedItems: result.data, totalItems: result.total } diff --git a/server/models/activitypub/actor-follow.ts b/server/models/activitypub/actor-follow.ts index 3373355ef..0a6935083 100644 --- a/server/models/activitypub/actor-follow.ts +++ b/server/models/activitypub/actor-follow.ts @@ -509,12 +509,12 @@ export class ActorFollowModel extends Model { tasks.push(ActorFollowModel.sequelize.query(query, options)) } - const [ followers, [ { total } ] ] = await Promise.all(tasks) + const [ followers, [ dataTotal ] ] = await Promise.all(tasks) const urls: string[] = followers.map(f => f.url) return { data: urls, - total: parseInt(total, 10) + total: dataTotal ? parseInt(dataTotal.total, 10) : 0 } }