[mastodon-client] GET /accounts/relationships

This commit is contained in:
Laura Hausmann 2023-09-28 21:03:17 +02:00
parent 5c999a20d0
commit deeb71856d
Signed by: zotan
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 { 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;

View file

@ -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<Note[]> {
@ -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<MastodonEntity.Relationship> {
const relation = await Users.getRelation(localUser.id, target.id);
public static async getUserRelationhipToMany(targetIds: string[], localUserId: string): Promise<MastodonEntity.Relationship[]> {
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 = {
id: target.id,
id: targetId,
following: relation.isFollowing,
followed_by: relation.isFollowed,
blocking: relation.isBlocking,