Skip to content

Commit

Permalink
bug fixes and refactoring (#65)
Browse files Browse the repository at this point in the history
* bug fixes and refactoring

* sqlx prepared data
  • Loading branch information
grunch authored May 3, 2023
1 parent c17778c commit e47353c
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 93 deletions.
20 changes: 20 additions & 0 deletions sqlx-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [],
Expand Down Expand Up @@ -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": [],
Expand Down
2 changes: 2 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
40 changes: 6 additions & 34 deletions src/app/cancel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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?;

Expand Down Expand Up @@ -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?;

Expand Down Expand Up @@ -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?;

Expand Down
1 change: 1 addition & 0 deletions src/app/dispute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(());
};

Expand Down
10 changes: 1 addition & 9 deletions src/app/fiat_sent.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::messages;
use crate::util::{send_dm, update_order_event};

use anyhow::Result;
Expand Down Expand Up @@ -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?;

Expand Down
12 changes: 2 additions & 10 deletions src/app/order.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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?;

Expand Down
1 change: 1 addition & 0 deletions src/app/rate_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(());
};

Expand Down
14 changes: 4 additions & 10 deletions src/app/release.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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() {
Expand Down
29 changes: 13 additions & 16 deletions src/app/take_buy.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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?;

Expand All @@ -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(());
}
Expand Down
12 changes: 2 additions & 10 deletions src/app/take_sell.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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?;

Expand Down
4 changes: 0 additions & 4 deletions src/messages.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down

0 comments on commit e47353c

Please sign in to comment.