Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions scripts/tap-consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
47 changes: 46 additions & 1 deletion src/lib/atproto/tap-listing-sync.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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();
});
});
128 changes: 128 additions & 0 deletions src/lib/atproto/tap-listing-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<boolean> {
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<string, unknown>,
);
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).
*
Expand All @@ -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);
Expand All @@ -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(
Expand Down
Loading