From 93cfc713c645deba2ff509ee5008ff96f431bc12 Mon Sep 17 00:00:00 2001 From: robrobinbin <8597693+robrobinbin@users.noreply.github.com> Date: Sat, 13 Feb 2021 21:55:23 +0100 Subject: [PATCH] Generate URL to restore settings, including subscriptions. Closes #89 (#116) * Start recursive comments * Update comment.html * Fix move error * Comment improvements * Fix merge * Remove extra endif from post.html * Fix post.html * Restore setting from link * Tweak settings page Co-authored-by: spikecodes <19519553+spikecodes@users.noreply.github.com> --- src/main.rs | 1 + src/settings.rs | 39 +++++++++++++++++++++++++++++++++++++++ src/subreddit.rs | 2 +- src/utils.rs | 4 ++-- static/style.css | 38 +++++++++++++------------------------- templates/settings.html | 32 +++++++++++++++++--------------- templates/subreddit.html | 2 +- templates/utils.html | 4 ++-- 8 files changed, 76 insertions(+), 46 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6a22083..be68803 100644 --- a/src/main.rs +++ b/src/main.rs @@ -166,6 +166,7 @@ async fn main() -> tide::Result<()> { // Configure settings app.at("/settings/").get(settings::get).post(settings::set); + app.at("/settings/restore/").get(settings::restore); // Subreddit services // See posts and info about subreddit diff --git a/src/settings.rs b/src/settings.rs index 0484973..d68e969 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -12,6 +12,7 @@ struct SettingsTemplate { } #[derive(serde::Deserialize, Default)] +#[serde(default)] pub struct SettingsForm { theme: Option, front_page: Option, @@ -19,6 +20,8 @@ pub struct SettingsForm { wide: Option, comment_sort: Option, show_nsfw: Option, + redirect: Option, + subscriptions: Option, } // FUNCTIONS @@ -56,3 +59,39 @@ pub async fn set(mut req: Request<()>) -> tide::Result { Ok(res) } + + +// Set cookies using response "Set-Cookie" header +pub async fn restore(req: Request<()>) -> tide::Result { + let form: SettingsForm = req.query()?; + + + let path = match form.redirect { + Some(value) => format!("/{}/", value), + None => "/".to_string() + }; + + let mut res = Response::builder(302) + .content_type("text/html") + .header("Location", path.to_owned()) + .body(format!("Redirecting to {0}...", path)) + .build(); + + let names = vec!["theme", "front_page", "layout", "wide", "comment_sort", "show_nsfw", "subscriptions"]; + let values = vec![form.theme, form.front_page, form.layout, form.wide, form.comment_sort, form.show_nsfw, form.subscriptions]; + + for (i, name) in names.iter().enumerate() { + match values.get(i) { + Some(value) => res.insert_cookie( + Cookie::build(name.to_owned(), value.to_owned().unwrap_or_default()) + .path("/") + .http_only(true) + .expires(OffsetDateTime::now_utc() + Duration::weeks(52)) + .finish(), + ), + None => res.remove_cookie(Cookie::named(name.to_owned())), + }; + } + + Ok(res) +} diff --git a/src/subreddit.rs b/src/subreddit.rs index ef8ed3f..6a9815f 100644 --- a/src/subreddit.rs +++ b/src/subreddit.rs @@ -87,7 +87,7 @@ pub async fn subscriptions(req: Request<()>) -> tide::Result { let query = req.url().query().unwrap_or_default().to_string(); let action: Vec = req.url().path().split('/').map(String::from).collect(); - let mut sub_list = prefs(req).subs; + let mut sub_list = prefs(req).subscriptions; // Find each subreddit name (separated by '+') in sub parameter for part in sub.split('+') { diff --git a/src/utils.rs b/src/utils.rs index ded70fb..451b0aa 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -146,7 +146,7 @@ pub struct Preferences { pub wide: String, pub show_nsfw: String, pub comment_sort: String, - pub subs: Vec, + pub subscriptions: Vec, } // @@ -162,7 +162,7 @@ pub fn prefs(req: Request<()>) -> Preferences { wide: cookie(&req, "wide"), show_nsfw: cookie(&req, "show_nsfw"), comment_sort: cookie(&req, "comment_sort"), - subs: cookie(&req, "subscriptions") + subscriptions: cookie(&req, "subscriptions") .split('+') .map(String::from) .filter(|s| !s.is_empty()) diff --git a/static/style.css b/static/style.css index 45ad3ed..5358e89 100644 --- a/static/style.css +++ b/static/style.css @@ -958,19 +958,21 @@ a.search_subreddit:hover { /* Settings */ -#settings, #settings > form { - display: flex; - flex-direction: column; - align-items: center; +#settings { + max-width: 450px; } #settings_note { font-size: 14px; - max-width: 300px; margin-top: 10px; opacity: 0.75; } +#settings_note a { + color: var(--accent); +} + + .prefs { display: flex; flex-direction: column; @@ -979,6 +981,7 @@ a.search_subreddit:hover { padding: 20px; background: var(--post); border-radius: 5px; + margin-bottom: 20px; } .prefs > div { @@ -987,10 +990,7 @@ a.search_subreddit:hover { width: 100%; height: 35px; align-items: center; -} - -.prefs > div:not(:last-of-type) { - margin-bottom: 10px; + margin-top: 10px; } .prefs select { @@ -1017,22 +1017,6 @@ input[type="submit"] { -moz-appearance: none; } -#settings_subs { - list-style: none; - padding: 0; -} - -#settings_subs > li { - display: flex; - margin: 10px 0; -} -#settings_subs > li:last-of-type { margin-bottom: 0; } - -#settings_subs > li > span { - padding: 10px 0; - margin-right: auto; -} - #settings_subs .unsubscribe { margin-left: 30px; } @@ -1176,6 +1160,10 @@ td, th { right: 10px; min-width: auto; } + + #settings { + max-width: unset; + } aside, #subreddit, #user { margin: 0; diff --git a/templates/settings.html b/templates/settings.html index bcb7de6..d6314a1 100644 --- a/templates/settings.html +++ b/templates/settings.html @@ -46,25 +46,27 @@ + -

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

- - {% if prefs.subs.len() > 0 %} - + {% for sub in prefs.subscriptions %} +
+ {{ sub }} +
+ +
+
+ {% endfor %} + {% endif %} + +
+

Note: settings and subscriptions are saved in browser cookies. Clearing your cookies will reset them.


+

You can restore your current settings and subscriptions after clearing your cookies using this link.

+
{% endblock %} diff --git a/templates/subreddit.html b/templates/subreddit.html index 1bde85a..4917a37 100644 --- a/templates/subreddit.html +++ b/templates/subreddit.html @@ -85,7 +85,7 @@
{{ sub.active }}
- {% if prefs.subs.contains(sub.name) %} + {% if prefs.subscriptions.contains(sub.name) %}
diff --git a/templates/utils.html b/templates/utils.html index 071191d..72634b2 100644 --- a/templates/utils.html +++ b/templates/utils.html @@ -41,7 +41,7 @@ {%- endmacro %} {% macro sub_list(current) -%} -{% if prefs.subs.len() > 0 %} +{% if prefs.subscriptions.len() > 0 %}
Feeds
@@ -50,7 +50,7 @@ Popular All

REDDIT FEEDS

- {% for sub in prefs.subs %} + {% for sub in prefs.subscriptions %} {{ sub }} {% endfor %}