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