mirror of
https://iceshrimp.dev/limepotato/jormungandr-bite.git
synced 2024-11-25 03:17:38 -07:00
[mastodon-client] GET /accounts/relationships
This commit is contained in:
parent
5c999a20d0
commit
deeb71856d
2 changed files with 19 additions and 26 deletions
|
@ -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;
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue