Fix Subreddit Icons
This commit is contained in:
parent
44d44a529c
commit
280e16bd7f
@ -80,13 +80,13 @@ async fn media(data: &serde_json::Value) -> (String, String) {
|
|||||||
let post_type: &str;
|
let post_type: &str;
|
||||||
let url = if !data["preview"]["reddit_video_preview"]["fallback_url"].is_null() {
|
let url = if !data["preview"]["reddit_video_preview"]["fallback_url"].is_null() {
|
||||||
post_type = "video";
|
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() {
|
} else if !data["secure_media"]["reddit_video"]["fallback_url"].is_null() {
|
||||||
post_type = "video";
|
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" {
|
} else if data["post_hint"].as_str().unwrap_or("") == "image" {
|
||||||
post_type = "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 {
|
} else {
|
||||||
post_type = "link";
|
post_type = "link";
|
||||||
data["url"].as_str().unwrap().to_string()
|
data["url"].as_str().unwrap().to_string()
|
||||||
|
@ -55,15 +55,9 @@ pub async fn render(sub_name: String, sort: Option<String>, ends: (Option<String
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
Ok(HttpResponse::Ok().status(StatusCode::NOT_FOUND).content_type("text/html").body(s))
|
Ok(HttpResponse::Ok().status(StatusCode::NOT_FOUND).content_type("text/html").body(s))
|
||||||
} else {
|
} else {
|
||||||
let mut sub = sub_result.unwrap();
|
let sub = sub_result.unwrap();
|
||||||
let items = items_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 {
|
let s = SubredditTemplate {
|
||||||
sub: sub,
|
sub: sub,
|
||||||
posts: items.0,
|
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 members = res["data"]["subscribers"].as_u64().unwrap_or(0);
|
||||||
let active = res["data"]["accounts_active"].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 {
|
let sub = Subreddit {
|
||||||
name: val(&res, "display_name").await,
|
name: val(&res, "display_name").await,
|
||||||
title: val(&res, "title").await,
|
title: val(&res, "title").await,
|
||||||
description: val(&res, "public_description").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()),
|
members: format_num(members.try_into().unwrap()),
|
||||||
active: format_num(active.try_into().unwrap()),
|
active: format_num(active.try_into().unwrap()),
|
||||||
};
|
};
|
||||||
|
@ -65,7 +65,7 @@ async fn user(name: &String) -> Result<User, &'static str> {
|
|||||||
// Parse the JSON output into a User struct
|
// Parse the JSON output into a User struct
|
||||||
Ok(User {
|
Ok(User {
|
||||||
name: name.to_string(),
|
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(),
|
karma: res["data"]["total_karma"].as_i64().unwrap(),
|
||||||
banner: nested_val(&res, "subreddit", "banner_img").await,
|
banner: nested_val(&res, "subreddit", "banner_img").await,
|
||||||
description: nested_val(&res, "subreddit", "public_description").await,
|
description: nested_val(&res, "subreddit", "public_description").await,
|
||||||
|
@ -79,7 +79,11 @@ pub struct ErrorTemplate {
|
|||||||
// FORMATTING
|
// 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")]
|
#[cfg(feature = "proxy")]
|
||||||
return "/proxy/".to_string() + encode(url).as_str();
|
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 {
|
for post in post_list {
|
||||||
let img = if val(post, "thumbnail").await.starts_with("https:/") {
|
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 {
|
} else {
|
||||||
String::new()
|
String::new()
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user