From c86ab31a29d23b8aee2b49951ca64d2fd4091333 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Tue, 26 Sep 2023 22:02:26 +0200 Subject: [PATCH] [mastodon-client] Proper pagination for /accounts/:id/followers --- .../server/api/mastodon/endpoints/account.ts | 33 +++++++++++-------- .../src/server/api/mastodon/helpers/user.ts | 24 +++++++++++--- 2 files changed, 38 insertions(+), 19 deletions(-) diff --git a/packages/backend/src/server/api/mastodon/endpoints/account.ts b/packages/backend/src/server/api/mastodon/endpoints/account.ts index 0c75e81a0..358337542 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/account.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/account.ts @@ -1,23 +1,14 @@ -import { Users } from "@/models/index.js"; -import { resolveUser } from "@/remote/resolve-user.js"; import Router from "@koa/router"; -import { FindOptionsWhere, IsNull } from "typeorm"; import { getClient } from "../ApiMastodonCompatibleService.js"; import { argsToBools, convertTimelinesArgsId, limitToInt, normalizeUrlQuery } from "./timeline.js"; import { convertId, IdType } from "../../index.js"; -import { - convertAccount, - convertFeaturedTag, - convertList, - convertRelationship, - convertStatus, -} from "../converters.js"; -import { getNote, getUser } from "@/server/api/common/getters.js"; +import { convertAccount, convertFeaturedTag, convertList, convertRelationship, convertStatus, } from "../converters.js"; +import { getUser } from "@/server/api/common/getters.js"; import { UserConverter } from "@/server/api/mastodon/converters/user.js"; import authenticate from "@/server/api/authenticate.js"; -import { TimelineHelpers } from "@/server/api/mastodon/helpers/timeline.js"; import { NoteConverter } from "@/server/api/mastodon/converters/note.js"; import { UserHelpers } from "@/server/api/mastodon/helpers/user.js"; +import config from "@/config/index.js"; const relationshipModel = { id: "", @@ -207,10 +198,24 @@ export function apiAccountMastodon(router: Router): void { const query = await UserHelpers.getUserCached(userId, cache); const args = normalizeUrlQuery(convertTimelinesArgsId(limitToInt(ctx.query as any))); - const followers = await UserHelpers.getUserFollowers(query, user, args.max_id, args.since_id, args.min_id, args.limit) - .then(f => UserConverter.encodeMany(f, cache)); + const res = await UserHelpers.getUserFollowers(query, user, args.max_id, args.since_id, args.min_id, args.limit); + const followers = await UserConverter.encodeMany(res.data, cache); ctx.body = followers.map((account) => convertAccount(account)); + + const link: string[] = []; + const limit = args.limit ?? 40; + if (res.maxId) { + const l = `<${config.url}/api/v1/accounts/${ctx.params.id}/followers?limit=${limit}&max_id=${convertId(res.maxId, IdType.MastodonId)}>; rel="next"`; + link.push(l); + } + if (res.minId) { + const l = `<${config.url}/api/v1/accounts/${ctx.params.id}/followers?limit=${limit}&min_id=${convertId(res.minId, IdType.MastodonId)}>; rel="prev"`; + link.push(l); + } + if (link.length > 0){ + ctx.response.append('Link', link.join(', ')); + } } catch (e: any) { console.error(e); console.error(e.response.data); diff --git a/packages/backend/src/server/api/mastodon/helpers/user.ts b/packages/backend/src/server/api/mastodon/helpers/user.ts index 4fc22e200..19ef69c7b 100644 --- a/packages/backend/src/server/api/mastodon/helpers/user.ts +++ b/packages/backend/src/server/api/mastodon/helpers/user.ts @@ -18,6 +18,12 @@ export type AccountCache = { users: User[]; }; +export type LinkPaginationObject = { + data: T; + maxId?: string | undefined; + minId?: string | undefined; +} + export class UserHelpers { public static async getUserStatuses(user: User, localUser: ILocalUser | null, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 20, onlyMedia: boolean = false, excludeReplies: boolean = false, excludeReblogs: boolean = false, pinned: boolean = false, tagged: string | undefined): Promise { if (limit > 40) limit = 40; @@ -70,22 +76,22 @@ export class UserHelpers { return NoteHelpers.execQuery(query, limit, minId !== undefined); } - public static async getUserFollowers(user: User, localUser: ILocalUser | null, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 40): Promise { + public static async getUserFollowers(user: User, localUser: ILocalUser | null, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 40): Promise> { if (limit > 80) limit = 80; const profile = await UserProfiles.findOneByOrFail({ userId: user.id }); if (profile.ffVisibility === "private") { - if (!localUser || user.id != localUser.id) return []; + if (!localUser || user.id != localUser.id) return { data: [] }; } else if (profile.ffVisibility === "followers") { - if (!localUser) return []; + if (!localUser) return { data: [] }; const isFollowed = await Followings.exist({ where: { followeeId: user.id, followerId: localUser.id, }, }); - if (!isFollowed) return []; + if (!isFollowed) return { data: [] }; } const query = PaginationHelpers.makePaginationQuery( @@ -97,7 +103,15 @@ export class UserHelpers { .andWhere("following.followeeId = :userId", { userId: user.id }) .innerJoinAndSelect("following.follower", "follower"); - return query.take(limit).getMany().then(p => p.map(p => p.follower).filter(p => p) as User[]); + return query.take(limit).getMany().then(p => { + if (minId !== undefined) p = p.reverse(); + + return { + data: p.map(p => p.follower).filter(p => p) as User[], + maxId: p.map(p => p.id).at(-1), + minId: p.map(p => p.id)[0], + }; + }); } public static async getUserCached(id: string, cache: AccountCache = UserHelpers.getFreshAccountCache()): Promise {