From 796f701adb0b122f43d16418cb132dcd4c6168de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francisco=20Calder=C3=B3n?= Date: Wed, 1 Nov 2023 11:05:33 -0300 Subject: [PATCH] Fixes on rating Upgrade mostro core version Add new nostr_tags_to_tuple() function to handle nostr tags --- Cargo.toml | 2 +- src/app/rate_user.rs | 18 +++++++++--------- src/util.rs | 14 ++++++++++++-- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1d4f818..2a43447 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,7 +37,7 @@ uuid = { version = "1.3.0", features = [ "serde", ] } reqwest = { version = "0.11", features = ["json"] } -mostro-core = "0.3.9" +mostro-core = "0.3.12" tracing = "0.1.37" tracing-subscriber = { version = "0.3.16", features = ["env-filter"] } config = "0.13.3" diff --git a/src/app/rate_user.rs b/src/app/rate_user.rs index 1e2901b..2bd245f 100644 --- a/src/app/rate_user.rs +++ b/src/app/rate_user.rs @@ -1,9 +1,10 @@ -use crate::util::{send_dm, update_user_rating_event}; +use crate::util::{nostr_tags_to_tuple, send_dm, update_user_rating_event}; use anyhow::Result; use log::error; use mostro_core::order::Order; -use mostro_core::{Action, Content, Message, Rating, NOSTR_REPLACEABLE_EVENT_KIND}; +use mostro_core::rating::Rating; +use mostro_core::{Action, Content, Message, NOSTR_REPLACEABLE_EVENT_KIND}; use nostr_sdk::prelude::*; use sqlx::{Pool, Sqlite}; use sqlx_crud::Crud; @@ -17,14 +18,12 @@ pub async fn get_counterpart_reputation( client: &Client, ) -> Result> { // Request NIP33 of the counterparts - + // TODO: filter by data_label=rating generic_tag let filters = Filter::new() .author(my_keys.public_key().to_string()) .kind(Kind::Custom(NOSTR_REPLACEABLE_EVENT_KIND)) .identifier(user.to_string()); - println!("Filter : {:?}", filters); - let mut user_reputation_event = client .get_events_of(vec![filters], Some(Duration::from_secs(10))) .await?; @@ -34,10 +33,10 @@ pub async fn get_counterpart_reputation( return Ok(None); }; - // Sore events by time + // Sort events by time user_reputation_event.sort_by(|a, b| a.created_at.cmp(&b.created_at)); - - let reputation = Rating::from_json(&user_reputation_event[0].content)?; + let tags = nostr_tags_to_tuple(user_reputation_event[0].tags.clone()); + let reputation = Rating::from_tags(tags)?; Ok(Some(reputation)) } @@ -143,6 +142,7 @@ pub async fn update_user_reputation_action( } else { reputation = Rating::new(1, rating as f64, min_rate, max_rate, rating); } + let reputation = reputation.to_tags()?; if buyer_rating || seller_rating { // Update db with rate flags @@ -150,7 +150,7 @@ pub async fn update_user_reputation_action( &counterpart, update_buyer_rate, update_seller_rate, - reputation.as_json().unwrap(), + reputation, order.id, my_keys, pool, diff --git a/src/util.rs b/src/util.rs index 0d36607..655fd46 100644 --- a/src/util.rs +++ b/src/util.rs @@ -174,14 +174,14 @@ pub async fn update_user_rating_event( user: &String, buyer_sent_rate: bool, seller_sent_rate: bool, - reputation: String, + tags: Vec<(String, String)>, order_id: Uuid, keys: &Keys, pool: &SqlitePool, rate_list: Arc>>, ) -> Result<()> { // nip33 kind with user as identifier - let event = new_event(keys, reputation, user.to_string(), vec![])?; + let event = new_event(keys, "".to_string(), user.to_string(), tags)?; info!("Sending replaceable event: {event:#?}"); // We update the order vote status if buyer_sent_rate { @@ -495,3 +495,13 @@ pub fn bytes_to_string(bytes: &[u8]) -> String { output }) } + +pub fn nostr_tags_to_tuple(tags: Vec) -> Vec<(String, String)> { + let mut tags_tuple = Vec::new(); + for tag in tags { + let t = tag.as_vec(); + tags_tuple.push((t[0].to_string(), t[1].to_string())); + } + + tags_tuple +}