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>
This commit is contained in:
robrobinbin
2021-02-13 21:55:23 +01:00
committed by GitHub
parent ff8685ae4c
commit 93cfc713c6
8 changed files with 76 additions and 46 deletions

View File

@@ -12,6 +12,7 @@ struct SettingsTemplate {
}
#[derive(serde::Deserialize, Default)]
#[serde(default)]
pub struct SettingsForm {
theme: Option<String>,
front_page: Option<String>,
@@ -19,6 +20,8 @@ pub struct SettingsForm {
wide: Option<String>,
comment_sort: Option<String>,
show_nsfw: Option<String>,
redirect: Option<String>,
subscriptions: Option<String>,
}
// 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 <a href=\"{0}\">{0}</a>...", 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)
}