-
Notifications
You must be signed in to change notification settings - Fork 13
Add dm message #97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add dm message #97
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ use mostro_core::order::{SmallOrder, Status}; | |
| use mostro_core::NOSTR_REPLACEABLE_EVENT_KIND; | ||
| use nip44::v2::{decrypt_to_bytes, encrypt_to_bytes, ConversationKey}; | ||
| use nostr_sdk::prelude::*; | ||
| use std::thread::sleep; | ||
| use std::time::Duration; | ||
| use std::{fs, path::Path}; | ||
| use tokio::time::timeout; | ||
|
|
@@ -80,58 +81,46 @@ pub async fn send_message_sync( | |
| identity_keys: Option<&Keys>, | ||
| trade_keys: &Keys, | ||
| receiver_pubkey: PublicKey, | ||
| message: String, | ||
| message: Message, | ||
| wait_for_dm_ans: bool, | ||
| to_user: bool, | ||
| ) -> Result<()> { | ||
| ) -> Result<Vec<(Message, u64)>> { | ||
| let mut dm: Vec<(Message, u64)> = Vec::new(); | ||
| let message_json = message.as_json()?; | ||
| // Send dm to receiver pubkey | ||
| println!( | ||
| "SENDING DM with trade keys: {:?}", | ||
| trade_keys.public_key().to_hex() | ||
| ); | ||
| send_dm( | ||
| client, | ||
| identity_keys, | ||
| trade_keys, | ||
| &receiver_pubkey, | ||
| message, | ||
| message_json, | ||
| to_user, | ||
| ) | ||
| .await?; | ||
|
|
||
| let mut notifications = client.notifications(); | ||
| while let Ok(notification) = notifications.recv().await { | ||
| if wait_for_dm_ans { | ||
| let dm = get_direct_messages(client, trade_keys, 1, to_user).await; | ||
| println!("DM: {:#?}", dm); | ||
| for el in dm.iter() { | ||
| if let Some(Payload::PaymentRequest(ord, inv, _)) = | ||
| &el.0.get_inner_message_kind().payload | ||
| { | ||
| println!("NEW MESSAGE:"); | ||
| println!( | ||
| "Mostro sent you this hold invoice for order id: {}", | ||
| ord.as_ref().unwrap().id.unwrap() | ||
| ); | ||
| println!(); | ||
| println!("Pay this invoice to continue --> {}", inv); | ||
| println!(); | ||
| } | ||
| sleep(Duration::from_secs(1)); | ||
|
|
||
| if wait_for_dm_ans { | ||
| dm = get_direct_messages(client, trade_keys, 1, to_user).await; | ||
| for el in dm.iter() { | ||
| if let Some(Payload::PaymentRequest(ord, inv, _)) = | ||
| &el.0.get_inner_message_kind().payload | ||
| { | ||
| println!("NEW MESSAGE:"); | ||
| println!( | ||
| "Mostro sent you this hold invoice for order id: {}", | ||
| ord.as_ref().unwrap().id.unwrap() | ||
| ); | ||
| println!(); | ||
| println!("Pay this invoice to continue --> {}", inv); | ||
| println!(); | ||
| } | ||
| break; | ||
| } else if let RelayPoolNotification::Message { | ||
| message: | ||
| RelayMessage::Ok { | ||
| event_id: _, | ||
| status: _, | ||
| message: _, | ||
| }, | ||
| .. | ||
| } = notification | ||
| { | ||
| println!( | ||
| "Message correctly sent to Mostro! Check messages with getdm or listorders command" | ||
| ); | ||
| break; | ||
| } | ||
| } | ||
| Ok(()) | ||
| Ok(dm) | ||
|
Comment on lines
83
to
+123
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Codebase verification Update required: Inconsistent handling of return values Based on the verification results, there are issues with how the return value is handled across different callers:
The change from 🔗 Analysis chainVerify updates in all caller functions The change in return type from Let me analyze the callers to verify if they handle the return value correctly. Let me try a different approach to check the usage patterns. Let me try one more search to see how the function calls are handled in other files. 🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Search for all calls to send_message_sync
rg "send_message_sync\(" --type rust -A 5 -B 5
Length of output: 4340 Script: #!/bin/bash
# Check how callers handle the return value
ast-grep --pattern 'send_message_sync($$$).await$_'
Length of output: 53 Script: #!/bin/bash
# Search for await patterns with send_message_sync
rg "send_message_sync\(.*\)\.await" --type rust -A 2 -B 2
Length of output: 317 Script: #!/bin/bash
# Search for let patterns with send_message_sync to see variable assignments
rg "let.*=.*send_message_sync\(" --type rust -A 2 -B 2
Length of output: 319 |
||
| } | ||
|
|
||
| pub async fn requests_relay( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Consider improving error handling and async operations
The current implementation has several areas for improvement:
Consider this improved implementation:
if wait_for_dm_ans { - dm = get_direct_messages(client, trade_keys, 1, to_user).await; - for el in dm.iter() { + match get_direct_messages(client, trade_keys, 1, to_user).await { + messages => { + dm = messages; + for el in dm.iter() { if let Some(Payload::PaymentRequest(ord, inv, _)) = &el.0.get_inner_message_kind().payload { - println!("NEW MESSAGE:"); - println!( - "Mostro sent you this hold invoice for order id: {}", - ord.as_ref().unwrap().id.unwrap() - ); - println!(); - println!("Pay this invoice to continue --> {}", inv); - println!(); + if let Some(order) = ord.as_ref() { + if let Some(order_id) = order.id { + println!("NEW MESSAGE:"); + println!( + "Mostro sent you this hold invoice for order id: {}", + order_id + ); + println!(); + println!("Pay this invoice to continue --> {}", inv); + println!(); + } + } } + } } } }Also, consider using
tokio::time::sleepinstead ofstd::thread::sleepto avoid blocking: