Skip to content

Commit

Permalink
First draft on message version 1
Browse files Browse the repository at this point in the history
  • Loading branch information
grunch committed Nov 28, 2023
1 parent 496456e commit 82123b5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ uuid = { version = "1.3.0", features = [
"serde",
] }
reqwest = { version = "0.11", features = ["json"] }
mostro-core = "0.3.13"
mostro-core = { path = "../mostro-core" }
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.16", features = ["env-filter"] }
config = "0.13.3"
Expand Down
32 changes: 16 additions & 16 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::app::take_sell::take_sell_action;
use crate::lightning::LndConnector;

use anyhow::Result;
use mostro_core::{Action, Message};
use mostro_core::message::{Action, Message};
use nostr_sdk::prelude::*;
use sqlx::{Pool, Sqlite};
use std::sync::Arc;
Expand Down Expand Up @@ -58,40 +58,40 @@ pub async fn run(
let message = Message::from_json(&m);
if let Ok(msg) = message {
if msg.verify() {
match msg.action {
Action::Order => {
match msg.get_action() {
Some(Action::NewOrder) => {
order_action(msg, &event, &my_keys, &client, &pool).await?;
}
Action::TakeSell => {
Some(Action::TakeSell) => {
take_sell_action(msg, &event, &my_keys, &client, &pool)
.await?;
}
Action::TakeBuy => {
Some(Action::TakeBuy) => {
take_buy_action(msg, &event, &my_keys, &client, &pool)
.await?;
}
Action::FiatSent => {
Some(Action::FiatSent) => {
fiat_sent_action(msg, &event, &my_keys, &client, &pool)
.await?;
}
Action::Release => {
Some(Action::Release) => {
release_action(
msg, &event, &my_keys, &client, &pool, ln_client,
)
.await?;
}
Action::Cancel => {
Some(Action::Cancel) => {
cancel_action(
msg, &event, &my_keys, &client, &pool, ln_client,
)
.await?;
}
Action::AddInvoice => {
Some(Action::AddInvoice) => {
add_invoice_action(msg, &event, &my_keys, &client, &pool)
.await?;
}
Action::PayInvoice => todo!(),
Action::RateUser => {
Some(Action::PayInvoice) => todo!(),
Some(Action::RateUser) => {
update_user_reputation_action(
msg,
&event,
Expand All @@ -102,29 +102,29 @@ pub async fn run(
)
.await?;
}
Action::Dispute => {
Some(Action::Dispute) => {
dispute_action(msg, &event, &my_keys, &client, &pool)
.await?;
}
Action::AdminCancel => {
Some(Action::AdminCancel) => {
admin_cancel_action(
msg, &event, &my_keys, &client, &pool, ln_client,
)
.await?;
}
Action::AdminSettle => {
Some(Action::AdminSettle) => {
admin_settle_action(
msg, &event, &my_keys, &client, &pool, ln_client,
)
.await?;
}
Action::AdminAddSolver => {
Some(Action::AdminAddSolver) => {
admin_add_solver_action(
msg, &event, &my_keys, &client, &pool,
)
.await?;
}
Action::AdminTakeDispute => {
Some(Action::AdminTakeDispute) => {
admin_take_dispute_action(
msg, &event, &my_keys, &client, &pool,
)
Expand Down
25 changes: 16 additions & 9 deletions src/app/add_invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ use crate::util::{send_dm, show_hold_invoice};

use anyhow::Result;
use log::error;
use mostro_core::message::{Action, Content, Message};
use mostro_core::order::SmallOrder;
use mostro_core::order::{Order, Status};
use mostro_core::{order::SmallOrder, Action, Content, Message};
use nostr_sdk::prelude::*;
use sqlx::{Pool, Sqlite};
use sqlx_crud::Crud;
Expand All @@ -19,19 +20,25 @@ pub async fn add_invoice_action(
client: &Client,
pool: &Pool<Sqlite>,
) -> Result<()> {
let order_msg = match msg.get_order() {
Some(order) => order,
None => {
error!("Message without order!");
return Ok(());
}
};
// Safe unwrap as we verified the message
let order_id = msg.order_id.unwrap();
let order_id = order_msg.id.unwrap();
let order = match Order::by_id(pool, order_id).await? {
Some(order) => order,
None => {
error!("Order Id {order_id} not found!");
return Ok(());
}
};

let pr: String;
// If a buyer sent me a lightning invoice we get it
if let Some(payment_request) = msg.get_payment_request() {
if let Some(payment_request) = order_msg.get_payment_request() {
// Verify if invoice is valid
match is_valid_invoice(
&payment_request,
Expand All @@ -46,7 +53,7 @@ pub async fn add_invoice_action(
| MostroError::WrongAmountError
| MostroError::MinAmountError => {
// We create a Message
let message = Message::new(
let message = Message::new_order(
0,
Some(order.id),
None,
Expand Down Expand Up @@ -84,7 +91,7 @@ pub async fn add_invoice_action(
// Only the buyer can add an invoice
if buyer_pubkey != event.pubkey {
// We create a Message
let message = Message::new(0, Some(order.id), None, Action::CantDo, None);
let message = Message::new_order(0, Some(order.id), None, Action::CantDo, None);
let message = message.as_json().unwrap();
send_dm(client, my_keys, &event.pubkey, message).await?;

Expand All @@ -95,7 +102,7 @@ pub async fn add_invoice_action(
Status::WaitingBuyerInvoice => {}
_ => {
// We create a Message
let message = Message::new(
let message = Message::new_order(
0,
Some(order.id),
None,
Expand Down Expand Up @@ -126,7 +133,7 @@ pub async fn add_invoice_action(
order.seller_pubkey.as_ref().cloned(),
);
// We send a confirmation message to seller
let message = Message::new(
let message = Message::new_order(
0,
Some(order.id),
None,
Expand All @@ -137,7 +144,7 @@ pub async fn add_invoice_action(

send_dm(client, my_keys, &seller_pubkey, message).await?;
// We send a message to buyer saying seller paid
let message = Message::new(
let message = Message::new_order(
0,
Some(order.id),
None,
Expand Down

0 comments on commit 82123b5

Please sign in to comment.