Default subreddit post sorting. Closes #166

This commit is contained in:
spikecodes 2021-03-25 21:41:58 -07:00
parent 2533e8cef5
commit 6a7f725c12
No known key found for this signature in database
GPG Key ID: 004CECFF9B463BCB
6 changed files with 16 additions and 20 deletions

View File

@ -3,7 +3,7 @@ name = "libreddit"
description = " Alternative private front-end to Reddit" description = " Alternative private front-end to Reddit"
license = "AGPL-3.0" license = "AGPL-3.0"
repository = "https://github.com/spikecodes/libreddit" repository = "https://github.com/spikecodes/libreddit"
version = "0.7.0" version = "0.8.0"
authors = ["spikecodes <19519553+spikecodes@users.noreply.github.com>"] authors = ["spikecodes <19519553+spikecodes@users.noreply.github.com>"]
edition = "2018" edition = "2018"
@ -13,7 +13,7 @@ async-recursion = "0.3.2"
cached = "0.23.0" cached = "0.23.0"
clap = { version = "2.33.3", default-features = false } clap = { version = "2.33.3", default-features = false }
regex = "1.4.5" regex = "1.4.5"
serde = { version = "1.0.124", features = ["derive"] } serde = { version = "1.0.125", features = ["derive"] }
cookie = "0.15.0" cookie = "0.15.0"
futures-lite = "1.11.3" futures-lite = "1.11.3"
hyper = { version = "0.14.4", features = ["full"] } hyper = { version = "0.14.4", features = ["full"] }

View File

@ -40,7 +40,7 @@ pub async fn item(req: Request<Body>) -> Result<Response<Body>, String> {
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
dbg!(req.param("id").unwrap_or_default()); 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(); let highlighted_comment = &req.param("comment_id").unwrap_or_default();
// Send a request to the url, receive JSON in response // Send a request to the url, receive JSON in response
@ -57,7 +57,7 @@ pub async fn item(req: Request<Body>) -> Result<Response<Body>, String> {
post, post,
sort, sort,
prefs: Preferences::new(req), prefs: Preferences::new(req),
single_thread: *single_thread, single_thread,
}) })
} }
// If the Reddit API returns an error, exit and send error page to user // If the Reddit API returns an error, exit and send error page to user

View File

@ -16,19 +16,6 @@ struct SettingsTemplate {
prefs: Preferences, prefs: Preferences,
} }
#[derive(serde::Deserialize, Default, Debug)]
#[serde(default)]
pub struct Form {
theme: Option<String>,
front_page: Option<String>,
layout: Option<String>,
wide: Option<String>,
comment_sort: Option<String>,
show_nsfw: Option<String>,
redirect: Option<String>,
subscriptions: Option<String>,
}
// FUNCTIONS // FUNCTIONS
// Retrieve cookies from request "Cookie" header // Retrieve cookies from request "Cookie" header
@ -63,7 +50,7 @@ pub async fn set(req: Request<Body>) -> Result<Response<Body>, String> {
let mut res = redirect("/settings".to_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 { for name in names {
match form.get(name) { match form.get(name) {
@ -98,7 +85,7 @@ pub async fn restore(req: Request<Body>) -> Result<Response<Body>, String> {
let form = url::form_urlencoded::parse(query).collect::<HashMap<_, _>>(); let form = url::form_urlencoded::parse(query).collect::<HashMap<_, _>>();
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") { let path = match form.get("redirect") {
Some(value) => format!("/{}/", value), Some(value) => format!("/{}/", value),

View File

@ -32,7 +32,8 @@ pub async fn community(req: Request<Body>) -> Result<Response<Body>, String> {
// Build Reddit API path // Build Reddit API path
let subscribed = cookie(&req, "subscriptions"); let subscribed = cookie(&req, "subscriptions");
let front_page = cookie(&req, "front_page"); 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() { let sub = req.param("sub").map(String::from).unwrap_or(if front_page == "default" || front_page.is_empty() {
if subscribed.is_empty() { if subscribed.is_empty() {

View File

@ -365,6 +365,7 @@ pub struct Preferences {
pub wide: String, pub wide: String,
pub show_nsfw: String, pub show_nsfw: String,
pub comment_sort: String, pub comment_sort: String,
pub post_sort: String,
pub subscriptions: Vec<String>, pub subscriptions: Vec<String>,
} }
@ -378,6 +379,7 @@ impl Preferences {
wide: cookie(&req, "wide"), wide: cookie(&req, "wide"),
show_nsfw: cookie(&req, "show_nsfw"), show_nsfw: cookie(&req, "show_nsfw"),
comment_sort: cookie(&req, "comment_sort"), 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(), subscriptions: cookie(&req, "subscriptions").split('+').map(String::from).filter(|s| !s.is_empty()).collect(),
} }
} }

View File

@ -36,6 +36,12 @@
<input type="checkbox" name="wide" {% if prefs.wide == "on" %}checked{% endif %}> <input type="checkbox" name="wide" {% if prefs.wide == "on" %}checked{% endif %}>
</div> </div>
<p>Content</p> <p>Content</p>
<div id="post_sort">
<label for="post_sort" title="Applies only to subreddit feeds">Default subreddit post sort:</label>
<select name="post_sort">
{% call utils::options(prefs.post_sort, ["hot", "new", "top", "rising", "controversial"], "hot") %}
</select>
</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">