Skip to content

Commit

Permalink
Fetch pagerank repo
Browse files Browse the repository at this point in the history
  • Loading branch information
dcadenas committed Sep 18, 2024
1 parent 79b2058 commit 1336e16
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/http_server/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ where
"/recommendations/:pubkey",
get(get_recommendations_handler::<T>),
) // Make handler generic
.route("/maybe_spammer/:pubkey", get(maybe_spammer::<T>)) // Make handler generic
.layer(tracing_layer)
.layer(TimeoutLayer::new(Duration::from_secs(1)))
.layer(TimeoutLayer::new(Duration::from_secs(5)))
.with_state(state)) // Attach state to the router
}

Expand Down Expand Up @@ -67,6 +68,19 @@ where
Ok(Json(recommendations))
}

async fn maybe_spammer<T>(
State(state): State<Arc<AppState<T>>>, // Extract shared state with generic RepoTrait
axum::extract::Path(pubkey): axum::extract::Path<String>, // Extract pubkey from the path
) -> Result<Json<bool>, ApiError>
where
T: RepoTrait,
{
let public_key = PublicKey::from_hex(&pubkey).map_err(|_| ApiError::InvalidPublicKey)?;
let pagerank = state.repo.get_pagerank(&public_key).await?;

Ok(Json(pagerank < 0.2))
}

async fn serve_root_page(_headers: HeaderMap) -> impl IntoResponse {
let body = r#"
<html>
Expand Down
38 changes: 38 additions & 0 deletions src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ pub trait RepoTrait: Sync + Send {
{
async { panic!("Not implemented") }
}

fn get_pagerank(
&self,
_public_key: &PublicKey,
) -> impl std::future::Future<Output = Result<f64, RepoError>> + std::marker::Send {
async { panic!("Not implemented") }
}
}

impl RepoTrait for Repo {
Expand Down Expand Up @@ -511,6 +518,34 @@ impl RepoTrait for Repo {

Ok(recommendations)
}

async fn get_pagerank(&self, public_key: &PublicKey) -> Result<f64, RepoError> {
let statement = r#"
MATCH (user:User {pubkey: $pubkey_val})
RETURN user.pagerank AS pagerank
"#;

let query = query(statement).param("pubkey_val", public_key.to_hex());

let mut records = self
.graph
.execute(query)
.await
.map_err(RepoError::GetPageRank)?;

match records.next().await {
Ok(Some(row)) => {
let pagerank = row.get::<f64>("pagerank").map_err(|e| {
RepoError::deserialization_with_context(e, "deserializing 'pagerank' field")
})?;
Ok(pagerank)
}
Ok(None) => Err(RepoError::General(neo4rs::Error::DeserializationError(
neo4rs::DeError::PropertyMissingButRequired,
))),
Err(e) => Err(RepoError::General(e)),
}
}
}

/// A function to read as DateTime<Utc> a value stored either as LocalDatetime or DateTime<Utc>
Expand Down Expand Up @@ -577,6 +612,9 @@ pub enum RepoError {

#[error("Failed to get recommendations pubkey: {0}")]
GetRecommendationsPubkey(nostr_sdk::key::Error),

#[error("Failed to get pagerank: {0}")]
GetPageRank(neo4rs::Error),
}

impl RepoError {
Expand Down

0 comments on commit 1336e16

Please sign in to comment.