-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathsend_msg.rs
More file actions
123 lines (110 loc) · 4.15 KB
/
send_msg.rs
File metadata and controls
123 lines (110 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
use crate::cli::{Commands, Context};
use crate::db::{Order, User};
use crate::parser::dms::print_commands_results;
use crate::parser::parse_dm_events;
use crate::util::{send_dm, wait_for_dm};
use anyhow::Result;
use mostro_core::prelude::*;
use nostr_sdk::prelude::*;
use uuid::Uuid;
pub async fn execute_send_msg(
command: Commands,
order_id: Option<Uuid>,
ctx: &Context,
text: Option<&str>,
) -> Result<()> {
// Map CLI command to action
let requested_action = match command {
Commands::FiatSent { .. } => Action::FiatSent,
Commands::Release { .. } => Action::Release,
Commands::Cancel { .. } => Action::Cancel,
Commands::Dispute { .. } => Action::Dispute,
Commands::AdmCancel { .. } => Action::AdminCancel,
Commands::AdmSettle { .. } => Action::AdminSettle,
Commands::AdmAddSolver { .. } => Action::AdminAddSolver,
_ => {
return Err(anyhow::anyhow!("Invalid command for send msg"));
}
};
match order_id {
Some(id) => println!(
"Sending {} command for order {} to mostro pubId {}",
requested_action, id, ctx.mostro_pubkey
),
None => return Err(anyhow::anyhow!("Missing order id!")),
};
// Determine payload
let payload = match requested_action {
Action::FiatSent | Action::Release => create_next_trade_payload(ctx, &order_id).await?,
_ => text.map(|t| Payload::TextMessage(t.to_string())),
};
// Update last trade index if next trade payload
if let Some(Payload::NextTrade(_, trade_index)) = &payload {
// Update last trade index
match User::get(&ctx.pool).await {
Ok(mut user) => {
user.set_last_trade_index(*trade_index as i64);
if let Err(e) = user.save(&ctx.pool).await {
println!("Failed to update user: {}", e);
}
}
Err(e) => println!("Failed to get user: {}", e),
}
}
// Create request id
let request_id = Uuid::new_v4().as_u128() as u64;
// Create and send the message
let message = Message::new_order(order_id, Some(request_id), None, requested_action, payload);
if let Some(order_id) = order_id {
let order = Order::get_by_id(&ctx.pool, &order_id.to_string()).await?;
if let Some(trade_keys_str) = order.trade_keys.clone() {
let trade_keys = Keys::parse(&trade_keys_str)?;
// Send DM
let message_json = message
.as_json()
.map_err(|e| anyhow::anyhow!("Failed to serialize message: {e}"))?;
// Send DM
send_dm(
&ctx.client,
Some(&ctx.identity_keys),
&trade_keys,
&ctx.mostro_pubkey,
message_json,
None,
false,
)
.await?;
// Wait for incoming DM
let recv_event = wait_for_dm(ctx, Some(&trade_keys)).await?;
let messages = parse_dm_events(recv_event, &trade_keys, None).await;
if let Some(message) = messages.first() {
let message = message.0.get_inner_message_kind();
println!("Message: {:?}", message);
if message.request_id == Some(request_id) {
let _ = print_commands_results(message, None, ctx).await;
}
}
}
}
Ok(())
}
async fn create_next_trade_payload(
ctx: &Context,
order_id: &Option<Uuid>,
) -> Result<Option<Payload>> {
if let Some(order_id) = order_id {
let order = Order::get_by_id(&ctx.pool, &order_id.to_string()).await?;
if let (Some(_), Some(min_amount), Some(max_amount)) =
(order.is_mine, order.min_amount, order.max_amount)
{
if max_amount - order.fiat_amount >= min_amount {
let (trade_keys, trade_index) = User::get_next_trade_keys(&ctx.pool).await?;
return Ok(Some(Payload::NextTrade(
trade_keys.public_key().to_string(),
trade_index.try_into()?,
)));
}
}
}
Ok(None)
}