Răsfoiți Sursa

Added new crate personal-relayer

Cesar Rodas 3 luni în urmă
părinte
comite
58438f04ec

+ 1 - 0
Cargo.lock

@@ -941,6 +941,7 @@ dependencies = [
 name = "nostr-rs-personal-relayer"
 version = "0.1.0"
 dependencies = [
+ "futures",
  "nostr-rs-client",
  "nostr-rs-relayer",
  "nostr-rs-storage-base",

+ 13 - 0
crates/personal-relayer/Cargo.toml

@@ -0,0 +1,13 @@
+[package]
+name = "nostr-rs-personal-relayer"
+version = "0.1.0"
+edition = "2021"
+
+[dependencies]
+nostr-rs-types = { path = "../types" }
+nostr-rs-storage-base = { path = "../storage/base" }
+nostr-rs-client = { path = "../client" }
+nostr-rs-relayer = { path = "../relayer" }
+thiserror = "1.0.39"
+url = { version = "2.5.2", features = ["serde"] }
+futures = "0.3.30"

+ 48 - 0
crates/personal-relayer/src/lib.rs

@@ -0,0 +1,48 @@
+use futures::future::join_all;
+use nostr_rs_client::Pool;
+use nostr_rs_relayer::Relayer;
+use nostr_rs_storage_base::Storage;
+use nostr_rs_types::types::{Addr, Filter};
+use url::Url;
+
+pub struct PersonalRelayer<T: Storage + Send + Sync + 'static> {
+    relayer: Relayer<T>,
+    accounts: Vec<Addr>,
+}
+
+#[derive(thiserror::Error, Debug)]
+pub enum Error {
+    #[error("Relayer: {0}")]
+    Relayer(#[from] nostr_rs_relayer::Error),
+}
+
+impl<T: Storage + Send + Sync + 'static> PersonalRelayer<T> {
+    pub async fn new(
+        storage: T,
+        accounts: Vec<Addr>,
+        client_urls: Vec<Url>,
+    ) -> Result<Self, Error> {
+        let pool = Pool::new_with_clients(client_urls);
+
+        let subscriptions = join_all(
+            accounts
+                .iter()
+                .map(|account| {
+                    pool.subscribe(
+                        Filter {
+                            authors: vec![account.clone()],
+                            ..Default::default()
+                        }
+                        .into(),
+                    )
+                })
+                .collect::<Vec<_>>(),
+        )
+        .await;
+
+        Ok(Self {
+            relayer: Relayer::new(Some(storage), Some(pool))?,
+            accounts,
+        })
+    }
+}

+ 3 - 0
crates/personal-relayer/src/main.rs

@@ -0,0 +1,3 @@
+fn main() {
+    println!("Hello, world!");
+}