summaryrefslogtreecommitdiff
path: root/src/ui/handlers/conversation.rs
blob: 9a87d4028de3d6c657a886ac010e21760a660461 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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::{self, Asset, Assets},
        error::NotFound,
    },
};

pub async fn handler(
    State(conversations): State<Conversations>,
    identity: Option<Identity>,
    Path(conversation): Path<conversation::Id>,
    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()),
        }
    };

    Ok(response)
}

pub enum Response {
    NotLoggedIn,
    NotFound(Asset),
    Asset(assets::Response),
}

impl IntoResponse for Response {
    fn into_response(self) -> response::Response {
        match self {
            Self::NotLoggedIn => Redirect::temporary("/login").into_response(),
            Self::NotFound(asset) => NotFound(asset).into_response(),
            Self::Asset(asset) => asset.into_response(),
        }
    }
}