diff --git a/src/app/rate_user.rs b/src/app/rate_user.rs index ee91c4c5..1e2901be 100644 --- a/src/app/rate_user.rs +++ b/src/app/rate_user.rs @@ -124,12 +124,10 @@ pub async fn update_user_reputation_action( let min_rate = 1; let max_rate = 5; - if rep.is_none() { - reputation = Rating::new(1, rating as f64, min_rate, max_rate, rating); - } else { + if let Some(r) = rep { // Update user reputation // Going on with calculation - reputation = rep.unwrap(); + reputation = r; let old_rating = reputation.total_rating; let last_rating = reputation.last_rating; let new_rating = @@ -142,6 +140,8 @@ 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); } if buyer_rating || seller_rating { diff --git a/src/lightning/mod.rs b/src/lightning/mod.rs index b0a2d370..5f8743f5 100644 --- a/src/lightning/mod.rs +++ b/src/lightning/mod.rs @@ -3,6 +3,7 @@ use std::cmp::Ordering; use crate::cli::settings::Settings; use crate::lightning::invoice::decode_invoice; +use crate::util::bytes_to_string; use anyhow::Result; use easy_hasher::easy_hasher::*; @@ -158,11 +159,7 @@ impl LndConnector { let invoice = decode_invoice(payment_request).unwrap(); let payment_hash = invoice.payment_hash(); let payment_hash = payment_hash.to_vec(); - let hash: String = payment_hash - .clone() - .iter() - .map(|b| format!("{:02x}", b)) - .collect(); + let hash = bytes_to_string(&payment_hash); let mostro_settings = Settings::get_mostro(); // We need to set a max fee amount diff --git a/src/util.rs b/src/util.rs index 2456e76d..0d366074 100644 --- a/src/util.rs +++ b/src/util.rs @@ -15,6 +15,7 @@ use nostr_sdk::prelude::*; use sqlx::types::chrono::Utc; use sqlx::SqlitePool; use sqlx::{Pool, Sqlite}; +use std::fmt::Write; use std::str::FromStr; use std::sync::Arc; use std::thread; @@ -295,9 +296,8 @@ pub async fn show_hold_invoice( if let Some(invoice) = payment_request { db::edit_buyer_invoice_order(pool, order.id, &invoice).await?; }; - let preimage: String = preimage.iter().map(|b| format!("{:02x}", b)).collect(); - let hash_str: String = hash.iter().map(|b| format!("{:02x}", b)).collect(); - + let preimage = bytes_to_string(&preimage); + let hash_str = bytes_to_string(&hash); db::edit_order( pool, &Status::WaitingPayment, @@ -345,7 +345,7 @@ pub async fn show_hold_invoice( async move { // Receiving msgs from the invoice subscription. while let Some(msg) = rx.recv().await { - let hash: String = msg.hash.iter().map(|b| format!("{:02x}", b)).collect(); + let hash = bytes_to_string(msg.hash.as_ref()); // If this invoice was paid by the seller if msg.state == InvoiceState::Accepted { flow::hold_invoice_paid(&hash).await; @@ -488,3 +488,10 @@ pub async fn settle_seller_hold_invoice( Ok(()) } + +pub fn bytes_to_string(bytes: &[u8]) -> String { + bytes.iter().fold(String::new(), |mut output, b| { + let _ = write!(output, "{:02x}", b); + output + }) +}