Skip to content

Commit

Permalink
sdk: save event in database if never seen before
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed Oct 16, 2023
1 parent a56989f commit 73b7d9e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
1 change: 1 addition & 0 deletions crates/nostr-sdk-db/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ impl From<Error> for DatabaseError {
#[derive(Debug, Default)]
pub struct MemoryDatabase {
seen_event_ids: Arc<RwLock<HashMap<EventId, HashSet<Url>>>>,
// TODO: add messages queue? (messages not sent)
}

impl MemoryDatabase {
Expand Down
16 changes: 5 additions & 11 deletions crates/nostr-sdk/src/relay/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,16 +433,6 @@ impl Relay {
self.relay_sender.max_capacity() - self.relay_sender.capacity()
}

/* fn is_auto_connect_loop_running(&self) -> bool {
self.auto_connect_loop_running.load(Ordering::SeqCst)
}
fn set_auto_connect_loop_running(&self, value: bool) {
let _ =
self.auto_connect_loop_running
.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |_| Some(value));
} */

fn is_scheduled_for_stop(&self) -> bool {
self.scheduled_for_stop.load(Ordering::SeqCst)
}
Expand Down Expand Up @@ -787,7 +777,11 @@ impl Relay {
if size <= max_size {
match RawRelayMessage::from_json(&data) {
Ok(msg) => {
tracing::trace!("Received message to {}: {:?}", relay.url, msg);
tracing::trace!(
"Received message from {}: {:?}",
relay.url,
msg
);
if let Err(err) = relay
.pool_sender
.send(RelayPoolMessage::ReceivedMsg {
Expand Down
11 changes: 10 additions & 1 deletion crates/nostr-sdk/src/relay/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,18 @@ impl RelayPoolTask {
{
Ok(seen) => {
if !seen {
if let Err(e) =
this.database.save_event(&event).await
{
tracing::error!(
"Impossible to save event {}: {e}",
event.id
);
}

let notification = RelayPoolNotification::Event(
relay_url.clone(),
event.as_ref().clone(),
*event.clone(),
);
let _ =
this.notification_sender.send(notification);
Expand Down

0 comments on commit 73b7d9e

Please sign in to comment.