aboutsummaryrefslogtreecommitdiff
path: root/server/src/helper/mod.rs
blob: a4e0e1ff1481aef0475d7b489ef0e23c20245461 (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
58
59
60
61
62
63
64
65
66
67
68
/*
    This file is part of jellything (https://codeberg.org/metamuffin/jellything)
    which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
    Copyright (C) 2025 metamuffin <metamuffin.org>
*/
pub mod accept;
pub mod cache;
pub mod cors;
pub mod filter_sort;
pub mod language;
pub mod node_id;
pub mod session;

use crate::ui::error::{MyError, MyResult};
use accept::Accept;
use jellyimport::is_importing;
use jellylogic::session::Session;
use jellyui::{
    locale::Language,
    scaffold::{RenderInfo, SessionInfo},
};
use language::lang_from_request;
use rocket::{
    async_trait,
    http::Status,
    request::{FromRequest, Outcome},
    Request,
};
use session::session_from_request;

#[derive(Debug, Clone, Copy, Default)]
pub struct A<T>(pub T);

pub struct RequestInfo {
    pub lang: Language,
    pub accept: Accept,
    pub session: Session,
}

impl RequestInfo {
    pub async fn from_request_ut(request: &Request<'_>) -> MyResult<Self> {
        Ok(Self {
            lang: lang_from_request(request),
            accept: Accept::from_request_ut(request),
            session: session_from_request(request).await?,
        })
    }
    pub fn render_info(&self) -> RenderInfo {
        RenderInfo {
            importing: is_importing(),
            session: Some(SessionInfo {
                user: self.session.user.clone(), // TODO no clone?
            }),
            lang: self.lang,
        }
    }
}

#[async_trait]
impl<'r> FromRequest<'r> for RequestInfo {
    type Error = MyError;
    async fn from_request(request: &'r Request<'_>) -> Outcome<Self, Self::Error> {
        match Self::from_request_ut(request).await {
            Ok(a) => Outcome::Success(a),
            Err(a) => Outcome::Error((Status::BadRequest, a)),
        }
    }
}