Skip to content

Commit 6451dd9

Browse files
committed
refactor: completing new commands refactor, neworder and take order works now
1 parent e2db862 commit 6451dd9

12 files changed

Lines changed: 204 additions & 208 deletions

File tree

src/cli.rs

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,6 @@ pub enum Commands {
177177
#[arg(short, long)]
178178
#[clap(default_value_t = 30)]
179179
since: i64,
180-
/// If true, get messages from counterparty, otherwise from Mostro
181-
#[arg(short)]
182-
from_user: bool,
183180
},
184181
/// Get direct messages sent to any trade keys
185182
GetDmUser {
@@ -194,9 +191,6 @@ pub enum Commands {
194191
#[arg(short, long)]
195192
#[clap(default_value_t = 30)]
196193
since: i64,
197-
/// If true, get messages from counterparty, otherwise from Mostro
198-
#[arg(short)]
199-
from_user: bool,
200194
},
201195
/// Send direct message to a user
202196
SendDm {
@@ -543,6 +537,7 @@ impl Commands {
543537
&ctx.identity_keys,
544538
ctx.mostro_pubkey,
545539
&ctx.client,
540+
&ctx.pool,
546541
)
547542
.await
548543
}
@@ -558,32 +553,14 @@ impl Commands {
558553
}
559554

560555
// DM retrieval commands
561-
Commands::GetDm { since, from_user } => {
562-
execute_get_dm(
563-
since,
564-
ctx.trade_index,
565-
&ctx.mostro_keys,
566-
&ctx.client,
567-
*from_user,
568-
false,
569-
&ctx.mostro_pubkey,
570-
)
571-
.await
556+
Commands::GetDm { since } => {
557+
execute_get_dm(since, ctx.trade_index, &ctx.mostro_keys, &ctx.client, false).await
572558
}
573559
Commands::GetDmUser { since } => {
574-
execute_get_dm_user(since, &ctx.client, &ctx.mostro_pubkey).await
560+
execute_get_dm_user(since, &ctx.client, &ctx.mostro_pubkey, &ctx.pool).await
575561
}
576-
Commands::GetAdminDm { since, from_user } => {
577-
execute_get_dm(
578-
since,
579-
ctx.trade_index,
580-
&ctx.mostro_keys,
581-
&ctx.client,
582-
*from_user,
583-
true,
584-
&ctx.mostro_pubkey,
585-
)
586-
.await
562+
Commands::GetAdminDm { since } => {
563+
execute_get_dm(since, ctx.trade_index, &ctx.mostro_keys, &ctx.client, true).await
587564
}
588565

589566
// Admin commands

src/cli/add_invoice.rs

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use crate::db::connect;
21
use crate::util::{send_dm, wait_for_dm};
32
use crate::{db::Order, lightning::is_valid_invoice};
43
use anyhow::Result;
54
use lnurl::lightning_address::LightningAddress;
65
use mostro_core::prelude::*;
76
use nostr_sdk::prelude::*;
7+
use sqlx::SqlitePool;
88
use std::str::FromStr;
99
use uuid::Uuid;
1010

@@ -14,9 +14,9 @@ pub async fn execute_add_invoice(
1414
identity_keys: &Keys,
1515
mostro_key: PublicKey,
1616
client: &Client,
17+
pool: &SqlitePool,
1718
) -> Result<()> {
18-
let pool = connect().await?;
19-
let order = Order::get_by_id(&pool, &order_id.to_string()).await?;
19+
let order = Order::get_by_id(pool, &order_id.to_string()).await?;
2020
let trade_keys = order
2121
.trade_keys
2222
.clone()
@@ -40,6 +40,8 @@ pub async fn execute_add_invoice(
4040
}
4141
}
4242
};
43+
44+
// Create request id
4345
let request_id = Uuid::new_v4().as_u128() as u64;
4446
// Create AddInvoice message
4547
let add_invoice_message = Message::new_order(
@@ -50,35 +52,24 @@ pub async fn execute_add_invoice(
5052
payload,
5153
);
5254

55+
// Serialize the message
5356
let message_json = add_invoice_message
5457
.as_json()
5558
.map_err(|_| anyhow::anyhow!("Failed to serialize message"))?;
5659

57-
// Clone the keys and client for the async call
58-
let identity_keys = identity_keys.clone();
59-
let trade_keys_clone = trade_keys.clone();
60-
let client_clone = client.clone();
61-
62-
// Spawn a new task to send the DM
63-
// This is so we can wait for the gift wrap event in the main thread
64-
tokio::spawn(async move {
65-
if let Err(e) = send_dm(
66-
&client_clone,
67-
Some(&identity_keys.clone()),
68-
&trade_keys_clone,
69-
&mostro_key,
70-
message_json,
71-
None,
72-
false,
73-
)
74-
.await
75-
{
76-
eprintln!("Failed to send DM: {}", e);
77-
}
78-
});
79-
80-
// Wait for the DM to be sent from mostro
81-
wait_for_dm(client, &trade_keys, request_id, 0, Some(order)).await?;
60+
// Send DM
61+
send_dm(
62+
client,
63+
Some(identity_keys),
64+
&trade_keys,
65+
&mostro_key,
66+
message_json,
67+
None,
68+
false,
69+
)
70+
.await?;
8271

72+
// Wait for the DM to be sent from mostro and update the order
73+
wait_for_dm(client, &trade_keys, request_id, None, Some(order)).await?;
8374
Ok(())
8475
}

src/cli/adm_send_dm.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@ pub async fn execute_adm_send_dm(
1414
}
1515
};
1616

17-
println!("SENDING DM with admin keys: {}", admin_keys.public_key().to_hex());
17+
println!(
18+
"SENDING DM with admin keys: {}",
19+
admin_keys.public_key().to_hex()
20+
);
1821

1922
send_admin_gift_wrap_dm(client, &admin_keys, &receiver, message).await?;
2023

2124
println!("Admin gift wrap message sent to {}", receiver);
2225

2326
Ok(())
24-
}
27+
}

