From f65ee2eb6a7f89424caa40dc2ff4acba2cb7dcd3 Mon Sep 17 00:00:00 2001 From: spikecodes <19519553+spikecodes@users.noreply.github.com> Date: Tue, 29 Dec 2020 17:11:47 -0800 Subject: [PATCH] Sort Top by Timeframe --- src/popular.rs | 16 +++++++++------- src/subreddit.rs | 16 +++++++++------- src/user.rs | 16 +++++++++------- src/utils.rs | 1 + static/style.css | 11 ++++++++--- templates/popular.html | 21 ++++++++++++++------- templates/subreddit.html | 19 +++++++++++++------ templates/user.html | 19 +++++++++++++------ 8 files changed, 76 insertions(+), 43 deletions(-) diff --git a/src/popular.rs b/src/popular.rs index 76bbd86..e27f28e 100644 --- a/src/popular.rs +++ b/src/popular.rs @@ -8,21 +8,23 @@ use askama::Template; #[template(path = "popular.html", escape = "none")] struct PopularTemplate { posts: Vec, - sort: String, + sort: (String, String), ends: (String, String), } // RENDER -async fn render(sub_name: String, sort: Option, ends: (Option, Option)) -> Result { +async fn render(sort: Option, t: Option, ends: (Option, Option)) -> Result { let sorting = sort.unwrap_or("hot".to_string()); let before = ends.1.clone().unwrap_or(String::new()); // If there is an after, there must be a before + let timeframe = match &t { Some(val) => format!("&t={}", val), None => String::new() }; + // Build the Reddit JSON API url let url = match ends.0 { - Some(val) => format!("r/{}/{}.json?before={}&count=25", sub_name, sorting, val), + Some(val) => format!("r/popular/{}.json?before={}&count=25{}", sorting, val, timeframe), None => match ends.1 { - Some(val) => format!("r/{}/{}.json?after={}&count=25", sub_name, sorting, val), - None => format!("r/{}/{}.json", sub_name, sorting), + Some(val) => format!("r/popular/{}.json?after={}&count=25{}", sorting, val, timeframe), + None => format!("r/popular/{}.json?{}", sorting, timeframe), }, }; @@ -40,7 +42,7 @@ async fn render(sub_name: String, sort: Option, ends: (Option, O let s = PopularTemplate { posts: items.0, - sort: sorting, + sort: (sorting, t.unwrap_or(String::new())), ends: (before, items.1), } .render() @@ -51,5 +53,5 @@ async fn render(sub_name: String, sort: Option, ends: (Option, O // SERVICES pub async fn page(params: web::Query) -> Result { - render("popular".to_string(), params.sort.clone(), (params.before.clone(), params.after.clone())).await + render(params.sort.clone(), params.t.clone(), (params.before.clone(), params.after.clone())).await } diff --git a/src/subreddit.rs b/src/subreddit.rs index ed571ad..57ba189 100644 --- a/src/subreddit.rs +++ b/src/subreddit.rs @@ -10,26 +10,28 @@ use std::convert::TryInto; struct SubredditTemplate { sub: Subreddit, posts: Vec, - sort: String, + sort: (String, String), ends: (String, String), } // SERVICES #[allow(dead_code)] pub async fn page(web::Path(sub): web::Path, params: web::Query) -> Result { - render(sub, params.sort.clone(), (params.before.clone(), params.after.clone())).await + render(sub, params.sort.clone(), params.t.clone(), (params.before.clone(), params.after.clone())).await } -pub async fn render(sub_name: String, sort: Option, ends: (Option, Option)) -> Result { +pub async fn render(sub_name: String, sort: Option, t: Option, ends: (Option, Option)) -> Result { let sorting = sort.unwrap_or("hot".to_string()); let before = ends.1.clone().unwrap_or(String::new()); // If there is an after, there must be a before + let timeframe = match &t { Some(val) => format!("&t={}", val), None => String::new() }; + // Build the Reddit JSON API url let url = match ends.0 { - Some(val) => format!("r/{}/{}.json?before={}&count=25", sub_name, sorting, val), + Some(val) => format!("r/{}/{}.json?before={}&count=25{}", sub_name, sorting, val, timeframe), None => match ends.1 { - Some(val) => format!("r/{}/{}.json?after={}&count=25", sub_name, sorting, val), - None => format!("r/{}/{}.json", sub_name, sorting), + Some(val) => format!("r/{}/{}.json?after={}&count=25{}", sub_name, sorting, val, timeframe), + None => format!("r/{}/{}.json?{}", sub_name, sorting, timeframe), }, }; @@ -54,7 +56,7 @@ pub async fn render(sub_name: String, sort: Option, ends: (Option, - sort: String, + sort: (String, String), ends: (String, String), } -async fn render(username: String, sort: Option, ends: (Option, Option)) -> Result { +async fn render(username: String, sort: Option, t: Option, ends: (Option, Option)) -> Result { let sorting = sort.unwrap_or("new".to_string()); let before = ends.1.clone().unwrap_or(String::new()); // If there is an after, there must be a before + let timeframe = match &t { Some(val) => format!("&t={}", val), None => String::new() }; + // Build the Reddit JSON API url let url = match ends.0 { - Some(val) => format!("user/{}/.json?sort={}&before={}&count=25&raw_json=1", username, sorting, val), + Some(val) => format!("user/{}/.json?sort={}&before={}&count=25&raw_json=1{}", username, sorting, val, timeframe), None => match ends.1 { - Some(val) => format!("user/{}/.json?sort={}&after={}&count=25&raw_json=1", username, sorting, val), - None => format!("user/{}/.json?sort={}&raw_json=1", username, sorting), + Some(val) => format!("user/{}/.json?sort={}&after={}&count=25&raw_json=1{}", username, sorting, val, timeframe), + None => format!("user/{}/.json?sort={}&raw_json=1{}", username, sorting, timeframe), }, }; @@ -44,7 +46,7 @@ async fn render(username: String, sort: Option, ends: (Option, O let s = UserTemplate { user: user.unwrap(), posts: posts_unwrapped.0, - sort: sorting, + sort: (sorting, t.unwrap_or(String::new())), ends: (before, posts_unwrapped.1) } .render() @@ -55,7 +57,7 @@ async fn render(username: String, sort: Option, ends: (Option, O // SERVICES pub async fn page(web::Path(username): web::Path, params: web::Query) -> Result { - render(username, params.sort.clone(), (params.before.clone(), params.after.clone())).await + render(username, params.sort.clone(), params.t.clone(), (params.before.clone(), params.after.clone())).await } // USER diff --git a/src/utils.rs b/src/utils.rs index 9b4b649..4d8a248 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -66,6 +66,7 @@ pub struct Subreddit { // Parser for query params, used in sorting (eg. /r/rust/?sort=hot) #[derive(serde::Deserialize)] pub struct Params { + pub t: Option, pub sort: Option, pub after: Option, pub before: Option, diff --git a/static/style.css b/static/style.css index 950004b..12d395d 100644 --- a/static/style.css +++ b/static/style.css @@ -138,7 +138,7 @@ aside { /* Sorting */ -#sort { +#sort, #timeframe { background: var(--outside); box-shadow: var(--black-contrast); border: 0; @@ -150,6 +150,11 @@ aside { appearance: none; } +#timeframe { + border-radius: 0; + border-left: 4px solid var(--highlighted); +} + #sort_submit { background: var(--highlighted); border: 0; @@ -158,7 +163,7 @@ aside { border-radius: 0 5px 5px 0; } -#sort:hover { background: var(--foreground); } +#sort:hover, #timeframe:hover { background: var(--foreground); } #sort_submit:hover { color: var(--accent); } #sort > div, footer > a { @@ -498,7 +503,7 @@ td, th { max-width: 100%; } - #sidebar { + #user, #sidebar { margin: 20px 0; } } \ No newline at end of file diff --git a/templates/popular.html b/templates/popular.html index d802b8c..cfd96a8 100644 --- a/templates/popular.html +++ b/templates/popular.html @@ -3,11 +3,18 @@
+ + + + + {% if sort.0 == "top" %}{% endif %}
{% for post in posts %}
@@ -37,11 +44,11 @@
{% if ends.0 != "" %} - PREV + PREV {% endif %} {% if ends.1 != "" %} - NEXT + NEXT {% endif %}
diff --git a/templates/subreddit.html b/templates/subreddit.html index d9de2a3..f2c0117 100644 --- a/templates/subreddit.html +++ b/templates/subreddit.html @@ -9,10 +9,17 @@
+ + + + {% if sort.0 == "top" %}{% endif %}
{% for post in posts %}
@@ -42,11 +49,11 @@
{% if ends.0 != "" %} - PREV + PREV {% endif %} {% if ends.1 != "" %} - NEXT + NEXT {% endif %}
diff --git a/templates/user.html b/templates/user.html index 7894ccb..a23dfec 100644 --- a/templates/user.html +++ b/templates/user.html @@ -5,10 +5,17 @@
+ + + + {% if sort.0 == "top" %}{% endif %}
{% for post in posts %} {% if post.title != "Comment" %} @@ -55,11 +62,11 @@ {% endfor %}