From 70d75f1d57ccaa751778b13cf3b7c8e2e360c8aa Mon Sep 17 00:00:00 2001 From: Hazelnoot Date: Mon, 5 May 2025 10:12:21 -0400 Subject: [PATCH] check summary.haveNoteLocally after setting summary.activityPub to improve support for Akkoma --- .../src/server/web/UrlPreviewService.ts | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/packages/backend/src/server/web/UrlPreviewService.ts b/packages/backend/src/server/web/UrlPreviewService.ts index 0bd3eff3ba..2ae626f8b7 100644 --- a/packages/backend/src/server/web/UrlPreviewService.ts +++ b/packages/backend/src/server/web/UrlPreviewService.ts @@ -31,7 +31,7 @@ export type LocalSummalyResult = SummalyResult & { }; // Increment this to invalidate cached previews after a major change. -const cacheFormatVersion = 1; +const cacheFormatVersion = 2; @Injectable() export class UrlPreviewService { @@ -159,11 +159,11 @@ export class UrlPreviewService { summary.icon = this.wrap(summary.icon); summary.thumbnail = this.wrap(summary.thumbnail); + // Summaly cannot always detect links to a fedi post, so do some additional tests to try and find missed cases. + summary.activityPub ??= await this.inferActivityPubLink(summary); + if (summary.activityPub) { summary.haveNoteLocally = !!await this.apDbResolverService.getNoteFromApId(summary.activityPub); - } else { - // Summaly cannot always detect links to a fedi post, so check if it matches anything we already have - await this.inferActivityPubLink(summary); } // Await this to avoid hammering redis when a bunch of URLs are fetched at once @@ -261,14 +261,12 @@ export class UrlPreviewService { } } - private async inferActivityPubLink(summary: LocalSummalyResult) { + private async inferActivityPubLink(summary: LocalSummalyResult): Promise { // Match canonical URI first. // This covers local and remote links. const isCanonicalUri = !!await this.apDbResolverService.getNoteFromApId(summary.url); if (isCanonicalUri) { - summary.activityPub = summary.url; - summary.haveNoteLocally = true; - return; + return summary.url; } // Try public URL next. @@ -286,18 +284,17 @@ export class UrlPreviewService { // Older versions did not validate URL, so do it now to avoid impersonation. const matchByUrl = urlMatches.find(({ uri }) => this.apUtilityService.haveSameAuthority(uri, summary.url)); if (matchByUrl) { - summary.activityPub = matchByUrl.uri; - summary.haveNoteLocally = true; - return; + return matchByUrl.uri; } // Finally, attempt a signed GET in case it's a direct link to an instance with authorized fetch. const instanceActor = await this.systemAccountService.getInstanceActor(); const remoteObject = await this.apRequestService.signedGet(summary.url, instanceActor).catch(() => null); if (remoteObject) { - summary.activityPub = remoteObject.id; - summary.haveNoteLocally = false; - return; + return remoteObject.id; } + + // No match :( + return null; } }