From 9b5176f7b981cd87f5914f494c48481bfd93add8 Mon Sep 17 00:00:00 2001 From: spikecodes <19519553+spikecodes@users.noreply.github.com> Date: Thu, 18 Mar 2021 21:32:54 -0700 Subject: [PATCH] Sub icons and truncated subscribers in search results --- src/search.rs | 26 +++++++++++++++++++------- static/style.css | 21 ++++++++++++++++++++- templates/search.html | 15 +++++++++------ 3 files changed, 48 insertions(+), 14 deletions(-) diff --git a/src/search.rs b/src/search.rs index 3fc9c66..28afefb 100644 --- a/src/search.rs +++ b/src/search.rs @@ -1,5 +1,5 @@ // CRATES -use crate::utils::{cookie, error, param, template, val, Post, Preferences}; +use crate::utils::{cookie, error, format_num, format_url, param, template, val, Post, Preferences}; use crate::{client::json, RequestExt}; use askama::Template; use hyper::{Body, Request, Response}; @@ -18,8 +18,9 @@ struct SearchParams { struct Subreddit { name: String, url: String, + icon: String, description: String, - subscribers: i64, + subscribers: String, } #[derive(Template)] @@ -81,11 +82,22 @@ async fn search_subreddits(q: &str) -> Vec { // For each subreddit from subreddit list Some(list) => list .iter() - .map(|subreddit| Subreddit { - name: val(subreddit, "display_name_prefixed"), - url: val(subreddit, "url"), - description: val(subreddit, "public_description"), - subscribers: subreddit["data"]["subscribers"].as_f64().unwrap_or_default() as i64, + .map(|subreddit| { + // Fetch subreddit icon either from the community_icon or icon_img value + let community_icon: &str = subreddit["data"]["community_icon"].as_str().map_or("", |s| s.split('?').collect::>()[0]); + let icon = if community_icon.is_empty() { + val(&subreddit, "icon_img") + } else { + community_icon.to_string() + }; + + Subreddit { + name: val(subreddit, "display_name_prefixed"), + url: val(subreddit, "url"), + icon: format_url(&icon), + description: val(subreddit, "public_description"), + subscribers: format_num(subreddit["data"]["subscribers"].as_f64().unwrap_or_default() as i64), + } }) .collect::>(), _ => Vec::new(), diff --git a/static/style.css b/static/style.css index f49179f..7f0f5e4 100644 --- a/static/style.css +++ b/static/style.css @@ -522,7 +522,26 @@ button.submit:hover > svg { stroke: var(--accent); } .search_subreddit { padding: 16px 20px; - display: block; + display: flex; +} + +.search_subreddit_left { + display: flex; + align-items: center; +} + +.search_subreddit_left:not(:empty) { + margin-right: 10px; +} + +.search_subreddit_left img { + width: 35px; + height: 35px; + border-radius: 100%; +} + +.search_subreddit_right { + overflow: auto; } a.search_subreddit:hover { diff --git a/templates/search.html b/templates/search.html index 00e91d1..9ef869a 100644 --- a/templates/search.html +++ b/templates/search.html @@ -34,12 +34,15 @@