|
@@ -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,
|
|
|
+ })
|
|
|
+ }
|
|
|
+}
|