Skip to content

Commit

Permalink
ffi(sdk): add RelayPool
Browse files Browse the repository at this point in the history
Impl PartialEq, Eq and Hash for EventId
Add `Relay::reconcile_with_items`

Closes #324
  • Loading branch information
yukibtc committed Mar 11, 2024
1 parent 3c5d657 commit 6b370e2
Show file tree
Hide file tree
Showing 7 changed files with 464 additions and 17 deletions.
2 changes: 1 addition & 1 deletion bindings/nostr-ffi/src/event/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use super::Kind;
use crate::error::Result;
use crate::{PublicKey, Tag, Timestamp};

#[derive(Object)]
#[derive(PartialEq, Eq, Hash, Object)]
pub struct EventId {
inner: nostr::EventId,
}
Expand Down
11 changes: 2 additions & 9 deletions bindings/nostr-sdk-ffi/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
// Distributed under the MIT software license

use std::collections::HashMap;
use std::fmt::Debug;
use std::ops::Deref;
use std::sync::Arc;
use std::time::Duration;

use async_utility::thread;
use nostr_ffi::{
ClientMessage, Event, EventBuilder, EventId, FileMetadata, Filter, Metadata, PublicKey,
RelayMessage, Timestamp,
Timestamp,
};
use nostr_sdk::client::Client as ClientSdk;
use nostr_sdk::pool::RelayPoolNotification as RelayPoolNotificationSdk;
Expand All @@ -29,7 +28,7 @@ pub use self::signer::NostrSigner;
use self::zapper::{ZapDetails, ZapEntity};
use crate::error::Result;
use crate::relay::options::{NegentropyOptions, SubscribeAutoCloseOptions};
use crate::{NostrDatabase, Relay};
use crate::{HandleNotification, NostrDatabase, Relay};

#[derive(Object)]
pub struct Client {
Expand Down Expand Up @@ -529,9 +528,3 @@ impl Client {
Ok(())
}
}

#[uniffi::export(callback_interface)]
pub trait HandleNotification: Send + Sync + Debug {
fn handle_msg(&self, relay_url: String, msg: Arc<RelayMessage>);
fn handle(&self, relay_url: String, subscription_id: String, event: Arc<Event>);
}
6 changes: 6 additions & 0 deletions bindings/nostr-sdk-ffi/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ impl From<nostr_sdk::pool::relay::Error> for NostrSdkError {
}
}

impl From<nostr_sdk::pool::pool::Error> for NostrSdkError {
fn from(e: nostr_sdk::pool::pool::Error) -> NostrSdkError {
Self::Generic(e.to_string())
}
}

impl From<AddrParseError> for NostrSdkError {
fn from(e: AddrParseError) -> NostrSdkError {
Self::Generic(e.to_string())
Expand Down
13 changes: 8 additions & 5 deletions bindings/nostr-sdk-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,21 @@ pub mod client;
pub mod database;
pub mod error;
pub mod logger;
pub mod notifications;
pub mod nwc;
pub mod pool;
pub mod profile;
pub mod relay;

trait FromResult<T>: Sized {
fn from_result(_: T) -> error::Result<Self>;
}

pub use crate::client::{Client, ClientBuilder, HandleNotification, Options};
pub use crate::database::NostrDatabase;
pub use crate::error::NostrSdkError;
pub use crate::logger::{init_logger, LogLevel};
pub use crate::relay::{Relay, RelayConnectionStats, RelayStatus};
pub use self::client::{Client, ClientBuilder, Options};
pub use self::database::NostrDatabase;
pub use self::error::NostrSdkError;
pub use self::logger::{init_logger, LogLevel};
pub use self::notifications::HandleNotification;
pub use self::relay::{Relay, RelayConnectionStats, RelayStatus};

uniffi::setup_scaffolding!("nostr_sdk");
14 changes: 14 additions & 0 deletions bindings/nostr-sdk-ffi/src/notifications.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) 2022-2023 Yuki Kishimoto
// Copyright (c) 2023-2024 Rust Nostr Developers
// Distributed under the MIT software license

use std::fmt::Debug;
use std::sync::Arc;

use nostr_ffi::{Event, RelayMessage};

#[uniffi::export(callback_interface)]
pub trait HandleNotification: Send + Sync + Debug {
fn handle_msg(&self, relay_url: String, msg: Arc<RelayMessage>);
fn handle(&self, relay_url: String, subscription_id: String, event: Arc<Event>);
}
Loading

0 comments on commit 6b370e2

Please sign in to comment.