From b8cdc605a284b7f93a5dd82ea3621b9873520f7c Mon Sep 17 00:00:00 2001 From: spikecodes <19519553+spikecodes@users.noreply.github.com> Date: Fri, 8 Jan 2021 20:55:40 -0800 Subject: [PATCH] Front page config and settings note --- src/settings.rs | 5 +++-- src/subreddit.rs | 11 ++++++++--- src/user.rs | 4 ++-- src/utils.rs | 3 +++ static/style.css | 9 +++++++++ templates/settings.html | 7 +++++++ 6 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/settings.rs b/src/settings.rs index 5846f8f..6678591 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -13,6 +13,7 @@ struct SettingsTemplate { #[derive(serde::Deserialize)] pub struct SettingsForm { + front_page: Option, layout: Option, comment_sort: Option, hide_nsfw: Option, @@ -30,8 +31,8 @@ pub async fn get(req: HttpRequest) -> HttpResponse { pub async fn set(req: HttpRequest, form: Form) -> HttpResponse { let mut res = HttpResponse::Found(); - let names = vec!["layout", "comment_sort", "hide_nsfw"]; - let values = vec![&form.layout, &form.comment_sort, &form.hide_nsfw]; + let names = vec!["front_page", "layout", "comment_sort", "hide_nsfw"]; + let values = vec![&form.front_page, &form.layout, &form.comment_sort, &form.hide_nsfw]; for (i, name) in names.iter().enumerate() { match values[i] { diff --git a/src/subreddit.rs b/src/subreddit.rs index 5150196..3e474a8 100644 --- a/src/subreddit.rs +++ b/src/subreddit.rs @@ -1,5 +1,5 @@ // CRATES -use crate::utils::{error, fetch_posts, format_num, format_url, param, prefs, request, rewrite_url, val, Post, Preferences, Subreddit}; +use crate::utils::{cookie, error, fetch_posts, format_num, format_url, param, prefs, request, rewrite_url, val, Post, Preferences, Subreddit}; use actix_web::{HttpRequest, HttpResponse, Result}; use askama::Template; @@ -25,10 +25,15 @@ struct WikiTemplate { // SERVICES pub async fn page(req: HttpRequest) -> HttpResponse { let path = format!("{}.json?{}", req.path(), req.query_string()); - let sub_name = req.match_info().get("sub").unwrap_or("popular").to_string(); + let default = cookie(&req, "front_page"); + let sub_name = req + .match_info() + .get("sub") + .unwrap_or(if default.is_empty() { "popular" } else { default.as_str() }) + .to_string(); let sort = req.match_info().get("sort").unwrap_or("hot").to_string(); - let sub = if !&sub_name.contains('+') && sub_name != "popular" { + let sub = if !&sub_name.contains('+') && sub_name != "popular" && sub_name != "all" { subreddit(&sub_name).await.unwrap_or_default() } else { Subreddit::default() diff --git a/src/user.rs b/src/user.rs index 1ecd13a..cfac229 100644 --- a/src/user.rs +++ b/src/user.rs @@ -31,7 +31,7 @@ pub async fn profile(req: HttpRequest) -> HttpResponse { match posts { Ok((posts, after)) => { let s = UserTemplate { - user: user.unwrap(), + user: user.unwrap_or_default(), posts, sort: (sort, param(&path, "t")), ends: (param(&path, "after"), after), @@ -42,7 +42,7 @@ pub async fn profile(req: HttpRequest) -> HttpResponse { HttpResponse::Ok().content_type("text/html").body(s) } // If there is an error show error page - Err(msg) => error(msg.to_string()).await, + Err(msg) => {dbg!(msg);error(msg.to_string()).await}, } } diff --git a/src/utils.rs b/src/utils.rs index 0b0ff26..0279852 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -51,6 +51,7 @@ pub struct Comment { pub replies: Vec, } +#[derive(Default)] // User struct containing metadata about user pub struct User { pub name: String, @@ -93,6 +94,7 @@ pub struct ErrorTemplate { } pub struct Preferences { + pub front_page: String, pub layout: String, pub hide_nsfw: String, pub comment_sort: String, @@ -105,6 +107,7 @@ pub struct Preferences { // Build preferences from cookies pub fn prefs(req: HttpRequest) -> Preferences { Preferences { + front_page: cookie(&req, "front_page"), layout: cookie(&req, "layout"), hide_nsfw: cookie(&req, "hide_nsfw"), comment_sort: cookie(&req, "comment_sort"), diff --git a/static/style.css b/static/style.css index 4fd20d4..5da54a6 100644 --- a/static/style.css +++ b/static/style.css @@ -70,6 +70,7 @@ main { footer { display: flex; justify-content: center; + margin-top: 20px; } footer > a { @@ -631,6 +632,13 @@ input[type="submit"]:hover { color: var(--accent); } align-items: center; } +#settings_note { + font-size: 14px; + max-width: 300px; + margin-top: 10px; + opacity: 0.75; +} + #prefs { display: flex; flex-direction: column; @@ -645,6 +653,7 @@ input[type="submit"]:hover { color: var(--accent); } display: flex; justify-content: space-between; width: 100%; + height: 35px; align-items: center; } diff --git a/templates/settings.html b/templates/settings.html index ba59e45..827dea1 100644 --- a/templates/settings.html +++ b/templates/settings.html @@ -11,6 +11,12 @@
+
+ + +
+

Note: settings are saved in browser cookies. Clearing your cookie data will reset them.