about summary refs log tree commit diff
path: root/kittybox-rs/src/frontend/mod.rs
blob: bc9925f8b01bf5ba94dddb642f052e604ebadcec (plain) (blame)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
use crate::database::{Storage, StorageError};
use axum::{
    extract::{Host, Path, Query},
    http::{StatusCode, Uri},
    response::IntoResponse,
    Extension,
};
use futures_util::FutureExt;
use serde::Deserialize;
use std::convert::TryInto;
use tracing::{debug, error};
//pub mod login;
pub mod onboarding;

use kittybox_templates::{Entry, ErrorPage, Feed, MainPage, Template, VCard, POSTS_PER_PAGE};

pub use kittybox_util::IndiewebEndpoints;

#[derive(Debug, Deserialize)]
pub struct QueryParams {
    after: Option<String>,
}

#[derive(Debug)]
struct FrontendError {
    msg: String,
    source: Option<Box<dyn std::error::Error + Send + Sync + 'static>>,
    code: StatusCode,
}

impl FrontendError {
    pub fn with_code<C>(code: C, msg: &str) -> Self
    where
        C: TryInto<StatusCode>,
    {
        Self {
            msg: msg.to_string(),
            source: None,
            code: code.try_into().unwrap_or(StatusCode::INTERNAL_SERVER_ERROR),
        }
    }
    pub fn msg(&self) -> &str {
        &self.msg
    }
    pub fn code(&self) -> StatusCode {
        self.code
    }
}

impl From<StorageError> for FrontendError {
    fn from(err: StorageError) -> Self {
        Self {
            msg: "Database error".to_string(),
            source: Some(Box::new(err)),
            code: StatusCode::INTERNAL_SERVER_ERROR,
        }
    }
}

impl std::error::Error for FrontendError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        self.source
            .as_ref()
            .map(|e| e.as_ref() as &(dyn std::error::Error + 'static))
    }
}

impl std::fmt::Display for FrontendError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.msg)
    }
}

async fn get_post_from_database<S: Storage>(
    db: &S,
    url: &str,
    after: Option<String>,
    user: &Option<String>,
) -> std::result::Result<serde_json::Value, FrontendError> {
    match db
        .read_feed_with_limit(url, &after, POSTS_PER_PAGE, user)
        .await
    {
        Ok(result) => match result {
            Some(post) => Ok(post),
            None => Err(FrontendError::with_code(
                StatusCode::NOT_FOUND,
                "Post not found in the database",
            )),
        },
        Err(err) => match err.kind() {
            crate::database::ErrorKind::PermissionDenied => {
                // TODO: Authentication
                if user.is_some() {
                    Err(FrontendError::with_code(
                        StatusCode::FORBIDDEN,
                        "User authenticated AND forbidden to access this resource",
                    ))
                } else {
                    Err(FrontendError::with_code(
                        StatusCode::UNAUTHORIZED,
                        "User needs to authenticate themselves",
                    ))
                }
            }
            _ => Err(err.into()),
        },
    }
}

#[tracing::instrument(skip(db))]
pub async fn homepage<D: Storage>(
    Host(host): Host,
    Query(query): Query<QueryParams>,
    Extension(db): Extension<D>,
) -> impl IntoResponse {
    let user = None; // TODO authentication
    let path = format!("https://{}/", host);
    let feed_path = format!("https://{}/feeds/main", host);

    match tokio::try_join!(
        get_post_from_database(&db, &path, None, &user),
        get_post_from_database(&db, &feed_path, query.after, &user)
    ) {
        Ok((hcard, hfeed)) => {
            // Here, we know those operations can't really fail
            // (or it'll be a transient failure that will show up on
            // other requests anyway if it's serious...)
            //
            // btw is it more efficient to fetch these in parallel?
            let (blogname, channels) = tokio::join!(
                db.get_setting(crate::database::Settings::SiteName, &path)
                    .map(|i| i.unwrap_or_else(|_| "Kittybox".to_owned())),
                db.get_channels(&path).map(|i| i.unwrap_or_default())
            );
            // Render the homepage
            (
                StatusCode::OK,
                [(
                    axum::http::header::CONTENT_TYPE,
                    r#"text/html; charset="utf-8""#,
                )],
                Template {
                    title: &blogname,
                    blog_name: &blogname,
                    endpoints: None, // XXX this will be deprecated soon anyway
                    feeds: channels,
                    user,
                    content: MainPage {
                        feed: &hfeed,
                        card: &hcard,
                    }
                    .to_string(),
                }
                .to_string(),
            )
        }
        Err(err) => {
            if err.code == StatusCode::NOT_FOUND {
                debug!("Transferring to onboarding...");
                // Transfer to onboarding
                (
                    StatusCode::FOUND,
                    [(axum::http::header::LOCATION, "/.kittybox/onboarding")],
                    String::default(),
                )
            } else {
                error!("Error while fetching h-card and/or h-feed: {}", err);
                // Return the error
                let (blogname, channels) = tokio::join!(
                    db.get_setting(crate::database::Settings::SiteName, &path)
                        .map(|i| i.unwrap_or_else(|_| "Kittybox".to_owned())),
                    db.get_channels(&path).map(|i| i.unwrap_or_default())
                );

                (
                    err.code(),
                    [(
                        axum::http::header::CONTENT_TYPE,
                        r#"text/html; charset="utf-8""#,
                    )],
                    Template {
                        title: &blogname,
                        blog_name: &blogname,
                        endpoints: None, // XXX this will be deprecated soon anyway
                        feeds: channels,
                        user,
                        content: ErrorPage {
                            code: err.code(),
                            msg: Some(err.msg().to_string()),
                        }
                        .to_string(),
                    }
                    .to_string(),
                )
            }
        }
    }
}

