diff options
Diffstat (limited to 'src/ui/handlers/conversation.rs')
| -rw-r--r-- | src/ui/handlers/conversation.rs | 57 |
1 files changed, 27 insertions, 30 deletions
diff --git a/src/ui/handlers/conversation.rs b/src/ui/handlers/conversation.rs index 102efc6..9a87d40 100644 --- a/src/ui/handlers/conversation.rs +++ b/src/ui/handlers/conversation.rs @@ -2,13 +2,15 @@ use axum::{ extract::{Path, State}, response::{self, IntoResponse, Redirect}, }; +use axum_extra::TypedHeader; +use headers::IfNoneMatch; use crate::{ conversation::{self, app, app::Conversations}, error::Internal, token::extract::Identity, ui::{ - assets::{Asset, Assets}, + assets::{self, Asset, Assets}, error::NotFound, }, }; @@ -17,44 +19,39 @@ pub async fn handler( State(conversations): State<Conversations>, identity: Option<Identity>, Path(conversation): Path<conversation::Id>, -) -> Result<Asset, Error> { - let _ = identity.ok_or(Error::NotLoggedIn)?; - conversations - .get(&conversation) - .await - .map_err(Error::from)?; + TypedHeader(if_none_match): TypedHeader<IfNoneMatch>, +) -> Result<Response, Internal> { + let response = if identity.is_none() { + Response::NotLoggedIn + } else { + match conversations.get(&conversation).await { + Ok(_) => { + let index = assets::Response::index(&if_none_match)?; + Response::Asset(index) + } + Err(app::GetError::NotFound(_) | app::GetError::Deleted(_)) => { + let index = Assets::index()?; + Response::NotFound(index) + } + Err(err @ app::GetError::Failed(_)) => return Err(err.into()), + } + }; - Assets::index().map_err(Error::Internal) + Ok(response) } -#[derive(Debug, thiserror::Error)] -pub enum Error { - #[error("conversation not found")] - NotFound, - #[error("not logged in")] +pub enum Response { NotLoggedIn, - #[error("{0}")] - Internal(Internal), -} - -impl From<app::GetError> for Error { - fn from(error: app::GetError) -> Self { - match error { - app::GetError::NotFound(_) | app::GetError::Deleted(_) => Self::NotFound, - app::GetError::Failed(_) => Self::Internal(error.into()), - } - } + NotFound(Asset), + Asset(assets::Response), } -impl IntoResponse for Error { +impl IntoResponse for Response { fn into_response(self) -> response::Response { match self { - Self::NotFound => match Assets::index() { - Ok(asset) => NotFound(asset).into_response(), - Err(internal) => internal.into_response(), - }, Self::NotLoggedIn => Redirect::temporary("/login").into_response(), - Self::Internal(error) => error.into_response(), + Self::NotFound(asset) => NotFound(asset).into_response(), + Self::Asset(asset) => asset.into_response(), } } } |