src/cli/dm_to_user.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@ pub async fn execute_dm_to_user(
1919
None => anyhow::bail!("No trade_keys found for this order"),
2020
};
2121

22-
println!("SENDING DM with trade keys: {}", trade_keys.public_key().to_hex());
22+
println!(
23+
"SENDING DM with trade keys: {}",
24+
trade_keys.public_key().to_hex()
25+
);
2326

2427
send_gift_wrap_dm(client, &trade_keys, &receiver, message).await?;
2528

2629
Ok(())
27-
}
30+
}

src/cli/get_dm.rs

Lines changed: 4 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
use anyhow::Result;
2-
use chrono::DateTime;
32
use mostro_core::prelude::*;
43
use nostr_sdk::prelude::*;
54

65
use crate::{
7-
db::{connect, Order, User},
8-
parser::dms::parse_dm_events,
6+
db::{connect, User},
7+
parser::dms::{parse_dm_events, print_direct_messages},
98
util::{create_filter, ListKind},
109
};
1110

@@ -14,11 +13,9 @@ pub async fn execute_get_dm(
1413
trade_index: i64,
1514
mostro_keys: &Keys,
1615
client: &Client,
17-
_from_user: bool,
1816
admin: bool,
19-
_mostro_pubkey: &PublicKey,
2017
) -> Result<()> {
21-
let mut dm: Vec<(Message, u64)> = Vec::new();
18+
let mut dm: Vec<(Message, u64, PublicKey)> = Vec::new();
2219
let pool = connect().await?;
2320
if !admin {
2421
for index in 1..=trade_index {
@@ -39,79 +36,6 @@ pub async fn execute_get_dm(
3936
dm.extend(dm_temp);
4037
}
4138

42-
if dm.is_empty() {
43-
println!();
44-
println!("No new messages");
45-
println!();
46-
} else {
47-
for m in dm.iter() {
48-
let message = m.0.get_inner_message_kind();
49-
let date = DateTime::from_timestamp(m.1 as i64, 0).unwrap();
50-
if message.id.is_some() {
51-
println!(
52-
"Mostro sent you this message for order id: {} at {}",
53-
m.0.get_inner_message_kind().id.unwrap(),
54-
date
55-
);
56-
}
57-
if let Some(payload) = &message.payload {
58-
match payload {
59-
Payload::PaymentRequest(_, inv, _) => {
60-
println!();
61-
println!("Pay this invoice to continue --> {}", inv);
62-
println!();
63-
}
64-
Payload::TextMessage(text) => {
65-
println!();
66-
println!("{text}");
67-
println!();
68-
}
69-
Payload::Dispute(id, info) => {
70-
println!("Action: {}", message.action);
71-
println!("Dispute id: {}", id);
72-
if let Some(info) = info {
73-
println!();
74-
println!("Dispute info: {:#?}", info);
75-
println!();
76-
}
77-
}
78-
Payload::CantDo(Some(cant_do_reason)) => {
79-
println!();
80-
println!("Error: {:?}", cant_do_reason);
81-
println!();
82-
}
83-
Payload::Order(new_order) if message.action == Action::NewOrder => {
84-
if new_order.id.is_some() {
85-
let db_order =
86-
Order::get_by_id(&pool, &new_order.id.unwrap().to_string()).await;
87-
if db_order.is_err() {
88-
let trade_index = message.trade_index.unwrap();
89-
let trade_keys = User::get_trade_keys(&pool, trade_index).await?;
90-
let _ = Order::new(&pool, new_order.clone(), &trade_keys, None)
91-
.await
92-
.map_err(|e| {
93-
anyhow::anyhow!("Failed to create DB order: {:?}", e)
94-
})?;
95-
}
96-
}
97-
println!();
98-
println!("Order: {:#?}", new_order);
99-
println!();
100-
}
101-
_ => {
102-
println!();
103-
println!("Action: {}", message.action);
104-
println!("Payload: {:#?}", message.payload);
105-
println!();
106-
}
107-
}
108-
} else {
109-
println!();
110-
println!("Action: {}", message.action);
111-
println!("Payload: {:#?}", message.payload);
112-
println!();
113-
}
114-
}
115-
}
39+
print_direct_messages(&dm, &pool).await?;
11640
Ok(())
11741
}

src/cli/get_dm_user.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ use comfy_table::presets::UTF8_FULL;
55
use comfy_table::Table;
66
use mostro_core::prelude::*;
77
use nostr_sdk::prelude::*;
8+
use sqlx::SqlitePool;
89

910
pub async fn execute_get_dm_user(
1011
since: &i64,
1112
client: &Client,
1213
mostro_pubkey: &PublicKey,
14+
pool: &SqlitePool,
1315
) -> Result<()> {
14-
let pool = crate::db::connect().await?;
15-
1616
// Get all trade keys from orders
17-
let mut trade_keys_hex = Order::get_all_trade_keys(&pool).await?;
17+
let mut trade_keys_hex = Order::get_all_trade_keys(pool).await?;
1818

1919
// Add admin private key to search for messages sent TO admin
2020
if let Ok(admin_privkey_hex) = std::env::var("NSEC_PRIVKEY") {

src/cli/new_order.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ pub async fn execute_new_order(
161161
});
162162

163163
// Wait for the DM to be sent from mostro
164-
wait_for_dm(client, trade_keys, request_id, trade_index, None).await?;
164+
wait_for_dm(client, trade_keys, request_id, Some(trade_index), None).await?;
165165

166166
Ok(())
167167
}

src/cli/send_msg.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ pub async fn execute_send_msg(
111111
});
112112

113113
// Wait for the DM to be sent from mostro
114-
wait_for_dm(client, &trade_keys, request_id, 0, Some(order)).await?;
114+
wait_for_dm(client, &trade_keys, request_id, None, Some(order)).await?;
115115
}
116116
}
117117

src/cli/take_buy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub async fn execute_take_buy(
7070
});
7171

7272
// Wait for the DM to be sent from mostro
73-
wait_for_dm(client, trade_keys, request_id, trade_index, None).await?;
73+
wait_for_dm(client, trade_keys, request_id, Some(trade_index), None).await?;
7474

7575
Ok(())
7676
}

src/cli/take_sell.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ pub async fn execute_take_sell(
112112
client.subscribe(subscription, None).await?;
113113

114114
// Wait for the DM to be sent from mostro
115-
wait_for_dm(client, trade_keys, request_id, trade_index, None).await?;
115+
wait_for_dm(client, trade_keys, request_id, Some(trade_index), None).await?;
116116

117117
Ok(())
118118
}

0 commit comments

Comments
 (0)