Skip to content

Commit

Permalink
Message refactoring (#222)
Browse files Browse the repository at this point in the history
* update mostro core to ver 5.4

* refactoring cantdo mess

* bigger refactoring of message sending - moving client instance to a global one

* big refactor of message sending

* refactored all cantdo message and neworder message
  • Loading branch information
arkanoider authored Mar 24, 2024
1 parent 9d1ac84 commit 08bb5aa
Show file tree
Hide file tree
Showing 18 changed files with 355 additions and 497 deletions.
52 changes: 18 additions & 34 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use tokio::sync::Mutex;

pub async fn run(
my_keys: Keys,
client: Client,
client: &Client,
ln_client: &mut LndConnector,
pool: Pool<Sqlite>,
rate_list: Arc<Mutex<Vec<Event>>>,
Expand All @@ -61,78 +61,62 @@ pub async fn run(
if let Some(action) = msg.inner_action() {
match action {
Action::NewOrder => {
order_action(msg, &event, &my_keys, &client, &pool)
.await?;
order_action(msg, &event, &my_keys, &pool).await?;
}
Action::TakeSell => {
take_sell_action(msg, &event, &my_keys, &client, &pool)
.await?;
take_sell_action(msg, &event, &my_keys, &pool).await?;
}
Action::TakeBuy => {
take_buy_action(msg, &event, &my_keys, &client, &pool)
.await?;
take_buy_action(msg, &event, &my_keys, &pool).await?;
}
Action::FiatSent => {
fiat_sent_action(msg, &event, &my_keys, &client, &pool)
.await?;
fiat_sent_action(msg, &event, &my_keys, &pool).await?;
}
Action::Release => {
release_action(
msg, &event, &my_keys, &client, &pool, ln_client,
)
.await?;
release_action(msg, &event, &my_keys, &pool, ln_client)
.await?;
}
Action::Cancel => {
cancel_action(
msg, &event, &my_keys, &client, &pool, ln_client,
)
.await?;
cancel_action(msg, &event, &my_keys, &pool, ln_client)
.await?;
}
Action::AddInvoice => {
add_invoice_action(
msg, &event, &my_keys, &client, &pool,
)
.await?;
add_invoice_action(msg, &event, &my_keys, &pool)
.await?;
}
Action::PayInvoice => todo!(),
Action::RateUser => {
update_user_reputation_action(
msg,
&event,
&my_keys,
&client,
&pool,
rate_list.clone(),
)
.await?;
}
Action::Dispute => {
dispute_action(msg, &event, &my_keys, &client, &pool)
.await?;
dispute_action(msg, &event, &my_keys, &pool).await?;
}
Action::AdminCancel => {
admin_cancel_action(
msg, &event, &my_keys, &client, &pool, ln_client,
msg, &event, &my_keys, &pool, ln_client,
)
.await?;
}
Action::AdminSettle => {
admin_settle_action(
msg, &event, &my_keys, &client, &pool, ln_client,
msg, &event, &my_keys, &pool, ln_client,
)
.await?;
}
Action::AdminAddSolver => {
admin_add_solver_action(
msg, &event, &my_keys, &client, &pool,
)
.await?;
admin_add_solver_action(msg, &event, &my_keys, &pool)
.await?;
}
Action::AdminTakeDispute => {
admin_take_dispute_action(
msg, &event, &my_keys, &client, &pool,
)
.await?;
admin_take_dispute_action(msg, &event, &my_keys, &pool)
.await?;
}
_ => todo!(),
}
Expand Down
72 changes: 29 additions & 43 deletions src/app/add_invoice.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::lightning::invoice::is_valid_invoice;
use crate::util::{send_dm, show_hold_invoice, update_order_event};
use crate::util::{send_cant_do_msg, send_new_order_msg, show_hold_invoice, update_order_event};

use anyhow::Result;

Expand All @@ -16,7 +16,6 @@ pub async fn add_invoice_action(
msg: Message,
event: &Event,
my_keys: &Keys,
client: &Client,
pool: &Pool<Sqlite>,
) -> Result<()> {
let order_msg = msg.get_inner_message_kind();
Expand Down Expand Up @@ -55,10 +54,12 @@ pub async fn add_invoice_action(
};
// Only the buyer can add an invoice
if buyer_pubkey != event.pubkey {
let message = Message::cant_do(Some(order.id), None, None);
let message = message.as_json().unwrap();
send_dm(client, my_keys, &event.pubkey, message).await?;

send_cant_do_msg(
Some(order_id),
Some("Not allowed".to_string()),
&event.pubkey,
)
.await;
return Ok(());
}

Expand All @@ -77,15 +78,7 @@ pub async fn add_invoice_action(
{
Ok(_) => payment_request,
Err(e) => {
// We create a Message
let message = Message::cant_do(
Some(order.id),
None,
Some(Content::TextMessage(e.to_string())),
);
let message = message.as_json()?;
send_dm(client, my_keys, &event.pubkey, message).await?;
error!("{e}");
send_cant_do_msg(Some(order_id), Some(e.to_string()), &event.pubkey).await;
return Ok(());
}
}
Expand All @@ -102,28 +95,26 @@ pub async fn add_invoice_action(
Status::SettledHoldInvoice => {
order.payment_attempts = 0;
order.update(pool).await?;
let message = Message::new_order(
send_new_order_msg(
Some(order_id),
None,
Action::AddInvoice,
Some(Content::TextMessage(format!(
"Order Id {order_id}: Invoice updated!"
))),
);
let message = message.as_json()?;
send_dm(client, my_keys, &buyer_pubkey, message).await?;
&buyer_pubkey,
)
.await;
return Ok(());
}
_ => {
let message = Message::cant_do(
Some(order.id),
None,
Some(Content::TextMessage(format!(
send_cant_do_msg(
Some(order_id),
Some(format!(
"Order Id {order_id} status must be WaitingBuyerInvoice!"
))),
);
let message = message.as_json()?;
send_dm(client, my_keys, &buyer_pubkey, message).await?;
)),
&buyer_pubkey,
)
.await;
return Ok(());
}
}
Expand All @@ -148,33 +139,28 @@ pub async fn add_invoice_action(
);
// We publish a new replaceable kind nostr event with the status updated
// and update on local database the status and new event id
if let Ok(order_updated) = update_order_event(client, my_keys, Status::Active, &order).await
{
if let Ok(order_updated) = update_order_event(my_keys, Status::Active, &order).await {
let _ = order_updated.update(pool).await;
}

// We send a confirmation message to seller
let message = Message::new_order(
send_new_order_msg(
Some(order.id),
None,
Action::BuyerTookOrder,
Some(Content::Order(order_data.clone())),
);

send_dm(client, my_keys, &seller_pubkey, message.as_json()?).await?;
&seller_pubkey,
)
.await;
// We send a message to buyer saying seller paid
let message = Message::new_order(
send_new_order_msg(
Some(order.id),
None,
Action::HoldInvoicePaymentAccepted,
Some(Content::Order(order_data)),
);

send_dm(client, my_keys, &buyer_pubkey, message.as_json()?)
.await
.unwrap();
&buyer_pubkey,
)
.await;
} else {
show_hold_invoice(client, my_keys, None, &buyer_pubkey, &seller_pubkey, order).await?;
show_hold_invoice(my_keys, None, &buyer_pubkey, &seller_pubkey, order).await?;
}

Ok(())
Expand Down
10 changes: 3 additions & 7 deletions src/app/admin_add_solver.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::util::send_dm;
use crate::util::{send_cant_do_msg, send_dm};

use anyhow::Result;
use mostro_core::message::{Action, Content, Message};
Expand All @@ -12,7 +12,6 @@ pub async fn admin_add_solver_action(
msg: Message,
event: &Event,
my_keys: &Keys,
client: &Client,
pool: &Pool<Sqlite>,
) -> Result<()> {
let inner_message = msg.get_inner_message_kind();
Expand All @@ -32,10 +31,7 @@ pub async fn admin_add_solver_action(
// Check if the pubkey is Mostro
if event.pubkey.to_string() != my_keys.public_key().to_string() {
// We create a Message
let message = Message::cant_do(None, None, None);
let message = message.as_json()?;
send_dm(client, my_keys, &event.pubkey, message).await?;

send_cant_do_msg(None, Some("Not allowed".to_string()), &event.pubkey).await;
return Ok(());
}
let user = User::new(npubkey.to_string(), 0, 1, 0, 0);
Expand All @@ -48,7 +44,7 @@ pub async fn admin_add_solver_action(
let message = Message::new_dispute(None, None, Action::AdminAddSolver, None);
let message = message.as_json()?;
// Send the message
send_dm(client, my_keys, &event.pubkey, message).await?;
send_dm(&event.pubkey, message).await?;

Ok(())
}
20 changes: 8 additions & 12 deletions src/app/admin_cancel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use std::str::FromStr;
use crate::db::find_dispute_by_order_id;
use crate::lightning::LndConnector;
use crate::nip33::new_event;
use crate::util::{send_dm, update_order_event};
use crate::util::{send_cant_do_msg, send_dm, update_order_event};
use crate::NOSTR_CLIENT;

use anyhow::Result;
use mostro_core::dispute::Status as DisputeStatus;
Expand All @@ -18,7 +19,6 @@ pub async fn admin_cancel_action(
msg: Message,
event: &Event,
my_keys: &Keys,
client: &Client,
pool: &Pool<Sqlite>,
ln_client: &mut LndConnector,
) -> Result<()> {
Expand All @@ -34,10 +34,7 @@ pub async fn admin_cancel_action(
// Check if the pubkey is Mostro
if event.pubkey.to_string() != my_keys.public_key().to_string() {
// We create a Message
let message = Message::cant_do(Some(order.id), None, None);
let message = message.as_json()?;
send_dm(client, my_keys, &event.pubkey, message).await?;

send_cant_do_msg(Some(order_id), None, &event.pubkey).await;
return Ok(());
}

Expand Down Expand Up @@ -65,35 +62,34 @@ pub async fn admin_cancel_action(
// nip33 kind with dispute id as identifier
let event = new_event(my_keys, "", dispute_id.to_string(), tags)?;

client.send_event(event).await?;
NOSTR_CLIENT.get().unwrap().send_event(event).await?;
}

// We publish a new replaceable kind nostr event with the status updated
// and update on local database the status and new event id
let order_updated =
update_order_event(client, my_keys, Status::CanceledByAdmin, &order).await?;
let order_updated = update_order_event(my_keys, Status::CanceledByAdmin, &order).await?;
order_updated.update(pool).await?;
// We create a Message
let message = Message::new_dispute(Some(order.id), None, Action::AdminCancel, None);
let message = message.as_json()?;
// Message to admin
send_dm(client, my_keys, &event.pubkey, message.clone()).await?;
send_dm(&event.pubkey, message.clone()).await?;
let seller_pubkey = match XOnlyPublicKey::from_str(order.seller_pubkey.as_ref().unwrap()) {
Ok(pk) => pk,
Err(e) => {
error!("Error parsing seller pubkey: {:#?}", e);
return Ok(());
}
};
send_dm(client, my_keys, &seller_pubkey, message.clone()).await?;
send_dm(&seller_pubkey, message.clone()).await?;
let buyer_pubkey = match XOnlyPublicKey::from_str(order.buyer_pubkey.as_ref().unwrap()) {
Ok(pk) => pk,
Err(e) => {
error!("Error parsing buyer pubkey: {:#?}", e);
return Ok(());
}
};
send_dm(client, my_keys, &buyer_pubkey, message.clone()).await?;
send_dm(&buyer_pubkey, message.clone()).await?;

Ok(())
}
Loading

0 comments on commit 08bb5aa

Please sign in to comment.