From 2d77a911509c5a61b6691bb4806ac754b143a245 Mon Sep 17 00:00:00 2001 From: spikecodes <19519553+spikecodes@users.noreply.github.com> Date: Thu, 31 Dec 2020 20:21:56 -0800 Subject: [PATCH] Refactor Page Titles and Add Subreddit/User Titles --- src/subreddit.rs | 6 +++--- src/user.rs | 13 +++++++------ src/utils.rs | 3 +-- static/style.css | 26 +++++++++++++++----------- templates/search.html | 2 ++ templates/subreddit.html | 4 +++- templates/user.html | 3 ++- templates/utils.html | 4 ++-- 8 files changed, 35 insertions(+), 26 deletions(-) diff --git a/src/subreddit.rs b/src/subreddit.rs index 08fc105..e0e834d 100644 --- a/src/subreddit.rs +++ b/src/subreddit.rs @@ -72,7 +72,7 @@ async fn subreddit(sub: &String) -> Result { let active = res["data"]["accounts_active"].as_u64().unwrap_or(0); // Fetch subreddit icon either from the community_icon or icon_img value - let community_icon: &str = res["data"]["community_icon"].as_str().unwrap().split("?").collect::>()[0]; + let community_icon: &str = res["data"]["community_icon"].as_str().unwrap_or("").split("?").collect::>()[0]; let icon = if community_icon.is_empty() { val(&res, "icon_img").await } else { @@ -85,8 +85,8 @@ async fn subreddit(sub: &String) -> Result { description: val(&res, "public_description").await, info: val(&res, "description_html").await.replace("\\", ""), icon: format_url(icon).await, - members: format_num(members.try_into().unwrap()), - active: format_num(active.try_into().unwrap()), + members: format_num(members.try_into().unwrap_or(0)), + active: format_num(active.try_into().unwrap_or(0)), }; Ok(sub) diff --git a/src/user.rs b/src/user.rs index 5fa2ee2..2042210 100644 --- a/src/user.rs +++ b/src/user.rs @@ -56,17 +56,17 @@ pub async fn profile(req: HttpRequest) -> Result { // USER async fn user(name: &String) -> Result { - // Build the Reddit JSON API url - let url: String = format!("user/{}/about.json", name); - + // Build the Reddit JSON API path + let path: String = format!("user/{}/about.json", name); + // Send a request to the url, receive JSON in response - let req = request(url).await; - + let req = request(path).await; + // If the Reddit API returns an error, exit this function if req.is_err() { return Err(req.err().unwrap()); } - + // Otherwise, grab the JSON output from the request let res = req.unwrap(); @@ -76,6 +76,7 @@ async fn user(name: &String) -> Result { // Parse the JSON output into a User struct Ok(User { name: name.to_string(), + title: nested_val(&res, "subreddit", "title").await, icon: format_url(nested_val(&res, "subreddit", "icon_img").await).await, karma: res["data"]["total_karma"].as_i64().unwrap(), created: Utc.timestamp(created, 0).format("%b %e, %Y").to_string(), diff --git a/src/utils.rs b/src/utils.rs index 9aec9b8..2c8edee 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -50,6 +50,7 @@ pub struct Comment { // User struct containing metadata about user pub struct User { pub name: String, + pub title: String, pub icon: String, pub karma: i64, pub created: String, @@ -194,8 +195,6 @@ pub async fn fetch_posts(path: String, fallback_title: String) -> Result<(Vec
diff --git a/templates/subreddit.html b/templates/subreddit.html index bb1b3a4..a769b1b 100644 --- a/templates/subreddit.html +++ b/templates/subreddit.html @@ -2,7 +2,8 @@ {% import "utils.html" as utils %} {% block title %} - {% if sub.name != "" %}r/{{ sub.name }}: {{ sub.description }} + {% if sub.title != "" %}{{ sub.title }} + {% else if sub.name != "" %}{{ sub.name }} {% else %}Libreddit{% endif %} {% endblock %} @@ -66,6 +67,7 @@