From 280e16bd7fa4c229d9a72f1d8c6bcd4bf65ae241 Mon Sep 17 00:00:00 2001 From: spikecodes <19519553+spikecodes@users.noreply.github.com> Date: Wed, 23 Dec 2020 20:36:49 -0800 Subject: [PATCH] Fix Subreddit Icons --- src/post.rs | 6 +++--- src/subreddit.rs | 17 +++++++++-------- src/user.rs | 2 +- src/utils.rs | 8 ++++++-- 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/post.rs b/src/post.rs index 8680d6f..2cecb0d 100644 --- a/src/post.rs +++ b/src/post.rs @@ -80,13 +80,13 @@ async fn media(data: &serde_json::Value) -> (String, String) { let post_type: &str; let url = if !data["preview"]["reddit_video_preview"]["fallback_url"].is_null() { post_type = "video"; - format_url(data["preview"]["reddit_video_preview"]["fallback_url"].as_str().unwrap()).await + format_url(data["preview"]["reddit_video_preview"]["fallback_url"].as_str().unwrap().to_string()).await } else if !data["secure_media"]["reddit_video"]["fallback_url"].is_null() { post_type = "video"; - format_url(data["secure_media"]["reddit_video"]["fallback_url"].as_str().unwrap()).await + format_url(data["secure_media"]["reddit_video"]["fallback_url"].as_str().unwrap().to_string()).await } else if data["post_hint"].as_str().unwrap_or("") == "image" { post_type = "image"; - format_url(data["preview"]["images"][0]["source"]["url"].as_str().unwrap()).await + format_url(data["preview"]["images"][0]["source"]["url"].as_str().unwrap().to_string()).await } else { post_type = "link"; data["url"].as_str().unwrap().to_string() diff --git a/src/subreddit.rs b/src/subreddit.rs index 16a58b2..8847c47 100644 --- a/src/subreddit.rs +++ b/src/subreddit.rs @@ -55,15 +55,9 @@ pub async fn render(sub_name: String, sort: Option, ends: (Option"#, sub.icon) - } else { - String::new() - }; - let s = SubredditTemplate { sub: sub, posts: items.0, @@ -95,11 +89,18 @@ async fn subreddit(sub: &String) -> Result { let members = res["data"]["subscribers"].as_u64().unwrap_or(0); let active = res["data"]["accounts_active"].as_u64().unwrap_or(0); + let community_icon: &str = res["data"]["community_icon"].as_str().unwrap().split("?").collect::>()[0]; + let icon = if community_icon.is_empty() { + val(&res, "icon_img").await + } else { + community_icon.to_string() + }; + let sub = Subreddit { name: val(&res, "display_name").await, title: val(&res, "title").await, description: val(&res, "public_description").await, - icon: format_url(val(&res, "icon_img").await.as_str()).await, + icon: format_url(icon).await, members: format_num(members.try_into().unwrap()), active: format_num(active.try_into().unwrap()), }; diff --git a/src/user.rs b/src/user.rs index 42043c3..db13b68 100644 --- a/src/user.rs +++ b/src/user.rs @@ -65,7 +65,7 @@ async fn user(name: &String) -> Result { // Parse the JSON output into a User struct Ok(User { name: name.to_string(), - icon: format_url(nested_val(&res, "subreddit", "icon_img").await.as_str()).await, + icon: format_url(nested_val(&res, "subreddit", "icon_img").await).await, karma: res["data"]["total_karma"].as_i64().unwrap(), banner: nested_val(&res, "subreddit", "banner_img").await, description: nested_val(&res, "subreddit", "public_description").await, diff --git a/src/utils.rs b/src/utils.rs index e2e6c09..d731e7f 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -79,7 +79,11 @@ pub struct ErrorTemplate { // FORMATTING // -pub async fn format_url(url: &str) -> String { +pub async fn format_url(url: String) -> String { + if url.is_empty() { + return String::new(); + }; + #[cfg(feature = "proxy")] return "/proxy/".to_string() + encode(url).as_str(); @@ -130,7 +134,7 @@ pub async fn fetch_posts(url: String, fallback_title: String) -> Result<(Vec