From c20322556ba031ddb3d6e97d332a5d1419954f0a Mon Sep 17 00:00:00 2001 From: Andrew Lisowski Date: Wed, 11 Nov 2026 16:56:29 -0800 Subject: [PATCH] Rebuild a listing's page snapshot when newer mentions arrive The "On Bluesky" preview on listing pages comes from the page snapshot, which is rebuilt when the listing, its reviews or its funding change. The mentions watcher only inserts rows, so the preview could show posts weeks older than the full mentions page. On each listing view, one indexed max() checks for a mention newer than the snapshot; if there is one, the snapshot is rebuilt in the background (at most once a minute per listing, never concurrently) so the next view is current without slowing this one down. Co-Authored-By: Claude Opus 5.5 --- .../api-directory-listings.functions.ts | 63 ++++++++++++++++++- 1 file changed, 61 insertions(+), 2 deletions(-) diff --git a/src/integrations/tanstack-query/api-directory-listings.functions.ts b/src/integrations/tanstack-query/api-directory-listings.functions.ts index 2291b78..5e49393 100644 --- a/src/integrations/tanstack-query/api-directory-listings.functions.ts +++ b/src/integrations/tanstack-query/api-directory-listings.functions.ts @@ -103,6 +103,7 @@ import { inArray, isNotNull, isNull, + max, ne, or, sql, @@ -3208,6 +3209,50 @@ async function selectDirectoryListingDetailRow(db: Database, whereClause: SQL) { return row ?? null; } +/** Don't rebuild one listing's page snapshot more often than this. */ +const SNAPSHOT_MENTION_REFRESH_MIN_INTERVAL_MS = 60_000; +const snapshotRefreshesInFlight = new Set(); + +/** + * The page snapshot (which holds the "On Bluesky" preview) is rebuilt when the + * listing, its reviews or its funding change, but the mentions watcher only + * inserts rows. If a newer mention exists than the snapshot, rebuild it in the + * background so the next view is current without slowing this one down + * (the rebuild calls the Bluesky API). One cheap indexed max() per view. + */ +async function refreshSnapshotIfMentionsAreNewer( + db: Database, + listingId: string, + refreshedAt: Date, +) { + if (snapshotRefreshesInFlight.has(listingId)) return; + if ( + Date.now() - refreshedAt.getTime() < + SNAPSHOT_MENTION_REFRESH_MIN_INTERVAL_MS + ) { + return; + } + const mentions = dbSchema.storeListingMentions; + const [latest] = await db + .select({ at: max(mentions.postCreatedAt) }) + .from(mentions) + .where(eq(mentions.storeListingId, listingId)); + if (!latest?.at || latest.at <= refreshedAt) return; + + snapshotRefreshesInFlight.add(listingId); + void refreshListingPageSnapshot(db, listingId) + .catch((error: unknown) => { + console.error( + "[listing-snapshot] mention refresh failed", + listingId, + error, + ); + }) + .finally(() => { + snapshotRefreshesInFlight.delete(listingId); + }); +} + export async function loadProductPageByListingId( db: Database, listingId: string, @@ -3222,15 +3267,29 @@ export async function loadProductPageByListingId( } let [snapshot] = await db - .select({ payload: dbSchema.storeListingPageSnapshots.payload }) + .select({ + payload: dbSchema.storeListingPageSnapshots.payload, + refreshedAt: dbSchema.storeListingPageSnapshots.refreshedAt, + }) .from(dbSchema.storeListingPageSnapshots) .where(eq(dbSchema.storeListingPageSnapshots.storeListingId, listingId)) .limit(1); + if (snapshot) { + await refreshSnapshotIfMentionsAreNewer( + db, + listingId, + snapshot.refreshedAt, + ); + } + if (!snapshot && (options?.refreshIfMissing ?? true)) { await refreshListingPageSnapshot(db, listingId); [snapshot] = await db - .select({ payload: dbSchema.storeListingPageSnapshots.payload }) + .select({ + payload: dbSchema.storeListingPageSnapshots.payload, + refreshedAt: dbSchema.storeListingPageSnapshots.refreshedAt, + }) .from(dbSchema.storeListingPageSnapshots) .where(eq(dbSchema.storeListingPageSnapshots.storeListingId, listingId)) .limit(1);