From 3824767cc9802c2d6f7b889be1e8a89a4d9d1712 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=81=E5=91=A8=E9=83=A8=E8=90=BD?= Date: Sun, 25 Feb 2024 09:54:16 +0800 Subject: [PATCH] [backend] Fix resolver cannot parse some follows and notes request --- .../src/remote/activitypub/resolver.ts | 38 ++++++++++++++----- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/packages/backend/src/remote/activitypub/resolver.ts b/packages/backend/src/remote/activitypub/resolver.ts index a6c407be5..bc70466ea 100644 --- a/packages/backend/src/remote/activitypub/resolver.ts +++ b/packages/backend/src/remote/activitypub/resolver.ts @@ -24,6 +24,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 { In, IsNull, Not } from "typeorm"; export default class Resolver { private history: Set; @@ -154,7 +155,7 @@ export default class Resolver { 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))); + return renderActivity(renderCreate(renderNote(note), note)); } else { return renderNote(note); } @@ -176,15 +177,34 @@ export default class Resolver { (reaction) => 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)) { + return Promise.all( + [parsed.id, parsed.rest].map((id) => Users.findOneByOrFail({ id })), + ).then(([follower, followee]) => + 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 = FollowRequests.findOneBy({ + id: parsed.id, + }); + if (followRequest == null) { + throw new Error("resolveLocal: invalid follow URI"); + } + const follower = Users.findOneBy({ + id: followRequest.followerId, + host: IsNull(), + }); + const followee = 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`); }