Skip to content

Commit

Permalink
Clean policy
Browse files Browse the repository at this point in the history
  • Loading branch information
dcadenas committed Sep 24, 2024
1 parent 4b41fcc commit ff968d0
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 48 deletions.
6 changes: 3 additions & 3 deletions src/domain/account_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ pub async fn refresh_friendly_id<T: GetEventsOf>(
nostr_client: &Arc<T>,
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
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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 =
Expand Down
5 changes: 2 additions & 3 deletions src/domain/follows_differ.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
28 changes: 10 additions & 18 deletions src/http_server/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -70,27 +69,20 @@ async fn check_if_trusted<T>(repo: &Arc<T>, public_key: &PublicKey) -> Result<bo
where
T: RepoTrait,
{
let Some(account_info) = repo.get_account_info(public_key).await? else {
return Err(ApiError::NotFound);
};
let maybe_account_info = repo.get_account_info(public_key).await?;

info!("Checking if account is trusted: {}", account_info);
info!(
"Checking if {}is trusted. Account: {:?}",
public_key, maybe_account_info
);

let followed_by_enough_people = account_info.follower_count.is_some_and(|c| c > 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 {
Expand Down
3 changes: 0 additions & 3 deletions src/http_server/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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(_) => {
Expand Down
23 changes: 3 additions & 20 deletions src/http_server/trust_policy.rs
Original file line number Diff line number Diff line change
@@ -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<Utc>,
_latest_contact_list_at: DateTime<Utc>,
_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
}
2 changes: 1 addition & 1 deletion src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit ff968d0

Please sign in to comment.