diff --git a/src/subreddit.rs b/src/subreddit.rs index 4630074..88ce5de 100644 --- a/src/subreddit.rs +++ b/src/subreddit.rs @@ -99,8 +99,27 @@ pub async fn subscriptions(req: Request) -> Result, String> let mut sub_list = Preferences::new(req).subscriptions; + // Retrieve list of posts for these subreddits to extract display names + let display = json(format!("/r/{}/hot.json?raw_json=1", sub)).await?; + let display_lookup: Vec<(String, &str)> = display["data"]["children"].as_array().unwrap().iter().map(|post| { + let display_name = post["data"]["subreddit"].as_str().unwrap(); + (display_name.to_lowercase(), display_name) + }).collect(); + // Find each subreddit name (separated by '+') in sub parameter for part in sub.split('+') { + // Retrieve display name for the subreddit + let display; + let part = if let Some(&(_, display)) = display_lookup.iter().find(|x| x.0 == part.to_lowercase()) { + // This is already known, doesn't require seperate request + display + } else { + // This subreddit display name isn't known, retrieve it + let path: String = format!("/r/{}/about.json?raw_json=1", part); + display = json(path).await?; + display["data"]["display_name"].as_str().ok_or_else(|| "Failed to query subreddit name".to_string())? + }; + // Modify sub list based on action if action.contains(&"subscribe".to_string()) && !sub_list.contains(&part.to_owned()) { // Add each sub name to the subscribed list diff --git a/templates/utils.html b/templates/utils.html index f9c41af..d7f43de 100644 --- a/templates/utils.html +++ b/templates/utils.html @@ -74,7 +74,8 @@ {% if post.flair.flair_parts.len() > 0 %} {% call render_flair(post.flair.flair_parts) %} + style="color:{{ post.flair.foreground_color }}; background:{{ post.flair.background_color }};" + dir="ltr">{% call render_flair(post.flair.flair_parts) %} {% endif %} {{ post.title }}{% if post.flags.nsfw %} NSFW{% endif %}