connection.rs (1413B)
1 use crate::error::{NetError, Result}; 2 use tracing::error; 3 4 use super::manager::NostrClientManager; 5 6 impl NostrClientManager { 7 pub fn set_relays(&self, urls: &[String]) { 8 if let Ok(mut guard) = self.inner.relays.lock() { 9 *guard = urls.to_vec(); 10 } 11 } 12 13 pub fn connect(&self) -> Result<()> { 14 let inner = self.inner.clone(); 15 let urls = { 16 let g = inner.relays.lock().ok(); 17 g.map(|v| v.clone()).unwrap_or_default() 18 }; 19 20 if urls.is_empty() { 21 if let Ok(mut e) = inner.last_error.lock() { 22 *e = Some("no relays configured".to_string()); 23 } 24 return Err(NetError::Msg("no relays configured".into())); 25 } 26 27 let inner_for_task = inner.clone(); 28 let rt = inner.rt.clone(); 29 rt.spawn(async move { 30 for u in &urls { 31 match inner_for_task.client.add_relay(u.as_str()).await { 32 Ok(_) => {} 33 Err(e) => { 34 if let Ok(mut last) = inner_for_task.last_error.lock() { 35 *last = Some(format!("add_relay {}: {}", u, e)); 36 } 37 error!("add_relay failed for {}: {}", u, e); 38 } 39 } 40 } 41 inner_for_task.client.connect().await; 42 }); 43 44 Ok(()) 45 } 46 }