From a227837ddaac7a29babeb6d243821526706ce028 Mon Sep 17 00:00:00 2001 From: Andrew Lisowski Date: Sun, 6 Dec 2026 00:53:50 -0800 Subject: [PATCH] Keep a listing when its record is deleted but a duplicate survives An owner whose create was retried can end up with two listing.detail records for one listing. If they delete the one the store row points at, Tap ingest deleted the row, taking an approved listing off the store (along with its favorites and reviews) even though the other record was still live. That's what happened to ZelkTV. Now the delete handler looks for another live record in the same repo with the row's slug (or product URL) and moves the row onto it, keeping its moderation status and recording the deleted URI in prior_at_uris. Co-Authored-By: Claude Opus 5.5 --- scripts/tap-consumer.ts | 1 + src/lib/atproto/tap-listing-sync.test.ts | 47 ++++++++- src/lib/atproto/tap-listing-sync.ts | 128 +++++++++++++++++++++++ 3 files changed, 175 insertions(+), 1 deletion(-) diff --git a/scripts/tap-consumer.ts b/scripts/tap-consumer.ts index 4be42b4..6cdb4ae 100644 --- a/scripts/tap-consumer.ts +++ b/scripts/tap-consumer.ts @@ -878,6 +878,7 @@ async function main() { db, did: evt.did, rkey: evt.rkey, + trustedPublisher: trusted.has(evt.did), }); console.log(`[tap] delete applied rkey=${evt.rkey}`); return; diff --git a/src/lib/atproto/tap-listing-sync.test.ts b/src/lib/atproto/tap-listing-sync.test.ts index ceaaca2..ceab46b 100644 --- a/src/lib/atproto/tap-listing-sync.test.ts +++ b/src/lib/atproto/tap-listing-sync.test.ts @@ -1,7 +1,10 @@ import type { FyiAtstoreListingDetail } from "#/lib/atproto/listing-record"; import { COLLECTION } from "#/lib/atproto/nsids"; -import { resolveListingVerificationStatus } from "#/lib/atproto/tap-listing-sync"; +import { + findSurvivingListingRecord, + resolveListingVerificationStatus, +} from "#/lib/atproto/tap-listing-sync"; import { describe, expect, it } from "vitest"; const STORE = "did:plc:store"; @@ -79,3 +82,45 @@ describe("resolveListingVerificationStatus", () => { ).toBe("unverified"); }); }); + +describe("findSurvivingListingRecord", () => { + const rec = (slug: string, externalUrl: string) => + ({ slug, externalUrl }) as FyiAtstoreListingDetail; + const listing = { + deletedRkey: "a", + slug: "zelk-tv", + sourceUrl: "https://tv.zelk.space", + }; + + it("picks the other record with the same slug", () => { + const survivor = findSurvivingListingRecord( + [ + { rkey: "a", record: rec("zelk-tv", "https://tv.zelk.space") }, + { rkey: "c", record: rec("other", "https://other.example") }, + { rkey: "b", record: rec("zelk-tv", "https://tv.zelk.space") }, + ], + listing, + ); + expect(survivor?.rkey).toBe("b"); + }); + + it("falls back to the same product URL when the slug changed", () => { + const survivor = findSurvivingListingRecord( + [{ rkey: "b", record: rec("zelktv", " https://tv.zelk.space ") }], + listing, + ); + expect(survivor?.rkey).toBe("b"); + }); + + it("returns null when only the deleted record or unrelated ones remain", () => { + expect( + findSurvivingListingRecord( + [ + { rkey: "a", record: rec("zelk-tv", "https://tv.zelk.space") }, + { rkey: "c", record: rec("other", "https://other.example") }, + ], + listing, + ), + ).toBeNull(); + }); +}); diff --git a/src/lib/atproto/tap-listing-sync.ts b/src/lib/atproto/tap-listing-sync.ts index e16fe0d..50daf37 100644 --- a/src/lib/atproto/tap-listing-sync.ts +++ b/src/lib/atproto/tap-listing-sync.ts @@ -13,6 +13,10 @@ import { explainMissingBlobUrl, getBlobCidString, } from "#/lib/atproto/blob-cdn-url"; +import { + paginateListRecords, + rkeyFromCollectionAtUri, +} from "#/lib/atproto/list-records"; import { LISTING_LINK_LABEL_MAX_LENGTH, LISTING_LINK_MAX_COUNT, @@ -780,6 +784,119 @@ function isSourceUrlUniqueViolation(err: unknown): boolean { return false; } +/** + * Among a repo's live `listing.detail` records, the one that still describes the listing whose record + * `deletedRkey` was just deleted: same slug first, else same product URL. Null when none is left. + */ +export function findSurvivingListingRecord( + records: Array<{ rkey: string; record: FyiAtstoreListingDetail }>, + listing: { deletedRkey: string; slug: string; sourceUrl: string }, +): { rkey: string; record: FyiAtstoreListingDetail } | null { + const others = records.filter((r) => r.rkey !== listing.deletedRkey); + return ( + others.find((r) => r.record.slug === listing.slug) ?? + others.find( + (r) => r.record.externalUrl.trim() === listing.sourceUrl.trim(), + ) ?? + null + ); +} + +/** + * An owner can end up with two records for one listing (a create retried before Tap mirrored the + * first one). The row follows whichever record Tap saw last, so when the owner deletes "the + * duplicate" it may be the one the row points at — and deleting the row would take an approved + * listing off the store while its other record is still live. Move the row onto the surviving + * record instead, keeping its moderation status, reviews, and favorites. + * + * Returns false (caller deletes as before) when there's no row, no surviving record, or the PDS + * can't be listed. + */ +async function repointListingToSurvivingRecord( + db: Database, + input: { did: string; rkey: string; trustedPublisher: boolean }, +): Promise { + const { did, rkey, trustedPublisher } = input; + const t = schema.storeListings; + const [row] = await db + .select({ + id: t.id, + slug: t.slug, + sourceUrl: t.sourceUrl, + priorAtUris: t.priorAtUris, + }) + .from(t) + .where(and(eq(t.repoDid, did), eq(t.rkey, rkey))) + .limit(1); + if (!row) return false; + + const pds = await resolveAtprotoPdsBaseUrl(did); + if (!pds) return false; + + const live: Array<{ rkey: string; record: FyiAtstoreListingDetail }> = []; + try { + for await (const rec of paginateListRecords( + pds, + did, + COLLECTION.listingDetail, + )) { + const recRkey = rkeyFromCollectionAtUri( + rec.uri, + COLLECTION.listingDetail, + ); + const parsed = tryParseListingDetailRecord( + rec.value as Record, + ); + if (recRkey && parsed.ok) + live.push({ rkey: recRkey, record: parsed.record }); + } + } catch (error) { + console.warn( + `[tap-ingest] could not list ${did} listing records to look for a surviving duplicate of ${atUriFor(did, rkey)}`, + error, + ); + return false; + } + + const survivor = findSurvivingListingRecord(live, { + deletedRkey: rkey, + slug: row.slug, + sourceUrl: row.sourceUrl, + }); + if (!survivor) return false; + + const deletedAtUri = atUriFor(did, rkey); + const survivorAtUri = atUriFor(did, survivor.rkey); + /** + * Point the row at the survivor first so the upsert below sees the same (repo, rkey) and keeps + * the row's verification status (see `resolveListingVerificationStatus`), and remember the + * deleted URI so favorites/reviews written against it still resolve. + */ + await db + .update(t) + .set({ + atUri: survivorAtUri, + rkey: survivor.rkey, + priorAtUris: row.priorAtUris.includes(deletedAtUri) + ? row.priorAtUris + : [...row.priorAtUris, deletedAtUri], + }) + .where(eq(t.id, row.id)); + + console.log( + `[tap-ingest] listing ${deletedAtUri} deleted but ${survivorAtUri} still describes slug=${row.slug}; repointed row ${row.id} instead of deleting it`, + ); + + await upsertDirectoryListingFromTap({ + db, + did, + rkey: survivor.rkey, + record: survivor.record, + trustedPublisher, + }); + return true; +} + /** * Remove the mirrored `store_listings` row when the listing record is deleted on the PDS (matched by repo + rkey). * @@ -791,6 +908,7 @@ export async function markListingRemovedFromTap(input: { db: Database; did: string; rkey: string; + trustedPublisher?: boolean; }) { const { db, did, rkey } = input; const deletedAtUri = atUriFor(did, rkey); @@ -813,6 +931,16 @@ export async function markListingRemovedFromTap(input: { return; } + if ( + await repointListingToSurvivingRecord(db, { + did, + rkey, + trustedPublisher: input.trustedPublisher ?? false, + }) + ) { + return; + } + await db .delete(schema.storeListings) .where(