Skip to content
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

New management of takesell #18

Merged
merged 3 commits into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ async fn main() -> Result<()> {

match valid_invoice {
Ok(_) => {
send_order_id_cmd(&client, &my_key, mostro_key, takesell_message).await?;
send_order_id_cmd(&client, &my_key, mostro_key, takesell_message, true).await?;
std::process::exit(0);
}
Err(e) => println!("{}", e),
Expand All @@ -122,7 +122,7 @@ async fn main() -> Result<()> {
.as_json()
.unwrap();

send_order_id_cmd(&client, &my_key, mostro_key, takebuy_message).await?;
send_order_id_cmd(&client, &my_key, mostro_key, takebuy_message, false).await?;
std::process::exit(0);
}
Some(cli::Commands::GetDm { since }) => {
Expand Down Expand Up @@ -158,7 +158,7 @@ async fn main() -> Result<()> {
.as_json()
.unwrap();

send_order_id_cmd(&client, &my_key, mostro_key, message).await?;
send_order_id_cmd(&client, &my_key, mostro_key, message, false).await?;
std::process::exit(0);
}
Some(cli::Commands::Neworder {
Expand Down Expand Up @@ -214,7 +214,7 @@ async fn main() -> Result<()> {
.as_json()
.unwrap();

send_order_id_cmd(&client, &my_key, mostro_key, message).await?;
send_order_id_cmd(&client, &my_key, mostro_key, message, false).await?;
std::process::exit(0);
}
None => {}
Expand Down
1 change: 1 addition & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ pub struct Message {
pub enum Content {
Order(Order),
PaymentRequest(String),
PayHoldInvoice(Order, String),
}

#[allow(dead_code)]
Expand Down
36 changes: 32 additions & 4 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::types::Content;
use crate::types::Kind as Orderkind;
use crate::types::Message;
use crate::types::Order;
use crate::types::Status;
use anyhow::{Error, Result};
Expand Down Expand Up @@ -30,7 +32,7 @@ pub async fn send_dm(
info!("Sending event: {event:#?}");
// This will update relay send event to wait for tranmission.
if let Some(_wait_mes) = wait_for_connection {
let opts = Options::new().wait_for_send(true);
let opts = Options::new().wait_for_send(false);
client.update_opts(opts);
}
client.send_event(event).await?;
Expand Down Expand Up @@ -71,7 +73,7 @@ pub async fn connect_nostr() -> Result<Client> {
for r in relays.into_iter() {
client.add_relay(r, None).await?;
}
let opts = Options::new().wait_for_connection(true);
let opts = Options::new().wait_for_connection(false);
client.update_opts(opts);

// Connect to relays and keep connection alive
Expand All @@ -85,14 +87,40 @@ pub async fn send_order_id_cmd(
my_key: &Keys,
mostro_pubkey: XOnlyPublicKey,
message: String,
wait_for_dm_ans: bool,
) -> Result<()> {
// Send dm to mostro pub id
send_dm(client, my_key, &mostro_pubkey, message, Some(true)).await?;
send_dm(client, my_key, &mostro_pubkey, message, Some(false)).await?;

let mut notifications = client.notifications();

while let Ok(notification) = notifications.recv().await {
if let RelayPoolNotification::Message(
if wait_for_dm_ans {
let dm = get_direct_messages(client, mostro_pubkey, my_key, 1).await;

for el in dm.iter() {
match Message::from_json(&el.0) {
Ok(m) => {
if let Some(Content::PayHoldInvoice(ord, inv)) = m.content {
println!("NEW MESSAGE:");
println!(
"Mostro sent you this hold invoice for order id: {}",
ord.id.unwrap()
);
println!();
println!("Pay this invoice --> {}", inv);
println!();println!();
}
}
Err(_) => {
println!("NEW MESSAGE:");
println!("Mostro sent you this message --> {}", el.0);
println!();println!();
}
}
}
break;
} else if let RelayPoolNotification::Message(
_,
RelayMessage::Ok {
event_id: _,
Expand Down