[mastodon-client] GET /accounts/relationships

This commit is contained in:
Laura Hausmann 2023-09-28 21:03:17 +02:00
parent 5c999a20d0
commit deeb71856d
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
2 changed files with 19 additions and 26 deletions

View file

@ -9,6 +9,7 @@ import authenticate from "@/server/api/authenticate.js";
import { NoteConverter } from "@/server/api/mastodon/converters/note.js"; import { NoteConverter } from "@/server/api/mastodon/converters/note.js";
import { UserHelpers } from "@/server/api/mastodon/helpers/user.js"; import { UserHelpers } from "@/server/api/mastodon/helpers/user.js";
import { PaginationHelpers } from "@/server/api/mastodon/helpers/pagination.js"; import { PaginationHelpers } from "@/server/api/mastodon/helpers/pagination.js";
import { NotificationHelpers } from "@/server/api/mastodon/helpers/notification.js";
const relationshipModel = { const relationshipModel = {
id: "", id: "",
@ -92,32 +93,20 @@ export function apiAccountMastodon(router: Router): void {
} }
}); });
router.get("/v1/accounts/relationships", async (ctx) => { 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; let users;
try { try {
// TODO: this should be body const auth = await authenticate(ctx.headers.authorization, null);
let ids = ctx.request.query ? ctx.request.query["id[]"] : null; const user = auth[0] ?? null;
if (typeof ids === "string") {
ids = [ids]; if (!user) {
} ctx.status = 401;
users = ids;
relationshipModel.id = ids?.toString() || "1";
if (!ids) {
ctx.body = [relationshipModel];
return; return;
} }
let reqIds = []; const ids = (normalizeUrlQuery(ctx.query, ['id[]'])['id[]'] ?? [])
for (let i = 0; i < ids.length; i++) { .map((id: string) => convertId(id, IdType.IceshrimpId));
reqIds.push(convertId(ids[i], IdType.IceshrimpId)); const result = await UserHelpers.getUserRelationhipToMany(ids, user.id);
} ctx.body = result.map(rel => convertRelationship(rel));
const data = await client.getRelationships(reqIds);
ctx.body = data.data.map((relationship) =>
convertRelationship(relationship),
);
} catch (e: any) { } catch (e: any) {
console.error(e); console.error(e);
let data = e.response.data; let data = e.response.data;

View file

@ -46,7 +46,7 @@ export class UserHelpers {
if (!following && !requested) if (!following && !requested)
await createFollowing(localUser, target); await createFollowing(localUser, target);
return this.getUserRelationshipTo(target, localUser); return this.getUserRelationshipTo(target.id, localUser.id);
} }
public static async unfollowUser(target: User, localUser: ILocalUser) { public static async unfollowUser(target: User, localUser: ILocalUser) {
@ -57,7 +57,7 @@ export class UserHelpers {
if (requested) if (requested)
await cancelFollowRequest(target, localUser); 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<Note[]> { 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[]> {
@ -205,10 +205,14 @@ export class UserHelpers {
return this.getUserRelationships('following', user, localUser, maxId, sinceId, minId, limit); return this.getUserRelationships('following', user, localUser, maxId, sinceId, minId, limit);
} }
public static async getUserRelationshipTo(target: User, localUser: ILocalUser): Promise<MastodonEntity.Relationship> { public static async getUserRelationhipToMany(targetIds: string[], localUserId: string): Promise<MastodonEntity.Relationship[]> {
const relation = await Users.getRelation(localUser.id, target.id); return Promise.all(targetIds.map(targetId => this.getUserRelationshipTo(targetId, localUserId)));
}
public static async getUserRelationshipTo(targetId: string, localUserId: string): Promise<MastodonEntity.Relationship> {
const relation = await Users.getRelation(localUserId, targetId);
const response = { const response = {
id: target.id, id: targetId,
following: relation.isFollowing, following: relation.isFollowing,
followed_by: relation.isFollowed, followed_by: relation.isFollowed,
blocking: relation.isBlocking, blocking: relation.isBlocking,