From ff968d09c00230e483c3b82a9c753ace29d08b5a Mon Sep 17 00:00:00 2001 From: Daniel Cadenas Date: Tue, 24 Sep 2024 11:30:34 -0300 Subject: [PATCH] Clean policy --- src/domain/account_info.rs | 6 +++--- src/domain/follows_differ.rs | 5 ++--- src/http_server/handlers.rs | 28 ++++++++++------------------ src/http_server/router.rs | 3 --- src/http_server/trust_policy.rs | 23 +++-------------------- src/repo.rs | 2 +- 6 files changed, 19 insertions(+), 48 deletions(-) diff --git a/src/domain/account_info.rs b/src/domain/account_info.rs index 9e53b37..94017c7 100644 --- a/src/domain/account_info.rs +++ b/src/domain/account_info.rs @@ -258,7 +258,7 @@ pub async fn refresh_friendly_id( nostr_client: &Arc, public_key: &PublicKey, ) -> FriendlyId { - let mut account_info = AccountInfo::new(public_key.clone()); + let mut account_info = AccountInfo::new(*public_key); account_info.refresh_metadata(nostr_client, true).await; let friendly_id = account_info @@ -308,7 +308,7 @@ async fn get_verified_friendly_id( return metadata.get_friendly_id(public_key); }; - if !nip05_verifier.verify_nip05(public_key, &nip05_value).await { + if !nip05_verifier.verify_nip05(public_key, nip05_value).await { // Verification failed return metadata.get_friendly_id(public_key); } @@ -357,7 +357,7 @@ mod tests { None => &Keys::generate(), }; - let profile_event = EventBuilder::metadata(&metadata).to_event(&keys).unwrap(); + let profile_event = EventBuilder::metadata(&metadata).to_event(keys).unwrap(); let nostr_metadata = NostrMetadata::from(profile_event); let friendly_id = diff --git a/src/domain/follows_differ.rs b/src/domain/follows_differ.rs index c3ccb86..d219c74 100644 --- a/src/domain/follows_differ.rs +++ b/src/domain/follows_differ.rs @@ -59,8 +59,7 @@ where let event_created_at = convert_timestamp(event.created_at.as_u64())?; let maybe_account_info = self.repo.get_account_info(&follower).await?; - let mut account_info = - maybe_account_info.unwrap_or_else(|| AccountInfo::new(follower.clone())); + let mut account_info = maybe_account_info.unwrap_or_else(|| AccountInfo::new(follower)); // Check if the event is older than the latest stored update and skip if so if let Some(last_contact_list_at) = account_info.last_contact_list_at { @@ -176,7 +175,7 @@ where let mut unchanged = 0; let send_notifications = - should_send_notifications(&account_info, follower, event_created_at).await?; + should_send_notifications(account_info, follower, event_created_at).await?; for (followee, diff) in follows_diff { match diff.stored_follow { diff --git a/src/http_server/handlers.rs b/src/http_server/handlers.rs index 91bd785..8f98e83 100644 --- a/src/http_server/handlers.rs +++ b/src/http_server/handlers.rs @@ -4,7 +4,6 @@ use super::AppState; use crate::relay_subscriber::GetEventsOf; use crate::repo::{Recommendation, RepoTrait}; use axum::{extract::State, http::HeaderMap, response::Html, response::IntoResponse, Json}; -use chrono::Utc; use nostr_sdk::PublicKey; use std::sync::Arc; use tracing::info; @@ -70,27 +69,20 @@ async fn check_if_trusted(repo: &Arc, public_key: &PublicKey) -> Result 1); - let following_enough_people = account_info.followee_count.is_some_and(|c| c > 1); - let has_good_enough_followers = account_info.pagerank.is_some_and(|pr| pr > 0.2); - let oldest_event_seen_at = account_info.created_at.unwrap_or(Utc::now()); - let latest_contact_list_at = account_info.last_contact_list_at.unwrap_or(Utc::now()); + let found = maybe_account_info.is_some(); + let has_good_enough_followers = + maybe_account_info.is_some_and(|a| a.pagerank.is_some_and(|pr| pr > 0.2)); + // TODO: This is a placeholder, we need to implement this from a nos user registration endpoint let is_nos_user = false; - Ok(is_trusted( - has_good_enough_followers, - followed_by_enough_people, - following_enough_people, - oldest_event_seen_at, - latest_contact_list_at, - is_nos_user, - )) + Ok(is_trusted(found, has_good_enough_followers, is_nos_user)) } pub async fn serve_root_page(_headers: HeaderMap) -> impl IntoResponse { diff --git a/src/http_server/router.rs b/src/http_server/router.rs index 251e58a..b6cc4a1 100644 --- a/src/http_server/router.rs +++ b/src/http_server/router.rs @@ -104,8 +104,6 @@ async fn add_cache_header( pub enum ApiError { #[error("Invalid public key")] InvalidPublicKey(#[from] nostr_sdk::nostr::key::Error), - #[error("Not found, try later")] - NotFound, #[error(transparent)] RepoError(#[from] RepoError), #[error(transparent)] @@ -118,7 +116,6 @@ impl IntoResponse for ApiError { fn into_response(self) -> axum::response::Response { let (status, body) = match &self { ApiError::InvalidPublicKey(_) => (StatusCode::BAD_REQUEST, self.to_string()), - ApiError::NotFound => (StatusCode::NOT_FOUND, self.to_string()), ApiError::RepoError(_) => (StatusCode::INTERNAL_SERVER_ERROR, self.to_string()), ApiError::AxumError(_) => (StatusCode::INTERNAL_SERVER_ERROR, "Axum error".to_string()), ApiError::InternalServerError(_) => { diff --git a/src/http_server/trust_policy.rs b/src/http_server/trust_policy.rs index 7fcfca7..6023fac 100644 --- a/src/http_server/trust_policy.rs +++ b/src/http_server/trust_policy.rs @@ -1,24 +1,7 @@ -use chrono::{DateTime, Utc}; - -pub fn is_trusted( - has_quality_followers: bool, - _followed_by_enough_people: bool, - _following_enough_people: bool, - _oldest_event_seen_at: DateTime, - _latest_contact_list_at: DateTime, - _is_nos_user: bool, -) -> bool { - if _is_nos_user { +pub fn is_trusted(found: bool, has_quality_followers: bool, _is_nos_user: bool) -> bool { + if found && (_is_nos_user || has_quality_followers) { return true; } - if has_quality_followers { - return true; - } - - // if followed_by_enough_people && following_enough_people { - // return false; - // } - - return false; + false } diff --git a/src/repo.rs b/src/repo.rs index 645185a..5553a1d 100644 --- a/src/repo.rs +++ b/src/repo.rs @@ -558,7 +558,7 @@ impl RepoTrait for Repo { })? .map(FriendlyId::DB); - let account_info = AccountInfo::new(public_key.clone()) + let account_info = AccountInfo::new(*public_key) .with_pagerank(pagerank) .with_followee_count(followee_count) .with_follower_count(follower_count)