Skip to content

Commit

Permalink
Code refactoring removing old actions
Browse files Browse the repository at this point in the history
  • Loading branch information
grunch committed Dec 28, 2024
1 parent 5746830 commit ea15cd9
Show file tree
Hide file tree
Showing 13 changed files with 222 additions and 216 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ uuid = { version = "1.8.0", features = [
"serde",
] }
reqwest = { version = "0.12.1", features = ["json"] }
mostro-core = { version = "0.6.21", features = ["sqlx"] }
mostro-core = { version = "0.6.22", features = ["sqlx"] }
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
config = "0.14.0"
Expand Down
12 changes: 4 additions & 8 deletions src/app/add_invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,11 @@ pub async fn add_invoice_action(
{
Ok(_) => payment_request,
Err(_) => {
send_new_order_msg(
send_cant_do_msg(
request_id,
Some(order.id),
Action::IncorrectInvoiceAmount,
None,
Some(CantDoReason::InvalidAmount),
&event.rumor.pubkey,
None,
)
.await;
return Ok(());
Expand Down Expand Up @@ -120,13 +118,11 @@ pub async fn add_invoice_action(
return Ok(());
}
_ => {
send_new_order_msg(
send_cant_do_msg(
request_id,
Some(order.id),
Action::NotAllowedByStatus,
None,
Some(CantDoReason::NotAllowedByStatus),
&event.rumor.pubkey,
None,
)
.await;
return Ok(());
Expand Down
22 changes: 10 additions & 12 deletions src/app/admin_cancel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use std::str::FromStr;
use crate::db::{find_dispute_by_order_id, is_assigned_solver};
use crate::lightning::LndConnector;
use crate::nip33::new_event;
use crate::util::{get_nostr_client, send_dm, send_new_order_msg, update_order_event};
use crate::util::{get_nostr_client, send_cant_do_msg, send_dm, update_order_event};

use anyhow::{Error, Result};
use mostro_core::dispute::Status as DisputeStatus;
use mostro_core::message::{Action, Message, MessageKind};
use mostro_core::message::{Action, CantDoReason, Message, MessageKind};
use mostro_core::order::{Order, Status};
use nostr::nips::nip59::UnwrappedGift;
use nostr_sdk::prelude::*;
Expand All @@ -35,15 +35,14 @@ pub async fn admin_cancel_action(

match is_assigned_solver(pool, &event.rumor.pubkey.to_string(), order_id).await {
Ok(false) => {
send_new_order_msg(
inner_message.request_id,
send_cant_do_msg(
request_id,
Some(order_id),
Action::IsNotYourDispute,
None,
Some(CantDoReason::IsNotYourDispute),
&event.rumor.pubkey,
inner_message.trade_index,
)
.await;

return Ok(());
}
Err(e) => {
Expand Down Expand Up @@ -78,15 +77,14 @@ pub async fn admin_cancel_action(
}

if order.status != Status::Dispute.to_string() {
send_new_order_msg(
inner_message.request_id,
send_cant_do_msg(
request_id,
Some(order.id),
Action::NotAllowedByStatus,
None,
Some(CantDoReason::NotAllowedByStatus),
&event.rumor.pubkey,
inner_message.trade_index,
)
.await;

return Ok(());
}

Expand Down
22 changes: 10 additions & 12 deletions src/app/admin_settle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use crate::db::{find_dispute_by_order_id, is_assigned_solver};
use crate::lightning::LndConnector;
use crate::nip33::new_event;
use crate::util::{
get_nostr_client, send_dm, send_new_order_msg, settle_seller_hold_invoice, update_order_event,
get_nostr_client, send_cant_do_msg, send_dm, settle_seller_hold_invoice, update_order_event,
};

use anyhow::{Error, Result};
use mostro_core::dispute::Status as DisputeStatus;
use mostro_core::message::{Action, Message, MessageKind};
use mostro_core::message::{Action, CantDoReason, Message, MessageKind};
use mostro_core::order::{Order, Status};
use nostr::nips::nip59::UnwrappedGift;
use nostr_sdk::prelude::*;
Expand Down Expand Up @@ -37,15 +37,14 @@ pub async fn admin_settle_action(

match is_assigned_solver(pool, &event.rumor.pubkey.to_string(), order_id).await {
Ok(false) => {
send_new_order_msg(
msg.get_inner_message_kind().request_id,
send_cant_do_msg(
request_id,
Some(order_id),
Action::IsNotYourDispute,
None,
Some(CantDoReason::IsNotYourDispute),
&event.rumor.pubkey,
inner_message.trade_index,
)
.await;

return Ok(());
}
Err(e) => {
Expand Down Expand Up @@ -80,15 +79,14 @@ pub async fn admin_settle_action(
}

if order.status != Status::Dispute.to_string() {
send_new_order_msg(
inner_message.request_id,
send_cant_do_msg(
request_id,
Some(order.id),
Action::NotAllowedByStatus,
None,
Some(CantDoReason::NotAllowedByStatus),
&event.rumor.pubkey,
inner_message.trade_index,
)
.await;

return Ok(());
}

Expand Down
10 changes: 4 additions & 6 deletions src/app/admin_take_dispute.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::db::find_solver_pubkey;
use crate::nip33::new_event;
use crate::util::{get_nostr_client, send_cant_do_msg, send_dm, send_new_order_msg};
use crate::util::{get_nostr_client, send_cant_do_msg, send_dm};

use anyhow::{Error, Result};
use mostro_core::dispute::{Dispute, Status};
Expand Down Expand Up @@ -56,16 +56,14 @@ pub async fn admin_take_dispute_action(
let mut dispute = match Dispute::by_id(pool, dispute_id).await? {
Some(dispute) => dispute,
None => {
// We create a Message
send_new_order_msg(
send_cant_do_msg(
request_id,
Some(dispute_id),
Action::NotFound,
None,
Some(CantDoReason::NotFound),
&event.rumor.pubkey,
None,
)
.await;

return Ok(());
}
};
Expand Down
7 changes: 2 additions & 5 deletions src/app/cancel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,11 @@ pub async fn cancel_action(
if order.status == Status::Pending.to_string() {
// Validates if this user is the order creator
if user_pubkey != order.creator_pubkey {
// We create a Message
send_new_order_msg(
send_cant_do_msg(
request_id,
Some(order.id),
Action::IsNotYourOrder,
None,
Some(CantDoReason::IsNotYourOrder),
&event.rumor.pubkey,
None,
)
.await;
} else {
Expand Down
7 changes: 3 additions & 4 deletions src/app/dispute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,14 @@ async fn get_valid_order(
// Only allow disputes for Active or FiatSent orders
if !matches!(status, Status::Active | Status::FiatSent) {
// Notify the sender that the action is not allowed for this status
send_new_order_msg(
send_cant_do_msg(
request_id,
Some(order.id),
Action::NotAllowedByStatus,
None,
Some(CantDoReason::NotAllowedByStatus),
&event.rumor.pubkey,
None,
)
.await;

return Err(Error::msg(format!(
"Order {} with status {} does not allow disputes. Must be Active or FiatSent",
order.id, order.status
Expand Down
7 changes: 3 additions & 4 deletions src/app/fiat_sent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,14 @@ pub async fn fiat_sent_action(
};
// Send to user a DM with the error
if order.status != Status::Active.to_string() {
send_new_order_msg(
send_cant_do_msg(
request_id,
Some(order.id),
Action::NotAllowedByStatus,
None,
Some(CantDoReason::NotAllowedByStatus),
&event.rumor.pubkey,
None,
)
.await;

return Ok(());
}
// Check if the pubkey is the buyer
Expand Down
31 changes: 13 additions & 18 deletions src/app/order.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::cli::settings::Settings;
use crate::lightning::invoice::is_valid_invoice;
use crate::util::{get_bitcoin_price, publish_order, send_cant_do_msg, send_new_order_msg};
use crate::util::{get_bitcoin_price, publish_order, send_cant_do_msg};
use anyhow::Result;
use mostro_core::message::{Action, CantDoReason, Message};
use mostro_core::message::{CantDoReason, Message};
use nostr::nips::nip59::UnwrappedGift;
use nostr_sdk::prelude::*;
use nostr_sdk::Keys;
Expand All @@ -28,15 +28,14 @@ pub async fn order_action(
match is_valid_invoice(invoice.clone(), None, None).await {
Ok(_) => (),
Err(_) => {
send_new_order_msg(
send_cant_do_msg(
request_id,
order.id,
Action::IncorrectInvoiceAmount,
None,
Some(CantDoReason::InvalidAmount),
&event.rumor.pubkey,
None,
)
.await;

return Ok(());
}
}
Expand All @@ -59,15 +58,14 @@ pub async fn order_action(
return Ok(());
}
if order.amount != 0 {
send_new_order_msg(
send_cant_do_msg(
request_id,
None,
Action::InvalidSatsAmount,
None,
Some(CantDoReason::InvalidAmount),
&event.rumor.pubkey,
None,
)
.await;

return Ok(());
}
amount_vec.clear();
Expand Down Expand Up @@ -106,28 +104,25 @@ pub async fn order_action(

// Check amount is positive - extra safety check
if quote < 0 {
send_new_order_msg(
send_cant_do_msg(
request_id,
None,
Action::InvalidSatsAmount,
None,
Some(CantDoReason::InvalidAmount),
&event.rumor.pubkey,
None,
)
.await;

return Ok(());
}

if quote > mostro_settings.max_order_amount as i64
|| quote < mostro_settings.min_payment_amount as i64
{
send_new_order_msg(
send_cant_do_msg(
request_id,
None,
Action::OutOfRangeSatsAmount,
None,
Some(CantDoReason::OutOfRangeSatsAmount),
&event.rumor.pubkey,
None,
)
.await;
return Ok(());
Expand Down
Loading

0 comments on commit ea15cd9

Please sign in to comment.