147 lines
3.4 KiB
Rust
147 lines
3.4 KiB
Rust
use std::collections::HashMap;
|
|
|
|
// CRATES
|
|
use crate::server::ResponseExt;
|
|
use crate::utils::{redirect, template, Preferences};
|
|
use askama::Template;
|
|
use cookie::Cookie;
|
|
use futures_lite::StreamExt;
|
|
use hyper::{Body, Request, Response};
|
|
use time::{Duration, OffsetDateTime};
|
|
|
|
// STRUCTS
|
|
#[derive(Template)]
|
|
#[template(path = "settings.html")]
|
|
struct SettingsTemplate {
|
|
prefs: Preferences,
|
|
url: String,
|
|
}
|
|
|
|
// CONSTANTS
|
|
|
|
const PREFS: [&str; 17] = [
|
|
"theme",
|
|
"mascot",
|
|
"front_page",
|
|
"layout",
|
|
"wide",
|
|
"comment_sort",
|
|
"post_sort",
|
|
"show_nsfw",
|
|
"blur_nsfw",
|
|
"use_hls",
|
|
"hide_hls_notification",
|
|
"autoplay_videos",
|
|
"hide_sidebar_and_summary",
|
|
"fixed_navbar",
|
|
"hide_awards",
|
|
"hide_score",
|
|
"disable_visit_reddit_confirmation",
|
|
];
|
|
|
|
// FUNCTIONS
|
|
|
|
// Retrieve cookies from request "Cookie" header
|
|
pub async fn get(req: Request<Body>) -> Result<Response<Body>, String> {
|
|
let url = req.uri().to_string();
|
|
Ok(template(&SettingsTemplate {
|
|
prefs: Preferences::new(&req),
|
|
url,
|
|
}))
|
|
}
|
|
|
|
// Set cookies using response "Set-Cookie" header
|
|
pub async fn set(req: Request<Body>) -> Result<Response<Body>, String> {
|
|
// Split the body into parts
|
|
let (parts, mut body) = req.into_parts();
|
|
|
|
// Grab existing cookies
|
|
let _cookies: Vec<Cookie<'_>> = parts
|
|
.headers
|
|
.get_all("Cookie")
|
|
.iter()
|
|
.filter_map(|header| Cookie::parse(header.to_str().unwrap_or_default()).ok())
|
|
.collect();
|
|
|
|
// Aggregate the body...
|
|
// let whole_body = hyper::body::aggregate(req).await.map_err(|e| e.to_string())?;
|
|
let body_bytes = body
|
|
.try_fold(Vec::new(), |mut data, chunk| {
|
|
data.extend_from_slice(&chunk);
|
|
Ok(data)
|
|
})
|
|
.await
|
|
.map_err(|e| e.to_string())?;
|
|
|
|
let form = url::form_urlencoded::parse(&body_bytes).collect::<HashMap<_, _>>();
|
|
|
|
let mut response = redirect("/settings");
|
|
|
|
for &name in &PREFS {
|
|
match form.get(name) {
|
|
Some(value) => response.insert_cookie(
|
|
Cookie::build((name.to_owned(), value.clone()))
|
|
.path("/")
|
|
.http_only(true)
|
|
.expires(OffsetDateTime::now_utc() + Duration::weeks(52))
|
|
.into(),
|
|
),
|
|
None => response.remove_cookie(name.to_string()),
|
|
};
|
|
}
|
|
|
|
Ok(response)
|
|
}
|
|
|
|
fn set_cookies_method(req: Request<Body>, remove_cookies: bool) -> Response<Body> {
|
|
// Split the body into parts
|
|
let (parts, _) = req.into_parts();
|
|
|
|
// Grab existing cookies
|
|
let _cookies: Vec<Cookie<'_>> = parts
|
|
.headers
|
|
.get_all("Cookie")
|
|
.iter()
|
|
.filter_map(|header| Cookie::parse(header.to_str().unwrap_or_default()).ok())
|
|
.collect();
|
|
|
|
let query = parts.uri.query().unwrap_or_default().as_bytes();
|
|
|
|
let form = url::form_urlencoded::parse(query).collect::<HashMap<_, _>>();
|
|
|
|
let path = match form.get("redirect") {
|
|
Some(value) => format!("/{}", value.replace("%26", "&").replace("%23", "#")),
|
|
None => "/".to_string(),
|
|
};
|
|
|
|
let mut response = redirect(&path);
|
|
|
|
for name in [PREFS.to_vec(), vec!["subscriptions", "filters"]].concat() {
|
|
match form.get(name) {
|
|
Some(value) => response.insert_cookie(
|
|
Cookie::build((name.to_owned(), value.clone()))
|
|
.path("/")
|
|
.http_only(true)
|
|
.expires(OffsetDateTime::now_utc() + Duration::weeks(52))
|
|
.into(),
|
|
),
|
|
None => {
|
|
if remove_cookies {
|
|
response.remove_cookie(name.to_string());
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
response
|
|
}
|
|
|
|
// Set cookies using response "Set-Cookie" header
|
|
pub async fn restore(req: Request<Body>) -> Result<Response<Body>, String> {
|
|
Ok(set_cookies_method(req, true))
|
|
}
|
|
|
|
pub async fn update(req: Request<Body>) -> Result<Response<Body>, String> {
|
|
Ok(set_cookies_method(req, false))
|
|
}
|