Skip to content

Commit

Permalink
database: add NostrEventsDatabase trait
Browse files Browse the repository at this point in the history
Signed-off-by: Yuki Kishimoto <[email protected]>
  • Loading branch information
yukibtc committed Nov 22, 2024
1 parent 64c6593 commit f7cc854
Show file tree
Hide file tree
Showing 35 changed files with 407 additions and 478 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
* nostr: add `NostrSigner::backend` ([Yuki Kishimoto])
* nostr: add `EventBuilder::private_msg` ([Yuki Kishimoto])
* nostr: add `EventBuilder::tag` and `EventBuilder::tags` ([Yuki Kishimoto])
* database: add `NostrEventsDatabase` trait ([Yuki Kishimoto])
* pool: add relay reconnection and disconnection unit tests ([Yuki Kishimoto])
* sdk: allow to specify relay pool notification channel size in `Options` ([Yuki Kishimoto])
* relay-builder: add `RelayTestOptions` ([Yuki Kishimoto])
Expand Down
10 changes: 3 additions & 7 deletions bindings/nostr-sdk-ffi/src/client/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
use std::ops::Deref;
use std::sync::Arc;

use nostr_sdk::database::DynNostrDatabase;
use nostr_sdk::zapper::DynNostrZapper;
use uniffi::Object;

use super::zapper::NostrZapper;
Expand Down Expand Up @@ -42,16 +40,14 @@ impl ClientBuilder {
}

pub fn zapper(self: Arc<Self>, zapper: &NostrZapper) -> Self {
let zapper: Arc<DynNostrZapper> = zapper.deref().clone();
let mut builder = unwrap_or_clone_arc(self);
builder.inner = builder.inner.zapper(zapper);
builder.inner = builder.inner.zapper(zapper.deref().clone());
builder
}

