fix: duplicate connect relay call
This commit is contained in:
@@ -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/,
|
||||||
|
|||||||
@@ -290,31 +290,37 @@ 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 subscription_id == sub_id && event.kind == Kind::GiftWrap {
|
if let RelayMessage::Event { event, subscription_id, .. } = message {
|
||||||
if let Ok(UnwrappedGift { rumor, sender }) =
|
if subscription_id == sub_id && event.kind == Kind::GiftWrap {
|
||||||
client.unwrap_gift_wrap(&event).await
|
if let Ok(UnwrappedGift { rumor, sender }) =
|
||||||
{
|
client.unwrap_gift_wrap(&event).await
|
||||||
let rumor_clone = rumor.clone();
|
{
|
||||||
let ev = Event::new(
|
let rumor_clone = rumor.clone();
|
||||||
rumor_clone.id.unwrap(),
|
let ev = Event::new(
|
||||||
rumor_clone.pubkey,
|
rumor_clone.id.unwrap(),
|
||||||
rumor_clone.created_at,
|
rumor_clone.pubkey,
|
||||||
rumor_clone.kind,
|
rumor_clone.created_at,
|
||||||
rumor_clone.tags,
|
rumor_clone.kind,
|
||||||
rumor_clone.content,
|
rumor_clone.tags,
|
||||||
fake_sig,
|
rumor_clone.content,
|
||||||
);
|
fake_sig,
|
||||||
|
);
|
||||||
|
|
||||||
if let Err(e) = client.database().save_event(&ev).await {
|
if let Err(e) = client.database().save_event(&ev).await {
|
||||||
println!("Error: {}", e)
|
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)
|
Ok(false)
|
||||||
|
|||||||
@@ -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()),
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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();
|
||||||
|
|||||||
Reference in New Issue
Block a user