From 8c2b063c923770a1fb614e5815c49dac2a99202e Mon Sep 17 00:00:00 2001 From: arkanoider Date: Mon, 23 Dec 2024 23:28:44 +0100 Subject: [PATCH] Fix: first review special case - fix on user rate update for sqlx issue documented --- sqlx-data.json | 10 ++++++++++ src/app/rate_user.rs | 13 +++++++++---- src/db.rs | 32 +++++++++++++++----------------- 3 files changed, 34 insertions(+), 21 deletions(-) diff --git a/sqlx-data.json b/sqlx-data.json index 02ed93f..48a96de 100644 --- a/sqlx-data.json +++ b/sqlx-data.json @@ -10,6 +10,16 @@ }, "query": "\n UPDATE orders\n SET\n master_seller_pubkey = ?1\n WHERE id = ?2\n " }, + "071917afd7e1d00e2ae860779a1471d281f7aa45da61f7a7bdbd4625a3966a5d": { + "describe": { + "columns": [], + "nullable": [], + "parameters": { + "Right": 6 + } + }, + "query": "\n UPDATE users SET last_rating = ?1, min_rating = ?2, max_rating = ?3, total_reviews = ?4, total_rating = ?5 WHERE pubkey = ?6\n " + }, "77ea98f6af16fa6e5a7d604965593700c563f88d88cb10b348bdc4200c87ad1d": { "describe": { "columns": [], diff --git a/src/app/rate_user.rs b/src/app/rate_user.rs index 38c6589..75f5ff4 100644 --- a/src/app/rate_user.rs +++ b/src/app/rate_user.rs @@ -16,8 +16,8 @@ use std::time::Duration; use tokio::sync::Mutex; use tracing::error; -const MAX_RATING: u8 = 5; -const MIN_RATING: u8 = 1; +pub const MAX_RATING: u8 = 5; +pub const MIN_RATING: u8 = 1; pub async fn get_user_reputation(user: &str, my_keys: &Keys) -> Result> { // Request NIP33 of the counterparts @@ -151,8 +151,13 @@ pub async fn update_user_reputation_action( user_to_vote.total_reviews += 1; let old_rating = user_to_vote.total_rating as f64; // recompute new rating - user_to_vote.total_rating = old_rating - + ((user_to_vote.last_rating as f64) - old_rating) / (user_to_vote.total_reviews as f64); + if user_to_vote.total_reviews <= 1 { + user_to_vote.total_rating = rating.into(); + } else { + user_to_vote.total_rating = old_rating + + ((user_to_vote.last_rating as f64) - old_rating) + / (user_to_vote.total_reviews as f64); + } // Store last rating user_to_vote.last_rating = rating.into(); // Create new rating event diff --git a/src/db.rs b/src/db.rs index 0945d8d..3113289 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,3 +1,4 @@ +use crate::app::rate_user::{MAX_RATING, MIN_RATING}; use mostro_core::dispute::Dispute; use mostro_core::order::Order; use mostro_core::order::Status; @@ -391,7 +392,7 @@ pub async fn update_user_rating( max_rating: i64, total_reviews: i64, total_rating: f64, -) -> anyhow::Result { +) -> anyhow::Result { // Validate public key format (32-bytes hex) if !public_key.chars().all(|c| c.is_ascii_hexdigit()) || public_key.len() != 64 { return Err(anyhow::anyhow!("Invalid public key format")); @@ -403,7 +404,7 @@ pub async fn update_user_rating( if !(0..=5).contains(&min_rating) || !(0..=5).contains(&max_rating) { return Err(anyhow::anyhow!("Invalid min/max rating values")); } - if min_rating > last_rating || last_rating > max_rating { + if MIN_RATING as i64 > last_rating || last_rating > MAX_RATING as i64 { return Err(anyhow::anyhow!( "Rating values must satisfy: min_rating <= last_rating <= max_rating" )); @@ -414,25 +415,22 @@ pub async fn update_user_rating( if total_rating < 0.0 || total_rating > (total_reviews * 5) as f64 { return Err(anyhow::anyhow!("Invalid total rating")); } - if let Ok(user) = sqlx::query_as::<_, User>( + let rows_affected = sqlx::query!( r#" UPDATE users SET last_rating = ?1, min_rating = ?2, max_rating = ?3, total_reviews = ?4, total_rating = ?5 WHERE pubkey = ?6 - RETURNING * "#, + last_rating, + min_rating, + max_rating, + total_reviews, + total_rating, + public_key, ) - .bind(last_rating) - .bind(min_rating) - .bind(max_rating) - .bind(total_reviews) - .bind(total_rating) - .bind(public_key) - .fetch_one(pool) - .await{ - Ok(user) - } - else { - Err(anyhow::anyhow!("No user found")) - } + .execute(pool) + .await? + .rows_affected(); + + Ok(rows_affected > 0) } pub async fn is_assigned_solver(