fix: duplicate connect relay call

This commit is contained in:
reya
2024-08-06 16:36:44 +07:00
parent a3703bc348
commit b24b52e53c
5 changed files with 43 additions and 36 deletions

View File

@@ -1,4 +1,4 @@
wss://purplepag.es/, wss://purplepag.es/,
wss://directory.yabu.me/, wss://directory.yabu.me/,
wss://user.kindpag.es/, wss://user.kindpag.es/,
wss://bostr.online/, wss://relay.damus.io/,

View File

@@ -290,7 +290,8 @@ pub async fn login(
client client
.handle_notifications(|notification| async { .handle_notifications(|notification| async {
if let RelayPoolNotification::Event { event, subscription_id, .. } = notification { if let RelayPoolNotification::Message { message, .. } = notification {
if let RelayMessage::Event { event, subscription_id, .. } = message {
if subscription_id == sub_id && event.kind == Kind::GiftWrap { if subscription_id == sub_id && event.kind == Kind::GiftWrap {
if let Ok(UnwrappedGift { rumor, sender }) = if let Ok(UnwrappedGift { rumor, sender }) =
client.unwrap_gift_wrap(&event).await client.unwrap_gift_wrap(&event).await
@@ -310,12 +311,17 @@ pub async fn login(
println!("Error: {}", e) println!("Error: {}", e)
} }
let payload = let payload = EventPayload {
EventPayload { event: rumor.as_json(), sender: sender.to_hex() }; event: rumor.as_json(),
sender: sender.to_hex(),
};
handle.emit("event", payload).unwrap(); handle.emit("event", payload).unwrap();
} }
} }
} else {
println!("relay message: {}", message.as_json())
}
} }
Ok(false) Ok(false)
}) })

View File

@@ -61,6 +61,7 @@ pub async fn send_message(
state: State<'_, Nostr>, state: State<'_, Nostr>,
) -> Result<(), String> { ) -> Result<(), String> {
let client = &state.client; let client = &state.client;
let relays = state.inbox_relays.lock().await;
let signer = client.signer().await.map_err(|e| e.to_string())?; let signer = client.signer().await.map_err(|e| e.to_string())?;
let public_key = signer.public_key().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 // TODO: Add support reply_to
let rumor = EventBuilder::private_msg_rumor(receiver, message, None); let rumor = EventBuilder::private_msg_rumor(receiver, message, None);
// Get inbox state
let relays = state.inbox_relays.lock().await;
// Get inbox relays per member // Get inbox relays per member
let outbox = relays.get(&receiver); let outbox_urls = match relays.get(&receiver) {
let inbox = relays.get(&public_key);
let outbox_urls = match outbox {
Some(relays) => relays, Some(relays) => relays,
None => return Err("Receiver didn't have inbox relays to receive message.".into()), 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, Some(relays) => relays,
None => return Err("Please config inbox relays to backup your message.".into()), None => return Err("Please config inbox relays to backup your message.".into()),
}; };

View File

@@ -68,7 +68,7 @@ pub async fn collect_inbox_relays(
let public_key = PublicKey::parse(user_id).map_err(|e| e.to_string())?; 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); 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) => { Ok(events) => {
if let Some(event) = events.into_iter().next() { if let Some(event) = events.into_iter().next() {
let urls = event let urls = event
@@ -121,8 +121,14 @@ pub async fn connect_inbox_relays(
if !ignore_cache { if !ignore_cache {
if let Some(relays) = inbox_relays.get(&public_key) { if let Some(relays) = inbox_relays.get(&public_key) {
for relay in relays { for url in relays {
if let Err(e) = client.connect_relay(relay).await { 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) println!("Connect relay failed: {}", e)
} }
} }

View File

@@ -109,8 +109,8 @@ fn main() {
// Setup nostr client // Setup nostr client
let opts = Options::new() let opts = Options::new()
.autoconnect(true) .autoconnect(true)
.timeout(Duration::from_secs(40)) .timeout(Duration::from_secs(30))
.send_timeout(Some(Duration::from_secs(10))) .send_timeout(Some(Duration::from_secs(2)))
.connection_timeout(Some(Duration::from_secs(10))); .connection_timeout(Some(Duration::from_secs(10)));
let client = ClientBuilder::default().opts(opts).database(database).build(); let client = ClientBuilder::default().opts(opts).database(database).build();