Skip to content

Commit

Permalink
More cosmetics and payload related fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
grunch committed Dec 10, 2024
1 parent cf4140a commit 477f244
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 37 deletions.
13 changes: 7 additions & 6 deletions src/app/admin_add_solver.rs
Original file line number Diff line number Diff line change
@@ -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::*;
Expand All @@ -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!");
Expand All @@ -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();
Expand Down
18 changes: 14 additions & 4 deletions src/app/admin_cancel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(());
Expand All @@ -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,
);
Expand All @@ -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(());
Expand Down Expand Up @@ -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();
Expand Down
11 changes: 8 additions & 3 deletions src/app/admin_settle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -42,6 +43,7 @@ pub async fn admin_settle_action(
Action::IsNotYourDispute,
None,
&event.sender,
inner_message.trade_index,
)
.await;
return Ok(());
Expand All @@ -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,
);
Expand All @@ -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(());
Expand Down Expand Up @@ -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,
);
Expand Down
7 changes: 2 additions & 5 deletions src/app/admin_take_dispute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -63,7 +63,7 @@ pub async fn admin_take_dispute_action(
Action::NotFound,
None,
&event.sender,
None
None,
)
.await;
return Ok(());
Expand Down Expand Up @@ -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();
Expand All @@ -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(
Expand All @@ -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) {
Expand Down
35 changes: 26 additions & 9 deletions src/app/cancel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub async fn cancel_action(
Action::IsNotYourOrder,
None,
&event.sender,
None,
)
.await;
} else {
Expand All @@ -65,7 +66,7 @@ pub async fn cancel_action(
Action::Canceled,
None,
&event.sender,
None
None,
)
.await;
}
Expand Down Expand Up @@ -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)?;
Expand All @@ -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!");
Expand All @@ -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)?;
Expand All @@ -170,7 +171,7 @@ pub async fn cancel_action(
Action::CooperativeCancelInitiatedByPeer,
None,
&counterparty_pubkey,
None
None,
)
.await;
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions src/app/dispute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -121,6 +121,7 @@ async fn get_valid_order(
Action::NotAllowedByStatus,
None,
&event.sender,
None,
)
.await;
return Err(Error::msg(format!(
Expand Down Expand Up @@ -241,7 +242,7 @@ pub async fn dispute_action(
Action::DisputeInitiatedByYou,
Some(Payload::Dispute(dispute.clone().id, initiator_token)),
&initiator_pubkey,
None
None,
)
.await;

Expand All @@ -259,7 +260,7 @@ pub async fn dispute_action(
Action::DisputeInitiatedByPeer,
Some(Payload::Dispute(dispute.clone().id, counterpart_token)),
&counterpart_pubkey,
None
None,
)
.await;

Expand Down
1 change: 1 addition & 0 deletions src/app/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub async fn order_action(
Action::InvalidSatsAmount,
None,
&event.sender,
None,
)
.await;
return Ok(());
Expand Down
5 changes: 3 additions & 2 deletions src/app/rate_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"));
Expand Down Expand Up @@ -173,6 +173,7 @@ pub async fn update_user_reputation_action(
Action::RateReceived,
Some(Payload::RatingUser(rating)),
&event.sender,
None,
)
.await;
}
Expand Down
Loading

0 comments on commit 477f244

Please sign in to comment.