[backend/masto-client] Fix user profile html cache not updating

This commit is contained in:
Laura Hausmann 2024-04-27 18:48:03 +02:00
parent 562b378b1b
commit dcfa69ff9d
Signed by: zotan
GPG key ID: D044E84C5BE01605

View file

@ -35,7 +35,7 @@ export class UserConverter {
const cacheHit = cache.accounts.find(p => p.id == u.id); const cacheHit = cache.accounts.find(p => p.id == u.id);
if (cacheHit) return cacheHit; if (cacheHit) return cacheHit;
const identifier = `${u.id}:${(u.lastFetchedAt ?? u.createdAt).getTime()}`; const identifier = `${u.id}:${(u.lastFetchedAt ?? u.updatedAt ?? u.createdAt).getTime()}`;
let fqn = `${u.username}@${u.host ?? config.domain}`; let fqn = `${u.username}@${u.host ?? config.domain}`;
let acct = u.username; let acct = u.username;
let acctUrl = `https://${u.host || config.host}/@${u.username}`; let acctUrl = `https://${u.host || config.host}/@${u.username}`;
@ -243,7 +243,7 @@ export class UserConverter {
return Promise.resolve(dbHit) return Promise.resolve(dbHit)
.then(res => { .then(res => {
if (res === null || (res.updatedAt.getTime() !== (user.lastFetchedAt ?? user.createdAt).getTime())) { if (res === null || (res.updatedAt.getTime() !== (user.lastFetchedAt ?? user.updatedAt ?? user.createdAt).getTime())) {
return this.dbCacheMiss(user, profile, ctx); return this.dbCacheMiss(user, profile, ctx);
} }
return res; return res;
@ -251,7 +251,7 @@ export class UserConverter {
} }
private static async dbCacheMiss(user: User, profile: UserProfile | null, ctx: MastoContext): Promise<HtmlUserCacheEntry | null> { private static async dbCacheMiss(user: User, profile: UserProfile | null, ctx: MastoContext): Promise<HtmlUserCacheEntry | null> {
const identifier = `${user.id}:${(user.lastFetchedAt ?? user.createdAt).getTime()}`; const identifier = `${user.id}:${(user.lastFetchedAt ?? user.updatedAt ?? user.createdAt).getTime()}`;
const cache = ctx.cache as AccountCache; const cache = ctx.cache as AccountCache;
return cache.locks.acquire(identifier, async () => { return cache.locks.acquire(identifier, async () => {
const cachedBio = await this.userBioHtmlCache.get(identifier); const cachedBio = await this.userBioHtmlCache.get(identifier);
@ -277,7 +277,7 @@ export class UserConverter {
fields = Promise.all(profile!.fields.map(async p => this.encodeField(p, user.host, profile!.mentions)) ?? []); fields = Promise.all(profile!.fields.map(async p => this.encodeField(p, user.host, profile!.mentions)) ?? []);
} }
HtmlUserCacheEntries.upsert({ userId: user.id, updatedAt: user.lastFetchedAt ?? user.createdAt, bio: await bio, fields: await fields }, ["userId"]); HtmlUserCacheEntries.upsert({ userId: user.id, updatedAt: user.lastFetchedAt ?? user.updatedAt ?? user.createdAt, bio: await bio, fields: await fields }, ["userId"]);
await this.userBioHtmlCache.set(identifier, await bio); await this.userBioHtmlCache.set(identifier, await bio);
await this.userFieldsHtmlCache.set(identifier, await fields); await this.userFieldsHtmlCache.set(identifier, await fields);
@ -287,14 +287,14 @@ export class UserConverter {
} }
public static async prewarmCache(user: User, profile?: UserProfile | null, oldProfile?: UserProfile | null): Promise<void> { public static async prewarmCache(user: User, profile?: UserProfile | null, oldProfile?: UserProfile | null): Promise<void> {
const identifier = `${user.id}:${(user.lastFetchedAt ?? user.createdAt).getTime()}`; const identifier = `${user.id}:${(user.lastFetchedAt ?? user.updatedAt ?? user.createdAt).getTime()}`;
if (profile !== null) { if (profile !== null) {
if (config.htmlCache?.dbFallback) { if (config.htmlCache?.dbFallback) {
if (profile === undefined) { if (profile === undefined) {
profile = await UserProfiles.findOneBy({ userId: user.id }); profile = await UserProfiles.findOneBy({ userId: user.id });
} }
if (oldProfile !== undefined && profile?.fields === oldProfile?.fields && profile?.description === oldProfile?.description) { if (oldProfile !== undefined && profile?.fields === oldProfile?.fields && profile?.description === oldProfile?.description) {
HtmlUserCacheEntries.update({ userId: user.id }, { updatedAt: user.lastFetchedAt ?? user.createdAt }); HtmlUserCacheEntries.update({ userId: user.id }, { updatedAt: user.lastFetchedAt ?? user.updatedAt ?? user.createdAt });
return; return;
} }
} }
@ -313,7 +313,7 @@ export class UserConverter {
this.userBioHtmlCache.set(identifier, await bio); this.userBioHtmlCache.set(identifier, await bio);
if (config.htmlCache?.dbFallback) if (config.htmlCache?.dbFallback)
HtmlUserCacheEntries.upsert({ userId: user.id, updatedAt: user.lastFetchedAt ?? user.createdAt, bio: await bio }, ["userId"]); HtmlUserCacheEntries.upsert({ userId: user.id, updatedAt: user.lastFetchedAt ?? user.updatedAt ?? user.createdAt, bio: await bio }, ["userId"]);
} }
if (await this.userFieldsHtmlCache.get(identifier) === undefined) { if (await this.userFieldsHtmlCache.get(identifier) === undefined) {
@ -321,7 +321,7 @@ export class UserConverter {
this.userFieldsHtmlCache.set(identifier, fields); this.userFieldsHtmlCache.set(identifier, fields);
if (config.htmlCache?.dbFallback) if (config.htmlCache?.dbFallback)
HtmlUserCacheEntries.upsert({ userId: user.id, updatedAt: user.lastFetchedAt ?? user.createdAt, fields: fields }, ["userId"]); HtmlUserCacheEntries.upsert({ userId: user.id, updatedAt: user.lastFetchedAt ?? user.updatedAt ?? user.createdAt, fields: fields }, ["userId"]);
} }
} }
} }