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://directory.yabu.me/,
wss://user.kindpag.es/,
wss://bostr.online/,
wss://relay.damus.io/,

View File

@@ -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)

View File

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

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 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)
}
}

View File

@@ -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();