[mastodon-client] Skip invalid mentions

This commit is contained in:
Laura Hausmann 2023-10-17 20:36:07 +02:00
parent eadf9acdc3
commit 5dcd4c4fff
Signed by: zotan
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;
}
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 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 {

View file

@ -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;
},