redlib/src/subreddit.rs

113 lines
3.4 KiB
Rust
Raw Normal View History

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