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");
// 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() {

View File

@ -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)
}

View File

@ -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

View File

@ -14,18 +14,18 @@
<div id="layout">
<label for="layout">Layout:</label>
<select name="layout">
{% call utils::options(layout, ["card", "clean", "compact"], "clean") %}
{% call utils::options(prefs.layout, ["card", "clean", "compact"], "clean") %}
</select>
</div>
<div id="comment_sort">
<label for="comment_sort">Default comment sort:</label>
<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>
</div>
<div id="hide_nsfw">
<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>
<input id="save" type="submit" value="Save">