diff --git a/src/post.rs b/src/post.rs index 397f1ac..7308e01 100644 --- a/src/post.rs +++ b/src/post.rs @@ -90,6 +90,7 @@ async fn parse_post(json: &serde_json::Value) -> Post { post["data"]["author_flair_richtext"].as_array(), post["data"]["author_flair_text"].as_str(), ), + text: val(post, "link_flair_text"), background_color: val(post, "author_flair_background_color"), foreground_color: val(post, "author_flair_text_color"), }, @@ -112,6 +113,7 @@ async fn parse_post(json: &serde_json::Value) -> Post { post["data"]["link_flair_richtext"].as_array(), post["data"]["link_flair_text"].as_str(), ), + text: val(post, "link_flair_text"), background_color: val(post, "link_flair_background_color"), foreground_color: if val(post, "link_flair_text_color") == "dark" { "black".to_string() @@ -186,6 +188,7 @@ async fn parse_comments(json: &serde_json::Value, post_link: &str, post_author: data["author_flair_richtext"].as_array(), data["author_flair_text"].as_str(), ), + text: val(&comment, "link_flair_text"), background_color: val(&comment, "author_flair_background_color"), foreground_color: val(&comment, "author_flair_text_color"), }, diff --git a/src/search.rs b/src/search.rs index 247a1ac..1771988 100644 --- a/src/search.rs +++ b/src/search.rs @@ -36,6 +36,7 @@ pub async fn find(req: Request<()>) -> tide::Result { let nsfw_results = if cookie(&req, "show_nsfw") == "on" { "&include_over_18=on" } else { "" }; let path = format!("{}.json?{}{}", req.url().path(), req.url().query().unwrap_or_default(), nsfw_results); let sub = req.param("sub").unwrap_or("").to_string(); + let query = param(&path, "q"); let sort = if param(&path, "sort").is_empty() { "relevance".to_string() @@ -44,7 +45,7 @@ pub async fn find(req: Request<()>) -> tide::Result { }; let subreddits = if param(&path, "restrict_sr").is_empty() { - search_subreddits(param(&path, "q")).await + search_subreddits(&query).await } else { Vec::new() }; @@ -55,7 +56,7 @@ pub async fn find(req: Request<()>) -> tide::Result { subreddits, sub, params: SearchParams { - q: param(&path, "q"), + q: query.replace('"', """), sort, t: param(&path, "t"), before: param(&path, "after"), @@ -68,7 +69,7 @@ pub async fn find(req: Request<()>) -> tide::Result { } } -async fn search_subreddits(q: String) -> Vec { +async fn search_subreddits(q: &str) -> Vec { let subreddit_search_path = format!("/subreddits/search.json?q={}&limit=3", q.replace(' ', "+")); // Send a request to the url diff --git a/src/subreddit.rs b/src/subreddit.rs index 473ec29..69114a8 100644 --- a/src/subreddit.rs +++ b/src/subreddit.rs @@ -102,7 +102,7 @@ pub async fn subscriptions(req: Request<()>) -> tide::Result { // Redirect back to subreddit // check for redirect parameter if unsubscribing from outside sidebar - let redirect_path = param(format!("/?{}", query).as_str(), "redirect"); + let redirect_path = param(&format!("/?{}", query), "redirect"); let path = if !redirect_path.is_empty() { format!("/{}/", redirect_path) } else { @@ -166,7 +166,7 @@ async fn subreddit(sub: &str) -> Result { title: val(&res, "title"), description: val(&res, "public_description"), info: rewrite_urls(&val(&res, "description_html").replace("\\", "")), - icon: format_url(icon.as_str()), + icon: format_url(&icon), members: format_num(members), active: format_num(active), wiki: res["data"]["wiki_enabled"].as_bool().unwrap_or_default(), diff --git a/src/user.rs b/src/user.rs index 0e32013..223e96f 100644 --- a/src/user.rs +++ b/src/user.rs @@ -64,7 +64,7 @@ async fn user(name: &str) -> Result { Ok(User { name: name.to_string(), title: about("title"), - icon: format_url(about("icon_img").as_str()), + icon: format_url(&about("icon_img")), karma: res["data"]["total_karma"].as_i64().unwrap_or(0), created: OffsetDateTime::from_unix_timestamp(created).format("%b %d '%y"), banner: about("banner_img"), diff --git a/src/utils.rs b/src/utils.rs index a052420..ea1b831 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -16,6 +16,7 @@ use time::{Duration, OffsetDateTime}; // Post flair with content, background color and foreground color pub struct Flair { pub flair_parts: Vec, + pub text: String, pub background_color: String, pub foreground_color: String, } @@ -428,6 +429,7 @@ pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec Result<(Vec {{ post.title }} {% if post.flair.flair_parts.len() > 0 %} - {% call utils::render_flair(post.flair.flair_parts) %} + + {% call utils::render_flair(post.flair.flair_parts) %} + {% endif %} {% if post.flags.nsfw %} NSFW{% endif %}

diff --git a/templates/utils.html b/templates/utils.html index afbf388..2ba3bf4 100644 --- a/templates/utils.html +++ b/templates/utils.html @@ -33,11 +33,11 @@ {%- endmacro %} -{% macro render_flair(flair) -%} -{% for flair_part in flair %} - {% if flair_part.flair_part_type == "emoji" %} - {% else if flair_part.flair_part_type == "text" && !flair_part.value.is_empty() %}{{ flair_part.value }}{% endif %} -{% endfor %} +{% macro render_flair(flair_parts) -%} + {% for flair_part in flair_parts %} + {% if flair_part.flair_part_type == "emoji" %} + {% else if flair_part.flair_part_type == "text" && !flair_part.value.is_empty() %}{{ flair_part.value }}{% endif %} + {% endfor %} {%- endmacro %} {% macro sub_list(current) -%} @@ -69,7 +69,9 @@

{% if post.flair.flair_parts.len() > 0 %} - {% call render_flair(post.flair.flair_parts) %} + + {% call render_flair(post.flair.flair_parts) %} + {% endif %} {{ post.title }}{% if post.flags.nsfw %} NSFW{% endif %}

@@ -87,9 +89,9 @@ {% else if (prefs.layout.is_empty() || prefs.layout == "card") && post.post_type == "gif" %} - + {% else if (prefs.layout.is_empty() || prefs.layout == "card") && post.post_type == "video" %} - + {% else if post.post_type != "self" %} {% if post.thumbnail.url.is_empty() %}