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
|
/*
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) 2023 metamuffin <metamuffin.org>
*/
use crate::{
routes::ui::account::{
admin::rocket_uri_macro_r_account_admin_dashboard, rocket_uri_macro_r_account_login,
rocket_uri_macro_r_account_logout, rocket_uri_macro_r_account_register, session::Session,
settings::rocket_uri_macro_r_account_settings,
},
uri, CONF,
};
use async_std::task::block_on;
use markup::{DynRender, Render};
use rocket::{
http::ContentType,
response::{self, Responder},
Request, Response,
};
use std::io::Cursor;
markup::define! {
Layout<'a, Main: Render>(title: String, main: Main, class: Option<&'a str>, session: Option<Session>) {
@markup::doctype()
html {
head {
title { @title " - " @CONF.brand }
link[rel="stylesheet", href="/assets/style.css"];
script[src="/assets/bundle.js"] {}
}
body[class=class.unwrap_or("")] {
nav {
h1 { a[href="/"] { @CONF.brand } }
@if let Some(_) = session {
a[href="/library"] { "My Library" }
a[href="/items"] { "All Items" }
}
div.account {
@if let Some(session) = session {
span { "Logged in as " @session.user.display_name }
@if session.user.admin {
a[href=uri!(r_account_admin_dashboard())] { "Administration" }
}
a[href=uri!(r_account_settings())] { "Settings" }
a[href=uri!(r_account_logout())] { "Log out" }
} else {
a[href=uri!(r_account_register())] { "Register" }
a[href=uri!(r_account_login())] { "Log in" }
}
}
}
#main { @main }
footer {
p { @CONF.brand " - " @CONF.slogan " | powered by Jellything" }
}
}
}
}
}
pub type DynLayoutPage<'a> = LayoutPage<markup::DynRender<'a>>;
pub struct LayoutPage<T> {
pub title: String,
pub class: Option<&'static str>,
pub content: T,
}
impl Default for LayoutPage<DynRender<'_>> {
fn default() -> Self {
Self {
class: None,
content: markup::new!(),
title: String::new(),
}
}
}
impl<'r, Main: Render> Responder<'r, 'static> for LayoutPage<Main> {
fn respond_to(self, req: &'r Request<'_>) -> response::Result<'static> {
// TODO blocking the event loop here. it seems like there is no other way to
// TODO offload this, since the guard references `req` which has a lifetime.
// TODO therefore we just block. that is fine since the database is somewhat fast.
let session = block_on(req.guard::<Option<Session>>()).unwrap();
let mut out = String::new();
Layout {
main: self.content,
title: self.title,
class: self.class.as_deref(),
session,
}
.render(&mut out)
.unwrap();
Response::build()
.header(ContentType::HTML)
.streamed_body(Cursor::new(out))
.ok()
}
}
|