diff --git a/src/post.rs b/src/post.rs index 1809e14..92dbf88 100644 --- a/src/post.rs +++ b/src/post.rs @@ -24,7 +24,7 @@ pub async fn item(req: HttpRequest) -> HttpResponse { let mut sort: String = param(&path, "sort"); // Grab default comment sort method from Cookies - let default_sort = cookie(req.to_owned(), "comment_sort"); + let default_sort = cookie(&req, "comment_sort"); // If there's no sort query but there's a default sort, set sort to default_sort if sort.is_empty() && !default_sort.is_empty() { diff --git a/src/settings.rs b/src/settings.rs index c705260..5846f8f 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -1,5 +1,5 @@ // CRATES -use crate::utils::cookie; +use crate::utils::{prefs, Preferences}; use actix_web::{cookie::Cookie, web::Form, HttpMessage, HttpRequest, HttpResponse}; use askama::Template; use time::{Duration, OffsetDateTime}; @@ -8,9 +8,7 @@ use time::{Duration, OffsetDateTime}; #[derive(Template)] #[template(path = "settings.html")] struct SettingsTemplate { - layout: String, - comment_sort: String, - hide_nsfw: String, + prefs: Preferences, } #[derive(serde::Deserialize)] @@ -24,13 +22,7 @@ pub struct SettingsForm { // Retrieve cookies from request "Cookie" header pub async fn get(req: HttpRequest) -> HttpResponse { - let s = SettingsTemplate { - layout: cookie(req.to_owned(), "layout"), - comment_sort: cookie(req.to_owned(), "comment_sort"), - hide_nsfw: cookie(req, "hide_nsfw"), - } - .render() - .unwrap(); + let s = SettingsTemplate { prefs: prefs(req) }.render().unwrap(); HttpResponse::Ok().content_type("text/html").body(s) } diff --git a/src/utils.rs b/src/utils.rs index 7935a9f..0b0ff26 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,7 +1,7 @@ // // CRATES // -use actix_web::{cookie::Cookie, HttpResponse, Result}; +use actix_web::{cookie::Cookie, HttpRequest, HttpResponse, Result}; use askama::Template; use base64::encode; use regex::Regex; @@ -95,6 +95,7 @@ pub struct ErrorTemplate { pub struct Preferences { pub layout: String, pub hide_nsfw: String, + pub comment_sort: String, } // @@ -102,10 +103,11 @@ pub struct Preferences { // // Build preferences from cookies -pub fn prefs(req: actix_web::HttpRequest) -> Preferences { +pub fn prefs(req: HttpRequest) -> Preferences { Preferences { - layout: cookie(req.to_owned(), "layout"), - hide_nsfw: cookie(req, "hide_nsfw"), + layout: cookie(&req, "layout"), + hide_nsfw: cookie(&req, "hide_nsfw"), + comment_sort: cookie(&req, "comment_sort"), } } @@ -117,8 +119,8 @@ pub fn param(path: &str, value: &str) -> String { } // Parse Cookie value from request -pub fn cookie(req: actix_web::HttpRequest, name: &str) -> String { - actix_web::HttpMessage::cookie(&req, name).unwrap_or_else(|| Cookie::new(name, "")).value().to_string() +pub fn cookie(req: &HttpRequest, name: &str) -> String { + actix_web::HttpMessage::cookie(req, name).unwrap_or_else(|| Cookie::new(name, "")).value().to_string() } // Direct urls to proxy if proxy is enabled diff --git a/templates/settings.html b/templates/settings.html index 4c19e9c..ba59e45 100644 --- a/templates/settings.html +++ b/templates/settings.html @@ -14,18 +14,18 @@