From deeb71856ddf834cdb29d678fa08f97bcc51a45a Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Thu, 28 Sep 2023 21:03:17 +0200 Subject: [PATCH] [mastodon-client] GET /accounts/relationships --- .../server/api/mastodon/endpoints/account.ts | 31 ++++++------------- .../src/server/api/mastodon/helpers/user.ts | 14 ++++++--- 2 files changed, 19 insertions(+), 26 deletions(-) diff --git a/packages/backend/src/server/api/mastodon/endpoints/account.ts b/packages/backend/src/server/api/mastodon/endpoints/account.ts index a336ba12b..1a6fbe051 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/account.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/account.ts @@ -9,6 +9,7 @@ import authenticate from "@/server/api/authenticate.js"; import { NoteConverter } from "@/server/api/mastodon/converters/note.js"; import { UserHelpers } from "@/server/api/mastodon/helpers/user.js"; import { PaginationHelpers } from "@/server/api/mastodon/helpers/pagination.js"; +import { NotificationHelpers } from "@/server/api/mastodon/helpers/notification.js"; const relationshipModel = { id: "", @@ -92,32 +93,20 @@ export function apiAccountMastodon(router: Router): void { } }); router.get("/v1/accounts/relationships", async (ctx) => { - const BASE_URL = `${ctx.protocol}://${ctx.hostname}`; - const accessTokens = ctx.headers.authorization; - const client = getClient(BASE_URL, accessTokens); let users; try { - // TODO: this should be body - let ids = ctx.request.query ? ctx.request.query["id[]"] : null; - if (typeof ids === "string") { - ids = [ids]; - } - users = ids; - relationshipModel.id = ids?.toString() || "1"; - if (!ids) { - ctx.body = [relationshipModel]; + const auth = await authenticate(ctx.headers.authorization, null); + const user = auth[0] ?? null; + + if (!user) { + ctx.status = 401; return; } - let reqIds = []; - for (let i = 0; i < ids.length; i++) { - reqIds.push(convertId(ids[i], IdType.IceshrimpId)); - } - - const data = await client.getRelationships(reqIds); - ctx.body = data.data.map((relationship) => - convertRelationship(relationship), - ); + const ids = (normalizeUrlQuery(ctx.query, ['id[]'])['id[]'] ?? []) + .map((id: string) => convertId(id, IdType.IceshrimpId)); + const result = await UserHelpers.getUserRelationhipToMany(ids, user.id); + ctx.body = result.map(rel => convertRelationship(rel)); } catch (e: any) { console.error(e); let data = 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 55b3a3ab3..4b6075f79 100644 --- a/packages/backend/src/server/api/mastodon/helpers/user.ts +++ b/packages/backend/src/server/api/mastodon/helpers/user.ts @@ -46,7 +46,7 @@ export class UserHelpers { if (!following && !requested) await createFollowing(localUser, target); - return this.getUserRelationshipTo(target, localUser); + return this.getUserRelationshipTo(target.id, localUser.id); } public static async unfollowUser(target: User, localUser: ILocalUser) { @@ -57,7 +57,7 @@ export class UserHelpers { if (requested) await cancelFollowRequest(target, localUser); - return this.getUserRelationshipTo(target, localUser); + return this.getUserRelationshipTo(target.id, localUser.id); } 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 { @@ -205,10 +205,14 @@ export class UserHelpers { return this.getUserRelationships('following', user, localUser, maxId, sinceId, minId, limit); } - public static async getUserRelationshipTo(target: User, localUser: ILocalUser): Promise { - const relation = await Users.getRelation(localUser.id, target.id); + public static async getUserRelationhipToMany(targetIds: string[], localUserId: string): Promise { + return Promise.all(targetIds.map(targetId => this.getUserRelationshipTo(targetId, localUserId))); + } + + public static async getUserRelationshipTo(targetId: string, localUserId: string): Promise { + const relation = await Users.getRelation(localUserId, targetId); const response = { - id: target.id, + id: targetId, following: relation.isFollowing, followed_by: relation.isFollowed, blocking: relation.isBlocking,