redlib/src/subreddit.rs

94 lines
2.8 KiB
Rust
Raw Normal View History

2020-10-25 13:25:59 -07:00
// CRATES
2020-12-31 15:54:13 -08:00
use crate::utils::{fetch_posts, format_num, format_url, param, request, val, ErrorTemplate, Post, Subreddit};
use actix_web::{http::StatusCode, HttpRequest, HttpResponse, Result};
2020-10-25 13:25:59 -07:00
use askama::Template;
2020-12-07 10:53:22 -08:00
use std::convert::TryInto;
2020-11-17 11:37:40 -08:00
2020-10-25 13:25:59 -07:00
// STRUCTS
#[derive(Template)]
#[template(path = "subreddit.html", escape = "none")]
struct SubredditTemplate {
sub: Subreddit,
posts: Vec<Post>,
2020-12-29 17:11:47 -08:00
sort: (String, String),
2020-11-29 18:50:29 -08:00
ends: (String, String),
2020-10-25 13:25:59 -07:00
}
2020-11-19 13:49:32 -08:00
// SERVICES
2020-12-31 15:54:13 -08:00
// web::Path(sub): web::Path<String>, params: web::Query<Params>
pub async fn page(req: HttpRequest) -> Result<HttpResponse> {
let path = format!("{}.json?{}", req.path(), req.query_string());
let sub = req.match_info().get("sub").unwrap_or("popular").to_string();
let sort = req.match_info().get("sort").unwrap_or("hot").to_string();
let sub_result = if !&sub.contains("+") && sub != "popular" {
subreddit(&sub).await
2020-12-21 08:38:24 -08:00
} else {
2020-12-28 18:42:46 -08:00
Ok(Subreddit::default())
2020-12-20 17:45:26 -08:00
};
2020-12-31 15:54:13 -08:00
let posts = fetch_posts(path.clone(), String::new()).await;
2020-10-25 13:25:59 -07:00
2020-12-31 15:54:13 -08:00
if posts.is_err() {
2020-11-19 20:42:18 -08:00
let s = ErrorTemplate {
2020-12-31 15:54:13 -08:00
message: posts.err().unwrap().to_string(),
2020-11-19 20:42:18 -08:00
}
.render()
.unwrap();
2020-11-25 13:53:30 -08:00
Ok(HttpResponse::Ok().status(StatusCode::NOT_FOUND).content_type("text/html").body(s))
2020-10-25 20:57:19 -07:00
} else {
2020-12-31 15:54:13 -08:00
let sub = sub_result.unwrap_or(Subreddit::default());
let items = posts.unwrap();
2020-11-19 20:42:18 -08:00
let s = SubredditTemplate {
sub: sub,
posts: items.0,
2020-12-31 15:54:13 -08:00
sort: (sort, param(&path, "t").await),
ends: (param(&path, "after").await, items.1),
2020-11-19 20:42:18 -08:00
}
.render()
.unwrap();
Ok(HttpResponse::Ok().content_type("text/html").body(s))
2020-11-17 16:03:28 -08:00
}
2020-10-25 13:25:59 -07:00
}
// SUBREDDIT
2020-11-19 20:42:18 -08:00
async fn subreddit(sub: &String) -> Result<Subreddit, &'static str> {
2020-11-18 18:50:59 -08:00
// Build the Reddit JSON API url
2020-12-28 18:42:46 -08:00
let url: String = format!("r/{}/about.json?raw_json=1", sub);
2020-10-25 13:25:59 -07:00
2020-11-18 18:50:59 -08:00
// Send a request to the url, receive JSON in response
2020-11-19 20:42:18 -08:00
let req = request(url).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();
2020-10-25 13:25:59 -07:00
2020-12-25 18:06:33 -08:00
// Metadata regarding the subreddit
2020-11-22 16:43:23 -08:00
let members = res["data"]["subscribers"].as_u64().unwrap_or(0);
2020-11-29 18:50:29 -08:00
let active = res["data"]["accounts_active"].as_u64().unwrap_or(0);
2020-10-25 13:25:59 -07:00
2020-12-25 18:06:33 -08:00
// Fetch subreddit icon either from the community_icon or icon_img value
let community_icon: &str = res["data"]["community_icon"].as_str().unwrap_or("").split("?").collect::<Vec<&str>>()[0];
2020-12-23 20:36:49 -08:00
let icon = if community_icon.is_empty() {
val(&res, "icon_img").await
} else {
community_icon.to_string()
};
2020-11-18 18:50:59 -08:00
let sub = Subreddit {
name: val(&res, "display_name").await,
title: val(&res, "title").await,
description: val(&res, "public_description").await,
2020-12-28 18:42:46 -08:00
info: val(&res, "description_html").await.replace("\\", ""),
2020-12-23 20:36:49 -08:00
icon: format_url(icon).await,
members: format_num(members.try_into().unwrap_or(0)),
active: format_num(active.try_into().unwrap_or(0)),
2020-11-18 18:50:59 -08:00
};
Ok(sub)
2020-11-29 18:50:29 -08:00
}