Unify preferences under one struct

This commit is contained in:
spikecodes 2021-01-08 17:50:03 -08:00
parent b13874d0db
commit ef2f9ad12b
4 changed files with 15 additions and 21 deletions

View File

@ -24,7 +24,7 @@ pub async fn item(req: HttpRequest) -> HttpResponse {
let mut sort: String = param(&path, "sort"); let mut sort: String = param(&path, "sort");
// Grab default comment sort method from Cookies // 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 there's no sort query but there's a default sort, set sort to default_sort
if sort.is_empty() && !default_sort.is_empty() { if sort.is_empty() && !default_sort.is_empty() {

View File

@ -1,5 +1,5 @@
// CRATES // CRATES
use crate::utils::cookie; use crate::utils::{prefs, Preferences};
use actix_web::{cookie::Cookie, web::Form, HttpMessage, HttpRequest, HttpResponse}; use actix_web::{cookie::Cookie, web::Form, HttpMessage, HttpRequest, HttpResponse};
use askama::Template; use askama::Template;
use time::{Duration, OffsetDateTime}; use time::{Duration, OffsetDateTime};
@ -8,9 +8,7 @@ use time::{Duration, OffsetDateTime};
#[derive(Template)] #[derive(Template)]
#[template(path = "settings.html")] #[template(path = "settings.html")]
struct SettingsTemplate { struct SettingsTemplate {
layout: String, prefs: Preferences,
comment_sort: String,
hide_nsfw: String,
} }
#[derive(serde::Deserialize)] #[derive(serde::Deserialize)]
@ -24,13 +22,7 @@ pub struct SettingsForm {
// Retrieve cookies from request "Cookie" header // Retrieve cookies from request "Cookie" header
pub async fn get(req: HttpRequest) -> HttpResponse { pub async fn get(req: HttpRequest) -> HttpResponse {
let s = SettingsTemplate { let s = SettingsTemplate { prefs: prefs(req) }.render().unwrap();
layout: cookie(req.to_owned(), "layout"),
comment_sort: cookie(req.to_owned(), "comment_sort"),
hide_nsfw: cookie(req, "hide_nsfw"),
}
.render()
.unwrap();
HttpResponse::Ok().content_type("text/html").body(s) HttpResponse::Ok().content_type("text/html").body(s)
} }

View File

@ -1,7 +1,7 @@
// //
// CRATES // CRATES
// //
use actix_web::{cookie::Cookie, HttpResponse, Result}; use actix_web::{cookie::Cookie, HttpRequest, HttpResponse, Result};
use askama::Template; use askama::Template;
use base64::encode; use base64::encode;
use regex::Regex; use regex::Regex;
@ -95,6 +95,7 @@ pub struct ErrorTemplate {
pub struct Preferences { pub struct Preferences {
pub layout: String, pub layout: String,
pub hide_nsfw: String, pub hide_nsfw: String,
pub comment_sort: String,
} }
// //
@ -102,10 +103,11 @@ pub struct Preferences {
// //
// Build preferences from cookies // Build preferences from cookies
pub fn prefs(req: actix_web::HttpRequest) -> Preferences { pub fn prefs(req: HttpRequest) -> Preferences {
Preferences { Preferences {
layout: cookie(req.to_owned(), "layout"), layout: cookie(&req, "layout"),
hide_nsfw: cookie(req, "hide_nsfw"), 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 // Parse Cookie value from request
pub fn cookie(req: actix_web::HttpRequest, name: &str) -> String { pub fn cookie(req: &HttpRequest, name: &str) -> String {
actix_web::HttpMessage::cookie(&req, name).unwrap_or_else(|| Cookie::new(name, "")).value().to_string() actix_web::HttpMessage::cookie(req, name).unwrap_or_else(|| Cookie::new(name, "")).value().to_string()
} }
// Direct urls to proxy if proxy is enabled // Direct urls to proxy if proxy is enabled

View File

@ -14,18 +14,18 @@
<div id="layout"> <div id="layout">
<label for="layout">Layout:</label> <label for="layout">Layout:</label>
<select name="layout"> <select name="layout">
{% call utils::options(layout, ["card", "clean", "compact"], "clean") %} {% call utils::options(prefs.layout, ["card", "clean", "compact"], "clean") %}
</select> </select>
</div> </div>
<div id="comment_sort"> <div id="comment_sort">
<label for="comment_sort">Default comment sort:</label> <label for="comment_sort">Default comment sort:</label>
<select name="comment_sort"> <select name="comment_sort">
{% call utils::options(comment_sort, ["confidence", "top", "new", "controversial", "old"], "confidence") %} {% call utils::options(prefs.comment_sort, ["confidence", "top", "new", "controversial", "old"], "confidence") %}
</select> </select>
</div> </div>
<div id="hide_nsfw"> <div id="hide_nsfw">
<label for="hide_nsfw">Hide NSFW posts:</label> <label for="hide_nsfw">Hide NSFW posts:</label>
<input type="checkbox" name="hide_nsfw" {% if hide_nsfw == "on" %}checked{% endif %}> <input type="checkbox" name="hide_nsfw" {% if prefs.hide_nsfw == "on" %}checked{% endif %}>
</div> </div>
</div> </div>
<input id="save" type="submit" value="Save"> <input id="save" type="submit" value="Save">