Add Sub Member Count & Fix Icons

This commit is contained in:
spikecodes 2020-11-22 16:43:23 -08:00
parent 7e79a0b712
commit 1a33582966
2 changed files with 14 additions and 11 deletions

View File

@ -13,7 +13,7 @@ struct SubredditTemplate {
sub: Subreddit,
posts: Vec<Post>,
sort: String,
ends: (String, String),
ends: (String, String)
}
// SERVICES
@ -60,7 +60,7 @@ pub async fn render(sub_name: String, sort: Option<String>, ends: (Option<String
sub: sub,
posts: items.0,
sort: sorting,
ends: (before, items.1),
ends: (before, items.1)
}
.render()
.unwrap();
@ -84,15 +84,16 @@ async fn subreddit(sub: &String) -> Result<Subreddit, &'static str> {
// Otherwise, grab the JSON output from the request
let res = req.unwrap();
let icon: String = String::from(res["data"]["community_icon"].as_str().unwrap()); //val(&data, "community_icon");
let icon_split: std::str::Split<&str> = icon.split("?");
let icon_parts: Vec<&str> = icon_split.collect();
let members = res["data"]["subscribers"].as_u64().unwrap_or(0);
let active = res["data"]["accounts_active"].as_u64().unwrap_or(0);
let sub = Subreddit {
name: val(&res, "display_name").await,
title: val(&res, "title").await,
description: val(&res, "public_description").await,
icon: String::from(icon_parts[0]),
icon: val(&res, "icon_img").await,
members: if members > 1000 { format!("{}k", members / 1000) } else { members.to_string() },
active: if active > 1000 { format!("{}k", active / 1000) } else { active.to_string() },
};
Ok(sub)

View File

@ -21,7 +21,7 @@ pub struct Post {
pub score: String,
pub media: String,
pub time: String,
pub flair: Flair,
pub flair: Flair
}
#[allow(dead_code)]
@ -30,7 +30,7 @@ pub struct Comment {
pub body: String,
pub author: String,
pub score: String,
pub time: String,
pub time: String
}
#[allow(dead_code)]
@ -40,7 +40,7 @@ pub struct User {
pub icon: String,
pub karma: i64,
pub banner: String,
pub description: String,
pub description: String
}
#[allow(dead_code)]
@ -50,6 +50,8 @@ pub struct Subreddit {
pub title: String,
pub description: String,
pub icon: String,
pub members: String,
pub active: String
}
// Parser for query params, used in sorting (eg. /r/rust/?sort=hot)
@ -57,14 +59,14 @@ pub struct Subreddit {
pub struct Params {
pub sort: Option<String>,
pub after: Option<String>,
pub before: Option<String>,
pub before: Option<String>
}
// Error template
#[derive(askama::Template)]
#[template(path = "error.html", escape = "none")]
pub struct ErrorTemplate {
pub message: String,
pub message: String
}
//