use anyhow::Error; use crate::{ messages::user::{ UserBioRequest, UserDisplayImageRequest, UserDisplayNameRequest, UserRepositoriesRequest, UserRepositoriesResponse, }, model::authenticated::{Message, MessageHandler, NetworkMessage, State}, }; use super::{wrapper::ConnectionState, HandlerUnhandled}; pub async fn user_handle(message: &NetworkMessage, state: &ConnectionState) -> Result<(), Error> { if display_name.handle_message(&message, state).await.is_ok() { Ok(()) } else if display_image.handle_message(&message, state).await.is_ok() { Ok(()) } else if bio.handle_message(&message, state).await.is_ok() { Ok(()) } else if repositories.handle_message(&message, state).await.is_ok() { Ok(()) } else { Err(Error::from(HandlerUnhandled)) } } 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, ) -> Result<(), UserError> { let mut repository_backend = connection_state.repository_backend.lock().await; let repositories = repository_backend .repositories_for_user(&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)) { panic!() } let response: UserRepositoriesResponse = UserRepositoriesResponse { repositories }; connection_state.send(response).await?; Ok(()) } #[derive(Debug, thiserror::Error)] pub enum UserError { #[error("{0}")] Other(#[from] Error), }