/* 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 */ use crate::{ routes::ui::{ account::{ 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, }, admin::rocket_uri_macro_r_admin_dashboard, browser::rocket_uri_macro_r_all_items, node::rocket_uri_macro_r_library_node, }, uri, }; use futures::executor::block_on; use jellybase::CONF; use jellycommon::user::Theme; 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: &'a str, session: Option) { @markup::doctype() html { head { title { @title " - " @CONF.brand } link[rel="stylesheet", href="/assets/style.css"]; script[src="/assets/bundle.js"] {} } body[class=class] { nav { h1 { a[href="/"] { @CONF.brand } } " " @if let Some(_) = session { a.library[href=uri!(r_library_node("library"))] { "My Library" } " " a.library[href=uri!(r_all_items())] { "All Items" } " " } div.account { @if let Some(session) = session { span { "Logged in as " } span.username { @session.user.display_name } " " @if session.user.admin { a.admin.hybrid_button[href=uri!(r_admin_dashboard())] { p {"Administration"} } " " } a.settings.hybrid_button[href=uri!(r_account_settings())] { p {"Settings"} } " " a.logout.hybrid_button[href=uri!(r_account_logout())] { p {"Log out"} } } else { a.register.hybrid_button[href=uri!(r_account_register())] { p {"Register"} } " " a.login.hybrid_button[href=uri!(r_account_login())] { p {"Log in"} } } } } #main { @main } footer { p { @CONF.brand " - " @CONF.slogan " | powered by Jellything" } } } } } FlashDisplay(flash: Option>) { @if let Some(flash) = &flash { @match flash { Ok(mesg) => { section.message { p.success { @mesg } } } Err(err) => { section.message { p.error { @format!("{err}") } } } } } } } pub type DynLayoutPage<'a> = LayoutPage>; pub struct LayoutPage { pub title: String, pub class: Option<&'static str>, pub content: T, } impl Default for LayoutPage> { fn default() -> Self { Self { class: None, content: markup::new!(), title: String::new(), } } } impl<'r, Main: Render> Responder<'r, 'static> for LayoutPage
{ 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::>()).unwrap(); let mut out = String::new(); Layout { main: self.content, title: self.title, class: &format!( "{} theme-{:?}", self.class.as_deref().unwrap_or(""), session .as_ref() .map(|s| s.user.theme) .unwrap_or(Theme::Dark) ), session, } .render(&mut out) .unwrap(); Response::build() .header(ContentType::HTML) .streamed_body(Cursor::new(out)) .ok() } }