Skip to content

Commit 5627c40

Browse files
committed
pool: fix Event notification variant sent also for events sent by the SDK
Closes #508 Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent c710620 commit 5627c40

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060

6161
### Fixed
6262

63+
* pool: fix `Event` notification variant sent also for events sent by the SDK ([Yuki Kishimoto])
6364
* database: fix indexes `QueryPattern` ([Yuki Kishimoto])
6465

6566
### Removed

crates/nostr-database/src/memory.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,21 @@ impl MemoryDatabase {
7979
&self,
8080
seen_event_ids: &mut LruCache<EventId, HashSet<Url>>,
8181
event_id: EventId,
82-
relay_url: Url,
82+
relay_url: Option<Url>,
8383
) {
8484
match seen_event_ids.get_mut(&event_id) {
8585
Some(set) => {
86-
set.insert(relay_url);
86+
if let Some(url) = relay_url {
87+
set.insert(url);
88+
}
8789
}
8890
None => {
89-
let mut set = HashSet::with_capacity(1);
90-
set.insert(relay_url);
91+
let mut set: HashSet<Url> = HashSet::new();
92+
93+
if let Some(url) = relay_url {
94+
set.insert(url);
95+
}
96+
9197
seen_event_ids.put(event_id, set);
9298
}
9399
}
@@ -125,6 +131,10 @@ impl NostrDatabase for MemoryDatabase {
125131
Ok(false)
126132
}
127133
} else {
134+
// Mark it as seen
135+
let mut seen_event_ids = self.seen_event_ids.lock().await;
136+
self._event_id_seen(&mut seen_event_ids, event.id(), None);
137+
128138
Ok(false)
129139
}
130140
}
@@ -178,7 +188,7 @@ impl NostrDatabase for MemoryDatabase {
178188

179189
async fn event_id_seen(&self, event_id: EventId, relay_url: Url) -> Result<(), Self::Err> {
180190
let mut seen_event_ids = self.seen_event_ids.lock().await;
181-
self._event_id_seen(&mut seen_event_ids, event_id, relay_url);
191+
self._event_id_seen(&mut seen_event_ids, event_id, Some(relay_url));
182192
Ok(())
183193
}
184194

crates/nostr-relay-pool/src/relay/internal.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,25 +1024,25 @@ impl InternalRelay {
10241024
return Err(Error::EventExpired);
10251025
}
10261026

1027-
// Check if saved
1028-
if !saved {
1029-
// Verify event
1027+
// Verify event
1028+
if !seen && !saved {
10301029
event.verify()?;
1030+
}
10311031

1032-
// Save event
1032+
// Save event
1033+
if !saved {
10331034
self.database.save_event(&event).await?;
10341035
}
10351036

10361037
// Box event
1037-
let event: Box<Event> = Box::new(event);
1038+
let boxed_event: Box<Event> = Box::new(event);
10381039

1039-
// Check if seen
1040-
if !seen {
1041-
// Send notification
1040+
// Send notification
1041+
if !seen && !saved {
10421042
self.send_notification(
10431043
RelayNotification::Event {
10441044
subscription_id: SubscriptionId::new(&subscription_id),
1045-
event: event.clone(),
1045+
event: boxed_event.clone(),
10461046
},
10471047
true,
10481048
)
@@ -1051,7 +1051,7 @@ impl InternalRelay {
10511051

10521052
Ok(Some(RelayMessage::Event {
10531053
subscription_id: SubscriptionId::new(subscription_id),
1054-
event,
1054+
event: boxed_event,
10551055
}))
10561056
}
10571057
m => Ok(Some(RelayMessage::try_from(m)?)),

0 commit comments

Comments
 (0)