From 757f5f0e6794f6c9e626a59abf7493220b181c1a Mon Sep 17 00:00:00 2001 From: naskya Date: Wed, 27 Mar 2024 06:51:01 +0900 Subject: [PATCH] fix (backend): mark resolveLocal as async There are many type errors that need to be fixed :( --- .../src/remote/activitypub/resolver.ts | 72 +++++++++++-------- 1 file changed, 43 insertions(+), 29 deletions(-) diff --git a/packages/backend/src/remote/activitypub/resolver.ts b/packages/backend/src/remote/activitypub/resolver.ts index 691d3b049..19d24d9c3 100644 --- a/packages/backend/src/remote/activitypub/resolver.ts +++ b/packages/backend/src/remote/activitypub/resolver.ts @@ -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; @@ -129,48 +130,61 @@ export default class Resolver { return object; } - private resolveLocal(url: string): Promise { + private async resolveLocal(url: string): Promise { 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 - if (parsed.rest == null || !/^\w+$/.test(parsed.rest)) - throw new Error("resolveLocal: invalid follow URI"); + // if rest is a + 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`); } } }