Skip to content

Commit

Permalink
Add missing cantdo reasons on cantdo msgs (#407)
Browse files Browse the repository at this point in the history
Bug fixes
bumps mostro core version
  • Loading branch information
grunch authored Dec 27, 2024
1 parent cf85572 commit 1aac442
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 25 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.20", features = ["sqlx"] }
mostro-core = { version = "0.6.21", features = ["sqlx"] }
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
config = "0.14.0"
Expand Down
2 changes: 1 addition & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ async fn check_trade_index(pool: &Pool<Sqlite>, event: &UnwrappedGift, msg: &Mes
send_cant_do_msg(
None,
msg.get_inner_message_kind().id,
Some(CantDoReason::InvalidTextMessage),
Some(CantDoReason::CantCreateUser),
&event.rumor.pubkey,
)
.await;
Expand Down
10 changes: 8 additions & 2 deletions src/app/admin_add_solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::db::add_new_user;
use crate::util::{send_cant_do_msg, send_dm};

use anyhow::Result;
use mostro_core::message::{Action, Message, Payload};
use mostro_core::message::{Action, CantDoReason, Message, Payload};
use mostro_core::user::User;
use nostr::nips::nip59::UnwrappedGift;
use nostr_sdk::prelude::*;
Expand Down Expand Up @@ -35,7 +35,13 @@ pub async fn admin_add_solver_action(
// Check if the pubkey is Mostro
if event.rumor.pubkey.to_string() != my_keys.public_key().to_string() {
// We create a Message
send_cant_do_msg(request_id, None, None, &event.rumor.pubkey).await;
send_cant_do_msg(
request_id,
None,
Some(CantDoReason::InvalidPubkey),
&event.rumor.pubkey,
)
.await;
return Ok(());
}
let trade_index = inner_message.trade_index.unwrap_or(0);
Expand Down
10 changes: 8 additions & 2 deletions src/app/admin_take_dispute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::util::{get_nostr_client, send_cant_do_msg, send_dm, send_new_order_ms

use anyhow::{Error, Result};
use mostro_core::dispute::{Dispute, Status};
use mostro_core::message::{Action, Message, Payload, Peer};
use mostro_core::message::{Action, CantDoReason, Message, Payload, Peer};
use mostro_core::order::Order;
use nostr::nips::nip59::UnwrappedGift;
use nostr_sdk::prelude::*;
Expand Down Expand Up @@ -74,7 +74,13 @@ pub async fn admin_take_dispute_action(
if let Ok(dispute_status) = Status::from_str(&dispute.status) {
if !pubkey_event_can_solve(pool, &event.rumor.pubkey, dispute_status).await {
// We create a Message
send_cant_do_msg(request_id, Some(dispute_id), None, &event.rumor.pubkey).await;
send_cant_do_msg(
request_id,
Some(dispute_id),
Some(CantDoReason::InvalidPubkey),
&event.rumor.pubkey,
)
.await;
return Ok(());
}
} else {
Expand Down
19 changes: 16 additions & 3 deletions src/app/cancel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::lightning::LndConnector;
use crate::util::{send_cant_do_msg, send_new_order_msg, update_order_event};

use anyhow::{Error, Result};
use mostro_core::message::{Action, Message};
use mostro_core::message::{Action, CantDoReason, Message};
use mostro_core::order::{Kind as OrderKind, Order, Status};
use nostr::nips::nip59::UnwrappedGift;
use nostr_sdk::prelude::*;
Expand Down Expand Up @@ -40,6 +40,20 @@ pub async fn cancel_action(
}
};

if order.status == Status::Canceled.to_string()
|| order.status == Status::CooperativelyCanceled.to_string()
|| order.status == Status::CanceledByAdmin.to_string()
{
send_cant_do_msg(
request_id,
Some(order_id),
Some(CantDoReason::OrderAlreadyCanceled),
&event.rumor.pubkey,
)
.await;
return Ok(());
}

if order.status == Status::Pending.to_string() {
// Validates if this user is the order creator
if user_pubkey != order.creator_pubkey {
Expand Down Expand Up @@ -75,8 +89,7 @@ pub async fn cancel_action(
}

if order.kind == OrderKind::Sell.to_string()
&& (order.status == Status::WaitingBuyerInvoice.to_string()
|| order.status == Status::WaitingBuyerInvoice.to_string())
&& order.status == Status::WaitingBuyerInvoice.to_string()
{
cancel_add_invoice(ln_client, &mut order, event, pool, my_keys, request_id).await?;
}
Expand Down
10 changes: 8 additions & 2 deletions src/app/dispute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::util::{get_nostr_client, send_cant_do_msg, send_new_order_msg};

use anyhow::{Error, Result};
use mostro_core::dispute::Dispute;
use mostro_core::message::{Action, Message, Payload};
use mostro_core::message::{Action, CantDoReason, Message, Payload};
use mostro_core::order::{Order, Status};
use nostr::nips::nip59::UnwrappedGift;
use nostr_sdk::prelude::*;
Expand Down Expand Up @@ -184,7 +184,13 @@ pub async fn dispute_action(
match get_counterpart_info(&message_sender, &buyer, &seller) {
Ok((counterpart, is_buyer_dispute)) => (counterpart, is_buyer_dispute),
Err(_) => {
send_cant_do_msg(request_id, Some(order.id), None, &event.rumor.pubkey).await;
send_cant_do_msg(
request_id,
Some(order.id),
Some(CantDoReason::InvalidPubkey),
&event.rumor.pubkey,
)
.await;
return Ok(());
}
};
Expand Down
10 changes: 8 additions & 2 deletions src/app/fiat_sent.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::util::{send_cant_do_msg, send_new_order_msg, update_order_event};

use anyhow::{Error, Result};
use mostro_core::message::{Action, Message, Payload, Peer};
use mostro_core::message::{Action, CantDoReason, Message, Payload, Peer};
use mostro_core::order::{Order, Status};
use nostr::nips::nip59::UnwrappedGift;
use nostr_sdk::prelude::*;
Expand Down Expand Up @@ -46,7 +46,13 @@ pub async fn fiat_sent_action(
}
// Check if the pubkey is the buyer
if Some(event.rumor.pubkey.to_string()) != order.buyer_pubkey {
send_cant_do_msg(request_id, Some(order.id), None, &event.rumor.pubkey).await;
send_cant_do_msg(
request_id,
Some(order.id),
Some(CantDoReason::InvalidPubkey),
&event.rumor.pubkey,
)
.await;
return Ok(());
}

Expand Down
21 changes: 16 additions & 5 deletions src/app/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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 anyhow::Result;
use mostro_core::message::{Action, Message};
use mostro_core::message::{Action, CantDoReason, Message};
use nostr::nips::nip59::UnwrappedGift;
use nostr_sdk::prelude::*;
use nostr_sdk::Keys;
Expand Down Expand Up @@ -49,7 +49,13 @@ pub async fn order_action(
// in case of single order do like usual
if let (Some(min), Some(max)) = (order.min_amount, order.max_amount) {
if min >= max {
send_cant_do_msg(request_id, order.id, None, &event.rumor.pubkey).await;
send_cant_do_msg(
request_id,
order.id,
Some(CantDoReason::InvalidAmount),
&event.rumor.pubkey,
)
.await;
return Ok(());
}
if order.amount != 0 {
Expand All @@ -70,11 +76,16 @@ pub async fn order_action(
}

let premium = (order.premium != 0).then_some(order.premium);
let amount = (order.amount != 0).then_some(order.amount);
let fiat_amount = (order.fiat_amount != 0).then_some(order.fiat_amount);

if premium.is_some() && amount.is_some() && fiat_amount.is_some() {
send_cant_do_msg(request_id, None, None, &event.sender).await;
if premium.is_some() && fiat_amount.is_some() {
send_cant_do_msg(
request_id,
None,
Some(CantDoReason::InvalidParameters),
&event.sender,
)
.await;
return Ok(());
}

Expand Down
8 changes: 7 additions & 1 deletion src/app/rate_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,13 @@ pub async fn update_user_reputation_action(
let message_sender = event.rumor.pubkey.to_string();

if order.status != Status::Success.to_string() {
send_cant_do_msg(request_id, Some(order.id), None, &event.rumor.pubkey).await;
send_cant_do_msg(
request_id,
Some(order.id),
Some(CantDoReason::InvalidOrderStatus),
&event.rumor.pubkey,
)
.await;
error!("Order Id {order_id} wrong status");
return Ok(());
}
Expand Down
10 changes: 8 additions & 2 deletions src/app/take_buy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::util::{
};

use anyhow::{Error, Result};
use mostro_core::message::{Action, Message};
use mostro_core::message::{Action, CantDoReason, Message};
use mostro_core::order::{Kind, Order, Status};
use nostr::nips::nip59::UnwrappedGift;
use nostr_sdk::prelude::*;
Expand Down Expand Up @@ -38,7 +38,13 @@ pub async fn take_buy_action(

// Maker can't take own order
if order.kind != Kind::Buy.to_string() || order.creator_pubkey == event.rumor.pubkey.to_hex() {
send_cant_do_msg(request_id, Some(order.id), None, &event.rumor.pubkey).await;
send_cant_do_msg(
request_id,
Some(order.id),
Some(CantDoReason::InvalidPubkey),
&event.rumor.pubkey,
)
.await;
return Ok(());
}

Expand Down
8 changes: 7 additions & 1 deletion src/app/take_sell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ pub async fn take_sell_action(

// Maker can't take own order
if order.creator_pubkey == event.rumor.pubkey.to_hex() {
send_cant_do_msg(request_id, Some(order.id), None, &event.rumor.pubkey).await;
send_cant_do_msg(
request_id,
Some(order.id),
Some(CantDoReason::InvalidPubkey),
&event.rumor.pubkey,
)
.await;
return Ok(());
}

Expand Down
2 changes: 1 addition & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ pub async fn settle_seller_hold_invoice(
send_cant_do_msg(
request_id,
Some(order.id),
Some(CantDoReason::InvalidPeer),
Some(CantDoReason::InvalidPubkey),
&event.rumor.pubkey,
)
.await;
Expand Down

0 comments on commit 1aac442

Please sign in to comment.