Fix AP collections pagination

This commit is contained in:
Chocobozzz 2018-11-16 15:38:09 +01:00
parent 8d4273463f
commit babecc3c09
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
3 changed files with 11 additions and 11 deletions

View File

@ -298,7 +298,7 @@ async function actorFollowing (req: express.Request, actor: ActorModel) {
return ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count) 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) { 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 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) { function videoRates (req: express.Request, rateType: VideoRateType, video: VideoModel, url: string) {

View File

@ -57,16 +57,16 @@ function activityPubContextify <T> (data: T) {
} }
type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>> type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>>
async function activityPubCollectionPagination (url: string, handler: ActivityPubCollectionPaginationHandler, page?: any) { async function activityPubCollectionPagination (baseUrl: string, handler: ActivityPubCollectionPaginationHandler, page?: any) {
if (!page || !validator.isInt(page)) { if (!page || !validator.isInt(page)) {
// We just display the first page URL, we only need the total items // We just display the first page URL, we only need the total items
const result = await handler(0, 1) const result = await handler(0, 1)
return { return {
id: url, id: baseUrl,
type: 'OrderedCollection', type: 'OrderedCollection',
totalItems: result.total, 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 // There are more results
if (result.total > page * ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) { if (result.total > page * ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) {
next = url + '?page=' + (page + 1) next = baseUrl + '?page=' + (page + 1)
} }
if (page > 1) { if (page > 1) {
prev = url + '?page=' + (page - 1) prev = baseUrl + '?page=' + (page - 1)
} }
return { return {
id: url + '?page=' + page, id: baseUrl + '?page=' + page,
type: 'OrderedCollectionPage', type: 'OrderedCollectionPage',
prev, prev,
next, next,
partOf: url, partOf: baseUrl,
orderedItems: result.data, orderedItems: result.data,
totalItems: result.total totalItems: result.total
} }

View File

@ -509,12 +509,12 @@ export class ActorFollowModel extends Model<ActorFollowModel> {
tasks.push(ActorFollowModel.sequelize.query(query, options)) 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) const urls: string[] = followers.map(f => f.url)
return { return {
data: urls, data: urls,
total: parseInt(total, 10) total: dataTotal ? parseInt(dataTotal.total, 10) : 0
} }
} }