libreddit/src/subreddit.rs

106 lines
3.1 KiB
Rust
Raw Normal View History

2020-10-25 21:25:59 +01:00
// CRATES
2021-01-01 06:03:44 +01:00
use crate::utils::{error, fetch_posts, format_num, format_url, param, request, val, Post, Subreddit};
use actix_web::{HttpRequest, HttpResponse, Result};
2020-10-25 21:25:59 +01:00
use askama::Template;
2020-11-17 20:37:40 +01:00
2020-10-25 21:25:59 +01:00
// STRUCTS
#[derive(Template)]
#[template(path = "subreddit.html", escape = "none")]
struct SubredditTemplate {
sub: Subreddit,
posts: Vec<Post>,
2020-12-30 02:11:47 +01:00
sort: (String, String),
2020-11-30 03:50:29 +01:00
ends: (String, String),
2020-10-25 21:25:59 +01:00
}
2021-01-02 07:21:43 +01:00
#[derive(Template)]
#[template(path = "wiki.html", escape = "none")]
struct WikiTemplate {
sub: String,
wiki: String,
2021-01-02 07:34:25 +01:00
page: String
2021-01-02 07:21:43 +01:00
}
2020-11-19 22:49:32 +01:00
// SERVICES
2021-01-01 00:54:13 +01:00
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-01 21:33:57 +01:00
let sub_result = if !&sub.contains('+') && sub != "popular" {
2021-01-02 00:28:13 +01:00
subreddit(&sub).await.unwrap_or_default()
2020-12-21 17:38:24 +01:00
} else {
2021-01-02 00:28:13 +01:00
Subreddit::default()
2020-12-21 02:45:26 +01:00
};
2020-10-25 21:25:59 +01:00
2021-01-02 00:28:13 +01:00
match fetch_posts(&path, String::new()).await {
Ok(items) => {
let s = SubredditTemplate {
sub: sub_result,
posts: items.0,
sort: (sort, param(&path, "t")),
ends: (param(&path, "after"), items.1),
}
.render()
.unwrap();
Ok(HttpResponse::Ok().content_type("text/html").body(s))
2021-01-02 07:21:43 +01:00
}
Err(msg) => error(msg.to_string()).await,
}
}
pub async fn wiki(req: HttpRequest) -> Result<HttpResponse> {
let sub = req.match_info().get("sub").unwrap();
let page = req.match_info().get("page").unwrap_or("index");
let path: String = format!("r/{}/wiki/{}.json?raw_json=1", sub, page);
match request(&path).await {
Ok(res) => {
let s = WikiTemplate {
sub: sub.to_string(),
wiki: res["data"]["content_html"].as_str().unwrap().to_string(),
2021-01-02 07:34:25 +01:00
page: page.to_string()
2021-01-02 07:21:43 +01:00
}
.render()
.unwrap();
Ok(HttpResponse::Ok().content_type("text/html").body(s))
}
Err(msg) => error(msg.to_string()).await,
2020-11-18 01:03:28 +01:00
}
2020-10-25 21:25:59 +01:00
}
// SUBREDDIT
2021-01-01 21:33:57 +01:00
async fn subreddit(sub: &str) -> Result<Subreddit, &'static str> {
2020-11-19 03:50:59 +01:00
// Build the Reddit JSON API url
2021-01-02 00:28:13 +01:00
let path: String = format!("r/{}/about.json?raw_json=1", sub);
2020-10-25 21:25:59 +01:00
2021-01-02 00:28:13 +01:00
// Send a request to the url
match request(&path).await {
// If success, receive JSON in response
2021-01-02 07:21:43 +01:00
Ok(res) => {
// Metadata regarding the subreddit
let members: i64 = res["data"]["subscribers"].as_u64().unwrap_or_default() as i64;
let active: i64 = res["data"]["accounts_active"].as_u64().unwrap_or_default() as i64;
2020-11-20 05:42:18 +01:00
2021-01-02 07:21:43 +01: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];
let icon = if community_icon.is_empty() { val(&res, "icon_img") } else { community_icon.to_string() };
2020-10-25 21:25:59 +01:00
2021-01-02 07:21:43 +01:00
let sub = Subreddit {
name: val(&res, "display_name"),
title: val(&res, "title"),
description: val(&res, "public_description"),
info: val(&res, "description_html").replace("\\", ""),
icon: format_url(icon).await,
members: format_num(members),
active: format_num(active),
wiki: res["data"]["wiki_enabled"].as_bool().unwrap_or_default(),
};
2020-12-24 05:36:49 +01:00
2021-01-02 07:21:43 +01:00
Ok(sub)
}
// If the Reddit API returns an error, exit this function
Err(msg) => return Err(msg),
}
2020-11-30 03:50:29 +01:00
}