Optimize Sequencing
This commit is contained in:
10
src/post.rs
10
src/post.rs
@ -47,13 +47,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().to_string()).await
|
||||
format_url(data["preview"]["reddit_video_preview"]["fallback_url"].as_str().unwrap().to_string())
|
||||
} 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().to_string()).await
|
||||
format_url(data["secure_media"]["reddit_video"]["fallback_url"].as_str().unwrap().to_string())
|
||||
} else if data["post_hint"].as_str().unwrap_or("") == "image" {
|
||||
post_type = "image";
|
||||
format_url(data["preview"]["images"][0]["source"]["url"].as_str().unwrap().to_string()).await
|
||||
format_url(data["preview"]["images"][0]["source"]["url"].as_str().unwrap().to_string())
|
||||
} else {
|
||||
post_type = "link";
|
||||
data["url"].as_str().unwrap().to_string()
|
||||
@ -79,7 +79,7 @@ async fn parse_post(json: &serde_json::Value) -> Result<Post, &'static str> {
|
||||
let post = Post {
|
||||
title: val(post_data, "title"),
|
||||
community: val(post_data, "subreddit"),
|
||||
body: rewrite_url(&val(post_data, "selftext_html")).await,
|
||||
body: rewrite_url(&val(post_data, "selftext_html")),
|
||||
author: val(post_data, "author"),
|
||||
author_flair: Flair(
|
||||
val(post_data, "author_flair_text"),
|
||||
@ -125,7 +125,7 @@ async fn parse_comments(json: &serde_json::Value) -> Result<Vec<Comment>, &'stat
|
||||
}
|
||||
|
||||
let score = comment["data"]["score"].as_i64().unwrap_or(0);
|
||||
let body = rewrite_url(&val(comment, "body_html")).await;
|
||||
let body = rewrite_url(&val(comment, "body_html"));
|
||||
|
||||
let replies: Vec<Comment> = if comment["data"]["replies"].is_object() {
|
||||
parse_comments(&comment["data"]["replies"]).await.unwrap_or_default()
|
||||
|
@ -58,7 +58,7 @@ pub async fn wiki(req: HttpRequest) -> Result<HttpResponse> {
|
||||
Ok(res) => {
|
||||
let s = WikiTemplate {
|
||||
sub: sub.to_string(),
|
||||
wiki: rewrite_url(res["data"]["content_html"].as_str().unwrap()).await,
|
||||
wiki: rewrite_url(res["data"]["content_html"].as_str().unwrap()),
|
||||
page: page.to_string(),
|
||||
}
|
||||
.render()
|
||||
@ -90,8 +90,8 @@ async fn subreddit(sub: &str) -> Result<Subreddit, &'static str> {
|
||||
name: val(&res, "display_name"),
|
||||
title: val(&res, "title"),
|
||||
description: val(&res, "public_description"),
|
||||
info: rewrite_url(&val(&res, "description_html").replace("\\", "")).await,
|
||||
icon: format_url(icon).await,
|
||||
info: rewrite_url(&val(&res, "description_html").replace("\\", "")),
|
||||
icon: format_url(icon),
|
||||
members: format_num(members),
|
||||
active: format_num(active),
|
||||
wiki: res["data"]["wiki_enabled"].as_bool().unwrap_or_default(),
|
||||
|
@ -67,7 +67,7 @@ async fn user(name: &str) -> Result<User, &'static str> {
|
||||
Ok(User {
|
||||
name: name.to_string(),
|
||||
title: nested_val(&res, "subreddit", "title"),
|
||||
icon: format_url(nested_val(&res, "subreddit", "icon_img")).await,
|
||||
icon: format_url(nested_val(&res, "subreddit", "icon_img")),
|
||||
karma: res["data"]["total_karma"].as_i64().unwrap(),
|
||||
created: Utc.timestamp(created, 0).format("%b %e, %Y").to_string(),
|
||||
banner: nested_val(&res, "subreddit", "banner_img"),
|
||||
|
@ -103,7 +103,7 @@ pub fn param(path: &str, value: &str) -> String {
|
||||
}
|
||||
|
||||
// Direct urls to proxy if proxy is enabled
|
||||
pub async fn format_url(url: String) -> String {
|
||||
pub fn format_url(url: String) -> String {
|
||||
if url.is_empty() {
|
||||
return String::new();
|
||||
};
|
||||
@ -116,7 +116,7 @@ pub async fn format_url(url: String) -> String {
|
||||
}
|
||||
|
||||
// Rewrite Reddit links to Libreddit in body of text
|
||||
pub async fn rewrite_url(text: &str) -> String {
|
||||
pub fn rewrite_url(text: &str) -> String {
|
||||
let re = Regex::new(r#"href="(https://|http://|)(www.|)(reddit).(com)/"#).unwrap();
|
||||
re.replace_all(text, r#"href="/"#).to_string()
|
||||
}
|
||||
@ -171,7 +171,7 @@ pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec<Post
|
||||
|
||||
for post in post_list {
|
||||
let img = if val(post, "thumbnail").starts_with("https:/") {
|
||||
format_url(val(post, "thumbnail")).await
|
||||
format_url(val(post, "thumbnail"))
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
@ -182,7 +182,7 @@ pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec<Post
|
||||
posts.push(Post {
|
||||
title: if title.is_empty() { fallback_title.to_owned() } else { title },
|
||||
community: val(post, "subreddit"),
|
||||
body: rewrite_url(&val(post, "body_html")).await,
|
||||
body: rewrite_url(&val(post, "body_html")),
|
||||
author: val(post, "author"),
|
||||
author_flair: Flair(
|
||||
val(post, "author_flair_text"),
|
||||
|
Reference in New Issue
Block a user