diff --git a/src-tauri/resources/relays.txt b/src-tauri/resources/relays.txt index 336148f..00e204e 100644 --- a/src-tauri/resources/relays.txt +++ b/src-tauri/resources/relays.txt @@ -1,4 +1,4 @@ wss://purplepag.es/, wss://directory.yabu.me/, wss://user.kindpag.es/, -wss://bostr.online/, +wss://relay.damus.io/, diff --git a/src-tauri/src/commands/account.rs b/src-tauri/src/commands/account.rs index b1918d2..eb96c8e 100644 --- a/src-tauri/src/commands/account.rs +++ b/src-tauri/src/commands/account.rs @@ -290,31 +290,37 @@ pub async fn login( client .handle_notifications(|notification| async { - if let RelayPoolNotification::Event { event, subscription_id, .. } = notification { - if subscription_id == sub_id && event.kind == Kind::GiftWrap { - if let Ok(UnwrappedGift { rumor, sender }) = - client.unwrap_gift_wrap(&event).await - { - let rumor_clone = rumor.clone(); - let ev = Event::new( - rumor_clone.id.unwrap(), - rumor_clone.pubkey, - rumor_clone.created_at, - rumor_clone.kind, - rumor_clone.tags, - rumor_clone.content, - fake_sig, - ); + if let RelayPoolNotification::Message { message, .. } = notification { + if let RelayMessage::Event { event, subscription_id, .. } = message { + if subscription_id == sub_id && event.kind == Kind::GiftWrap { + if let Ok(UnwrappedGift { rumor, sender }) = + client.unwrap_gift_wrap(&event).await + { + let rumor_clone = rumor.clone(); + let ev = Event::new( + rumor_clone.id.unwrap(), + rumor_clone.pubkey, + rumor_clone.created_at, + rumor_clone.kind, + rumor_clone.tags, + rumor_clone.content, + fake_sig, + ); - if let Err(e) = client.database().save_event(&ev).await { - println!("Error: {}", e) + if let Err(e) = client.database().save_event(&ev).await { + println!("Error: {}", e) + } + + let payload = EventPayload { + event: rumor.as_json(), + sender: sender.to_hex(), + }; + + handle.emit("event", payload).unwrap(); } - - let payload = - EventPayload { event: rumor.as_json(), sender: sender.to_hex() }; - - handle.emit("event", payload).unwrap(); } + } else { + println!("relay message: {}", message.as_json()) } } Ok(false) diff --git a/src-tauri/src/commands/chat.rs b/src-tauri/src/commands/chat.rs index 245cab1..993983e 100644 --- a/src-tauri/src/commands/chat.rs +++ b/src-tauri/src/commands/chat.rs @@ -61,6 +61,7 @@ pub async fn send_message( state: State<'_, Nostr>, ) -> Result<(), String> { let client = &state.client; + let relays = state.inbox_relays.lock().await; let signer = client.signer().await.map_err(|e| e.to_string())?; let public_key = signer.public_key().await.map_err(|e| e.to_string())?; @@ -69,19 +70,13 @@ pub async fn send_message( // TODO: Add support reply_to let rumor = EventBuilder::private_msg_rumor(receiver, message, None); - // Get inbox state - let relays = state.inbox_relays.lock().await; - // Get inbox relays per member - let outbox = relays.get(&receiver); - let inbox = relays.get(&public_key); - - let outbox_urls = match outbox { + let outbox_urls = match relays.get(&receiver) { Some(relays) => relays, None => return Err("Receiver didn't have inbox relays to receive message.".into()), }; - let inbox_urls = match inbox { + let inbox_urls = match relays.get(&public_key) { Some(relays) => relays, None => return Err("Please config inbox relays to backup your message.".into()), }; diff --git a/src-tauri/src/commands/relay.rs b/src-tauri/src/commands/relay.rs index 6d7051c..32c241b 100644 --- a/src-tauri/src/commands/relay.rs +++ b/src-tauri/src/commands/relay.rs @@ -68,7 +68,7 @@ pub async fn collect_inbox_relays( let public_key = PublicKey::parse(user_id).map_err(|e| e.to_string())?; let inbox = Filter::new().kind(Kind::Custom(10050)).author(public_key).limit(1); - match client.get_events_of(vec![inbox], None).await { + match client.get_events_of(vec![inbox], Some(Duration::from_secs(2))).await { Ok(events) => { if let Some(event) = events.into_iter().next() { let urls = event @@ -121,8 +121,14 @@ pub async fn connect_inbox_relays( if !ignore_cache { if let Some(relays) = inbox_relays.get(&public_key) { - for relay in relays { - if let Err(e) = client.connect_relay(relay).await { + for url in relays { + if let Ok(relay) = client.relay(url).await { + if !relay.is_connected().await { + if let Err(e) = client.connect_relay(url).await { + println!("Connect relay failed: {}", e) + } + } + } else if let Err(e) = client.add_relay(url).await { println!("Connect relay failed: {}", e) } } diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 29f6644..b44bbcf 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -109,8 +109,8 @@ fn main() { // Setup nostr client let opts = Options::new() .autoconnect(true) - .timeout(Duration::from_secs(40)) - .send_timeout(Some(Duration::from_secs(10))) + .timeout(Duration::from_secs(30)) + .send_timeout(Some(Duration::from_secs(2))) .connection_timeout(Some(Duration::from_secs(10))); let client = ClientBuilder::default().opts(opts).database(database).build();