diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e1251790..71a2569e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -76,6 +76,7 @@ * ffi: remove `profile` module ([Yuki Kishimoto]) * ffi: remove `NostrLibrary` struct and keep only `git_hash_version` func ([Yuki Kishimoto]) * ffi: remove embedded tor client ([Yuki Kishimoto]) +* bindings: cleanup `Relay` methods ([Yuki Kishimoto]) ### Deprecated diff --git a/bindings/nostr-sdk-ffi/src/relay/mod.rs b/bindings/nostr-sdk-ffi/src/relay/mod.rs index 46bc9f7d2..ef7e27021 100644 --- a/bindings/nostr-sdk-ffi/src/relay/mod.rs +++ b/bindings/nostr-sdk-ffi/src/relay/mod.rs @@ -3,11 +3,9 @@ // Distributed under the MIT software license use std::collections::HashMap; -use std::ops::Deref; use std::sync::Arc; -use std::time::Duration; -use nostr_sdk::{pool, RelayUrl, SubscriptionId}; +use nostr_sdk::{pool, SubscriptionId}; use uniffi::{Object, Record}; pub mod filtering; @@ -18,15 +16,10 @@ pub mod status; pub use self::filtering::{RelayFiltering, RelayFilteringMode}; pub use self::limits::RelayLimits; -use self::options::SyncOptions; pub use self::options::{ConnectionMode, RelayOptions, ReqExitPolicy, SubscribeOptions}; pub use self::stats::RelayConnectionStats; pub use self::status::RelayStatus; -use crate::database::events::Events; -use crate::database::NostrDatabase; -use crate::error::Result; -use crate::negentropy::NegentropyItem; -use crate::protocol::{ClientMessage, Event, EventId, Filter, RelayInformationDocument}; +use crate::protocol::{Event, EventId, Filter, RelayInformationDocument}; #[derive(Record)] pub struct ReconciliationSendFailureItem { @@ -100,34 +93,6 @@ impl From for Relay { #[uniffi::export(async_runtime = "tokio")] impl Relay { - /// Create new `Relay` with **default** `options` and `in-memory database` - #[uniffi::constructor] - pub fn new(url: &str) -> Result { - let url: RelayUrl = RelayUrl::parse(url)?; - Ok(Self { - inner: nostr_sdk::Relay::new(url), - }) - } - - /// Create new `Relay` with default `in-memory database` and custom `options` - #[uniffi::constructor] - pub fn with_opts(url: &str, opts: &RelayOptions) -> Result { - let url: RelayUrl = RelayUrl::parse(url)?; - let opts = opts.deref().clone(); - Ok(Self { - inner: nostr_sdk::Relay::with_opts(url, opts), - }) - } - - /// Create new `Relay` with **custom** `database` and/or `options` - #[uniffi::constructor] - pub fn custom(url: &str, database: &NostrDatabase, opts: &RelayOptions) -> Result { - let url: RelayUrl = RelayUrl::parse(url)?; - Ok(Self { - inner: nostr_sdk::Relay::custom(url, database.deref().clone(), opts.deref().clone()), - }) - } - /// Get relay url pub fn url(&self) -> String { self.inner.url().to_string() @@ -148,209 +113,20 @@ impl Relay { self.inner.flags() } */ - /// Get relay filtering - pub fn filtering(&self) -> RelayFiltering { - self.inner.filtering().clone().into() - } - /// Check if `Relay` is connected pub fn is_connected(&self) -> bool { self.inner.is_connected() } - pub async fn document(&self) -> Arc { - Arc::new(self.inner.document().await.into()) - } - - pub async fn subscriptions(&self) -> HashMap>> { - self.inner - .subscriptions() - .await - .into_iter() - .map(|(id, filters)| { - ( - id.to_string(), - filters.into_iter().map(|f| Arc::new(f.into())).collect(), - ) - }) - .collect() - } - - /// Get filters by subscription ID - pub async fn subscription(&self, id: String) -> Option>> { - let id = SubscriptionId::new(id); - self.inner - .subscription(&id) - .await - .map(|f| f.into_iter().map(|f| Arc::new(f.into())).collect()) + pub async fn document(&self) -> RelayInformationDocument { + self.inner.document().await.into() } pub fn opts(&self) -> RelayOptions { self.inner.opts().clone().into() } - pub fn stats(&self) -> Arc { - Arc::new(self.inner.stats().clone().into()) - } - - // TODO: add notifications - - /// Connect to relay - /// - /// This method returns immediately and doesn't provide any information on if the connection was successful or not. - pub fn connect(&self) { - self.inner.connect() - } - - /// Try to connect to relay - /// - /// This method returns an error if the connection fails. - /// If the connection fails, - /// a task will continue to retry in the background (unless configured differently in `RelayOptions`. - pub async fn try_connect(&self, timeout: Duration) -> Result<()> { - Ok(self.inner.try_connect(timeout).await?) - } - - /// Disconnect from relay and set status to 'Terminated' - pub fn disconnect(&self) -> Result<()> { - Ok(self.inner.disconnect()?) - } - - /// Send msg to relay - pub fn send_msg(&self, msg: Arc) -> Result<()> { - Ok(self.inner.send_msg(msg.as_ref().deref().clone())?) - } - - /// Send multiple `ClientMessage` at once - pub fn batch_msg(&self, msgs: Vec>) -> Result<()> { - let msgs = msgs - .into_iter() - .map(|msg| msg.as_ref().deref().clone()) - .collect(); - Ok(self.inner.batch_msg(msgs)?) - } - - /// Send event and wait for `OK` relay msg - pub async fn send_event(&self, event: &Event) -> Result> { - Ok(Arc::new( - self.inner.send_event(event.deref().clone()).await?.into(), - )) - } - - /// Subscribe to filters - /// - /// Internally generate a new random subscription ID. Check `subscribe_with_id` method to use a custom subscription ID. - /// - /// ### Auto-closing subscription - /// - /// It's possible to automatically close a subscription by configuring the `SubscribeOptions`. - /// - /// Note: auto-closing subscriptions aren't saved in subscriptions map! - pub async fn subscribe( - &self, - filters: Vec>, - opts: &SubscribeOptions, - ) -> Result { - Ok(self - .inner - .subscribe( - filters - .into_iter() - .map(|f| f.as_ref().deref().clone()) - .collect(), - **opts, - ) - .await? - .to_string()) - } - - /// Subscribe with custom subscription ID - /// - /// ### Auto-closing subscription - /// - /// It's possible to automatically close a subscription by configuring the `SubscribeOptions`. - /// - /// Note: auto-closing subscriptions aren't saved in subscriptions map! - pub async fn subscribe_with_id( - &self, - id: String, - filters: Vec>, - opts: &SubscribeOptions, - ) -> Result<()> { - Ok(self - .inner - .subscribe_with_id( - SubscriptionId::new(id), - filters - .into_iter() - .map(|f| f.as_ref().deref().clone()) - .collect(), - **opts, - ) - .await?) - } - - /// Unsubscribe - pub async fn unsubscribe(&self, id: String) -> Result<()> { - Ok(self.inner.unsubscribe(SubscriptionId::new(id)).await?) - } - - /// Unsubscribe from all subscriptions - pub async fn unsubscribe_all(&self) -> Result<()> { - Ok(self.inner.unsubscribe_all().await?) - } - - /// Fetch events - pub async fn fetch_events( - &self, - filters: Vec>, - timeout: Duration, - policy: ReqExitPolicy, - ) -> Result { - let filters = filters - .into_iter() - .map(|f| f.as_ref().deref().clone()) - .collect(); - Ok(self - .inner - .fetch_events(filters, timeout, policy.into()) - .await? - .into()) - } - - /// Count events - pub async fn count_events(&self, filters: Vec>, timeout: Duration) -> Result { - let filters = filters - .into_iter() - .map(|f| f.as_ref().deref().clone()) - .collect(); - Ok(self.inner.count_events(filters, timeout).await? as u64) - } - - /// Sync events with relays (negentropy reconciliation) - pub async fn sync(&self, filter: &Filter, opts: &SyncOptions) -> Result { - Ok(self - .inner - .sync(filter.deref().clone(), opts.deref()) - .await? - .into()) - } - - /// Sync events with relays (negentropy reconciliation) - pub async fn sync_with_items( - &self, - filter: &Filter, - items: Vec, - opts: &SyncOptions, - ) -> Result { - let items = items - .into_iter() - .map(|item| (**item.id, **item.timestamp)) - .collect(); - Ok(self - .inner - .sync_with_items(filter.deref().clone(), items, opts.deref()) - .await? - .into()) + pub fn stats(&self) -> RelayConnectionStats { + self.inner.stats().clone().into() } } diff --git a/bindings/nostr-sdk-js/src/relay/mod.rs b/bindings/nostr-sdk-js/src/relay/mod.rs index 72bb05a34..15d61d910 100644 --- a/bindings/nostr-sdk-js/src/relay/mod.rs +++ b/bindings/nostr-sdk-js/src/relay/mod.rs @@ -101,16 +101,6 @@ impl From for JsRelayStatus { #[wasm_bindgen(js_class = Relay)] impl JsRelay { - /// Create new `Relay` with `in-memory` database - #[wasm_bindgen(constructor)] - pub fn new(url: &str, opts: Option) -> Result { - let url: RelayUrl = RelayUrl::parse(url).map_err(into_err)?; - let opts: RelayOptions = opts.map(|o| o.deref().clone()).unwrap_or_default(); - Ok(Self { - inner: Relay::with_opts(url, opts), - }) - } - /// Get relay url pub fn url(&self) -> String { self.inner.url().to_string() @@ -126,11 +116,6 @@ impl JsRelay { self.inner.flags().clone().into() } - /// Get relay filtering - pub fn filtering(&self) -> JsRelayFiltering { - self.inner.filtering().clone().into() - } - /// Check if relay is connected #[wasm_bindgen(js_name = isConnected)] pub fn is_connected(&self) -> bool { @@ -142,149 +127,10 @@ impl JsRelay { self.inner.document().await.into() } - // TODO: ad subscriptions - - // TODO: add subscription - /// Get options pub fn opts(&self) -> JsRelayOptions { self.inner.opts().clone().into() } // TODO: add stats - - /// Connect to relay - /// - /// This method returns immediately and doesn't provide any information on if the connection was successful or not. - pub fn connect(&self) { - self.inner.connect() - } - - /// Try to connect to relay - /// - /// This method returns an error if the connection fails. - /// If the connection fails, - /// a task will continue to retry in the background (unless configured differently in `RelayOptions`. - #[wasm_bindgen(js_name = tryConnect)] - pub async fn try_connect(&self, timeout: &JsDuration) -> Result<()> { - self.inner.try_connect(**timeout).await.map_err(into_err) - } - - /// Disconnect from relay and set status to 'Terminated' - pub fn disconnect(&self) -> Result<()> { - self.inner.disconnect().map_err(into_err) - } - - /// Send msg to relay - #[wasm_bindgen(js_name = sendMsg)] - pub fn send_msg(&self, msg: &JsClientMessage) -> Result<()> { - self.inner.send_msg(msg.deref().clone()).map_err(into_err) - } - - /// Send multiple `ClientMessage` at once - #[wasm_bindgen(js_name = batchMsg)] - pub fn batch_msg(&self, msgs: Vec) -> Result<()> { - let msgs = msgs.into_iter().map(|msg| msg.deref().clone()).collect(); - self.inner.batch_msg(msgs).map_err(into_err) - } - - /// Send event and wait for `OK` relay msg - #[wasm_bindgen(js_name = sendEvent)] - pub async fn send_event(&self, event: &JsEvent) -> Result { - Ok(self - .inner - .send_event(event.deref().clone()) - .await - .map_err(into_err)? - .into()) - } - - /// Subscribe to filters - /// - /// ### Auto-closing subscription - /// - /// It's possible to automatically close a subscription by configuring the `SubscribeOptions`. - pub async fn subscribe( - &self, - filters: Vec, - opts: &JsSubscribeOptions, - ) -> Result { - let filters: Vec = filters.into_iter().map(|f| f.into()).collect(); - Ok(self - .inner - .subscribe(filters, **opts) // TODO: allow to pass opts as reference - .await - .map_err(into_err)? - .to_string()) - } - - /// Subscribe with custom subscription ID - /// - /// ### Auto-closing subscription - /// - /// It's possible to automatically close a subscription by configuring the `SubscribeOptions`. - #[wasm_bindgen(js_name = subscribeWithId)] - pub async fn subscribe_with_id( - &self, - id: &str, - filters: Vec, - opts: &JsSubscribeOptions, - ) -> Result<()> { - let filters: Vec = filters.into_iter().map(|f| f.into()).collect(); - self.inner - .subscribe_with_id(SubscriptionId::new(id), filters, **opts) // TODO: allow to pass opts as reference - .await - .map_err(into_err) - } - - /// Unsubscribe - pub async fn unsubscribe(&self, id: String) -> Result<()> { - self.inner - .unsubscribe(SubscriptionId::new(id)) - .await - .map_err(into_err) - } - - /// Unsubscribe from all subscriptions - #[wasm_bindgen(js_name = unsubscribeAll)] - pub async fn unsubscribe_all(&self) -> Result<()> { - self.inner.unsubscribe_all().await.map_err(into_err) - } - - /// Fetch events - #[wasm_bindgen(js_name = fetchEvents)] - pub async fn fetch_events( - &self, - filters: Vec, - timeout: &JsDuration, - policy: &JsReqExitPolicy, - ) -> Result { - let filters: Vec = filters.into_iter().map(|f| f.into()).collect(); - Ok(self - .inner - .fetch_events(filters, **timeout, **policy) - .await - .map_err(into_err)? - .into()) - } - - /// Count events - #[wasm_bindgen(js_name = countEvents)] - pub async fn count_events(&self, filters: Vec, timeout: &JsDuration) -> Result { - let filters: Vec = filters.into_iter().map(|f| f.into()).collect(); - Ok(self - .inner - .count_events(filters, **timeout) - .await - .map_err(into_err)? as u64) - } - - /// Sync events with relay (negentropy reconciliation) - pub async fn sync(&self, filter: &JsFilter, opts: &JsSyncOptions) -> Result { - self.inner - .sync(filter.deref().clone(), opts.deref()) - .await - .map_err(into_err) - .map(|o| o.into()) - } }