1
0
Fork 1
mirror of https://example.com synced 2024-11-24 23:16:38 +09:00

fix (backend): mark resolveLocal as async

There are many type errors that need to be fixed :(
This commit is contained in:
naskya 2024-03-27 06:51:01 +09:00
parent 79533ceec9
commit 757f5f0e67
Signed by: naskya
GPG key ID: 712D413B3A9FED5C

View file

@ -6,7 +6,7 @@ import { extractHost, isSelfHost } from "backend-rs";
import { apGet } from "./request.js";
import type { IObject, ICollection, IOrderedCollection } from "./type.js";
import { isCollectionOrOrderedCollection, getApId } from "./type.js";
import { Notes, NoteReactions, Polls, Users } from "@/models/index.js";
import { FollowRequests, Notes, NoteReactions, Polls, Users } from "@/models/index.js";
import { parseUri } from "./db-resolver.js";
import renderNote from "@/remote/activitypub/renderer/note.js";
import { renderLike } from "@/remote/activitypub/renderer/like.js";
@ -17,6 +17,7 @@ import { renderActivity } from "@/remote/activitypub/renderer/index.js";
import renderFollow from "@/remote/activitypub/renderer/follow.js";
import { shouldBlockInstance } from "@/misc/should-block-instance.js";
import { apLogger } from "@/remote/activitypub/logger.js";
import { IsNull, Not } from "typeorm";
export default class Resolver {
private history: Set<string>;
@ -129,48 +130,61 @@ export default class Resolver {
return object;
}
private resolveLocal(url: string): Promise<IObject> {
private async resolveLocal(url: string): Promise<IObject> {
const parsed = parseUri(url);
if (!parsed.local) throw new Error("resolveLocal: not local");
switch (parsed.type) {
case "notes":
return Notes.findOneByOrFail({ id: parsed.id }).then((note) => {
if (parsed.rest === "activity") {
// this refers to the create activity and not the note itself
return renderActivity(renderCreate(renderNote(note)));
} else {
return renderNote(note);
}
});
const note = await Notes.findOneByOrFail({ id: parsed.id });
if (parsed.rest === "activity") {
// this refers to the create activity and not the note itself
return renderActivity(renderCreate(renderNote(note), note));
} else {
return renderNote(note);
}
case "users":
return Users.findOneByOrFail({ id: parsed.id }).then((user) =>
renderPerson(user as ILocalUser),
);
const user = await Users.findOneByOrFail({ id: parsed.id });
return await renderPerson(user as ILocalUser);
case "questions":
// Polls are indexed by the note they are attached to.
return Promise.all([
const [note_2, poll] = await Promise.all([
Notes.findOneByOrFail({ id: parsed.id }),
Polls.findOneByOrFail({ noteId: parsed.id }),
]).then(([note, poll]) =>
renderQuestion({ id: note.userId }, note, poll),
);
]);
return await renderQuestion({ id: note_2.userId }, note_2, poll);
case "likes":
return NoteReactions.findOneByOrFail({ id: parsed.id }).then(
(reaction) => renderActivity(renderLike(reaction, { uri: null })),
);
const reaction = await NoteReactions.findOneByOrFail({ id: parsed.id });
return renderActivity(renderLike(reaction, { uri: null }));
case "follows":
// rest should be <followee id>
if (parsed.rest == null || !/^\w+$/.test(parsed.rest))
throw new Error("resolveLocal: invalid follow URI");
// if rest is a <followee id>
if (parsed.rest != null && /^\w+$/.test(parsed.rest)) {
const [follower, followee] = await Promise.all(
[parsed.id, parsed.rest].map((id) => Users.findOneByOrFail({ id })));
return renderActivity(renderFollow(follower, followee, url));
}
return Promise.all(
[parsed.id, parsed.rest].map((id) => Users.findOneByOrFail({ id })),
).then(([follower, followee]) =>
renderActivity(renderFollow(follower, followee, url)),
);
// Another situation is there is only requestId, then obtained object from database.
const followRequest = await FollowRequests.findOneBy({
id: parsed.id,
});
if (followRequest == null) {
throw new Error("resolveLocal: invalid follow URI");
}
const follower = await Users.findOneBy({
id: followRequest.followerId,
host: IsNull(),
});
const followee = await Users.findOneBy({
id: followRequest.followeeId,
host: Not(IsNull()),
});
if (follower == null || followee == null) {
throw new Error("resolveLocal: invalid follow URI");
}
return renderActivity(renderFollow(follower, followee, url));
default:
throw new Error(`resolveLocal: type ${type} unhandled`);
throw new Error(`resolveLocal: type ${parsed.type} unhandled`);
}
}
}