From aedbb2e1663a5249a0fc0e27fd0aa480d80c9b10 Mon Sep 17 00:00:00 2001 From: Nick Lowery Date: Wed, 19 Jan 2022 19:22:16 -0700 Subject: [PATCH] Add user listing buttons --- src/main.rs | 1 + src/subreddit.rs | 6 +++++- src/user.rs | 15 +++++++++++++-- static/style.css | 15 ++++++++++----- templates/subreddit.html | 10 +++++----- templates/user.html | 17 ++++++++++------- 6 files changed, 44 insertions(+), 20 deletions(-) diff --git a/src/main.rs b/src/main.rs index 206d5cf..9ffbfed 100644 --- a/src/main.rs +++ b/src/main.rs @@ -193,6 +193,7 @@ async fn main() { app.at("/user/[deleted]").get(|req| error(req, "User has deleted their account".to_string()).boxed()); app.at("/user/:name").get(|r| user::profile(r).boxed()); + app.at("/user/:name/:listing").get(|r| user::profile(r).boxed()); app.at("/user/:name/comments/:id").get(|r| post::item(r).boxed()); app.at("/user/:name/comments/:id/:title").get(|r| post::item(r).boxed()); app.at("/user/:name/comments/:id/:title/:comment_id").get(|r| post::item(r).boxed()); diff --git a/src/subreddit.rs b/src/subreddit.rs index d673601..119ba65 100644 --- a/src/subreddit.rs +++ b/src/subreddit.rs @@ -19,6 +19,7 @@ struct SubredditTemplate { ends: (String, String), prefs: Preferences, url: String, + redirect_url: String, /// Whether the subreddit itself is filtered. is_filtered: bool, /// Whether all fetched posts are filtered (to differentiate between no posts fetched in the first place, @@ -98,6 +99,7 @@ pub async fn community(req: Request) -> Result, String> { let path = format!("/r/{}/{}.json?{}&raw_json=1", sub_name.clone(), sort, req.uri().query().unwrap_or_default()); let url = String::from(req.uri().path_and_query().map_or("", |val| val.as_str())); + let redirect_url = url[1..].replace('?', "%3F").replace('&', "%26"); let filters = get_filters(&req); // If all requested subs are filtered, we don't need to fetch posts. @@ -109,6 +111,7 @@ pub async fn community(req: Request) -> Result, String> { ends: (param(&path, "after").unwrap_or_default(), "".to_string()), prefs: Preferences::new(req), url, + redirect_url, is_filtered: true, all_posts_filtered: false, }) @@ -124,6 +127,7 @@ pub async fn community(req: Request) -> Result, String> { ends: (param(&path, "after").unwrap_or_default(), after), prefs: Preferences::new(req), url, + redirect_url, is_filtered: false, all_posts_filtered, }) @@ -253,7 +257,7 @@ pub async fn subscriptions_filters(req: Request) -> Result, // Redirect back to subreddit // check for redirect parameter if unsubscribing/unfiltering from outside sidebar let path = if let Some(redirect_path) = param(&format!("?{}", query), "redirect") { - format!("/{}/", redirect_path) + format!("/{}", redirect_path) } else { format!("/r/{}", sub) }; diff --git a/src/user.rs b/src/user.rs index d60e65d..a5a11d0 100644 --- a/src/user.rs +++ b/src/user.rs @@ -15,8 +15,11 @@ struct UserTemplate { posts: Vec, sort: (String, String), ends: (String, String), + /// "overview", "comments", or "submitted" + listing: String, prefs: Preferences, url: String, + redirect_url: String, /// Whether the user themself is filtered. is_filtered: bool, /// Whether all fetched posts are filtered (to differentiate between no posts fetched in the first place, @@ -26,13 +29,17 @@ struct UserTemplate { // FUNCTIONS pub async fn profile(req: Request) -> Result, String> { + let listing = req.param("listing").unwrap_or_else(|| "overview".to_string()); + // Build the Reddit JSON API path let path = format!( - "/user/{}.json?{}&raw_json=1", + "/user/{}/{}.json?{}&raw_json=1", req.param("name").unwrap_or_else(|| "reddit".to_string()), - req.uri().query().unwrap_or_default() + listing, + req.uri().query().unwrap_or_default(), ); let url = String::from(req.uri().path_and_query().map_or("", |val| val.as_str())); + let redirect_url = url[1..].replace('?', "%3F").replace('&', "%26"); // Retrieve other variables from Libreddit request let sort = param(&path, "sort").unwrap_or_default(); @@ -46,8 +53,10 @@ pub async fn profile(req: Request) -> Result, String> { posts: Vec::new(), sort: (sort, param(&path, "t").unwrap_or_default()), ends: (param(&path, "after").unwrap_or_default(), "".to_string()), + listing, prefs: Preferences::new(req), url, + redirect_url, is_filtered: true, all_posts_filtered: false, }) @@ -62,8 +71,10 @@ pub async fn profile(req: Request) -> Result, String> { posts, sort: (sort, param(&path, "t").unwrap_or_default()), ends: (param(&path, "after").unwrap_or_default(), after), + listing, prefs: Preferences::new(req), url, + redirect_url, is_filtered: false, all_posts_filtered, }) diff --git a/static/style.css b/static/style.css index 92ef7c2..b522bcf 100644 --- a/static/style.css +++ b/static/style.css @@ -487,7 +487,7 @@ aside { /* Sorting and Search */ -select, #search, #sort_options, #inside, #searchbox > *, #sort_submit { +select, #search, #sort_options, #listing_options, #inside, #searchbox > *, #sort_submit { height: 38px; } @@ -563,6 +563,11 @@ button.submit:hover > svg { stroke: var(--accent); } border-radius: 5px 0px 0px 5px; } +#listing_options + #sort_select { + margin-left: 10px; + border-radius: 5px 0px 0px 5px; +} + #search_sort { background: var(--highlighted); border-radius: 5px; @@ -591,7 +596,7 @@ button.submit:hover > svg { stroke: var(--accent); } margin-bottom: 20px; } -#sort_options, footer > a { +#sort_options, #listing_options, footer > a { border-radius: 5px; align-items: center; box-shadow: var(--shadow); @@ -600,7 +605,7 @@ button.submit:hover > svg { stroke: var(--accent); } overflow: hidden; } -#sort_options > a, footer > a { +#sort_options > a, #listing_options > a, footer > a { color: var(--text); padding: 10px 20px; text-align: center; @@ -608,12 +613,12 @@ button.submit:hover > svg { stroke: var(--accent); } transition: 0.2s background; } -#sort_options > a.selected { +#sort_options > a.selected, #listing_options > a.selected { background: var(--accent); color: var(--foreground); } -#sort_options > a:not(.selected):hover { +#sort_options > a:not(.selected):hover, #listing_options > a:not(.selected):hover { background: var(--foreground); } diff --git a/templates/subreddit.html b/templates/subreddit.html index be22acc..764c63e 100644 --- a/templates/subreddit.html +++ b/templates/subreddit.html @@ -41,7 +41,7 @@ {% if sub.name.contains("+") %} -
+
{% endif %} @@ -101,22 +101,22 @@
{% if prefs.subscriptions.contains(sub.name) %} -
+
{% else %} -
+
{% endif %}
{% if prefs.filters.contains(sub.name) %} -
+
{% else %} -
+
{% endif %} diff --git a/templates/user.html b/templates/user.html index 8095d06..4c81596 100644 --- a/templates/user.html +++ b/templates/user.html @@ -16,9 +16,12 @@ {% if !is_filtered %}
- {% if sort.0 == "top" %} + {% call utils::options(sort.0, ["hot", "new", "top", "controversial"], "") %} + {% if sort.0 == "top" || sort.0 == "controversial" %}{% endif %}
{% else %} -
+
{% endif %}
{% if prefs.filters.contains(name) %} -
+
{% else %} -
+
{% endif %}