From 6a7f725c12fe230ec023f279273471b01b36b7b5 Mon Sep 17 00:00:00 2001 From: spikecodes <19519553+spikecodes@users.noreply.github.com> Date: Thu, 25 Mar 2021 21:41:58 -0700 Subject: [PATCH] Default subreddit post sorting. Closes #166 --- Cargo.toml | 4 ++-- src/post.rs | 4 ++-- src/settings.rs | 17 ++--------------- src/subreddit.rs | 3 ++- src/utils.rs | 2 ++ templates/settings.html | 6 ++++++ 6 files changed, 16 insertions(+), 20 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 34d916a..938dae3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "libreddit" description = " Alternative private front-end to Reddit" license = "AGPL-3.0" repository = "https://github.com/spikecodes/libreddit" -version = "0.7.0" +version = "0.8.0" authors = ["spikecodes <19519553+spikecodes@users.noreply.github.com>"] edition = "2018" @@ -13,7 +13,7 @@ async-recursion = "0.3.2" cached = "0.23.0" clap = { version = "2.33.3", default-features = false } regex = "1.4.5" -serde = { version = "1.0.124", features = ["derive"] } +serde = { version = "1.0.125", features = ["derive"] } cookie = "0.15.0" futures-lite = "1.11.3" hyper = { version = "0.14.4", features = ["full"] } diff --git a/src/post.rs b/src/post.rs index 9321c95..0f06f8d 100644 --- a/src/post.rs +++ b/src/post.rs @@ -40,7 +40,7 @@ pub async fn item(req: Request) -> Result, String> { #[cfg(debug_assertions)] dbg!(req.param("id").unwrap_or_default()); - let single_thread = &req.param("comment_id").is_some(); + let single_thread = req.param("comment_id").is_some(); let highlighted_comment = &req.param("comment_id").unwrap_or_default(); // Send a request to the url, receive JSON in response @@ -57,7 +57,7 @@ pub async fn item(req: Request) -> Result, String> { post, sort, prefs: Preferences::new(req), - single_thread: *single_thread, + single_thread, }) } // If the Reddit API returns an error, exit and send error page to user diff --git a/src/settings.rs b/src/settings.rs index 5318870..df5bab9 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -16,19 +16,6 @@ struct SettingsTemplate { prefs: Preferences, } -#[derive(serde::Deserialize, Default, Debug)] -#[serde(default)] -pub struct Form { - theme: Option, - front_page: Option, - layout: Option, - wide: Option, - comment_sort: Option, - show_nsfw: Option, - redirect: Option, - subscriptions: Option, -} - // FUNCTIONS // Retrieve cookies from request "Cookie" header @@ -63,7 +50,7 @@ pub async fn set(req: Request) -> Result, String> { let mut res = redirect("/settings".to_string()); - let names = vec!["theme", "front_page", "layout", "wide", "comment_sort", "show_nsfw", "subscriptions"]; + let names = vec!["theme", "front_page", "layout", "wide", "comment_sort", "post_sort", "show_nsfw", "subscriptions"]; for name in names { match form.get(name) { @@ -98,7 +85,7 @@ pub async fn restore(req: Request) -> Result, String> { let form = url::form_urlencoded::parse(query).collect::>(); - let names = vec!["theme", "front_page", "layout", "wide", "comment_sort", "show_nsfw", "subscriptions"]; + let names = vec!["theme", "front_page", "layout", "wide", "comment_sort", "post_sort", "show_nsfw", "subscriptions"]; let path = match form.get("redirect") { Some(value) => format!("/{}/", value), diff --git a/src/subreddit.rs b/src/subreddit.rs index 3d72105..3f15fa3 100644 --- a/src/subreddit.rs +++ b/src/subreddit.rs @@ -32,7 +32,8 @@ pub async fn community(req: Request) -> Result, String> { // Build Reddit API path let subscribed = cookie(&req, "subscriptions"); let front_page = cookie(&req, "front_page"); - let sort = req.param("sort").unwrap_or_else(|| req.param("id").unwrap_or("hot".to_string())); + let post_sort = req.cookie("post_sort").map(|c| c.value().to_string()).unwrap_or("hot".to_string()); + let sort = req.param("sort").unwrap_or(req.param("id").unwrap_or(post_sort)); let sub = req.param("sub").map(String::from).unwrap_or(if front_page == "default" || front_page.is_empty() { if subscribed.is_empty() { diff --git a/src/utils.rs b/src/utils.rs index 274081a..412b3bf 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -365,6 +365,7 @@ pub struct Preferences { pub wide: String, pub show_nsfw: String, pub comment_sort: String, + pub post_sort: String, pub subscriptions: Vec, } @@ -378,6 +379,7 @@ impl Preferences { wide: cookie(&req, "wide"), show_nsfw: cookie(&req, "show_nsfw"), comment_sort: cookie(&req, "comment_sort"), + post_sort: cookie(&req, "post_sort"), subscriptions: cookie(&req, "subscriptions").split('+').map(String::from).filter(|s| !s.is_empty()).collect(), } } diff --git a/templates/settings.html b/templates/settings.html index e63fa7e..ffc2bd4 100644 --- a/templates/settings.html +++ b/templates/settings.html @@ -36,6 +36,12 @@

Content

+
+ + +