Default subreddit post sorting. Closes #166

pull/171/head v0.8.0
spikecodes 2 years ago
parent 2533e8cef5
commit 6a7f725c12
No known key found for this signature in database
GPG Key ID: 004CECFF9B463BCB
  1. 4
      Cargo.toml
  2. 4
      src/post.rs
  3. 17
      src/settings.rs
  4. 3
      src/subreddit.rs
  5. 2
      src/utils.rs
  6. 6
      templates/settings.html

@ -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"] }

@ -40,7 +40,7 @@ pub async fn item(req: Request<Body>) -> Result<Response<Body>, 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<Body>) -> Result<Response<Body>, 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

@ -16,19 +16,6 @@ struct SettingsTemplate {
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
// 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 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<Body>) -> Result<Response<Body>, String> {
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") {
Some(value) => format!("/{}/", value),

@ -32,7 +32,8 @@ pub async fn community(req: Request<Body>) -> Result<Response<Body>, 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() {

@ -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<String>,
}
@ -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(),
}
}

@ -36,6 +36,12 @@
<input type="checkbox" name="wide" {% if prefs.wide == "on" %}checked{% endif %}>
</div>
<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">
<label for="comment_sort">Default comment sort:</label>
<select name="comment_sort">

Loading…
Cancel
Save