From dcfa69ff9d46efbc1da1be0715f17f0109f39310 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Sat, 27 Apr 2024 18:48:03 +0200 Subject: [PATCH] [backend/masto-client] Fix user profile html cache not updating --- .../src/server/api/mastodon/converters/user.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/backend/src/server/api/mastodon/converters/user.ts b/packages/backend/src/server/api/mastodon/converters/user.ts index 54f9568db..957b14744 100644 --- a/packages/backend/src/server/api/mastodon/converters/user.ts +++ b/packages/backend/src/server/api/mastodon/converters/user.ts @@ -35,7 +35,7 @@ export class UserConverter { const cacheHit = cache.accounts.find(p => p.id == u.id); 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 acct = u.username; let acctUrl = `https://${u.host || config.host}/@${u.username}`; @@ -243,7 +243,7 @@ export class UserConverter { return Promise.resolve(dbHit) .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 res; @@ -251,7 +251,7 @@ export class UserConverter { } private static async dbCacheMiss(user: User, profile: UserProfile | null, ctx: MastoContext): Promise { - 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; return cache.locks.acquire(identifier, async () => { 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)) ?? []); } - 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.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 { - const identifier = `${user.id}:${(user.lastFetchedAt ?? user.createdAt).getTime()}`; + const identifier = `${user.id}:${(user.lastFetchedAt ?? user.updatedAt ?? user.createdAt).getTime()}`; if (profile !== null) { if (config.htmlCache?.dbFallback) { if (profile === undefined) { profile = await UserProfiles.findOneBy({ userId: user.id }); } 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; } } @@ -313,7 +313,7 @@ export class UserConverter { this.userBioHtmlCache.set(identifier, await bio); 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) { @@ -321,7 +321,7 @@ export class UserConverter { this.userFieldsHtmlCache.set(identifier, fields); 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"]); } } }