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