Skip to content

Commit

Permalink
Subscribe to all events, skip duplicates.
Browse files Browse the repository at this point in the history
  • Loading branch information
chmac committed Aug 11, 2024
1 parent c52c3b7 commit 0d31eaf
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/nostr/subscribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export const subscribe = async ({
return;
};

/*
console.log("#6MgNzq Starting subscription", eventsFilter);
// NOTE: This just handles events until the EOSE event. It does not maintain
// an active subscription. So we do that below.
Expand All @@ -106,6 +107,7 @@ export const subscribe = async ({
events.forEach(onNoteEvent);
logStateToConsole();
*/

backgroundNoteEventsFetching(onNoteEvent);
};
Expand Down Expand Up @@ -133,15 +135,27 @@ function fetchProfileFactory(pubKey) {
}

async function backgroundNoteEventsFetching(onEventReceived) {
const seenEventIdentifiers = new Set<string>();

const relayPool = await _initRelays();
const filter = {
kinds: [MAP_NOTE_REPOST_KIND],
"#L": ["open-location-code"],
since: Math.floor(Date.now() / 1000),
// since: Math.floor(Date.now() / 1000),
authors: TRUSTED_VALIDATION_PUBKEYS,
};
for await (const msg of relayPool.req([filter])) {
if (msg[0] === "EVENT") onEventReceived(msg[2] as Kind30398Event);
for await (const message of relayPool.req([filter])) {
const [messageType, subscriptionId, event] = message;
if (messageType === "EVENT") {
const eventAuthorPublicKey = getPublicKeyFromEvent({ event });
const eventDTag = getTagFirstValueFromEvent({ event, tag: "d" });
const eventIdentifier = `${eventAuthorPublicKey}.${eventDTag}`;
if (seenEventIdentifiers.has(eventIdentifier)) {
continue;
}
seenEventIdentifiers.add(eventIdentifier);
onEventReceived(event as Kind30398Event);
}
}
}

Expand Down

0 comments on commit 0d31eaf

Please sign in to comment.