[mastodon-client] Skip invalid mentions

This commit is contained in:
Laura Hausmann 2023-10-17 20:36:07 +02:00
parent eadf9acdc3
commit 5dcd4c4fff
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
2 changed files with 22 additions and 12 deletions

View file

@ -207,12 +207,13 @@ export function getMentionFallbackUri(username: string, host: string | null, obj
return fallback; return fallback;
} }
export async function resolveMentionWithFallback(username: string, host: string | null, objectHost: string | null, cache: IMentionedRemoteUsers): Promise<string> { export async function resolveMentionWithFallback(username: string, host: string | null, objectHost: string | null, cache: IMentionedRemoteUsers, cachedOnly: boolean = false): Promise<string> {
const fallback = getMentionFallbackUri(username, host, objectHost); const fallback = getMentionFallbackUri(username, host, objectHost);
const cached = cache.find(r => r.username.toLowerCase() === username.toLowerCase() && r.host === host); const cached = cache.find(r => r.username.toLowerCase() === username.toLowerCase() && r.host === host);
if (cached) return cached.url ?? cached.uri ?? fallback; if (cached) return cached.url ?? cached.uri ?? fallback;
if ((host === null && objectHost === null) || host === config.domain) return fallback; if ((host === null && objectHost === null) || host === config.domain) return fallback;
if (cachedOnly) return fallback;
return mentionUriCache.fetch(fallback, async () => { return mentionUriCache.fetch(fallback, async () => {
try { try {

View file

@ -136,18 +136,27 @@ export class MfmHelpers {
}, },
async mention(node) { 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; const { username, host, acct } = node.props;
a.href = await resolveMentionWithFallback(username, host, objectHost, mentionedRemoteUsers); const fallback = await resolveMentionWithFallback(username, host, objectHost, mentionedRemoteUsers, true);
a.className = "u-url mention"; const isLocal = (host === null && objectHost === null) || host === config.domain;
const span = doc.createElement("span"); const isInvalid = fallback.startsWith(config.url) && !isLocal;
span.textContent = username;
a.textContent = '@'; const el = doc.createElement("span");
a.appendChild(span); if (isInvalid) {
el.appendChild(a); 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; return el;
}, },