Skip to content

Commit

Permalink
skip by default, PRE that have been tagged as "deleted"
Browse files Browse the repository at this point in the history
  • Loading branch information
pablof7z committed Nov 12, 2024
1 parent 5939a3e commit 7bddeff
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/ten-rats-pretend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nostr-dev-kit/ndk-svelte": minor
---

skip by default, PRE that have been tagged as "deleted"
44 changes: 32 additions & 12 deletions ndk-svelte/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export type NDKEventStore<T extends NDKEvent> = Writable<ExtendedBaseType<T>[]>
refCount: number;
subscription: NDKSubscription | undefined;
eosed: boolean;
skipDeleted: boolean;
startSubscription: () => void;
unsubscribe: Unsubscriber;
onEose: (cb: () => void) => void;
Expand Down Expand Up @@ -60,6 +61,12 @@ type NDKSubscribeOptions = NDKSubscriptionOptions & {
*/
relaySet?: NDKRelaySet;

/**
* Whether deleted PRE/addressable-events should be skipped.
* @default true
*/
skipDeleted?: boolean;

/**
* Callback to be called when the subscription EOSEs
*/
Expand All @@ -82,6 +89,7 @@ class NDKSvelte extends NDK {
id: Math.random().toString(36).substring(7),
refCount: 0,
filters,
skipDeleted: true,
subscription: undefined,
eosed: false,
set: store.set,
Expand Down Expand Up @@ -112,10 +120,6 @@ class NDKSvelte extends NDK {
return [NDKKind.Repost, NDKKind.GenericRepost].includes(event.kind!);
}

private eventIsLabel(event: NDKEvent): boolean {
return [NDKKind.Label].includes(event.kind!);
}

public storeSubscribe<T extends NDKEvent>(
filters: NDKFilter | NDKFilter[],
opts?: NDKSubscribeOptions,
Expand All @@ -127,10 +131,13 @@ class NDKSvelte extends NDK {
);
const autoStart = opts?.autoStart ?? true;
const relaySet = opts?.relaySet;
const skipDeleted = opts?.skipDeleted ?? true;

const handleEventLabel = (event: NDKEvent) => {
handleEventReposts(event);
};
/**
* Tracks the created_at of PRE that have been deleted.
* If the most recent version of a PRE is deleted, we don't include it in the store when skipDeleted is true.
*/
const deletedPRETimestamps = new Map<string, number>();

const getEventArrayFromMap = () => {
return Array.from(events.values());
Expand Down Expand Up @@ -197,11 +204,6 @@ class NDKSvelte extends NDK {
return;
}

if (this.eventIsLabel(event)) {
handleEventLabel(event);
return;
}

let e = event;
if (klass) {
const ev = klass.from(event);
Expand Down Expand Up @@ -233,6 +235,24 @@ class NDKSvelte extends NDK {
}
}

if (skipDeleted && event.isParamReplaceable()) {
const currentDeletedTimestamp = deletedPRETimestamps.get(dedupKey);

// if we already have a newer deletion than this event's, don't do anything
if (currentDeletedTimestamp && currentDeletedTimestamp > event.created_at!) return;

const isDeleted = event.hasTag("deleted");

if (isDeleted) {
// flag the deletion of this dTag
deletedPRETimestamps.set(dedupKey, event.created_at!);
return;
} else {
// remove any deletion flag and proceed to adding the event
deletedPRETimestamps.delete(dedupKey);
}
}

events.set(dedupKey, e as ExtendedBaseType<T>);

store.set(getEventArrayFromMap());
Expand Down

0 comments on commit 7bddeff

Please sign in to comment.