From 846377b586b4e843d9bb9e0d7cfe7059df7255a4 Mon Sep 17 00:00:00 2001 From: ayaka Date: Mon, 17 Jun 2024 22:29:52 +1200 Subject: [PATCH] implement mascots --- src/main.rs | 19 ++++++++++++++++++- src/settings.rs | 3 ++- src/utils.rs | 16 ++++++++++++++++ static/mascots/redsunlib.png | Bin 0 -> 4866 bytes static/style.css | 14 ++++++++++++++ templates/base.html | 7 +++++++ templates/settings.html | 6 ++++++ 7 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 static/mascots/redsunlib.png diff --git a/src/main.rs b/src/main.rs index e228dcd..fe034af 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,7 +26,7 @@ use client::{canonical_path, proxy}; use log::info; use once_cell::sync::Lazy; use server::RequestExt; -use utils::{error, redirect, ThemeAssets}; +use utils::{error, redirect, ThemeAssets, MascotAssets}; use crate::client::OAUTH_CLIENT; @@ -111,6 +111,20 @@ async fn style() -> Result, String> { ) } +/// Serve mascot +async fn mascot_image(req: Request) -> Result, String> { + let res = MascotAssets::get(&req.param("name").unwrap()) + .unwrap_or(MascotAssets::get("redsunlib.png").unwrap()); + Ok( + Response::builder() + .status(200) + .header("content-type", "image/png") + .header("Cache-Control", "public, max-age=1209600, s-maxage=86400") + .body(res.data.into()) + .unwrap_or_default(), + ) +} + #[tokio::main] async fn main() { // Load environment variables @@ -265,6 +279,9 @@ async fn main() { app.at("/settings/restore").get(|r| settings::restore(r).boxed()); app.at("/settings/update").get(|r| settings::update(r).boxed()); + // Mascots + app.at("/mascot/:name").get(|r| mascot_image(r).boxed()); + // Subreddit services app .at("/r/:sub") diff --git a/src/settings.rs b/src/settings.rs index 6964675..8c51a41 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -19,8 +19,9 @@ struct SettingsTemplate { // CONSTANTS -const PREFS: [&str; 16] = [ +const PREFS: [&str; 17] = [ "theme", + "mascot", "front_page", "layout", "wide", diff --git a/src/utils.rs b/src/utils.rs index dbaf1de..62f8f45 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -570,7 +570,9 @@ pub struct Params { #[derive(Default)] pub struct Preferences { pub available_themes: Vec, + pub available_mascots: Vec, pub theme: String, + pub mascot: String, pub front_page: String, pub layout: String, pub wide: String, @@ -595,6 +597,11 @@ pub struct Preferences { #[include = "*.css"] pub struct ThemeAssets; +#[derive(RustEmbed)] +#[folder = "static/mascots/"] +#[include = "*.png"] +pub struct MascotAssets; + impl Preferences { // Build preferences from cookies pub fn new(req: &Request) -> Self { @@ -605,9 +612,18 @@ impl Preferences { let chunks: Vec<&str> = file.as_ref().split(".css").collect(); themes.push(chunks[0].to_owned()); } + // Read available mascot names from embedded png files. + // Always make default "none" option available. + let mut mascots = vec!["none".to_string()]; + for file in MascotAssets::iter() { + let chunks: Vec<&str> = file.as_ref().split(".png").collect(); + mascots.push(chunks[0].to_owned()); + } Self { available_themes: themes, + available_mascots: mascots, theme: setting(req, "theme"), + mascot: setting(req, "mascot"), front_page: setting(req, "front_page"), layout: setting(req, "layout"), wide: setting(req, "wide"), diff --git a/static/mascots/redsunlib.png b/static/mascots/redsunlib.png new file mode 100644 index 0000000000000000000000000000000000000000..e3bf580eaff45ea72d8c107df4591015ab36e984 GIT binary patch literal 4866 zcmd5<30PBC7QP{YBrGF=uqhf=iNK%&$|4xH2(hvxfVhB+$SOr-kwwHPieRusw3P}d z5>Qbqj9L)cB7%Z;K%ufLtFkKl7D)2u1z`r9qV0TbKW}pK&bjC2-t*sc{&(LyZD(UC zCcIo203b%OvY-JV0ILE3gM=@o*HkLt{?#5kD4rCGI>5kv0Q?OWEZ_kYK)@Pb0OEtr zHxLVS0R$58=Q#!o64v=+h4`{SmzvZ0;}8NL<~arH0&{w*C~UwDGy8!Wpf(`Bbqi$+ zybynJ_y=kR<^UrkgccISpwVb779%W*7ZVi`5tWmWT!vq{oUm%;a(Q_L6)iOdB~4{{ zd7|DL&2{T_b#)192FCi@Mp`<$+WbinSS(glL{wHxOjcV_UQzpZH|Qb2VfZm25lTP+ zh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} \ No newline at end of file diff --git a/templates/base.html b/templates/base.html index 84dc9c8..b2b2945 100644 --- a/templates/base.html +++ b/templates/base.html @@ -59,6 +59,13 @@ + + {% if prefs.mascot != "none" && prefs.mascot != "" %} + +
+ +
+ {% endif %} {% block body %} diff --git a/templates/settings.html b/templates/settings.html index 496b9b1..aaf8152 100644 --- a/templates/settings.html +++ b/templates/settings.html @@ -19,6 +19,12 @@ {% call utils::options(prefs.theme, prefs.available_themes, "system") %} +
+ + +
Interface