mirror of
https://iceshrimp.dev/crimekillz/iceshrimp-161sh.git
synced 2024-11-22 04:03:49 +01:00
[backend] Add cache for resolveMentionToUserAndProfile
This commit is contained in:
parent
544b5a1678
commit
97c733dd72
@ -10,10 +10,21 @@ import { createPerson, updatePerson } from "./activitypub/models/person.js";
|
|||||||
import { remoteLogger } from "./logger.js";
|
import { remoteLogger } from "./logger.js";
|
||||||
import { Cache } from "@/misc/cache.js";
|
import { Cache } from "@/misc/cache.js";
|
||||||
import { IMentionedRemoteUsers } from "@/models/entities/note.js";
|
import { IMentionedRemoteUsers } from "@/models/entities/note.js";
|
||||||
|
import { UserProfile } from "@/models/entities/user-profile.js";
|
||||||
|
|
||||||
const logger = remoteLogger.createSubLogger("resolve-user");
|
const logger = remoteLogger.createSubLogger("resolve-user");
|
||||||
const uriHostCache = new Cache<string>("resolveUserUriHost", 60 * 60 * 24);
|
const uriHostCache = new Cache<string>("resolveUserUriHost", 60 * 60 * 24);
|
||||||
const localUsernameCache = new Cache<string | null>("localUserNameCapitalization", 60 * 60 * 24);
|
const localUsernameCache = new Cache<string | null>("localUserNameCapitalization", 60 * 60 * 24);
|
||||||
|
const profileMentionCache = new Cache<ProfileMention | null>("resolveProfileMentions", 60 * 60);
|
||||||
|
|
||||||
|
type ProfileMention = {
|
||||||
|
user: User;
|
||||||
|
profile: UserProfile | null;
|
||||||
|
data: {
|
||||||
|
username: string;
|
||||||
|
host: string | null;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
export async function resolveUser(
|
export async function resolveUser(
|
||||||
username: string,
|
username: string,
|
||||||
@ -184,16 +195,18 @@ export async function resolveUser(
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function resolveMentionToUserAndProfile(username: string, host: string | null, objectHost: string | null) {
|
export async function resolveMentionToUserAndProfile(username: string, host: string | null, objectHost: string | null) {
|
||||||
try {
|
return profileMentionCache.fetch(`${username}@${host ?? objectHost}`, async () => {
|
||||||
const user = await resolveUser(username, host ?? objectHost, false);
|
try {
|
||||||
const profile = await UserProfiles.findOneBy({ userId: user.id });
|
const user = await resolveUser(username, host ?? objectHost, false);
|
||||||
const data = { username, host: host ?? objectHost };
|
const profile = await UserProfiles.findOneBy({ userId: user.id });
|
||||||
|
const data = { username, host: host ?? objectHost };
|
||||||
|
|
||||||
return { user, profile, data };
|
return { user, profile, data };
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getMentionFallbackUri(username: string, host: string | null, objectHost: string | null): string {
|
export function getMentionFallbackUri(username: string, host: string | null, objectHost: string | null): string {
|
||||||
|
Loading…
Reference in New Issue
Block a user