Skip to content

Commit

Permalink
Refactor user homeserver and add validator
Browse files Browse the repository at this point in the history
  • Loading branch information
SHAcollision committed Aug 26, 2024
1 parent 7e257ba commit 5976ef8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 18 deletions.
25 changes: 17 additions & 8 deletions src/events/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::models::{
homeserver::HomeserverUser,
traits::Collection,
user::{UserCounts, UserDetails},
};
Expand Down Expand Up @@ -88,7 +89,8 @@ impl Event {
if let Some(start) = self.uri.path.find(pattern) {
let start_idx = start + pattern.len();
if let Some(end_idx) = self.uri.path[start_idx..].find(pub_segment) {
return Some(self.uri.path[start_idx..start_idx + end_idx].to_string());
let user_id = self.uri.path[start_idx..start_idx + end_idx].to_string();
return Some(user_id);
}
}

Expand Down Expand Up @@ -122,16 +124,23 @@ impl Event {
// Process profile.json and update the databases
debug!("Processing User resource at {}", self.uri.path);

// Index new user event into the DBs
// Serialize and validate
let user = HomeserverUser::try_from(&content).await?;

// Create UserDetails object
let user_details = match self.get_user_id() {
None => return Ok(()),
Some(user_id) => UserDetails::from_homeserver(&user_id, &content).await?,
Some(user_id) => UserDetails::from_homeserver(&user_id, user).await?,
None => {
error!("Error getting user_id from event uri. Skipping event.");
return Ok(());
}
};

if let Some(user_details) = user_details {
user_details.save().await?;
UserDetails::add_to_sorted_sets(&[Some(user_details)]).await;
}
// Index new user event into the Graph and Index
user_details.save().await?;

// Add to other sorted sets and indexes
UserDetails::add_to_sorted_sets(&[Some(user_details)]).await;
}
ResourceType::Post => {
// Process Post resource and update the databases
Expand Down
14 changes: 14 additions & 0 deletions src/models/homeserver/user.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use axum::body::Bytes;
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;

Expand All @@ -17,3 +18,16 @@ pub struct UserLink {
pub title: String,
pub url: String,
}

impl HomeserverUser {
pub async fn try_from(blob: &Bytes) -> Result<Self, Box<dyn std::error::Error + Send + Sync>> {
let user: Self = serde_json::from_slice(blob)?;
user.validate().await?;
Ok(user)
}

//TODO: implement full validation rules. Min/Max length of links, of bio, of username, etc.
pub async fn validate(&self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
Ok(())
}
}
21 changes: 11 additions & 10 deletions src/models/user/details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{queries, RedisOps};
use axum::async_trait;
use chrono::Utc;
use neo4rs::Query;
use pkarr::PublicKey;
use serde::{Deserialize, Deserializer, Serialize};
use serde_json;
use utoipa::ToSchema;
Expand Down Expand Up @@ -66,18 +67,18 @@ impl UserDetails {

pub async fn from_homeserver(
user_id: &str,
content: &[u8],
) -> Result<Option<Self>, Box<dyn std::error::Error + Send + Sync>> {
let user: HomeserverUser = serde_json::from_slice(content)?;

Ok(Some(UserDetails {
name: user.name,
bio: user.bio,
status: user.status,
links: user.links,
homeserver_user: HomeserverUser,
) -> Result<Self, Box<dyn std::error::Error + Send + Sync>> {
// Validate user_id is a valid pkarr public key
PublicKey::try_from(user_id)?;
Ok(UserDetails {
name: homeserver_user.name,
bio: homeserver_user.bio,
status: homeserver_user.status,
links: homeserver_user.links,
id: user_id.to_string(),
indexed_at: Utc::now().timestamp_millis(),
}))
})
}

pub async fn save(&self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
Expand Down

0 comments on commit 5976ef8

Please sign in to comment.