From 5dcd4c4fff82dc3d2a5de6cdce00c6525c28c8a0 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Tue, 17 Oct 2023 20:36:07 +0200 Subject: [PATCH] [mastodon-client] Skip invalid mentions --- packages/backend/src/remote/resolve-user.ts | 3 +- .../src/server/api/mastodon/helpers/mfm.ts | 31 ++++++++++++------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/packages/backend/src/remote/resolve-user.ts b/packages/backend/src/remote/resolve-user.ts index 8122a7626..68f4d2d90 100644 --- a/packages/backend/src/remote/resolve-user.ts +++ b/packages/backend/src/remote/resolve-user.ts @@ -207,12 +207,13 @@ export function getMentionFallbackUri(username: string, host: string | null, obj return fallback; } -export async function resolveMentionWithFallback(username: string, host: string | null, objectHost: string | null, cache: IMentionedRemoteUsers): Promise { +export async function resolveMentionWithFallback(username: string, host: string | null, objectHost: string | null, cache: IMentionedRemoteUsers, cachedOnly: boolean = false): Promise { const fallback = getMentionFallbackUri(username, host, objectHost); const cached = cache.find(r => r.username.toLowerCase() === username.toLowerCase() && r.host === host); if (cached) return cached.url ?? cached.uri ?? fallback; if ((host === null && objectHost === null) || host === config.domain) return fallback; + if (cachedOnly) return fallback; return mentionUriCache.fetch(fallback, async () => { try { diff --git a/packages/backend/src/server/api/mastodon/helpers/mfm.ts b/packages/backend/src/server/api/mastodon/helpers/mfm.ts index 39c20e5d4..8326a4b53 100644 --- a/packages/backend/src/server/api/mastodon/helpers/mfm.ts +++ b/packages/backend/src/server/api/mastodon/helpers/mfm.ts @@ -136,18 +136,27 @@ export class MfmHelpers { }, async mention(node) { - const el = doc.createElement("span"); - el.setAttribute("class", "h-card"); - el.setAttribute("translate", "no"); - const a = doc.createElement("a"); const { username, host, acct } = node.props; - a.href = await resolveMentionWithFallback(username, host, objectHost, mentionedRemoteUsers); - a.className = "u-url mention"; - const span = doc.createElement("span"); - span.textContent = username; - a.textContent = '@'; - a.appendChild(span); - el.appendChild(a); + const fallback = await resolveMentionWithFallback(username, host, objectHost, mentionedRemoteUsers, true); + const isLocal = (host === null && objectHost === null) || host === config.domain; + const isInvalid = fallback.startsWith(config.url) && !isLocal; + + const el = doc.createElement("span"); + if (isInvalid) { + el.textContent = acct; + } else { + el.setAttribute("class", "h-card"); + el.setAttribute("translate", "no"); + const a = doc.createElement("a"); + a.href = fallback; + a.className = "u-url mention"; + const span = doc.createElement("span"); + span.textContent = username; + a.textContent = '@'; + a.appendChild(span); + el.appendChild(a); + } + return el; },