Fix Subreddit Icons

This commit is contained in:
spikecodes 2020-12-23 20:36:49 -08:00
parent 44d44a529c
commit 280e16bd7f
4 changed files with 19 additions and 14 deletions

View File

@ -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()

View File

@ -55,15 +55,9 @@ pub async fn render(sub_name: String, sort: Option<String>, ends: (Option<String
.unwrap();
Ok(HttpResponse::Ok().status(StatusCode::NOT_FOUND).content_type("text/html").body(s))
} else {
let mut sub = sub_result.unwrap();
let sub = sub_result.unwrap();
let items = items_result.unwrap();
sub.icon = if sub.icon != "" {
format!(r#"<img class="subreddit_icon" src="{}">"#, sub.icon)
} else {
String::new()
};
let s = SubredditTemplate {
sub: sub,
posts: items.0,
@ -95,11 +89,18 @@ async fn subreddit(sub: &String) -> Result<Subreddit, &'static str> {
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::<Vec<&str>>()[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()),
};

View File

@ -65,7 +65,7 @@ async fn user(name: &String) -> Result<User, &'static str> {
// 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,

View File

@ -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<Pos
for post in post_list {
let img = if val(post, "thumbnail").await.starts_with("https:/") {
format_url(val(post, "thumbnail").await.as_str()).await
format_url(val(post, "thumbnail").await).await
} else {
String::new()
};