pub fn database(self: Arc<Self>, database: Arc<NostrDatabase>) -> Self {
let database: Arc<DynNostrDatabase> = database.as_ref().into();
pub fn database(self: Arc<Self>, database: &NostrDatabase) -> Self {
let mut builder = unwrap_or_clone_arc(self);
builder.inner = builder.inner.database(database);
builder.inner = builder.inner.database(database.deref().clone());
builder
}

Expand Down
10 changes: 5 additions & 5 deletions bindings/nostr-sdk-ffi/src/database/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@
use std::ops::Deref;
use std::sync::Arc;

use nostr_sdk::database;
use nostr_sdk::prelude;
use uniffi::Object;

use crate::protocol::Event;

#[derive(Clone, Object)]
pub struct Events {
inner: database::Events,
inner: prelude::Events,
}

impl From<database::Events> for Events {
fn from(inner: database::Events) -> Self {
impl From<prelude::Events> for Events {
fn from(inner: prelude::Events) -> Self {
Self { inner }
}
}

impl Deref for Events {
type Target = database::Events;
type Target = prelude::Events;

fn deref(&self) -> &Self::Target {
&self.inner
Expand Down
29 changes: 17 additions & 12 deletions bindings/nostr-sdk-ffi/src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use std::ops::Deref;
use std::sync::Arc;

use nostr_sdk::database::{DynNostrDatabase, IntoNostrDatabase, NostrDatabaseExt};
use nostr_sdk::prelude::{self, IntoNostrDatabase, NostrEventsDatabaseExt};
#[cfg(feature = "ndb")]
use nostr_sdk::NdbDatabase;
#[cfg(feature = "lmdb")]
Expand All @@ -16,23 +16,24 @@ pub mod events;

use self::events::Events;
use crate::error::Result;
use crate::profile::Profile;
use crate::protocol::{Event, EventId, Filter, PublicKey};
use crate::protocol::{Event, EventId, Filter, Metadata, PublicKey};

#[derive(Object)]
pub struct NostrDatabase {
inner: Arc<DynNostrDatabase>,
inner: Arc<dyn prelude::NostrDatabase>,
}

impl From<Arc<DynNostrDatabase>> for NostrDatabase {
fn from(inner: Arc<DynNostrDatabase>) -> Self {
Self { inner }
impl Deref for NostrDatabase {
type Target = Arc<dyn prelude::NostrDatabase>;

fn deref(&self) -> &Self::Target {
&self.inner
}
}

impl From<&NostrDatabase> for Arc<DynNostrDatabase> {
fn from(db: &NostrDatabase) -> Self {
db.inner.clone()
impl From<Arc<dyn prelude::NostrDatabase>> for NostrDatabase {
fn from(inner: Arc<dyn prelude::NostrDatabase>) -> Self {
Self { inner }
}
}

Expand Down Expand Up @@ -112,7 +113,11 @@ impl NostrDatabase {
Ok(self.inner.wipe().await?)
}

pub async fn profile(&self, public_key: &PublicKey) -> Result<Arc<Profile>> {
Ok(Arc::new(self.inner.profile(**public_key).await?.into()))
pub async fn metadata(&self, public_key: &PublicKey) -> Result<Option<Arc<Metadata>>> {
Ok(self
.inner
.metadata(**public_key)
.await?
.map(|m| Arc::new(m.into())))
}
}
4 changes: 2 additions & 2 deletions bindings/nostr-sdk-ffi/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,8 @@ impl From<nostr_sdk::pool::pool::Error> for NostrSdkError {
}
}

impl From<nostr_sdk::database::DatabaseError> for NostrSdkError {
fn from(e: nostr_sdk::database::DatabaseError) -> NostrSdkError {
impl From<nostr_sdk::prelude::DatabaseError> for NostrSdkError {
fn from(e: nostr_sdk::prelude::DatabaseError) -> NostrSdkError {
Self::Generic(e.to_string())
}
}
Expand Down
7 changes: 4 additions & 3 deletions bindings/nostr-sdk-ffi/src/pool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use std::ops::Deref;
use std::sync::Arc;
use std::time::Duration;

use nostr_sdk::database::DynNostrDatabase;
use nostr_sdk::{RelayPoolOptions, SubscriptionId};
use uniffi::Object;

Expand Down Expand Up @@ -46,9 +45,11 @@ impl RelayPool {
/// Create new `RelayPool` with `custom` database
#[uniffi::constructor]
pub fn with_database(database: &NostrDatabase) -> Self {
let database: Arc<DynNostrDatabase> = database.into();
Self {
inner: nostr_sdk::RelayPool::with_database(RelayPoolOptions::default(), database),
inner: nostr_sdk::RelayPool::with_database(
RelayPoolOptions::default(),
database.deref().clone(),
),
}
}

Expand Down
10 changes: 5 additions & 5 deletions bindings/nostr-sdk-ffi/src/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
use std::ops::Deref;
use std::sync::Arc;

use nostr_sdk::database;
use nostr_sdk::prelude;
use uniffi::Object;

use crate::protocol::{Metadata, PublicKey};

#[derive(Debug, PartialEq, Eq, Hash, Object)]
#[uniffi::export(Debug, Eq, Hash)]
pub struct Profile {
inner: database::Profile,
inner: prelude::Profile,
}

impl From<database::Profile> for Profile {
fn from(inner: database::Profile) -> Self {
impl From<prelude::Profile> for Profile {
fn from(inner: prelude::Profile) -> Self {
Self { inner }
}
}
Expand All @@ -28,7 +28,7 @@ impl Profile {
#[uniffi::constructor]
pub fn new(public_key: &PublicKey, metadata: Arc<Metadata>) -> Self {
Self {
inner: database::Profile::new(**public_key, metadata.as_ref().deref().clone()),
inner: prelude::Profile::new(**public_key, metadata.as_ref().deref().clone()),
}
}

Expand Down
5 changes: 1 addition & 4 deletions bindings/nostr-sdk-ffi/src/relay/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use std::ops::Deref;
use std::sync::Arc;
use std::time::Duration;

use nostr_sdk::database::DynNostrDatabase;
use nostr_sdk::{pool, FilterOptions, SubscriptionId, Url};
use uniffi::{Object, Record};

Expand Down Expand Up @@ -124,10 +123,8 @@ impl Relay {
#[uniffi::constructor]
pub fn custom(url: String, database: &NostrDatabase, opts: &RelayOptions) -> Result<Self> {
let url: Url = Url::parse(&url)?;
let database: Arc<DynNostrDatabase> = database.into();
let opts = opts.deref().clone();
Ok(Self {
inner: nostr_sdk::Relay::custom(url, database, opts),
inner: nostr_sdk::Relay::custom(url, database.deref().clone(), opts.deref().clone()),
})
}

Expand Down
4 changes: 1 addition & 3 deletions bindings/nostr-sdk-js/src/client/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// Distributed under the MIT software license

use std::ops::Deref;
use std::sync::Arc;

use nostr_sdk::prelude::*;
use wasm_bindgen::prelude::*;
Expand Down Expand Up @@ -43,8 +42,7 @@ impl JsClientBuilder {
}

pub fn database(self, database: &JsNostrDatabase) -> Self {
let database: Arc<DynNostrDatabase> = database.into();
self.inner.database(database).into()
self.inner.database(database.deref().clone()).into()
}

pub fn opts(self, opts: &JsOptions) -> Self {
Expand Down
30 changes: 15 additions & 15 deletions bindings/nostr-sdk-js/src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,43 @@ use std::ops::Deref;
use std::sync::Arc;

use js_sys::Array;
use nostr_sdk::database::{DynNostrDatabase, IntoNostrDatabase, NostrDatabaseExt};
use nostr_sdk::WebDatabase;
use nostr_sdk::prelude::*;
use wasm_bindgen::prelude::*;

pub mod events;

pub use self::events::JsEvents;
use crate::error::{into_err, Result};
use crate::profile::JsProfile;
use crate::protocol::event::{JsEvent, JsEventId};
use crate::protocol::key::JsPublicKey;
use crate::protocol::types::JsFilter;
use crate::protocol::types::{JsFilter, JsMetadata};
use crate::JsStringArray;

/// Nostr Database
#[wasm_bindgen(js_name = NostrDatabase)]
pub struct JsNostrDatabase {
inner: Arc<DynNostrDatabase>,
inner: Arc<dyn NostrDatabase>,
}

impl From<Arc<DynNostrDatabase>> for JsNostrDatabase {
fn from(inner: Arc<DynNostrDatabase>) -> Self {
Self { inner }
impl Deref for JsNostrDatabase {
type Target = Arc<dyn NostrDatabase>;

fn deref(&self) -> &Self::Target {
&self.inner
}
}

impl From<&JsNostrDatabase> for Arc<DynNostrDatabase> {
fn from(db: &JsNostrDatabase) -> Self {
db.inner.clone()
impl From<Arc<dyn NostrDatabase>> for JsNostrDatabase {
fn from(inner: Arc<dyn NostrDatabase>) -> Self {
Self { inner }
}
}

#[wasm_bindgen(js_class = NostrDatabase)]
impl JsNostrDatabase {
/// Open/Create database with **unlimited** capacity
pub async fn indexeddb(name: &str) -> Result<JsNostrDatabase> {
let db = Arc::new(WebDatabase::open(name).await.map_err(into_err)?);
let db = WebDatabase::open(name).await.map_err(into_err)?;
Ok(Self {
inner: db.into_nostr_database(),
})
Expand Down Expand Up @@ -114,12 +114,12 @@ impl JsNostrDatabase {
self.inner.wipe().await.map_err(into_err)
}

pub async fn profile(&self, public_key: &JsPublicKey) -> Result<JsProfile> {
pub async fn metadata(&self, public_key: &JsPublicKey) -> Result<Option<JsMetadata>> {
Ok(self
.inner
.profile(**public_key)
.metadata(**public_key)
.await
.map_err(into_err)?
.into())
.map(|m| m.into()))
}
}
4 changes: 1 addition & 3 deletions bindings/nostr-sdk-js/src/pool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// Distributed under the MIT software license

use std::ops::Deref;
use std::sync::Arc;

use js_sys::Array;
use nostr_sdk::prelude::*;
Expand Down Expand Up @@ -46,9 +45,8 @@ impl JsRelayPool {
/// Create new `RelayPool` with `custom` database
#[wasm_bindgen(js_name = withDatabase)]
pub fn with_database(database: &JsNostrDatabase) -> Self {
let database: Arc<DynNostrDatabase> = database.into();
Self {
inner: RelayPool::with_database(RelayPoolOptions::default(), database),
inner: RelayPool::with_database(RelayPoolOptions::default(), database.deref().clone()),
}
}

Expand Down
2 changes: 1 addition & 1 deletion bindings/nostr-sdk-js/src/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use std::ops::Deref;

use nostr_sdk::database::Profile;
use nostr_sdk::prelude::*;
use wasm_bindgen::prelude::*;

use crate::protocol::key::JsPublicKey;
Expand Down
71 changes: 0 additions & 71 deletions crates/nostr-database/examples/helper.rs

This file was deleted.

Loading

0 comments on commit f7cc854

Please sign in to comment.