From 0747e24256780520ef9d3f8c7107d6bb64464e36 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Thu, 28 Sep 2023 22:23:17 +0200 Subject: [PATCH] [mastodon-client] GET /blocks --- .../server/api/mastodon/endpoints/account.ts | 21 ++++++++++----- .../src/server/api/mastodon/helpers/user.ts | 27 +++++++++++++++++++ 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/packages/backend/src/server/api/mastodon/endpoints/account.ts b/packages/backend/src/server/api/mastodon/endpoints/account.ts index 505e4045c..e16f89261 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/account.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/account.ts @@ -479,14 +479,21 @@ export function apiAccountMastodon(router: Router): void { } }); router.get("/v1/blocks", async (ctx) => { - const BASE_URL = `${ctx.protocol}://${ctx.hostname}`; - const accessTokens = ctx.headers.authorization; - const client = getClient(BASE_URL, accessTokens); try { - const data = await client.getBlocks( - convertTimelinesArgsId(limitToInt(ctx.query as any)), - ); - ctx.body = data.data.map((account) => convertAccount(account)); + const auth = await authenticate(ctx.headers.authorization, null); + const user = auth[0] ?? null; + + if (!user) { + ctx.status = 401; + return; + } + + const cache = UserHelpers.getFreshAccountCache(); + const args = normalizeUrlQuery(convertTimelinesArgsId(limitToInt(ctx.query as any))); + const res = await UserHelpers.getUserBlocks(user, args.max_id, args.since_id, args.min_id, args.limit); + const blocks = await UserConverter.encodeMany(res.data, cache); + ctx.body = blocks.map(b => convertAccount(b)); + PaginationHelpers.appendLinkPaginationHeader(args, ctx, res); } 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 4035e725a..0e2d9f98a 100644 --- a/packages/backend/src/server/api/mastodon/helpers/user.ts +++ b/packages/backend/src/server/api/mastodon/helpers/user.ts @@ -156,6 +156,33 @@ export class UserHelpers { }); } + public static async getUserBlocks(user: ILocalUser, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 40): Promise> { + if (limit > 80) limit = 80; + + const query = PaginationHelpers.makePaginationQuery( + Blockings.createQueryBuilder("blocking"), + sinceId, + maxId, + minId + ); + + query.andWhere("blocking.blockerId = :userId", {userId: user.id}) + .innerJoinAndSelect("blocking.blockee", "blockee"); + + return query.take(limit).getMany().then(async p => { + if (minId !== undefined) p = p.reverse(); + const users = p + .map(p => p.blockee) + .filter(p => p) as User[]; + + return { + data: users, + maxId: p.map(p => p.id).at(-1), + minId: p.map(p => p.id)[0], + }; + }); + } + 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;