Skip to content

Commit

Permalink
Code refactoring and fixes
Browse files Browse the repository at this point in the history
* bumps to mostro core v0.6.16
* add new fields on users table, fields related to user rating
* fix bug parsing message and content on app.rs
* change all dm destination, now is sending to the trade key
  • Loading branch information
grunch committed Dec 12, 2024
1 parent 9fec4cf commit 819670b
Show file tree
Hide file tree
Showing 16 changed files with 40 additions and 34 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.15", features = ["sqlx"] }
mostro-core = { version = "0.6.16", features = ["sqlx"] }
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
config = "0.14.0"
Expand Down
12 changes: 9 additions & 3 deletions migrations/20231005195154_users.sql
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
CREATE TABLE IF NOT EXISTS users (
pubkey char(64) primary key not null,
id char(36) primary key not null,
pubkey char(64) unique not null,
is_admin integer not null default 0,
is_solver integer not null default 0,
is_banned integer not null default 0,
category integer not null default 0,
created_at integer not null,
trade_index integer not null default 0
last_trade_index integer not null default 0,
total_reviews integer not null default 0,
total_rating integer not null default 0,
last_rating integer not null default 0,
max_rating integer not null default 0,
min_rating integer not null default 0,
created_at integer not null
);
12 changes: 6 additions & 6 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ async fn check_trade_index(pool: &Pool<Sqlite>, event: &UnwrappedGift, msg: &Mes
match is_user_present(pool, event.sender.to_string()).await {
Ok(mut user) => {
if let (true, index) = message_kind.has_trade_index() {
let (_, sig): (String, nostr_sdk::secp256k1::schnorr::Signature) =
let (_, sig): (Message, nostr_sdk::secp256k1::schnorr::Signature) =
serde_json::from_str(&event.rumor.content).unwrap();
if index > user.trade_index
if index > user.last_trade_index
&& msg
.get_inner_message_kind()
.verify_signature(event.sender, sig)
{
user.trade_index = index;
user.last_trade_index = index;
if user.update(pool).await.is_ok() {
tracing::info!("Update user trade index");
}
Expand All @@ -88,17 +88,17 @@ async fn check_trade_index(pool: &Pool<Sqlite>, event: &UnwrappedGift, msg: &Mes
None,
msg.get_inner_message_kind().id,
Some(CantDoReason::InvalidTradeIndex),
&event.sender,
&event.rumor.pubkey,
)
.await;
}
}
}
Err(_) => {
if let (true, trade_index) = message_kind.has_trade_index() {
if let (true, last_trade_index) = message_kind.has_trade_index() {
let new_user = User {
pubkey: event.sender.to_string(),
trade_index,
last_trade_index,
..Default::default()
};
if new_user.create(pool).await.is_ok() {
Expand Down
2 changes: 1 addition & 1 deletion src/app/add_invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub async fn add_invoice_action(
};
// Only the buyer can add an invoice
if buyer_pubkey != event.sender {
send_cant_do_msg(request_id, Some(order.id), None, &event.sender).await;
send_cant_do_msg(request_id, Some(order.id), None, &event.rumor.pubkey).await;
return Ok(());
}

Expand Down
2 changes: 1 addition & 1 deletion src/app/admin_add_solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub async fn admin_add_solver_action(
// Check if the pubkey is Mostro
if event.sender.to_string() != my_keys.public_key().to_string() {
// We create a Message
send_cant_do_msg(request_id, None, None, &event.sender).await;
send_cant_do_msg(request_id, None, None, &event.rumor.pubkey).await;
return Ok(());
}
let trade_index = inner_message.trade_index.unwrap_or(0);
Expand Down
2 changes: 1 addition & 1 deletion src/app/admin_take_dispute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub async fn admin_take_dispute_action(
if let Ok(dispute_status) = Status::from_str(&dispute.status) {
if !pubkey_event_can_solve(pool, &event.sender, dispute_status).await {
// We create a Message
send_cant_do_msg(request_id, Some(dispute_id), None, &event.sender).await;
send_cant_do_msg(request_id, Some(dispute_id), None, &event.rumor.pubkey).await;
return Ok(());
}
} else {
Expand Down
6 changes: 3 additions & 3 deletions src/app/cancel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ pub async fn cancel_action(
Some(ref initiator_pubkey) => {
if initiator_pubkey == &user_pubkey {
// We create a Message
send_cant_do_msg(request_id, Some(order_id), None, &event.sender).await;
send_cant_do_msg(request_id, Some(order_id), None, &event.rumor.pubkey).await;
return Ok(());
} else {
if let Some(hash) = &order.hash {
Expand Down Expand Up @@ -203,7 +203,7 @@ pub async fn cancel_add_invoice(

if buyer_pubkey != &user_pubkey {
// We create a Message
send_cant_do_msg(request_id, Some(order.id), None, &event.sender).await;
send_cant_do_msg(request_id, Some(order.id), None, &event.rumor.pubkey).await;
return Ok(());
}

Expand Down Expand Up @@ -274,7 +274,7 @@ pub async fn cancel_pay_hold_invoice(

if seller_pubkey.to_string() != user_pubkey {
// We create a Message
send_cant_do_msg(request_id, Some(order.id), None, &event.sender).await;
send_cant_do_msg(request_id, Some(order.id), None, &event.rumor.pubkey).await;
return Ok(());
}

Expand Down
2 changes: 1 addition & 1 deletion src/app/dispute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ 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.sender).await;
send_cant_do_msg(request_id, Some(order.id), None, &event.rumor.pubkey).await;
return Ok(());
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/app/fiat_sent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub async fn fiat_sent_action(
}
// Check if the pubkey is the buyer
if Some(event.sender.to_string()) != order.buyer_pubkey {
send_cant_do_msg(request_id, Some(order.id), None, &event.sender).await;
send_cant_do_msg(request_id, Some(order.id), None, &event.rumor.pubkey).await;
return Ok(());
}

Expand Down
12 changes: 6 additions & 6 deletions src/app/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub async fn order_action(
order.id,
Action::IncorrectInvoiceAmount,
None,
&event.sender,
&event.rumor.pubkey,
None,
)
.await;
Expand All @@ -49,7 +49,7 @@ 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.sender).await;
send_cant_do_msg(request_id, order.id, None, &event.rumor.pubkey).await;
return Ok(());
}
if order.amount != 0 {
Expand All @@ -58,7 +58,7 @@ pub async fn order_action(
None,
Action::InvalidSatsAmount,
None,
&event.sender,
&event.rumor.pubkey,
None,
)
.await;
Expand Down Expand Up @@ -91,7 +91,7 @@ pub async fn order_action(
None,
Action::InvalidSatsAmount,
None,
&event.sender,
&event.rumor.pubkey,
None,
)
.await;
Expand All @@ -106,7 +106,7 @@ pub async fn order_action(
None,
Action::OutOfRangeSatsAmount,
None,
&event.sender,
&event.rumor.pubkey,
None,
)
.await;
Expand All @@ -119,7 +119,7 @@ pub async fn order_action(
my_keys,
order,
&event.sender.to_string(),
event.sender,
event.rumor.pubkey,
request_id,
msg.get_inner_message_kind().trade_index,
)
Expand Down
4 changes: 2 additions & 2 deletions src/app/rate_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub async fn update_user_reputation_action(
let message_sender = event.sender.to_string();

if order.status != Status::Success.to_string() {
send_cant_do_msg(request_id, Some(order.id), None, &event.sender).await;
send_cant_do_msg(request_id, Some(order.id), None, &event.rumor.pubkey).await;
error!("Order Id {order_id} wrong status");
return Ok(());
}
Expand All @@ -100,7 +100,7 @@ pub async fn update_user_reputation_action(
// Add a check in case of no counterpart found
if counterpart.is_empty() {
// We create a Message
send_cant_do_msg(request_id, Some(order.id), None, &event.sender).await;
send_cant_do_msg(request_id, Some(order.id), None, &event.rumor.pubkey).await;
return Ok(());
};

Expand Down
2 changes: 1 addition & 1 deletion src/app/release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ pub async fn release_action(
}

if &seller_pubkey.to_string() != seller_pubkey_hex {
send_cant_do_msg(request_id, Some(order.id), None, &event.sender).await;
send_cant_do_msg(request_id, Some(order.id), None, &event.rumor.pubkey).await;
return Ok(());
}

Expand Down
2 changes: 1 addition & 1 deletion src/app/take_buy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub async fn take_buy_action(

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

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

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

Expand Down Expand Up @@ -73,7 +73,7 @@ pub async fn take_sell_action(
request_id,
Some(order.id),
Some(CantDoReason::InvalidInvoice),
&event.sender,
&event.rumor.pubkey,
)
.await;
error!("{e}");
Expand Down
4 changes: 2 additions & 2 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ pub async fn settle_seller_hold_invoice(
) -> Result<()> {
// Check if the pubkey is right
if !is_admin && event.sender.to_string() != *order.seller_pubkey.as_ref().unwrap().to_string() {
send_cant_do_msg(request_id, Some(order.id), None, &event.sender).await;
send_cant_do_msg(request_id, Some(order.id), None, &event.rumor.pubkey).await;
return Err(Error::msg("Not allowed"));
}

Expand All @@ -599,7 +599,7 @@ pub async fn settle_seller_hold_invoice(
ln_client.settle_hold_invoice(preimage).await?;
info!("{action}: Order Id {}: hold invoice settled", order.id);
} else {
send_cant_do_msg(request_id, Some(order.id), None, &event.sender).await;
send_cant_do_msg(request_id, Some(order.id), None, &event.rumor.pubkey).await;
return Err(Error::msg("No preimage"));
}
Ok(())
Expand Down

0 comments on commit 819670b

Please sign in to comment.