[backend] Cache mention uri in resolveMentionWithFallback

This commit is contained in:
Laura Hausmann 2023-10-14 17:39:47 +02:00
parent c0d7ecf5c3
commit fe165dd4d3
No known key found for this signature in database
GPG Key ID: D044E84C5BE01605

View File

@ -13,6 +13,7 @@ import { IMentionedRemoteUsers } from "@/models/entities/note.js";
const logger = remoteLogger.createSubLogger("resolve-user");
const uriHostCache = new Cache<string>("resolveUserUriHost", 60 * 60 * 24);
const mentionUriCache = new Cache<string>("resolveMentionUserUri", 60 * 60 * 72);
export async function resolveUser(
username: string,
@ -188,14 +189,16 @@ export async function resolveMentionWithFallback(username: string, host: string
if (cached) return cached.url ?? cached.uri;
if ((host === null && objectHost === null) || host === config.domain) return fallback;
try {
const user = await resolveUser(username, host ?? objectHost, false);
const profile = await UserProfiles.findOneBy({ userId: user.id });
return profile?.url ?? user.uri ?? fallback;
}
catch {
return fallback;
}
return mentionUriCache.fetch(fallback, async () => {
try {
const user = await resolveUser(username, host ?? objectHost, false);
const profile = await UserProfiles.findOneBy({ userId: user.id });
return profile?.url ?? user.uri ?? fallback;
}
catch {
return fallback;
}
});
}
export async function getSubjectHostFromUri(uri: string): Promise<string | null> {