Refactor Page Titles and Add Subreddit/User Titles

This commit is contained in:
spikecodes
2020-12-31 20:21:56 -08:00
parent 93c1db502d
commit 2d77a91150
8 changed files with 35 additions and 26 deletions

View File

@ -72,7 +72,7 @@ async fn subreddit(sub: &String) -> Result<Subreddit, &'static str> {
let active = res["data"]["accounts_active"].as_u64().unwrap_or(0);
// Fetch subreddit icon either from the community_icon or icon_img value
let community_icon: &str = res["data"]["community_icon"].as_str().unwrap().split("?").collect::<Vec<&str>>()[0];
let community_icon: &str = res["data"]["community_icon"].as_str().unwrap_or("").split("?").collect::<Vec<&str>>()[0];
let icon = if community_icon.is_empty() {
val(&res, "icon_img").await
} else {
@ -85,8 +85,8 @@ async fn subreddit(sub: &String) -> Result<Subreddit, &'static str> {
description: val(&res, "public_description").await,
info: val(&res, "description_html").await.replace("\\", ""),
icon: format_url(icon).await,
members: format_num(members.try_into().unwrap()),
active: format_num(active.try_into().unwrap()),
members: format_num(members.try_into().unwrap_or(0)),
active: format_num(active.try_into().unwrap_or(0)),
};
Ok(sub)

View File

@ -56,17 +56,17 @@ pub async fn profile(req: HttpRequest) -> Result<HttpResponse> {
// USER
async fn user(name: &String) -> Result<User, &'static str> {
// Build the Reddit JSON API url
let url: String = format!("user/{}/about.json", name);
// Build the Reddit JSON API path
let path: String = format!("user/{}/about.json", name);
// Send a request to the url, receive JSON in response
let req = request(url).await;
let req = request(path).await;
// If the Reddit API returns an error, exit this function
if req.is_err() {
return Err(req.err().unwrap());
}
// Otherwise, grab the JSON output from the request
let res = req.unwrap();
@ -76,6 +76,7 @@ async fn user(name: &String) -> Result<User, &'static str> {
// Parse the JSON output into a User struct
Ok(User {
name: name.to_string(),
title: nested_val(&res, "subreddit", "title").await,
icon: format_url(nested_val(&res, "subreddit", "icon_img").await).await,
karma: res["data"]["total_karma"].as_i64().unwrap(),
created: Utc.timestamp(created, 0).format("%b %e, %Y").to_string(),

View File

@ -50,6 +50,7 @@ pub struct Comment {
// User struct containing metadata about user
pub struct User {
pub name: String,
pub title: String,
pub icon: String,
pub karma: i64,
pub created: String,
@ -194,8 +195,6 @@ pub async fn fetch_posts(path: String, fallback_title: String) -> Result<(Vec<Po
});
}
dbg!(path);
Ok((posts, res["data"]["after"].as_str().unwrap_or("").to_string()))
}