Skip to content

Commit

Permalink
added maker rating on new order creation (#291)
Browse files Browse the repository at this point in the history
  • Loading branch information
arkanoider authored Jun 10, 2024
1 parent 832406c commit 4c4e2a6
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/app/rate_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ 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>> {
pub async fn get_user_reputation(user: &str, my_keys: &Keys) -> Result<Option<Rating>> {
// Request NIP33 of the counterparts
let filters = Filter::new()
.author(my_keys.public_key())
Expand Down Expand Up @@ -124,7 +124,7 @@ pub async fn update_user_reputation_action(
}

// Ask counterpart reputation
let rep = get_counterpart_reputation(&counterpart, my_keys).await?;
let rep = get_user_reputation(&counterpart, my_keys).await?;
// Here we have to update values of the review of the counterpart
let mut reputation;

Expand Down
4 changes: 2 additions & 2 deletions src/app/release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ async fn payment_success(
// CRUD order creation
new_order.clone().create(&pool).await?;
// We transform the order fields to tags to use in the event
let tags = crate::nip33::order_to_tags(&new_order);
let tags = crate::nip33::order_to_tags(&new_order, None);

info!("range order tags to be republished: {:#?}", tags);
// nip33 kind with order fields as tags and order id as identifier
Expand All @@ -278,7 +278,7 @@ async fn payment_success(
// CRUD order creation
new_order.clone().create(&pool).await?;
// We transform the order fields to tags to use in the event
let tags = crate::nip33::order_to_tags(&new_order);
let tags = crate::nip33::order_to_tags(&new_order, None);

info!("range order tags to be republished: {:#?}", tags);
// nip33 kind with order fields as tags and order id as identifier
Expand Down
14 changes: 13 additions & 1 deletion src/nip33.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::Settings;
use chrono::Duration;
use mostro_core::order::{Order, Status};
use mostro_core::rating::Rating;
use mostro_core::NOSTR_REPLACEABLE_EVENT_KIND;
use nostr::event::builder::Error;
use nostr_sdk::prelude::*;
Expand Down Expand Up @@ -49,13 +50,22 @@ fn create_fiat_amt_string(order: &Order) -> String {
order.fiat_amount.to_string()
}
}

fn create_rating_string(rating: Option<Rating>) -> String {
if rating.is_some() {
format!("{:?}", rating.unwrap(),)
} else {
"No user reputation received".to_string()
}
}

/// Transform an order fields to tags
///
/// # Arguments
///
/// * `order` - The order to transform
///
pub fn order_to_tags(order: &Order) -> Vec<(String, String)> {
pub fn order_to_tags(order: &Order, reputation: Option<Rating>) -> Vec<(String, String)> {
let tags = vec![
// kind (k) - The order kind (buy or sell)
("k".to_string(), order.kind.to_string()),
Expand All @@ -71,6 +81,8 @@ pub fn order_to_tags(order: &Order) -> Vec<(String, String)> {
("pm".to_string(), order.payment_method.to_string()),
// premium (premium) - The premium
("premium".to_string(), order.premium.to_string()),
// User rating
("Rating".to_string(), create_rating_string(reputation)),
// Label to identify this is a Mostro's order
("y".to_string(), "mostrop2p".to_string()),
// Table name
Expand Down
8 changes: 6 additions & 2 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::app::rate_user::get_user_reputation;
use crate::cli::settings::Settings;
use crate::db;
use crate::error::MostroError;
Expand Down Expand Up @@ -196,9 +197,12 @@ pub async fn publish_order(
// CRUD order creation
let mut order = new_order_db.clone().create(pool).await?;
let order_id = order.id;
// Get user reputation
let reputation = get_user_reputation(initiator_pubkey, keys).await?;

info!("New order saved Id: {}", order_id);
// We transform the order fields to tags to use in the event
let tags = order_to_tags(&new_order_db);
let tags = order_to_tags(&new_order_db, reputation);

info!("order tags to be published: {:#?}", tags);
// nip33 kind with order fields as tags and order id as identifier
Expand Down Expand Up @@ -292,7 +296,7 @@ pub async fn update_order_event(keys: &Keys, status: Status, order: &Order) -> R
// update order.status with new status
order_updated.status = status.to_string();
// We transform the order fields to tags to use in the event
let tags = order_to_tags(&order_updated);
let tags = order_to_tags(&order_updated, None);
// nip33 kind with order id as identifier and order fields as tags
let event = new_event(keys, "", order.id.to_string(), tags)?;
let order_id = order.id.to_string();
Expand Down

0 comments on commit 4c4e2a6

Please sign in to comment.