mirror of
https://iceshrimp.dev/limepotato/jormungandr-bite.git
synced 2024-11-25 11:27:31 -07:00
[mastodon-client] Proper pagination for /accounts/:id/followers
This commit is contained in:
parent
ba5bcbb16c
commit
c86ab31a29
2 changed files with 38 additions and 19 deletions
|
@ -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);
|
||||
|
|
|
@ -18,6 +18,12 @@ export type AccountCache = {
|
|||
users: User[];
|
||||
};
|
||||
|
||||
export type LinkPaginationObject<T> = {
|
||||
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<Note[]> {
|
||||
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<User[]> {
|
||||
public static async getUserFollowers(user: User, localUser: ILocalUser | null, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 40): Promise<LinkPaginationObject<User[]>> {
|
||||
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<User> {
|
||||
|
|
Loading…
Reference in a new issue