From 477f2448e97e65a56af6cb9d17c72e6f9cd0a3a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francisco=20Calder=C3=B3n?= Date: Tue, 10 Dec 2024 15:18:12 -0300 Subject: [PATCH] More cosmetics and payload related fixes --- src/app/admin_add_solver.rs | 13 +++++++------ src/app/admin_cancel.rs | 18 ++++++++++++++---- src/app/admin_settle.rs | 11 ++++++++--- src/app/admin_take_dispute.rs | 7 ++----- src/app/cancel.rs | 35 ++++++++++++++++++++++++++--------- src/app/dispute.rs | 7 ++++--- src/app/order.rs | 1 + src/app/rate_user.rs | 5 +++-- src/app/release.rs | 17 +++++++++++++---- src/flow.rs | 4 ++++ src/nip33.rs | 4 +++- 11 files changed, 85 insertions(+), 37 deletions(-) diff --git a/src/app/admin_add_solver.rs b/src/app/admin_add_solver.rs index 204ef66..6cf5615 100644 --- a/src/app/admin_add_solver.rs +++ b/src/app/admin_add_solver.rs @@ -1,7 +1,7 @@ use crate::util::{send_cant_do_msg, send_dm}; use anyhow::Result; -use mostro_core::message::{Action, Payload, Message}; +use mostro_core::message::{Action, Message, Payload}; use mostro_core::user::User; use nostr::nips::nip59::UnwrappedGift; use nostr_sdk::prelude::*; @@ -19,13 +19,13 @@ pub async fn admin_add_solver_action( let request_id = msg.get_inner_message_kind().request_id; let inner_message = msg.get_inner_message_kind(); - let content = if let Some(content) = &inner_message.content { - content + let payload = if let Some(payload) = &inner_message.payload { + payload } else { error!("No pubkey found!"); return Ok(()); }; - let npubkey = if let Payload::TextMessage(p) = content { + let npubkey = if let Payload::TextMessage(p) = payload { p } else { error!("No pubkey found!"); @@ -38,15 +38,16 @@ pub async fn admin_add_solver_action( send_cant_do_msg(request_id, None, None, &event.sender).await; return Ok(()); } + let trade_index = inner_message.trade_index.unwrap_or(0); let public_key = PublicKey::from_bech32(npubkey)?.to_hex(); - let user = User::new(public_key, 0, 1, 0, 0, None); + let user = User::new(public_key, 0, 1, 0, 0, trade_index); // Use CRUD to create user match user.create(pool).await { Ok(r) => info!("Solver added: {:#?}", r), Err(ee) => error!("Error creating solver: {:#?}", ee), } // We create a Message for admin - let message = Message::new_dispute(request_id, None, None, Action::AdminAddSolver, None, None); + let message = Message::new_dispute(None, request_id, None, Action::AdminAddSolver, None); let message = message.as_json()?; // Send the message let sender_keys = crate::util::get_keys().unwrap(); diff --git a/src/app/admin_cancel.rs b/src/app/admin_cancel.rs index 1c4d75c..c7acd84 100644 --- a/src/app/admin_cancel.rs +++ b/src/app/admin_cancel.rs @@ -31,15 +31,17 @@ pub async fn admin_cancel_action( } else { return Err(Error::msg("No order id")); }; + let inner_message = msg.get_inner_message_kind(); match is_assigned_solver(pool, &event.sender.to_string(), order_id).await { Ok(false) => { send_new_order_msg( - msg.get_inner_message_kind().request_id, + inner_message.request_id, Some(order_id), Action::IsNotYourDispute, None, &event.sender, + inner_message.trade_index, ) .await; return Ok(()); @@ -62,8 +64,9 @@ pub async fn admin_cancel_action( // Was order cooperatively cancelled? if order.status == Status::CooperativelyCanceled.to_string() { let message = MessageKind::new( - request_id, Some(order_id), + request_id, + inner_message.trade_index, Action::CooperativeCancelAccepted, None, ); @@ -76,11 +79,12 @@ pub async fn admin_cancel_action( if order.status != Status::Dispute.to_string() { send_new_order_msg( - msg.get_inner_message_kind().request_id, + inner_message.request_id, Some(order.id), Action::NotAllowedByStatus, None, &event.sender, + inner_message.trade_index, ) .await; return Ok(()); @@ -135,7 +139,13 @@ pub async fn admin_cancel_action( let order_updated = update_order_event(my_keys, Status::CanceledByAdmin, &order).await?; order_updated.update(pool).await?; // We create a Message for cancel - let message = Message::new_order(request_id, Some(order.id), Action::AdminCanceled, None); + let message = Message::new_order( + Some(order.id), + request_id, + inner_message.trade_index, + Action::AdminCanceled, + None, + ); let message = message.as_json()?; // Message to admin let sender_keys = crate::util::get_keys().unwrap(); diff --git a/src/app/admin_settle.rs b/src/app/admin_settle.rs index c5358a8..7003dd6 100644 --- a/src/app/admin_settle.rs +++ b/src/app/admin_settle.rs @@ -33,6 +33,7 @@ pub async fn admin_settle_action( } else { return Err(Error::msg("No order id")); }; + let inner_message = msg.get_inner_message_kind(); match is_assigned_solver(pool, &event.sender.to_string(), order_id).await { Ok(false) => { @@ -42,6 +43,7 @@ pub async fn admin_settle_action( Action::IsNotYourDispute, None, &event.sender, + inner_message.trade_index, ) .await; return Ok(()); @@ -64,8 +66,9 @@ pub async fn admin_settle_action( // Was orde cooperatively cancelled? if order.status == Status::CooperativelyCanceled.to_string() { let message = MessageKind::new( - msg.get_inner_message_kind().request_id, Some(order_id), + msg.get_inner_message_kind().request_id, + inner_message.trade_index, Action::CooperativeCancelAccepted, None, ); @@ -78,11 +81,12 @@ pub async fn admin_settle_action( if order.status != Status::Dispute.to_string() { send_new_order_msg( - msg.get_inner_message_kind().request_id, + inner_message.request_id, Some(order.id), Action::NotAllowedByStatus, None, &event.sender, + inner_message.trade_index, ) .await; return Ok(()); @@ -140,8 +144,9 @@ pub async fn admin_settle_action( } // We create a Message for settle let message = Message::new_order( - request_id, Some(order_updated.id), + request_id, + inner_message.trade_index, Action::AdminSettled, None, ); diff --git a/src/app/admin_take_dispute.rs b/src/app/admin_take_dispute.rs index 520e6cb..c41e24d 100644 --- a/src/app/admin_take_dispute.rs +++ b/src/app/admin_take_dispute.rs @@ -4,7 +4,7 @@ use crate::util::{get_nostr_client, send_cant_do_msg, send_dm, send_new_order_ms use anyhow::{Error, Result}; use mostro_core::dispute::{Dispute, Status}; -use mostro_core::message::{Action, Payload, Message, Peer}; +use mostro_core::message::{Action, Message, Payload, Peer}; use mostro_core::order::Order; use nostr::nips::nip59::UnwrappedGift; use nostr_sdk::prelude::*; @@ -63,7 +63,7 @@ pub async fn admin_take_dispute_action( Action::NotFound, None, &event.sender, - None + None, ) .await; return Ok(()); @@ -113,7 +113,6 @@ pub async fn admin_take_dispute_action( None, Action::AdminTookDispute, Some(Payload::Order(new_order)), - None, ); let message = message.as_json()?; let sender_keys = crate::util::get_keys().unwrap(); @@ -127,7 +126,6 @@ pub async fn admin_take_dispute_action( None, Action::AdminTookDispute, Some(Payload::Peer(solver_pubkey.clone())), - None, ); let msg_to_seller = Message::new_order( @@ -136,7 +134,6 @@ pub async fn admin_take_dispute_action( None, Action::AdminTookDispute, Some(Payload::Peer(solver_pubkey)), - None, ); let (seller_pubkey, buyer_pubkey) = match (&order.seller_pubkey, &order.buyer_pubkey) { diff --git a/src/app/cancel.rs b/src/app/cancel.rs index fc7a26b..9dc7fcf 100644 --- a/src/app/cancel.rs +++ b/src/app/cancel.rs @@ -50,6 +50,7 @@ pub async fn cancel_action( Action::IsNotYourOrder, None, &event.sender, + None, ) .await; } else { @@ -65,7 +66,7 @@ pub async fn cancel_action( Action::Canceled, None, &event.sender, - None + None, ) .await; } @@ -133,7 +134,7 @@ pub async fn cancel_action( Action::CooperativeCancelAccepted, None, &event.sender, - None + None, ) .await; let counterparty_pubkey = PublicKey::from_str(&counterparty_pubkey)?; @@ -143,7 +144,7 @@ pub async fn cancel_action( Action::CooperativeCancelAccepted, None, &counterparty_pubkey, - None + None, ) .await; info!("Cancel: Order Id {order_id} canceled cooperatively!"); @@ -160,7 +161,7 @@ pub async fn cancel_action( Action::CooperativeCancelInitiatedByYou, None, &event.sender, - None + None, ) .await; let counterparty_pubkey = PublicKey::from_str(&counterparty_pubkey)?; @@ -170,7 +171,7 @@ pub async fn cancel_action( Action::CooperativeCancelInitiatedByPeer, None, &counterparty_pubkey, - None + None, ) .await; } @@ -217,10 +218,18 @@ pub async fn cancel_add_invoice( Action::Canceled, None, &event.sender, - None + None, + ) + .await; + send_new_order_msg( + None, + Some(order.id), + Action::Canceled, + None, + &seller_pubkey, + None, ) .await; - send_new_order_msg(None, Some(order.id), Action::Canceled, None, &seller_pubkey, None).await; Ok(()) } else { // We re-publish the event with Pending status @@ -280,10 +289,18 @@ pub async fn cancel_pay_hold_invoice( Action::Canceled, None, &event.sender, - None + None, + ) + .await; + send_new_order_msg( + None, + Some(order.id), + Action::Canceled, + None, + &seller_pubkey, + None, ) .await; - send_new_order_msg(None, Some(order.id), Action::Canceled, None, &seller_pubkey, None).await; Ok(()) } else { // We re-publish the event with Pending status diff --git a/src/app/dispute.rs b/src/app/dispute.rs index a382894..6f33502 100644 --- a/src/app/dispute.rs +++ b/src/app/dispute.rs @@ -11,7 +11,7 @@ use crate::util::{get_nostr_client, send_cant_do_msg, send_new_order_msg}; use anyhow::{Error, Result}; use mostro_core::dispute::Dispute; -use mostro_core::message::{Action, Payload, Message}; +use mostro_core::message::{Action, Message, Payload}; use mostro_core::order::{Order, Status}; use nostr::nips::nip59::UnwrappedGift; use nostr_sdk::prelude::*; @@ -121,6 +121,7 @@ async fn get_valid_order( Action::NotAllowedByStatus, None, &event.sender, + None, ) .await; return Err(Error::msg(format!( @@ -241,7 +242,7 @@ pub async fn dispute_action( Action::DisputeInitiatedByYou, Some(Payload::Dispute(dispute.clone().id, initiator_token)), &initiator_pubkey, - None + None, ) .await; @@ -259,7 +260,7 @@ pub async fn dispute_action( Action::DisputeInitiatedByPeer, Some(Payload::Dispute(dispute.clone().id, counterpart_token)), &counterpart_pubkey, - None + None, ) .await; diff --git a/src/app/order.rs b/src/app/order.rs index 2fdd58d..30a6f96 100644 --- a/src/app/order.rs +++ b/src/app/order.rs @@ -59,6 +59,7 @@ pub async fn order_action( Action::InvalidSatsAmount, None, &event.sender, + None, ) .await; return Ok(()); diff --git a/src/app/rate_user.rs b/src/app/rate_user.rs index f57d95b..2bdf755 100644 --- a/src/app/rate_user.rs +++ b/src/app/rate_user.rs @@ -2,7 +2,7 @@ use crate::util::{send_cant_do_msg, send_new_order_msg, update_user_rating_event use crate::NOSTR_CLIENT; use anyhow::{Error, Result}; -use mostro_core::message::{Action, Payload, Message}; +use mostro_core::message::{Action, Message, Payload}; use mostro_core::order::{Order, Status}; use mostro_core::rating::Rating; use mostro_core::NOSTR_REPLACEABLE_EVENT_KIND; @@ -120,7 +120,7 @@ pub async fn update_user_reputation_action( // Check if content of Peer is the same of counterpart let rating; - if let Some(Payload::RatingUser(v)) = msg.get_inner_message_kind().content.to_owned() { + if let Some(Payload::RatingUser(v)) = msg.get_inner_message_kind().payload.to_owned() { rating = v; } else { return Err(Error::msg("No rating present")); @@ -173,6 +173,7 @@ pub async fn update_user_reputation_action( Action::RateReceived, Some(Payload::RatingUser(rating)), &event.sender, + None, ) .await; } diff --git a/src/app/release.rs b/src/app/release.rs index 1ac09b3..51f98bb 100644 --- a/src/app/release.rs +++ b/src/app/release.rs @@ -50,7 +50,7 @@ pub async fn check_failure_retries(order: &Order, request_id: Option) -> Re Action::PaymentFailed, None, &buyer_pubkey, - None + None, ) .await; @@ -108,6 +108,7 @@ pub async fn release_action( Action::NotAllowedByStatus, None, &event.sender, + None, ) .await; return Ok(()); @@ -138,7 +139,7 @@ pub async fn release_action( Action::HoldInvoicePaymentSettled, None, &seller_pubkey, - None + None, ) .await; @@ -147,7 +148,15 @@ pub async fn release_action( Some(buyer) => PublicKey::from_str(buyer.as_str())?, _ => return Err(Error::msg("Missing buyer pubkeys")), }; - send_new_order_msg(None, Some(order_id), Action::Released, None, &buyer_pubkey, None).await; + send_new_order_msg( + None, + Some(order_id), + Action::Released, + None, + &buyer_pubkey, + None, + ) + .await; let _ = do_payment(order_updated, request_id).await; @@ -254,7 +263,7 @@ async fn payment_success( Action::PurchaseCompleted, None, buyer_pubkey, - None + None, ) .await; diff --git a/src/flow.rs b/src/flow.rs index 5f11bc5..02d63cb 100644 --- a/src/flow.rs +++ b/src/flow.rs @@ -66,6 +66,7 @@ pub async fn hold_invoice_paid(hash: &str, request_id: Option) -> Result<() Action::BuyerTookOrder, Some(Payload::Order(order_data.clone())), &seller_pubkey, + None, ) .await; // We send a message to buyer saying seller paid @@ -75,6 +76,7 @@ pub async fn hold_invoice_paid(hash: &str, request_id: Option) -> Result<() Action::HoldInvoicePaymentAccepted, Some(Payload::Order(order_data)), &buyer_pubkey, + None, ) .await; } else { @@ -91,6 +93,7 @@ pub async fn hold_invoice_paid(hash: &str, request_id: Option) -> Result<() Action::AddInvoice, Some(Payload::Order(order_data)), &buyer_pubkey, + None, ) .await; @@ -101,6 +104,7 @@ pub async fn hold_invoice_paid(hash: &str, request_id: Option) -> Result<() Action::WaitingBuyerInvoice, None, &seller_pubkey, + None, ) .await; } diff --git a/src/nip33.rs b/src/nip33.rs index e3bef7e..1a66849 100644 --- a/src/nip33.rs +++ b/src/nip33.rs @@ -29,8 +29,10 @@ pub fn new_event( let mut tags: Vec = Vec::with_capacity(1 + extra_tags.len()); tags.push(Tag::identifier(identifier)); tags.extend(extra_tags); + let tags = Tags::new(tags); - EventBuilder::new(Kind::Custom(NOSTR_REPLACEABLE_EVENT_KIND), content, tags) + EventBuilder::new(Kind::Custom(NOSTR_REPLACEABLE_EVENT_KIND), content) + .tags(tags) .sign_with_keys(keys) }