Skip to content

Commit

Permalink
Fixes on rating
Browse files Browse the repository at this point in the history
Upgrade mostro core version
Add new nostr_tags_to_tuple() function to handle nostr tags
  • Loading branch information
grunch committed Nov 1, 2023
1 parent d4fa573 commit 796f701
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
18 changes: 9 additions & 9 deletions src/app/rate_user.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,14 +18,12 @@ pub async fn get_counterpart_reputation(
client: &Client,
) -> Result<Option<Rating>> {
// 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?;
Expand All @@ -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))
}
Expand Down Expand Up @@ -143,14 +142,15 @@ 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
update_user_rating_event(
&counterpart,
update_buyer_rate,
update_seller_rate,
reputation.as_json().unwrap(),
reputation,
order.id,
my_keys,
pool,
Expand Down
14 changes: 12 additions & 2 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Mutex<Vec<Event>>>,
) -> 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 {
Expand Down Expand Up @@ -495,3 +495,13 @@ pub fn bytes_to_string(bytes: &[u8]) -> String {
output
})
}

pub fn nostr_tags_to_tuple(tags: Vec<Tag>) -> 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
}

0 comments on commit 796f701

Please sign in to comment.