#[tracing::instrument(skip(db))]
pub async fn catchall<D: Storage>(
    Extension(db): Extension<D>,
    Host(host): Host,
    Query(query): Query<QueryParams>,
    uri: Uri,
) -> impl IntoResponse {
    let user = None; // TODO authentication
    let path = url::Url::parse(&format!("https://{}/", host))
        .unwrap()
        .join(uri.path())
        .unwrap();

    match get_post_from_database(&db, path.as_str(), query.after, &user).await {
        Ok(post) => {
            let (blogname, channels) = tokio::join!(
                db.get_setting(crate::database::Settings::SiteName, &host)
                    .map(|i| i.unwrap_or_else(|_| "Kittybox".to_owned())),
                db.get_channels(&host).map(|i| i.unwrap_or_default())
            );
            // Render the homepage
            (
                StatusCode::OK,
                [(
                    axum::http::header::CONTENT_TYPE,
                    r#"text/html; charset="utf-8""#,
                )],
                Template {
                    title: &blogname,
                    blog_name: &blogname,
                    endpoints: None, // XXX this will be deprecated soon anyway
                    feeds: channels,
                    user,
                    content: match post.pointer("/type/0").and_then(|i| i.as_str()) {
                        Some("h-entry") => Entry { post: &post }.to_string(),
                        Some("h-feed") => Feed { feed: &post }.to_string(),
                        Some("h-card") => VCard { card: &post }.to_string(),
                        unknown => {
                            unimplemented!("Template for MF2-JSON type {:?}", unknown)
                        }
                    },
                }
                .to_string(),
            )
        }
        Err(err) => {
            let (blogname, channels) = tokio::join!(
                db.get_setting(crate::database::Settings::SiteName, &host)
                    .map(|i| i.unwrap_or_else(|_| "Kittybox".to_owned())),
                db.get_channels(&host).map(|i| i.unwrap_or_default())
            );
            (
                err.code(),
                [(
                    axum::http::header::CONTENT_TYPE,
                    r#"text/html; charset="utf-8""#,
                )],
                Template {
                    title: &blogname,
                    blog_name: &blogname,
                    endpoints: None,
                    feeds: channels,
                    user,
                    content: ErrorPage {
                        code: err.code(),
                        msg: Some(err.msg().to_owned()),
                    }
                    .to_string(),
                }
                .to_string(),
            )
        }
    }
}

static STYLE_CSS: &[u8] = include_bytes!("./style.css");
static ONBOARDING_JS: &[u8] = include_bytes!("./onboarding.js");
static ONBOARDING_CSS: &[u8] = include_bytes!("./onboarding.css");

static MIME_JS: &str = "application/javascript";
static MIME_CSS: &str = "text/css";
static MIME_PLAIN: &str = "text/plain";

pub async fn statics(Path(name): Path<String>) -> impl IntoResponse {
    use axum::http::header::CONTENT_TYPE;

    match name.as_str() {
        "style.css" => (StatusCode::OK, [(CONTENT_TYPE, MIME_CSS)], STYLE_CSS),
        "onboarding.js" => (StatusCode::OK, [(CONTENT_TYPE, MIME_JS)], ONBOARDING_JS),
        "onboarding.css" => (StatusCode::OK, [(CONTENT_TYPE, MIME_CSS)], ONBOARDING_CSS),
        _ => (
            StatusCode::NOT_FOUND,
            [(CONTENT_TYPE, MIME_PLAIN)],
            "not found".as_bytes(),
        ),
    }
}