redsunlib/src/settings.rs

88 lines
2.4 KiB
Rust
Raw Normal View History

2021-01-06 15:04:49 +13:00
// CRATES
2021-02-25 18:29:23 +13:00
use crate::utils::{redirect, template, Preferences};
2021-01-06 15:04:49 +13:00
use askama::Template;
2021-02-14 12:02:38 +13:00
use tide::{http::Cookie, Request};
2021-01-06 15:04:49 +13:00
use time::{Duration, OffsetDateTime};
// STRUCTS
#[derive(Template)]
2021-01-08 05:38:05 +13:00
#[template(path = "settings.html")]
2021-01-06 15:04:49 +13:00
struct SettingsTemplate {
2021-01-09 14:50:03 +13:00
prefs: Preferences,
2021-01-06 15:04:49 +13:00
}
#[derive(serde::Deserialize, Default)]
#[serde(default)]
2021-03-09 15:49:06 +13:00
pub struct Form {
2021-01-11 15:15:34 +13:00
theme: Option<String>,
2021-01-09 17:55:40 +13:00
front_page: Option<String>,
2021-01-06 15:04:49 +13:00
layout: Option<String>,
2021-01-11 10:08:36 +13:00
wide: Option<String>,
2021-01-08 05:38:05 +13:00
comment_sort: Option<String>,
2021-01-31 18:43:46 +13:00
show_nsfw: Option<String>,
redirect: Option<String>,
subscriptions: Option<String>,
2021-01-06 15:04:49 +13:00
}
// FUNCTIONS
// Retrieve cookies from request "Cookie" header
pub async fn get(req: Request<()>) -> tide::Result {
2021-02-25 18:29:23 +13:00
template(SettingsTemplate { prefs: Preferences::new(req) })
2021-01-06 15:04:49 +13:00
}
// Set cookies using response "Set-Cookie" header
pub async fn set(mut req: Request<()>) -> tide::Result {
2021-03-09 15:49:06 +13:00
let form: Form = req.body_form().await.unwrap_or_default();
2021-02-14 12:02:38 +13:00
let mut res = redirect("/settings".to_string());
2021-01-06 15:04:49 +13:00
2021-01-31 18:43:46 +13:00
let names = vec!["theme", "front_page", "layout", "wide", "comment_sort", "show_nsfw"];
let values = vec![form.theme, form.front_page, form.layout, form.wide, form.comment_sort, form.show_nsfw];
2021-01-06 15:04:49 +13:00
2021-01-09 14:35:04 +13:00
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())
2021-01-09 14:35:04 +13:00
.path("/")
.http_only(true)
.expires(OffsetDateTime::now_utc() + Duration::weeks(52))
.finish(),
),
None => res.remove_cookie(Cookie::named(name.to_owned())),
2021-01-09 14:35:04 +13:00
};
}
2021-01-08 05:38:05 +13:00
Ok(res)
2021-01-06 15:04:49 +13:00
}
// Set cookies using response "Set-Cookie" header
pub async fn restore(req: Request<()>) -> tide::Result {
2021-03-09 15:49:06 +13:00
let form: Form = req.query()?;
let path = match form.redirect {
Some(value) => format!("/{}/", value),
2021-02-14 12:02:38 +13:00
None => "/".to_string(),
};
2021-02-14 12:02:38 +13:00
let mut res = redirect(path);
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)
}