diff --git a/src/post.rs b/src/post.rs index f84d424..f6199d1 100644 --- a/src/post.rs +++ b/src/post.rs @@ -1,5 +1,5 @@ // CRATES -use crate::utils::{cookie, error, format_num, format_url, media, param, prefs, request, rewrite_url, val, Comment, Flags, Flair, Post, Preferences}; +use crate::utils::{cookie, error, format_num, format_url, media, parse_rich_flair, param, prefs, request, rewrite_url, val, Comment, Flags, Flair, Post, Preferences}; use actix_web::{HttpRequest, HttpResponse}; use async_recursion::async_recursion; @@ -82,25 +82,25 @@ async fn parse_post(json: &serde_json::Value) -> Post { community: val(post, "subreddit"), body: rewrite_url(&val(post, "selftext_html")), author: val(post, "author"), - author_flair: Flair( - val(post, "author_flair_text"), - val(post, "author_flair_background_color"), - val(post, "author_flair_text_color"), - ), + author_flair: Flair{ + flair_parts: parse_rich_flair(post["data"]["author_flair_richtext"].as_array().unwrap()), + background_color: val(post, "author_flair_background_color"), + foreground_color: val(post, "author_flair_text_color"), + }, permalink: val(post, "permalink"), score: format_num(score), upvote_ratio: ratio as i64, post_type, thumbnail: format_url(val(post, "thumbnail").as_str()), - flair: Flair( - val(post, "link_flair_text"), - val(post, "link_flair_background_color"), - if val(post, "link_flair_text_color") == "dark" { + flair: Flair{ + flair_parts: parse_rich_flair(post["data"]["link_flair_richtext"].as_array().unwrap()), + background_color: val(post, "link_flair_background_color"), + foreground_color: if val(post, "link_flair_text_color") == "dark" { "black".to_string() } else { "white".to_string() }, - ), + }, flags: Flags { nsfw: post["data"]["over_18"].as_bool().unwrap_or(false), stickied: post["data"]["stickied"].as_bool().unwrap_or(false), @@ -145,11 +145,11 @@ async fn parse_comments(json: &serde_json::Value) -> Vec { score: format_num(score), time: OffsetDateTime::from_unix_timestamp(unix_time).format("%b %d %Y %H:%M UTC"), replies, - flair: Flair( - val(&comment, "author_flair_text"), - val(&comment, "author_flair_background_color"), - val(&comment, "author_flair_text_color"), - ), + flair: Flair{ + flair_parts: parse_rich_flair(comment["data"]["author_flair_richtext"].as_array().unwrap()), + background_color: val(&comment, "author_flair_background_color"), + foreground_color: val(&comment, "author_flair_text_color"), + }, }); } diff --git a/src/proxy.rs b/src/proxy.rs index 0f9dc36..c874b1e 100644 --- a/src/proxy.rs +++ b/src/proxy.rs @@ -8,6 +8,8 @@ pub async fn handler(web::Path(b64): web::Path) -> Result // THUMBNAILS "a.thumbs.redditmedia.com", "b.thumbs.redditmedia.com", + // EMOJI + "emoji.redditmedia.com", // ICONS "styles.redditmedia.com", "www.redditstatic.com", diff --git a/src/utils.rs b/src/utils.rs index 443e6d0..b049e53 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -5,7 +5,7 @@ use actix_web::{cookie::Cookie, HttpRequest, HttpResponse, Result}; use askama::Template; use base64::encode; use regex::Regex; -use serde_json::from_str; +use serde_json::{from_str, Value}; use std::collections::HashMap; use time::OffsetDateTime; use url::Url; @@ -13,8 +13,18 @@ use url::Url; // // STRUCTS // -// Post flair with text, background color and foreground color -pub struct Flair(pub String, pub String, pub String); +// Post flair with content, background color and foreground color +pub struct Flair{ + pub flair_parts: Vec, + pub background_color: String, + pub foreground_color: String, +} + +pub struct FlairPart{ + pub flair_part_type: String, + pub value: String, +} + // Post flags with nsfw and stickied pub struct Flags { pub nsfw: bool, @@ -185,6 +195,26 @@ pub async fn media(data: &serde_json::Value) -> (String, String) { (post_type.to_string(), url) } +pub fn parse_rich_flair(rich_flair: &Vec) -> Vec { + let mut result: Vec = Vec::new(); + for part in rich_flair { + let flair_part_type = part["e"].as_str().unwrap_or_default().to_string(); + let value = if flair_part_type == "text" { + part["t"].as_str().unwrap_or_default().to_string() + + } else if flair_part_type == "emoji" { + format_url(part["u"].as_str().unwrap_or_default()) + } else { + "".to_string() + }; + result.push(FlairPart { + flair_part_type, + value, + }); + } + result +} + // // JSON PARSING // @@ -238,26 +268,26 @@ pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec
u/{{ item.author }} - {% if item.flair.0 != "" %} - {{ item.flair.0 }} - {% endif %} + {% call utils::render_flair(item.flair) %} {{ item.time }}

