use anyhow::Error; use giterated_models::{ messages::user::{ UserBioRequest, UserDisplayImageRequest, UserDisplayNameRequest, UserRepositoriesRequest, UserRepositoriesResponse, UserSettingsRequest, UserSettingsResponse, UserWriteSettingsRequest, UserWriteSettingsResponse, }, model::user::User, }; use crate::message::{AuthenticatedUser, Message, MessageHandler, NetworkMessage, State}; use super::wrapper::ConnectionState; pub async fn user_handle( message_type: &str, message: &NetworkMessage, state: &ConnectionState, ) -> Result { match message_type { "&giterated_models::messages::user::UserDisplayNameRequest" => { display_name.handle_message(&message, state).await?; Ok(true) } "&giterated_models::messages::user::UserDisplayImageRequest" => { display_image.handle_message(&message, state).await?; Ok(true) } "&giterated_models::messages::user::UserBioRequest" => { bio.handle_message(&message, state).await?; Ok(true) } "&giterated_models::messages::user::UserRepositoriesRequest" => { repositories.handle_message(&message, state).await?; Ok(true) } "&giterated_models::messages::user::UserSettingsRequest" => { user_settings.handle_message(&message, state).await?; Ok(true) } "&giterated_models::messages::user::UserWriteSettingsRequest" => { write_user_settings.handle_message(&message, state).await?; Ok(true) } _ => Ok(false), } } async fn display_name( Message(request): Message, State(connection_state): State, ) -> Result<(), UserError> { let mut user_backend = connection_state.user_backend.lock().await; let response = user_backend.display_name(request.clone()).await?; drop(user_backend); connection_state.send(response).await?; Ok(()) } async fn display_image( Message(request): Message, State(connection_state): State, ) -> Result<(), UserError> { let mut user_backend = connection_state.user_backend.lock().await; let response = user_backend.display_image(request.clone()).await?; drop(user_backend); connection_state.send(response).await?; Ok(()) } async fn bio( Message(request): Message, State(connection_state): State, ) -> Result<(), UserError> { let mut user_backend = connection_state.user_backend.lock().await; let response = user_backend.bio(request.clone()).await?; drop(user_backend); connection_state.send(response).await?; Ok(()) } async fn repositories( Message(request): Message, State(connection_state): State, requesting_user: Option, ) -> Result<(), UserError> { let requesting_user = requesting_user.map(|u| u.0); let mut repository_backend = connection_state.repository_backend.lock().await; let repositories = repository_backend .repositories_for_user(requesting_user.as_ref(), &request.user) .await; let repositories = match repositories { Ok(repositories) => repositories, Err(err) => { error!("Error handling request: {:?}", err); return Ok(()); } }; drop(repository_backend); let mut user_backend = connection_state.user_backend.lock().await; let user_exists = user_backend.exists(&request.user).await; if repositories.is_empty() && !matches!(user_exists, Ok(true)) { return Err(UserError::InvalidUser(request.user)); } let response: UserRepositoriesResponse = UserRepositoriesResponse { repositories }; connection_state.send(response).await?; Ok(()) } async fn user_settings( Message(request): Message, State(connection_state): State, AuthenticatedUser(requesting_user): AuthenticatedUser, ) -> Result<(), UserError> { if request.user != requesting_user { return Err(UserError::InvalidUser(request.user)); } let mut settings_backend = connection_state.settings_backend.lock().await; let mut settings = settings_backend.user_get(&requesting_user).await?; drop(settings_backend); let response = UserSettingsResponse { settings: settings.drain(..).collect(), }; connection_state.send(response).await?; Ok(()) } async fn write_user_settings( Message(request): Message, State(connection_state): State, AuthenticatedUser(requesting_user): AuthenticatedUser, ) -> Result<(), UserError> { if request.user != requesting_user { return Err(UserError::InvalidUser(request.user)); } let mut settings_backend = connection_state.settings_backend.lock().await; settings_backend .user_write(&request.user, &request.settings) .await?; drop(settings_backend); let response = UserWriteSettingsResponse {}; connection_state.send(response).await?; Ok(()) } #[derive(Debug, thiserror::Error)] pub enum UserError { #[error("invalid user {0}")] InvalidUser(User), #[error("{0}")] Other(#[from] Error), }