-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathorder_ch_mng.rs
More file actions
351 lines (339 loc) · 12.9 KB
/
Copy pathorder_ch_mng.rs
File metadata and controls
351 lines (339 loc) · 12.9 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// Order channel manager - handles order result messages from async tasks
use crate::ui::helpers::build_active_order_chat_list;
use crate::ui::orders::{
strip_new_order_messages_and_clamp_selected, try_placeholder_order_message_from_success,
BuyerInvoicePreference, OrderSuccess,
};
use crate::ui::{
AppState, InvoiceInputState, InvoiceNotificationActionSelection, MessageNotification,
OperationResult, UiMode, UserMode,
};
use mostro_core::prelude::Action;
use uuid::Uuid;
fn remove_closed_trade_from_messages_tab(app: &mut AppState, order_id: Uuid) {
match app.messages.lock() {
Ok(mut messages) => {
messages.retain(|m| m.order_id != Some(order_id));
strip_new_order_messages_and_clamp_selected(
&mut messages,
&mut app.selected_message_idx,
);
}
Err(e) => {
crate::util::request_fatal_restart(format!(
"Mostrix encountered an internal error (poisoned messages lock: {e}). Please restart the app."
));
}
}
match app.active_order_trade_indices.lock() {
Ok(mut indices) => {
indices.remove(&order_id);
}
Err(e) => {
crate::util::request_fatal_restart(format!(
"Mostrix encountered an internal error (poisoned active order indices lock: {e}). Please restart the app."
));
}
}
app.order_chat_static.remove(&order_id);
app.buyer_invoice_preference.remove(&order_id);
}
fn remove_many_orders_from_messages_tab(app: &mut AppState, order_ids: &[Uuid]) {
let id_set: std::collections::HashSet<Uuid> = order_ids.iter().copied().collect();
match app.messages.lock() {
Ok(mut messages) => {
messages.retain(|m| m.order_id.map(|id| !id_set.contains(&id)).unwrap_or(true));
strip_new_order_messages_and_clamp_selected(
&mut messages,
&mut app.selected_message_idx,
);
let n = build_active_order_chat_list(&messages, &app.my_trades_maker_book).len();
if n == 0 {
app.selected_order_chat_idx = 0;
} else if app.selected_order_chat_idx >= n {
app.selected_order_chat_idx = n - 1;
}
}
Err(e) => {
crate::util::request_fatal_restart(format!(
"Mostrix encountered an internal error (poisoned messages lock: {e}). Please restart the app."
));
}
}
match app.active_order_trade_indices.lock() {
Ok(mut indices) => {
for order_id in order_ids {
indices.remove(order_id);
}
}
Err(e) => {
crate::util::request_fatal_restart(format!(
"Mostrix encountered an internal error (poisoned active order indices lock: {e}). Please restart the app."
));
}
}
for order_id in order_ids {
app.buyer_invoice_preference.remove(order_id);
let key = order_id.to_string();
app.order_chats.remove(&key);
app.order_chat_last_seen.remove(&key);
app.order_chat_static.remove(order_id);
if let Ok(mut dropped) = app.dropped_user_history_order_ids.lock() {
dropped.insert(*order_id);
}
}
}
/// If `Success` arrived before any DM row exists for this trade, append one placeholder so
/// **Orders In Progress** (`build_active_order_chat_list`) has a sidebar row without running
/// `sync_user_order_history_messages_from_db` (which would clobber real actions).
fn maybe_insert_my_trade_placeholder_message(app: &mut AppState, os: &OrderSuccess) {
let Some(order_id) = os.order_id else {
return;
};
if os.static_header.is_none() {
return;
}
let Some(placeholder) = try_placeholder_order_message_from_success(os) else {
return;
};
match app.messages.lock() {
Ok(mut messages) => {
if messages.iter().any(|m| m.order_id == Some(order_id)) {
return;
}
messages.push(placeholder);
messages.sort_by(|a, b| b.timestamp.cmp(&a.timestamp));
}
Err(e) => {
crate::util::request_fatal_restart(format!(
"Mostrix encountered an internal error (poisoned messages lock: {e}). Please restart the app."
));
}
}
}
/// Handle order result from the order result channel
pub fn handle_operation_result(mut result: OperationResult, app: &mut AppState) {
if let OperationResult::TradeClosed { order_id, message } = result {
remove_closed_trade_from_messages_tab(app, order_id);
result = OperationResult::Info(message);
}
if let OperationResult::OrderHistoryDeleted {
deleted_order_ids,
message,
} = result
{
remove_many_orders_from_messages_tab(app, &deleted_order_ids);
result = OperationResult::Info(message);
}
if let OperationResult::InvoiceSubmitted {
message,
remember_buyer_saved_ln_address_for_order,
} = result
{
if let Some(order_id) = remember_buyer_saved_ln_address_for_order {
app.buyer_invoice_preference
.insert(order_id, BuyerInvoicePreference::UseSavedLnAddress);
}
result = OperationResult::Info(message);
}
if let OperationResult::OrderChatAttachmentSent {
order_id,
chat_message,
info_message,
} = result
{
app.pending_order_attachment_sends.remove(&order_id);
if app.sending_attachment_order_id.as_deref() == Some(order_id.as_str()) {
app.sending_attachment_order_id = None;
}
crate::ui::helpers::save_order_chat_message(&order_id, &chat_message);
app.order_chats
.entry(order_id.clone())
.or_default()
.push(chat_message);
result = OperationResult::Info(info_message);
}
if let OperationResult::OrderChatAttachmentError { order_id, error } = result {
if app.sending_attachment_order_id.as_deref() == Some(&order_id) {
app.sending_attachment_order_id = None;
}
result = OperationResult::Error(error);
}
if let OperationResult::OrderChatAttachmentSendFailed { prepared, error } = result {
let order_id = prepared.order_id.clone();
let url = prepared.blossom_url.clone();
let filename = prepared.filename.clone();
app.pending_order_attachment_sends
.insert(order_id.clone(), prepared);
if app.sending_attachment_order_id.as_deref() == Some(order_id.as_str()) {
app.sending_attachment_order_id = None;
}
result = OperationResult::Error(format!(
"Uploaded {filename} to Blossom ({url}) but chat send failed: {error}. \
Press Ctrl+Shift+O to retry send without re-uploading."
));
}
match &result {
OperationResult::Success(os) => {
if let Some(h) = &os.static_header {
app.order_chat_static.insert(h.order_id, h.clone());
}
maybe_insert_my_trade_placeholder_message(app, os);
}
OperationResult::PaymentRequestRequired { static_header, .. } => {
app.order_chat_static
.insert(static_header.order_id, static_header.clone());
}
_ => {}
}
if let OperationResult::OpenInvoicePopup {
notification,
order_message,
} = &result
{
crate::util::dm_utils::notifications_ch_mng::apply_open_invoice_popup_from_execute(
app,
notification.clone(),
order_message,
);
return;
}
// Handle PaymentRequestRequired - show invoice popup for buy orders
if let OperationResult::PaymentRequestRequired {
order,
invoice,
sat_amount,
trade_index,
static_header: _,
action,
} = &result
{
// Track trade_index
if let Some(order_id) = order.id {
match app.active_order_trade_indices.lock() {
Ok(mut indices) => {
indices.insert(order_id, *trade_index);
}
Err(e) => {
crate::util::request_fatal_restart(format!(
"Mostrix encountered an internal error (poisoned active order indices lock: {e}). Please restart the app."
));
app.fatal_exit_on_close = true;
app.mode = UiMode::operation_result(OperationResult::Error(
"Internal error. Please restart Mostrix.".to_string(),
));
return;
}
}
log::info!(
"Tracking order {} with trade_index {}",
order_id,
trade_index
);
}
// Create MessageNotification to show the invoice popup. `action` distinguishes
// the trade hold invoice (`PayInvoice`) from the anti-abuse bond
// (`PayBondInvoice`), so each opens its own popup variant.
let preview = match action {
Action::PayBondInvoice => "Bond Invoice".to_string(),
_ => "Payment Request".to_string(),
};
let notification = MessageNotification {
order_id: order.id,
message_preview: preview,
timestamp: chrono::Utc::now().timestamp(),
action: action.clone(),
sat_amount: *sat_amount,
invoice: Some(invoice.clone()),
body: None,
maker_bond_publish: order.status == Some(mostro_core::order::Status::WaitingMakerBond),
};
let invoice_state = InvoiceInputState {
invoice_input: String::new(),
focused: false,
just_pasted: false,
copied_to_clipboard: false,
scroll_y: 0,
action_selection: InvoiceNotificationActionSelection::Primary,
};
app.mode = UiMode::NewMessageNotification(notification, action.clone(), invoice_state);
return;
}
// Track trade_index for taken orders
if let OperationResult::Success(OrderSuccess {
order_id,
trade_index,
..
}) = &result
{
if let (Some(order_id), Some(trade_index)) = (order_id, trade_index) {
match app.active_order_trade_indices.lock() {
Ok(mut indices) => {
indices.insert(*order_id, *trade_index);
}
Err(e) => {
crate::util::request_fatal_restart(format!(
"Mostrix encountered an internal error (poisoned active order indices lock: {e}). Please restart the app."
));
app.fatal_exit_on_close = true;
app.mode = UiMode::operation_result(OperationResult::Error(
"Internal error. Please restart Mostrix.".to_string(),
));
return;
}
}
log::info!(
"Tracking order {} with trade_index {}",
order_id,
trade_index
);
}
}
// Handle observer chat results directly (don't show popup)
match result {
OperationResult::ObserverChatLoaded(messages) => {
app.observer_loading = false;
app.observer_error = None;
app.observer_messages = messages;
return;
}
OperationResult::ObserverChatError(msg) => {
app.observer_loading = false;
app.observer_error = Some(msg.clone());
app.mode = UiMode::operation_result(OperationResult::Error(msg));
return;
}
_ => {}
}
// Set appropriate result mode based on current state
match &app.mode {
UiMode::UserMode(UserMode::WaitingTakeOrder(_)) => {
app.mode = UiMode::operation_result(result);
}
UiMode::UserMode(UserMode::WaitingAddInvoice) => {
app.mode = UiMode::operation_result(result);
}
UiMode::NewMessageNotification(_, action, _) => {
// Do not replace AddInvoice/PayInvoice/PayBondInvoice popups: the take-order task
// can finish after the DM listener already showed the invoice UI — overwriting
// would drop the popup.
if matches!(
action,
Action::AddInvoice
| Action::AddBondInvoice
| Action::PayInvoice
| Action::PayBondInvoice
) {
app.pending_post_take_operation_result = Some(result);
} else {
app.mode = UiMode::operation_result(result);
}
}
UiMode::ConfirmSavedLnAddressForInvoice(..) => {
app.pending_post_take_operation_result = Some(result);
}
_ => {
app.mode = UiMode::operation_result(result);
}
}
}