{{ item.body }}

@@ -50,17 +48,13 @@ r/{{ post.community }} - {% if post.author_flair.0 != "" %} - {{ post.author_flair.0 }} - {% endif %} + {% call utils::render_flair(post.author_flair) %} {{ post.time }}

{{ post.title }} - {% if post.flair.0 != "" %} - {{ post.flair.0 }} - {% endif %} + {% call utils::render_flair(post.flair) %} @@ -119,4 +113,4 @@ {%- endfor %} -{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/templates/search.html b/templates/search.html index f97a4e9..2862194 100644 --- a/templates/search.html +++ b/templates/search.html @@ -34,16 +34,12 @@ r/{{ post.community }} - {% if post.author_flair.0 != "" %} - {{ post.author_flair.0 }} - {% endif %} + {% call utils::render_flair(post.author_flair) %} {{ post.time }}

- {% if post.flair.0 != "" %} - {{ post.flair.0 }} - {% endif %} + {% call utils::render_flair(post.flair) %} {{ post.title }}

diff --git a/templates/subreddit.html b/templates/subreddit.html index fcfce7a..abfa12b 100644 --- a/templates/subreddit.html +++ b/templates/subreddit.html @@ -46,9 +46,7 @@ {{ post.time }}

- {% if post.flair.0 != "" %} - {{ post.flair.0 }} - {% endif %} + {% call utils::render_flair(post.flair) %} {{ post.title }}

diff --git a/templates/user.html b/templates/user.html index f1afd45..6725894 100644 --- a/templates/user.html +++ b/templates/user.html @@ -32,17 +32,15 @@

r/{{ post.community }} - {% if post.author_flair.0 != "" %} - {{ post.author_flair.0 }} - {% endif %} + {% call utils::render_flair(post.author_flair) %} {{ post.time }}

- {% if post.flair.0 == "Comment" %} - {% else if post.flair.0 == "" %} + {% if post.flair.background_color == "Comment" %} + {% else if post.flair.background_color == "" %} {% else %} - {{ post.flair.0 }} + {% call utils::render_flair(post.flair) %} {% endif %} {{ post.title }}

@@ -102,4 +100,4 @@
-{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/templates/utils.html b/templates/utils.html index a34d987..0a0b726 100644 --- a/templates/utils.html +++ b/templates/utils.html @@ -25,4 +25,18 @@ {% endif %} -{%- endmacro %} \ No newline at end of file +{%- endmacro %} + +{% macro render_flair(flair) -%} +{% if flair.flair_parts.len() > 0 %} + +{% for flair_part in flair.flair_parts %} +{% if flair_part.flair_part_type == "emoji" %} + +{% else if flair_part.flair_part_type == "text" %} + {{ flair_part.value }} +{% endif %} +{% endfor %} + +{% endif %} +{%- endmacro %}