diff --git a/crates/nostr-database/src/lib.rs b/crates/nostr-database/src/lib.rs index b1ae4e4e2..9c1d8e437 100644 --- a/crates/nostr-database/src/lib.rs +++ b/crates/nostr-database/src/lib.rs @@ -68,6 +68,9 @@ pub trait NostrDatabase: AsyncTraitDeps { /// Return `true` if event was successfully saved into database. async fn save_event(&self, event: &Event) -> Result; + /// Check if [`Event`] has already been saved + async fn has_event_already_been_saved(&self, event_id: EventId) -> Result; + /// Check if [`EventId`] has already been seen async fn has_event_already_been_seen(&self, event_id: EventId) -> Result; diff --git a/crates/nostr-database/src/memory.rs b/crates/nostr-database/src/memory.rs index 90ed4f7ad..7bc5da179 100644 --- a/crates/nostr-database/src/memory.rs +++ b/crates/nostr-database/src/memory.rs @@ -85,9 +85,6 @@ impl NostrDatabase for MemoryDatabase { } async fn save_event(&self, event: &Event) -> Result { - // Set event as seen - self.event_id_seen(event.id, None).await?; - if self.opts.events { let EventIndexResult { to_store, @@ -113,6 +110,15 @@ impl NostrDatabase for MemoryDatabase { } } + async fn has_event_already_been_saved(&self, event_id: EventId) -> Result { + if self.opts.events { + let events = self.events.read().await; + Ok(events.contains_key(&event_id)) + } else { + Ok(false) + } + } + async fn has_event_already_been_seen(&self, event_id: EventId) -> Result { let seen_event_ids = self.seen_event_ids.read().await; Ok(seen_event_ids.contains_key(&event_id)) diff --git a/crates/nostr-indexeddb/src/lib.rs b/crates/nostr-indexeddb/src/lib.rs index db3186931..9debb96cf 100644 --- a/crates/nostr-indexeddb/src/lib.rs +++ b/crates/nostr-indexeddb/src/lib.rs @@ -245,7 +245,12 @@ impl_nostr_database!({ } async fn count(&self) -> Result { - Err(DatabaseError::NotSupported.into()) + let tx = self + .db + .transaction_on_one_with_mode(EVENTS_CF, IdbTransactionMode::Readonly)?; + let store = tx.object_store(EVENTS_CF)?; + let count: u32 = store.count()?.await?; + Ok(count as usize) } #[tracing::instrument(skip_all, level = "trace")] @@ -282,11 +287,25 @@ impl_nostr_database!({ } } - async fn has_event_already_been_seen( + async fn has_event_already_been_saved( &self, - _event_id: EventId, + event_id: EventId, ) -> Result { - todo!() + let tx = self + .db + .transaction_on_one_with_mode(EVENTS_CF, IdbTransactionMode::Readonly)?; + let store = tx.object_store(EVENTS_CF)?; + let key = JsValue::from(event_id.to_hex()); + Ok(store.get(&key)?.await?.is_some()) + } + + async fn has_event_already_been_seen(&self, event_id: EventId) -> Result { + let tx = self + .db + .transaction_on_one_with_mode(EVENTS_SEEN_BY_RELAYS_CF, IdbTransactionMode::Readonly)?; + let store = tx.object_store(EVENTS_SEEN_BY_RELAYS_CF)?; + let key = JsValue::from(event_id.to_hex()); + Ok(store.get(&key)?.await?.is_some()) } async fn event_id_seen( diff --git a/crates/nostr-rocksdb/src/lib.rs b/crates/nostr-rocksdb/src/lib.rs index 3b95db1b3..ae6718d90 100644 --- a/crates/nostr-rocksdb/src/lib.rs +++ b/crates/nostr-rocksdb/src/lib.rs @@ -190,11 +190,16 @@ impl NostrDatabase for RocksDatabase { } } - async fn has_event_already_been_seen(&self, event_id: EventId) -> Result { + async fn has_event_already_been_saved(&self, event_id: EventId) -> Result { let cf = self.cf_handle(EVENTS_CF)?; Ok(self.db.key_may_exist_cf(&cf, event_id.as_bytes())) } + async fn has_event_already_been_seen(&self, event_id: EventId) -> Result { + let cf = self.cf_handle(EVENTS_SEEN_BY_RELAYS_CF)?; + Ok(self.db.key_may_exist_cf(&cf, event_id.as_bytes())) + } + async fn event_id_seen( &self, event_id: EventId,