Skip to content

Commit

Permalink
removed other unwraps and minor cosmetics (#272)
Browse files Browse the repository at this point in the history
  • Loading branch information
arkanoider authored Apr 21, 2024
1 parent cef4580 commit 57e7e0f
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 45 deletions.
6 changes: 5 additions & 1 deletion src/app/admin_cancel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ pub async fn admin_cancel_action(
return Ok(());
}

let order_id = msg.get_inner_message_kind().id.unwrap();
let order_id = if let Some(order_id) = msg.get_inner_message_kind().id {
order_id
} else {
return Err(Error::msg("No order id"));
};

match is_assigned_solver(pool, &event.pubkey.to_string(), order_id).await {
Ok(false) => {
Expand Down
9 changes: 7 additions & 2 deletions src/app/admin_settle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::nip33::new_event;
use crate::util::{send_cant_do_msg, send_dm, settle_seller_hold_invoice, update_order_event};
use crate::NOSTR_CLIENT;

use anyhow::Result;
use anyhow::{Error, Result};
use mostro_core::dispute::Status as DisputeStatus;
use mostro_core::message::{Action, Message};
use mostro_core::order::{Order, Status};
Expand All @@ -30,7 +30,12 @@ pub async fn admin_settle_action(

return Ok(());
}
let order_id = msg.get_inner_message_kind().id.unwrap();

let order_id = if let Some(order_id) = msg.get_inner_message_kind().id {
order_id
} else {
return Err(Error::msg("No order id"));
};

match is_assigned_solver(pool, &event.pubkey.to_string(), order_id).await {
Ok(false) => {
Expand Down
30 changes: 25 additions & 5 deletions src/app/admin_take_dispute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ use crate::nip33::new_event;
use crate::util::{send_cant_do_msg, send_dm};
use crate::NOSTR_CLIENT;

use anyhow::Result;
use anyhow::{Error, Result};
use mostro_core::dispute::{Dispute, Status};
use mostro_core::message::{Action, Content, Message, Peer};
use mostro_core::order::Order;
use nostr_sdk::prelude::*;
use sqlx::{Pool, Sqlite};
use sqlx_crud::Crud;
use std::str::FromStr;
use tracing::info;

pub async fn pubkey_event_can_solve(pool: &Pool<Sqlite>, ev_pubkey: &PublicKey) -> bool {
Expand Down Expand Up @@ -41,7 +42,13 @@ pub async fn admin_take_dispute_action(
send_cant_do_msg(None, Some("Not allowed".to_string()), &event.pubkey).await;
return Ok(());
}
let dispute_id = msg.get_inner_message_kind().id.unwrap();

let dispute_id = if let Some(dispute_id) = msg.get_inner_message_kind().id {
dispute_id
} else {
return Err(Error::msg("No order id"));
};

let mut dispute = match Dispute::by_id(pool, dispute_id).await? {
Some(dispute) => dispute,
None => {
Expand All @@ -55,7 +62,12 @@ pub async fn admin_take_dispute_action(
return Ok(());
}
};
let order = Order::by_id(pool, dispute.order_id).await?.unwrap();

let order = match Order::by_id(pool, dispute.order_id).await? {
Some(o) => o,
None => return Err(Error::msg("No order id")),
};

let mut new_order = order.as_new_order();
new_order.master_buyer_pubkey = order.master_buyer_pubkey.clone();
new_order.master_seller_pubkey = order.master_seller_pubkey.clone();
Expand Down Expand Up @@ -85,8 +97,16 @@ pub async fn admin_take_dispute_action(
Action::AdminTookDispute,
Some(Content::Peer(solver_pubkey)),
);
let buyer_pubkey = PublicKey::from_hex(order.buyer_pubkey.clone().unwrap())?;
let seller_pubkey = PublicKey::from_hex(order.seller_pubkey.clone().unwrap())?;

let (seller_pubkey, buyer_pubkey) = match (&order.seller_pubkey, &order.buyer_pubkey) {
(Some(seller), Some(buyer)) => (
PublicKey::from_str(seller.as_str())?,
PublicKey::from_str(buyer.as_str())?,
),
(None, _) => return Err(Error::msg("Missing seller pubkey")),
(_, None) => return Err(Error::msg("Missing buyer pubkey")),
};

let message = message.as_json()?;
send_dm(&buyer_pubkey, message.clone()).await?;
send_dm(&seller_pubkey, message).await?;
Expand Down
30 changes: 16 additions & 14 deletions src/app/cancel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ pub async fn cancel_action(
pool: &Pool<Sqlite>,
ln_client: &mut LndConnector,
) -> Result<()> {
let order_id = msg.get_inner_message_kind().id.unwrap();
let order_id = if let Some(order_id) = msg.get_inner_message_kind().id {
order_id
} else {
return Err(Error::msg("No order id"));
};
let mut order = match Order::by_id(pool, order_id).await? {
Some(order) => order,
None => {
Expand Down Expand Up @@ -89,9 +93,8 @@ pub async fn cancel_action(
send_cant_do_msg(Some(order_id), None, &event.pubkey).await;
return Ok(());
} else {
if order.hash.is_some() {
if let Some(hash) = &order.hash {
// We return funds to seller
let hash = order.hash.as_ref().unwrap();
ln_client.cancel_hold_invoice(hash).await?;
info!(
"Cooperative cancel: Order Id {}: Funds returned to seller",
Expand Down Expand Up @@ -154,12 +157,13 @@ pub async fn cancel_add_invoice(
pool: &Pool<Sqlite>,
my_keys: &Keys,
) -> Result<()> {
if order.hash.is_some() {
// We return funds to seller
let hash = order.hash.as_ref().unwrap();
if let Some(hash) = &order.hash {
ln_client.cancel_hold_invoice(hash).await?;
info!("Order Id {}: Funds returned to seller", &order.id);
} else {
return Err(Error::msg("No hash present"));
}

let user_pubkey = event.pubkey.to_string();

let (seller_pubkey, buyer_pubkey) = match (&order.seller_pubkey, &order.buyer_pubkey) {
Expand Down Expand Up @@ -214,15 +218,13 @@ pub async fn cancel_pay_hold_invoice(
info!("Order Id {}: Funds returned to seller", &order.id);
}
let user_pubkey = event.pubkey.to_string();
let buyer_pubkey = order.buyer_pubkey.as_ref().unwrap();
let seller_pubkey = order.seller_pubkey.as_ref().unwrap();
let seller_pubkey = match PublicKey::from_str(seller_pubkey) {
Ok(pk) => pk,
Err(e) => {
error!("Error parsing seller pubkey: {:#?}", e);
return Ok(());
}

let (seller_pubkey, buyer_pubkey) = match (&order.seller_pubkey, &order.buyer_pubkey) {
(Some(seller), Some(buyer)) => (PublicKey::from_str(seller.as_str())?, buyer),
(None, _) => return Err(Error::msg("Missing seller pubkey")),
(_, None) => return Err(Error::msg("Missing buyer pubkey")),
};

if seller_pubkey.to_string() != user_pubkey {
// We create a Message
send_cant_do_msg(Some(order.id), None, &event.pubkey).await;
Expand Down
16 changes: 12 additions & 4 deletions src/app/dispute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::nip33::new_event;
use crate::util::{send_cant_do_msg, send_new_order_msg};
use crate::NOSTR_CLIENT;

use anyhow::Result;
use anyhow::{Error, Result};
use mostro_core::dispute::Dispute;
use mostro_core::message::{Action, Message};
use mostro_core::order::{Order, Status};
Expand All @@ -20,7 +20,11 @@ pub async fn dispute_action(
my_keys: &Keys,
pool: &Pool<Sqlite>,
) -> Result<()> {
let order_id = msg.get_inner_message_kind().id.unwrap();
let order_id = if let Some(order_id) = msg.get_inner_message_kind().id {
order_id
} else {
return Err(Error::msg("No order id"));
};

// Check dispute for this order id is yet present.
if find_dispute_by_order_id(pool, order_id).await.is_ok() {
Expand Down Expand Up @@ -52,8 +56,12 @@ pub async fn dispute_action(
}
};

let buyer = order.buyer_pubkey.clone().unwrap();
let seller = order.seller_pubkey.clone().unwrap();
let (seller, buyer) = match (&order.seller_pubkey, &order.buyer_pubkey) {
(Some(seller), Some(buyer)) => (seller.to_owned(), buyer.to_owned()),
(None, _) => return Err(Error::msg("Missing seller pubkey")),
(_, None) => return Err(Error::msg("Missing buyer pubkey")),
};

let message_sender = event.pubkey.to_string();
// Get counterpart pubkey
let mut counterpart: String = String::new();
Expand Down
8 changes: 6 additions & 2 deletions src/app/fiat_sent.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::util::{send_cant_do_msg, send_new_order_msg, update_order_event};

use anyhow::Result;
use anyhow::{Error, Result};
use mostro_core::message::{Action, Content, Message, Peer};
use mostro_core::order::{Order, Status};
use nostr_sdk::prelude::*;
Expand All @@ -15,7 +15,11 @@ pub async fn fiat_sent_action(
my_keys: &Keys,
pool: &Pool<Sqlite>,
) -> Result<()> {
let order_id = msg.get_inner_message_kind().id.unwrap();
let order_id = if let Some(order_id) = msg.get_inner_message_kind().id {
order_id
} else {
return Err(Error::msg("No order id"));
};
let order = match Order::by_id(pool, order_id).await? {
Some(order) => order,
None => {
Expand Down
30 changes: 20 additions & 10 deletions src/app/rate_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::util::{
};
use crate::NOSTR_CLIENT;

use anyhow::Result;
use anyhow::{Error, Result};
use mostro_core::message::{Action, Content, Message};
use mostro_core::order::{Order, Status};
use mostro_core::rating::Rating;
Expand All @@ -16,6 +16,9 @@ use std::time::Duration;
use tokio::sync::Mutex;
use tracing::error;

const MAX_RATING: u8 = 5;
const MIN_RATING: u8 = 1;

pub async fn get_counterpart_reputation(user: &str, my_keys: &Keys) -> Result<Option<Rating>> {
// Request NIP33 of the counterparts
let filters = Filter::new()
Expand Down Expand Up @@ -50,7 +53,11 @@ pub async fn update_user_reputation_action(
pool: &Pool<Sqlite>,
rate_list: Arc<Mutex<Vec<Event>>>,
) -> Result<()> {
let order_id = msg.get_inner_message_kind().id.unwrap();
let order_id = if let Some(order_id) = msg.get_inner_message_kind().id {
order_id
} else {
return Err(Error::msg("No order id"));
};
let order = match Order::by_id(pool, order_id).await? {
Some(order) => order,
None => {
Expand All @@ -60,8 +67,12 @@ pub async fn update_user_reputation_action(
};

// Get needed info about users
let buyer = order.buyer_pubkey.unwrap();
let seller = order.seller_pubkey.unwrap();
let (seller, buyer) = match (&order.seller_pubkey, &order.buyer_pubkey) {
(Some(seller), Some(buyer)) => (seller.to_owned(), buyer.to_owned()),
(None, _) => return Err(Error::msg("Missing seller pubkey")),
(_, None) => return Err(Error::msg("Missing buyer pubkey")),
};

let message_sender = event.pubkey.to_string();

if order.status != Status::Success.to_string() {
Expand Down Expand Up @@ -104,19 +115,18 @@ pub async fn update_user_reputation_action(
};

// Check if content of Peer is the same of counterpart
let mut rating = 0_u8;
let rating;

if let Content::RatingUser(v) = msg.get_inner_message_kind().content.to_owned().unwrap() {
if let Some(Content::RatingUser(v)) = msg.get_inner_message_kind().content.to_owned() {
rating = v;
} else {
return Err(Error::msg("No rating present"));
}

// Ask counterpart reputation
let rep = get_counterpart_reputation(&counterpart, my_keys).await?;
// Here we have to update values of the review of the counterpart
let mut reputation;
// min_rate is 1 and max_rate is 5
let min_rate = 1;
let max_rate = 5;

if let Some(r) = rep {
// Update user reputation
Expand All @@ -135,7 +145,7 @@ pub async fn update_user_reputation_action(
// Assing new total rating to review
reputation.total_rating = new_rating;
} else {
reputation = Rating::new(1, rating as f64, min_rate, max_rate, rating);
reputation = Rating::new(1, rating as f64, MIN_RATING, MAX_RATING, rating);
}
let reputation = reputation.to_tags()?;

Expand Down
23 changes: 19 additions & 4 deletions src/app/release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@ pub async fn check_failure_retries(order: &Order) -> Result<Order> {
order.payment_attempts += 1;
}
let msg = format!("I tried to send you the sats but the payment of your invoice failed, I will try {} more times in {} minutes window, please check your node/wallet is online",retries_number,time_window);
let buyer_key = PublicKey::from_str(order.buyer_pubkey.as_ref().unwrap()).unwrap();

send_cant_do_msg(Some(order.id), Some(msg), &buyer_key).await;
let buyer_pubkey = match &order.buyer_pubkey {
Some(buyer) => PublicKey::from_str(buyer.as_str())?,
None => return Err(Error::msg("Missing buyer pubkey")),
};

send_cant_do_msg(Some(order.id), Some(msg), &buyer_pubkey).await;

// Update order
let result = order.update(&pool).await?;
Expand All @@ -54,7 +58,13 @@ pub async fn release_action(
pool: &Pool<Sqlite>,
ln_client: &mut LndConnector,
) -> Result<()> {
let order_id = msg.get_inner_message_kind().id.unwrap();
// Check if order id is ok
let order_id = if let Some(order_id) = msg.get_inner_message_kind().id {
order_id
} else {
return Err(Error::msg("No order id"));
};

let order = match Order::by_id(pool, order_id).await? {
Some(order) => order,
None => {
Expand All @@ -71,7 +81,12 @@ pub async fn release_action(
};
let seller_pubkey = event.pubkey;

let current_status = Status::from_str(&order.status).unwrap();
let current_status = if let Ok(current_status) = Status::from_str(&order.status) {
current_status
} else {
return Err(Error::msg("Wrong order status"));
};

if current_status != Status::Active
&& current_status != Status::FiatSent
&& current_status != Status::Dispute
Expand Down
9 changes: 7 additions & 2 deletions src/app/take_buy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::util::{
get_market_amount_and_fee, send_cant_do_msg, send_new_order_msg, show_hold_invoice,
};

use anyhow::Result;
use anyhow::{Error, Result};
use mostro_core::message::{Action, Content, Message};
use mostro_core::order::{Kind, Order, Status};
use nostr_sdk::prelude::*;
Expand All @@ -18,7 +18,12 @@ pub async fn take_buy_action(
pool: &Pool<Sqlite>,
) -> Result<()> {
// Safe unwrap as we verified the message
let order_id = msg.get_inner_message_kind().id.unwrap();
let order_id = if let Some(order_id) = msg.get_inner_message_kind().id {
order_id
} else {
return Err(Error::msg("No order id"));
};

let mut order = match Order::by_id(pool, order_id).await? {
Some(order) => order,
None => {
Expand Down
6 changes: 5 additions & 1 deletion src/app/take_sell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ pub async fn take_sell_action(
pool: &Pool<Sqlite>,
) -> Result<()> {
// Safe unwrap as we verified the message
let order_id = msg.get_inner_message_kind().id.unwrap();
let order_id = if let Some(order_id) = msg.get_inner_message_kind().id {
order_id
} else {
return Err(Error::msg("No order id"));
};

let mut order = match Order::by_id(pool, order_id).await? {
Some(order) => order,
Expand Down

0 comments on commit 57e7e0f

Please sign in to comment.