From e47353c85ee27a200681ac9568d01dde8b2bf15c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francisco=20Calder=C3=B3n?= Date: Wed, 3 May 2023 15:17:50 -0300 Subject: [PATCH] bug fixes and refactoring (#65) * bug fixes and refactoring * sqlx prepared data --- sqlx-data.json | 20 ++++++++++++++++++++ src/app.rs | 2 ++ src/app/cancel.rs | 40 ++++++---------------------------------- src/app/dispute.rs | 1 + src/app/fiat_sent.rs | 10 +--------- src/app/order.rs | 12 ++---------- src/app/rate_user.rs | 1 + src/app/release.rs | 14 ++++---------- src/app/take_buy.rs | 29 +++++++++++++---------------- src/app/take_sell.rs | 12 ++---------- src/messages.rs | 4 ---- 11 files changed, 52 insertions(+), 93 deletions(-) diff --git a/sqlx-data.json b/sqlx-data.json index 704bc30e..c3db9660 100644 --- a/sqlx-data.json +++ b/sqlx-data.json @@ -20,6 +20,16 @@ }, "query": "\n UPDATE orders\n SET\n status = ?1,\n amount = ?2,\n event_id = ?3\n WHERE id = ?4\n " }, + "1a4f24e4d6c6447e7f6b9c7207bd12bc23e87f37d2c8676bffd8f4eb86c31405": { + "describe": { + "columns": [], + "nullable": [], + "parameters": { + "Right": 2 + } + }, + "query": "\n UPDATE orders\n SET\n seller_dispute = ?1\n WHERE id = ?2\n " + }, "301fbda63c61bf75d60bc330d5b1aacae80248c253a2fe9e97f5a74a7acafe75": { "describe": { "columns": [], @@ -80,6 +90,16 @@ }, "query": "\n UPDATE orders\n SET\n buyer_invoice = ?1\n WHERE id = ?2\n " }, + "900597cf1e8ed114cb383856d398ffb23b1d15e48c1708fc2b40cfcc649df9fc": { + "describe": { + "columns": [], + "nullable": [], + "parameters": { + "Right": 2 + } + }, + "query": "\n UPDATE orders\n SET\n buyer_dispute = ?1\n WHERE id = ?2\n " + }, "97414183485e04346f420960beed77912ebe76089f58d30f0f526e232ce4be25": { "describe": { "columns": [], diff --git a/src/app.rs b/src/app.rs index 994c5a9a..852a1c9d 100644 --- a/src/app.rs +++ b/src/app.rs @@ -35,6 +35,8 @@ pub async fn run( while let Ok(notification) = notifications.recv().await { if let RelayPoolNotification::Event(_, event) = notification { if let Kind::EncryptedDirectMessage = event.kind { + // We validates if the event is correctly signed + event.verify()?; let message = decrypt( &my_keys.secret_key().unwrap(), &event.pubkey, diff --git a/src/app/cancel.rs b/src/app/cancel.rs index 26dd4433..595f5682 100644 --- a/src/app/cancel.rs +++ b/src/app/cancel.rs @@ -3,12 +3,11 @@ use crate::db::{ update_order_to_initial_state, }; use crate::lightning::LndConnector; -use crate::messages; use crate::util::{send_dm, update_order_event}; use anyhow::Result; use log::{error, info}; use mostro_core::order::Order; -use mostro_core::{Action, Content, Message, Status}; +use mostro_core::{Action, Message, Status}; use nostr_sdk::prelude::*; use sqlx::{Pool, Sqlite}; use sqlx_crud::Crud; @@ -33,17 +32,11 @@ pub async fn cancel_action( let user_pubkey = event.pubkey.to_bech32()?; // Validates if this user is the order creator if user_pubkey != order.creator_pubkey { - let text_message = messages::cant_do(); // We create a Message - let message = Message::new( - 0, - Some(order.id), - None, - Action::CantDo, - Some(Content::TextMessage(text_message)), - ); + let message = Message::new(0, Some(order.id), None, Action::CantDo, None); let message = message.as_json()?; send_dm(client, my_keys, &event.pubkey, message).await?; + return Ok(()); } // We publish a new replaceable kind nostr event with the status updated @@ -79,15 +72,8 @@ pub async fn cancel_action( match order.cancel_initiator_pubkey { Some(ref initiator_pubkey) => { if initiator_pubkey == &user_pubkey { - let text_message = messages::cant_do(); // We create a Message - let message = Message::new( - 0, - Some(order.id), - None, - Action::CantDo, - Some(Content::TextMessage(text_message)), - ); + let message = Message::new(0, Some(order.id), None, Action::CantDo, None); let message = message.as_json()?; send_dm(client, my_keys, &event.pubkey, message).await?; @@ -172,15 +158,8 @@ pub async fn cancel_add_invoice( let seller_pubkey = order.seller_pubkey.as_ref().cloned().unwrap(); let seller_pubkey = XOnlyPublicKey::from_bech32(seller_pubkey)?; if buyer_pubkey_bech32 != &user_pubkey { - let text_message = messages::cant_do(); // We create a Message - let message = Message::new( - 0, - Some(order.id), - None, - Action::CantDo, - Some(Content::TextMessage(text_message)), - ); + let message = Message::new(0, Some(order.id), None, Action::CantDo, None); let message = message.as_json()?; send_dm(client, my_keys, &event.pubkey, message).await?; @@ -234,15 +213,8 @@ pub async fn cancel_pay_hold_invoice( let seller_pubkey_bech32 = order.seller_pubkey.as_ref().unwrap(); let seller_pubkey = XOnlyPublicKey::from_bech32(seller_pubkey_bech32)?; if seller_pubkey_bech32 != &user_pubkey { - let text_message = messages::cant_do(); // We create a Message - let message = Message::new( - 0, - Some(order.id), - None, - Action::CantDo, - Some(Content::TextMessage(text_message)), - ); + let message = Message::new(0, Some(order.id), None, Action::CantDo, None); let message = message.as_json()?; send_dm(client, my_keys, &event.pubkey, message).await?; diff --git a/src/app/dispute.rs b/src/app/dispute.rs index 64a86fd4..2d432cd3 100644 --- a/src/app/dispute.rs +++ b/src/app/dispute.rs @@ -48,6 +48,7 @@ pub async fn dispute_action( let message = Message::new(0, Some(order.id), None, Action::CantDo, None); let message = message.as_json()?; send_dm(client, my_keys, &event.pubkey, message).await?; + return Ok(()); }; diff --git a/src/app/fiat_sent.rs b/src/app/fiat_sent.rs index 8ec3aed2..afd38189 100644 --- a/src/app/fiat_sent.rs +++ b/src/app/fiat_sent.rs @@ -1,4 +1,3 @@ -use crate::messages; use crate::util::{send_dm, update_order_event}; use anyhow::Result; @@ -31,15 +30,8 @@ pub async fn fiat_sent_action( } // Check if the pubkey is the buyer if Some(event.pubkey.to_bech32()?) != order.buyer_pubkey { - let text_message = messages::cant_do(); // We create a Message - let message = Message::new( - 0, - Some(order.id), - None, - Action::CantDo, - Some(Content::TextMessage(text_message)), - ); + let message = Message::new(0, Some(order.id), None, Action::CantDo, None); let message = message.as_json()?; send_dm(client, my_keys, &event.pubkey, message).await?; diff --git a/src/app/order.rs b/src/app/order.rs index bdd190ae..b0b8c460 100644 --- a/src/app/order.rs +++ b/src/app/order.rs @@ -1,8 +1,7 @@ -use crate::messages; use crate::util::{publish_order, send_dm}; use anyhow::Result; -use mostro_core::{Action, Content, Message}; +use mostro_core::{Action, Message}; use nostr_sdk::prelude::ToBech32; use nostr_sdk::{Client, Event, Keys}; use sqlx::{Pool, Sqlite}; @@ -19,15 +18,8 @@ pub async fn order_action( let master_pubkey = match msg.pubkey { Some(ref pk) => pk, None => { - let text_message = messages::cant_do(); // We create a Message - let message = Message::new( - 0, - order.id, - None, - Action::CantDo, - Some(Content::TextMessage(text_message)), - ); + let message = Message::new(0, order.id, None, Action::CantDo, None); let message = message.as_json()?; send_dm(client, my_keys, &event.pubkey, message).await?; diff --git a/src/app/rate_user.rs b/src/app/rate_user.rs index 0fc1e78d..620f46ab 100644 --- a/src/app/rate_user.rs +++ b/src/app/rate_user.rs @@ -183,6 +183,7 @@ pub async fn update_user_reputation_action( let message = Message::new(0, Some(order.id), None, Action::CantDo, None); let message = message.as_json()?; send_dm(client, my_keys, &event.pubkey, message).await?; + return Ok(()); }; diff --git a/src/app/release.rs b/src/app/release.rs index 9289d3a4..b8b8be6c 100644 --- a/src/app/release.rs +++ b/src/app/release.rs @@ -1,13 +1,12 @@ use crate::db::{self}; use crate::lightning::LndConnector; -use crate::messages; use crate::util::{connect_nostr, get_keys}; use crate::util::{rate_counterpart, send_dm, update_order_event}; use anyhow::Result; use log::{error, info}; use mostro_core::order::Order; -use mostro_core::{Action, Content, Message, Status}; +use mostro_core::{Action, Message, Status}; use nostr_sdk::prelude::*; use sqlx::{Pool, Sqlite}; use sqlx_crud::Crud; @@ -32,17 +31,12 @@ pub async fn release_action( }; let seller_pubkey = event.pubkey; if Some(seller_pubkey.to_bech32()?) != order.seller_pubkey { - let text_message = messages::cant_do(); // We create a Message - let message = Message::new( - 0, - Some(order.id), - None, - Action::CantDo, - Some(Content::TextMessage(text_message)), - ); + let message = Message::new(0, Some(order.id), None, Action::CantDo, None); let message = message.as_json()?; send_dm(client, my_keys, &event.pubkey, message).await?; + + return Ok(()); } if order.preimage.is_none() { diff --git a/src/app/take_buy.rs b/src/app/take_buy.rs index c4bc129f..6c2fe310 100644 --- a/src/app/take_buy.rs +++ b/src/app/take_buy.rs @@ -1,5 +1,4 @@ use crate::db::edit_master_seller_pubkey_order; -use crate::messages; use crate::util::{get_market_quote, send_dm, show_hold_invoice}; use anyhow::Result; @@ -29,15 +28,8 @@ pub async fn take_buy_action( }; // We check if the message have a pubkey if msg.pubkey.is_none() { - let text_message = messages::cant_do(); // We create a Message - let message = Message::new( - 0, - Some(order.id), - None, - Action::CantDo, - Some(Content::TextMessage(text_message)), - ); + let message = Message::new(0, Some(order.id), None, Action::CantDo, None); let message = message.as_json()?; send_dm(client, my_keys, &event.pubkey, message).await?; @@ -62,13 +54,18 @@ pub async fn take_buy_action( }; // Seller can take pending orders only if order_status != Status::Pending { - send_dm( - client, - my_keys, - &seller_pubkey, - format!("Order Id {order_id} was already taken!"), // TODO: send a Message - ) - .await?; + // We create a Message + let message = Message::new( + 0, + Some(order.id), + None, + Action::FiatSent, + Some(Content::TextMessage(format!( + "Order Id {order_id} was already taken!" + ))), + ); + let message = message.as_json().unwrap(); + send_dm(client, my_keys, &seller_pubkey, message).await?; return Ok(()); } diff --git a/src/app/take_sell.rs b/src/app/take_sell.rs index f673e383..7ed2b746 100644 --- a/src/app/take_sell.rs +++ b/src/app/take_sell.rs @@ -1,13 +1,12 @@ use crate::db::{edit_buyer_pubkey_order, edit_master_buyer_pubkey_order}; use crate::error::MostroError; use crate::lightning::invoice::is_valid_invoice; -use crate::messages; use crate::util::{send_dm, set_market_order_sats_amount, show_hold_invoice}; use anyhow::Result; use log::error; use mostro_core::order::Order; -use mostro_core::{Action, Content, Message, Status}; +use mostro_core::{Action, Message, Status}; use nostr_sdk::prelude::*; use sqlx::{Pool, Sqlite}; use sqlx_crud::Crud; @@ -36,15 +35,8 @@ pub async fn take_sell_action( } // We check if the message have a pubkey if msg.pubkey.is_none() { - let text_message = messages::cant_do(); // We create a Message - let message = Message::new( - 0, - Some(order.id), - None, - Action::CantDo, - Some(Content::TextMessage(text_message)), - ); + let message = Message::new(0, Some(order.id), None, Action::CantDo, None); let message = message.as_json()?; send_dm(client, my_keys, &event.pubkey, message).await?; diff --git a/src/messages.rs b/src/messages.rs index 08e6cfba..eef4368c 100644 --- a/src/messages.rs +++ b/src/messages.rs @@ -1,10 +1,6 @@ use anyhow::Result; use nostr_sdk::prelude::*; -pub fn cant_do() -> String { - "You can't do that!".to_string() -} - pub fn hold_invoice_description( mostro_pubkey: XOnlyPublicKey, order_id: &str,