2020-10-26 09:25:59 +13:00
|
|
|
// CRATES
|
2021-03-12 17:15:26 +13:00
|
|
|
use crate::esc;
|
2021-05-16 08:59:42 +12:00
|
|
|
use crate::utils::{catch_random, error, format_num, format_url, param, redirect, rewrite_urls, setting, template, val, Post, Preferences, Subreddit};
|
2021-03-18 11:30:33 +13:00
|
|
|
use crate::{client::json, server::ResponseExt, RequestExt};
|
2020-10-26 09:25:59 +13:00
|
|
|
use askama::Template;
|
2021-03-18 11:30:33 +13:00
|
|
|
use cookie::Cookie;
|
|
|
|
use hyper::{Body, Request, Response};
|
2021-01-30 20:00:00 +13:00
|
|
|
use time::{Duration, OffsetDateTime};
|
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),
|
2021-01-09 14:35:04 +13:00
|
|
|
prefs: Preferences,
|
2021-05-10 13:25:52 +12:00
|
|
|
url: String,
|
2020-10-26 09:25:59 +13:00
|
|
|
}
|
|
|
|
|
2021-01-02 19:21:43 +13:00
|
|
|
#[derive(Template)]
|
|
|
|
#[template(path = "wiki.html", escape = "none")]
|
|
|
|
struct WikiTemplate {
|
|
|
|
sub: String,
|
|
|
|
wiki: String,
|
2021-01-03 07:58:21 +13:00
|
|
|
page: String,
|
2021-01-11 10:08:36 +13:00
|
|
|
prefs: Preferences,
|
2021-01-02 19:21:43 +13:00
|
|
|
}
|
|
|
|
|
2020-11-20 10:49:32 +13:00
|
|
|
// SERVICES
|
2021-03-18 11:30:33 +13:00
|
|
|
pub async fn community(req: Request<Body>) -> Result<Response<Body>, String> {
|
2021-02-10 06:38:52 +13:00
|
|
|
// Build Reddit API path
|
2021-05-16 08:59:42 +12:00
|
|
|
let subscribed = setting(&req, "subscriptions");
|
|
|
|
let front_page = setting(&req, "front_page");
|
2021-03-27 16:00:47 +13:00
|
|
|
let post_sort = req.cookie("post_sort").map_or_else(|| "hot".to_string(), |c| c.value().to_string());
|
|
|
|
let sort = req.param("sort").unwrap_or_else(|| req.param("id").unwrap_or(post_sort));
|
2021-01-31 17:18:57 +13:00
|
|
|
|
2021-04-07 05:23:05 +12:00
|
|
|
let sub = req.param("sub").unwrap_or(if front_page == "default" || front_page.is_empty() {
|
|
|
|
if subscribed.is_empty() {
|
|
|
|
"popular".to_string()
|
2021-01-31 17:18:57 +13:00
|
|
|
} else {
|
2021-04-07 05:23:05 +12:00
|
|
|
subscribed.to_owned()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
front_page.to_owned()
|
|
|
|
});
|
|
|
|
|
2021-05-10 03:40:49 +12:00
|
|
|
// Handle random subreddits
|
|
|
|
if let Ok(random) = catch_random(&sub, "").await {
|
|
|
|
return Ok(random);
|
|
|
|
}
|
|
|
|
|
2021-04-07 05:23:05 +12:00
|
|
|
if req.param("sub").is_some() && sub.starts_with("u_") {
|
|
|
|
return Ok(redirect(["/user/", &sub[2..]].concat()));
|
|
|
|
}
|
2021-01-31 17:18:57 +13:00
|
|
|
|
2021-03-18 11:30:33 +13:00
|
|
|
let path = format!("/r/{}/{}.json?{}&raw_json=1", sub, sort, req.uri().query().unwrap_or_default());
|
2021-01-01 12:54:13 +13:00
|
|
|
|
2021-02-25 18:29:23 +13:00
|
|
|
match Post::fetch(&path, String::new()).await {
|
2021-01-08 05:38:05 +13:00
|
|
|
Ok((posts, after)) => {
|
2021-01-16 12:05:55 +13:00
|
|
|
// If you can get subreddit posts, also request subreddit metadata
|
2021-01-31 17:18:57 +13:00
|
|
|
let sub = if !sub.contains('+') && sub != subscribed && sub != "popular" && sub != "all" {
|
|
|
|
// Regular subreddit
|
2021-01-31 15:10:38 +13:00
|
|
|
subreddit(&sub).await.unwrap_or_default()
|
2021-01-31 17:18:57 +13:00
|
|
|
} else if sub == subscribed {
|
2021-01-31 17:24:09 +13:00
|
|
|
// Subscription feed
|
2021-03-18 11:30:33 +13:00
|
|
|
if req.uri().path().starts_with("/r/") {
|
2021-01-31 17:18:57 +13:00
|
|
|
subreddit(&sub).await.unwrap_or_default()
|
|
|
|
} else {
|
|
|
|
Subreddit::default()
|
|
|
|
}
|
2021-01-31 17:24:09 +13:00
|
|
|
} else if sub.contains('+') {
|
|
|
|
// Multireddit
|
|
|
|
Subreddit {
|
|
|
|
name: sub,
|
|
|
|
..Subreddit::default()
|
|
|
|
}
|
2021-01-16 12:05:55 +13:00
|
|
|
} else {
|
|
|
|
Subreddit::default()
|
|
|
|
};
|
|
|
|
|
2021-05-10 13:25:52 +12:00
|
|
|
let url = String::from(req.uri().path_and_query().map_or("", |val| val.as_str()));
|
|
|
|
|
2021-02-10 06:38:52 +13:00
|
|
|
template(SubredditTemplate {
|
2021-01-08 05:38:05 +13:00
|
|
|
sub,
|
|
|
|
posts,
|
2021-01-02 12:28:13 +13:00
|
|
|
sort: (sort, param(&path, "t")),
|
2021-01-07 18:27:24 +13:00
|
|
|
ends: (param(&path, "after"), after),
|
2021-02-25 18:29:23 +13:00
|
|
|
prefs: Preferences::new(req),
|
2021-05-10 13:25:52 +12:00
|
|
|
url,
|
2021-02-10 06:38:52 +13:00
|
|
|
})
|
2021-01-02 19:21:43 +13:00
|
|
|
}
|
2021-02-23 13:43:32 +13:00
|
|
|
Err(msg) => match msg.as_str() {
|
|
|
|
"quarantined" => error(req, format!("r/{} has been quarantined by Reddit", sub)).await,
|
|
|
|
"private" => error(req, format!("r/{} is a private community", sub)).await,
|
|
|
|
"banned" => error(req, format!("r/{} has been banned from Reddit", sub)).await,
|
|
|
|
_ => error(req, msg).await,
|
|
|
|
},
|
2021-01-02 19:21:43 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-30 20:18:53 +13:00
|
|
|
// Sub or unsub by setting subscription cookie using response "Set-Cookie" header
|
2021-03-18 11:30:33 +13:00
|
|
|
pub async fn subscriptions(req: Request<Body>) -> Result<Response<Body>, String> {
|
2021-03-27 16:00:47 +13:00
|
|
|
let sub = req.param("sub").unwrap_or_default();
|
2021-05-10 03:40:49 +12:00
|
|
|
// Handle random subreddits
|
|
|
|
if sub == "random" || sub == "randnsfw" {
|
|
|
|
return Err("Can't subscribe to random subreddit!".to_string());
|
|
|
|
}
|
|
|
|
|
2021-03-18 11:30:33 +13:00
|
|
|
let query = req.uri().query().unwrap_or_default().to_string();
|
|
|
|
let action: Vec<String> = req.uri().path().split('/').map(String::from).collect();
|
2021-01-30 20:00:00 +13:00
|
|
|
|
2021-02-25 18:29:23 +13:00
|
|
|
let mut sub_list = Preferences::new(req).subscriptions;
|
2021-01-30 20:00:00 +13:00
|
|
|
|
2021-05-07 07:11:25 +12:00
|
|
|
// Retrieve list of posts for these subreddits to extract display names
|
|
|
|
let display = json(format!("/r/{}/hot.json?raw_json=1", sub)).await?;
|
2021-05-07 07:31:59 +12:00
|
|
|
let display_lookup: Vec<(String, &str)> = display["data"]["children"]
|
|
|
|
.as_array()
|
|
|
|
.unwrap()
|
|
|
|
.iter()
|
|
|
|
.map(|post| {
|
|
|
|
let display_name = post["data"]["subreddit"].as_str().unwrap();
|
|
|
|
(display_name.to_lowercase(), display_name)
|
|
|
|
})
|
|
|
|
.collect();
|
2021-05-07 07:11:25 +12:00
|
|
|
|
2021-02-10 18:56:38 +13:00
|
|
|
// Find each subreddit name (separated by '+') in sub parameter
|
2021-02-13 17:47:54 +13:00
|
|
|
for part in sub.split('+') {
|
2021-05-07 07:11:25 +12:00
|
|
|
// Retrieve display name for the subreddit
|
|
|
|
let display;
|
|
|
|
let part = if let Some(&(_, display)) = display_lookup.iter().find(|x| x.0 == part.to_lowercase()) {
|
|
|
|
// This is already known, doesn't require seperate request
|
|
|
|
display
|
|
|
|
} else {
|
|
|
|
// This subreddit display name isn't known, retrieve it
|
|
|
|
let path: String = format!("/r/{}/about.json?raw_json=1", part);
|
|
|
|
display = json(path).await?;
|
|
|
|
display["data"]["display_name"].as_str().ok_or_else(|| "Failed to query subreddit name".to_string())?
|
|
|
|
};
|
|
|
|
|
2021-02-13 17:47:54 +13:00
|
|
|
// Modify sub list based on action
|
|
|
|
if action.contains(&"subscribe".to_string()) && !sub_list.contains(&part.to_owned()) {
|
|
|
|
// Add each sub name to the subscribed list
|
|
|
|
sub_list.push(part.to_owned());
|
|
|
|
// Reorder sub names alphabettically
|
|
|
|
sub_list.sort_by_key(|a| a.to_lowercase())
|
|
|
|
} else if action.contains(&"unsubscribe".to_string()) {
|
|
|
|
// Remove sub name from subscribed list
|
|
|
|
sub_list.retain(|s| s != part);
|
|
|
|
}
|
2021-01-30 20:00:00 +13:00
|
|
|
}
|
|
|
|
|
2021-02-10 06:38:52 +13:00
|
|
|
// Redirect back to subreddit
|
|
|
|
// check for redirect parameter if unsubscribing from outside sidebar
|
2021-02-21 10:59:16 +13:00
|
|
|
let redirect_path = param(&format!("/?{}", query), "redirect");
|
2021-03-09 15:49:06 +13:00
|
|
|
let path = if redirect_path.is_empty() {
|
2021-02-10 06:38:52 +13:00
|
|
|
format!("/r/{}", sub)
|
2021-03-09 15:49:06 +13:00
|
|
|
} else {
|
|
|
|
format!("/{}/", redirect_path)
|
2021-02-10 06:38:52 +13:00
|
|
|
};
|
|
|
|
|
2021-02-14 12:02:38 +13:00
|
|
|
let mut res = redirect(path);
|
2021-02-10 06:38:52 +13:00
|
|
|
|
2021-01-30 20:18:53 +13:00
|
|
|
// Delete cookie if empty, else set
|
|
|
|
if sub_list.is_empty() {
|
2021-03-18 11:30:33 +13:00
|
|
|
res.remove_cookie("subscriptions".to_string());
|
2021-01-30 20:18:53 +13:00
|
|
|
} else {
|
2021-02-10 06:38:52 +13:00
|
|
|
res.insert_cookie(
|
2021-01-31 17:18:57 +13:00
|
|
|
Cookie::build("subscriptions", sub_list.join("+"))
|
|
|
|
.path("/")
|
|
|
|
.http_only(true)
|
|
|
|
.expires(OffsetDateTime::now_utc() + Duration::weeks(52))
|
|
|
|
.finish(),
|
|
|
|
);
|
2021-01-30 20:18:53 +13:00
|
|
|
}
|
2021-01-30 20:00:00 +13:00
|
|
|
|
2021-02-10 06:38:52 +13:00
|
|
|
Ok(res)
|
2021-01-30 20:00:00 +13:00
|
|
|
}
|
|
|
|
|
2021-03-18 11:30:33 +13:00
|
|
|
pub async fn wiki(req: Request<Body>) -> Result<Response<Body>, String> {
|
2021-03-27 16:00:47 +13:00
|
|
|
let sub = req.param("sub").unwrap_or_else(|| "reddit.com".to_string());
|
2021-05-10 03:40:49 +12:00
|
|
|
// Handle random subreddits
|
|
|
|
if let Ok(random) = catch_random(&sub, "/wiki").await {
|
|
|
|
return Ok(random);
|
|
|
|
}
|
|
|
|
|
2021-03-27 16:00:47 +13:00
|
|
|
let page = req.param("page").unwrap_or_else(|| "index".to_string());
|
2021-01-12 07:33:42 +13:00
|
|
|
let path: String = format!("/r/{}/wiki/{}.json?raw_json=1", sub, page);
|
2021-01-02 19:21:43 +13:00
|
|
|
|
2021-03-18 11:30:33 +13:00
|
|
|
match json(path).await {
|
2021-03-09 15:49:06 +13:00
|
|
|
Ok(response) => template(WikiTemplate {
|
2021-02-10 06:38:52 +13:00
|
|
|
sub,
|
2021-05-05 05:30:54 +12:00
|
|
|
wiki: rewrite_urls(response["data"]["content_html"].as_str().unwrap_or("<h3>Wiki not found</h3>")),
|
2021-02-10 06:38:52 +13:00
|
|
|
page,
|
2021-02-25 18:29:23 +13:00
|
|
|
prefs: Preferences::new(req),
|
2021-02-10 06:38:52 +13:00
|
|
|
}),
|
2021-02-21 15:36:30 +13:00
|
|
|
Err(msg) => error(req, msg).await,
|
2020-11-18 13:03:28 +13:00
|
|
|
}
|
2020-10-26 09:25:59 +13:00
|
|
|
}
|
|
|
|
|
2021-03-22 15:28:05 +13:00
|
|
|
pub async fn sidebar(req: Request<Body>) -> Result<Response<Body>, String> {
|
2021-03-27 16:00:47 +13:00
|
|
|
let sub = req.param("sub").unwrap_or_else(|| "reddit.com".to_string());
|
2021-05-10 03:40:49 +12:00
|
|
|
// Handle random subreddits
|
|
|
|
if let Ok(random) = catch_random(&sub, "/about/sidebar").await {
|
|
|
|
return Ok(random);
|
|
|
|
}
|
2021-03-22 15:28:05 +13:00
|
|
|
|
|
|
|
// Build the Reddit JSON API url
|
|
|
|
let path: String = format!("/r/{}/about.json?raw_json=1", sub);
|
|
|
|
|
|
|
|
// Send a request to the url
|
|
|
|
match json(path).await {
|
|
|
|
// If success, receive JSON in response
|
|
|
|
Ok(response) => template(WikiTemplate {
|
2021-05-05 05:30:54 +12:00
|
|
|
wiki: format!(
|
|
|
|
"{}<hr><h1>Moderators</h1><br><ul>{}</ul>",
|
|
|
|
rewrite_urls(&val(&response, "description_html").replace("\\", "")),
|
|
|
|
moderators(&sub).await?.join(""),
|
|
|
|
),
|
2021-03-22 15:28:05 +13:00
|
|
|
sub,
|
|
|
|
page: "Sidebar".to_string(),
|
|
|
|
prefs: Preferences::new(req),
|
|
|
|
}),
|
|
|
|
Err(msg) => error(req, msg).await,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-05 05:30:54 +12:00
|
|
|
pub async fn moderators(sub: &str) -> Result<Vec<String>, String> {
|
|
|
|
// Retrieve and format the html for the moderators list
|
|
|
|
Ok(
|
|
|
|
moderators_list(sub)
|
|
|
|
.await?
|
|
|
|
.iter()
|
|
|
|
.map(|m| format!("<li><a style=\"color: var(--accent)\" href=\"/u/{name}\">{name}</a></li>", name = m))
|
|
|
|
.collect(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn moderators_list(sub: &str) -> Result<Vec<String>, String> {
|
|
|
|
// Build the moderator list URL
|
|
|
|
let path: String = format!("/r/{}/about/moderators.json?raw_json=1", sub);
|
|
|
|
|
|
|
|
// Retrieve response
|
|
|
|
let response = json(path).await?["data"]["children"].clone();
|
2021-05-16 08:59:42 +12:00
|
|
|
Ok(
|
2021-05-05 05:30:54 +12:00
|
|
|
// Traverse json tree and format into list of strings
|
|
|
|
response
|
2021-05-16 08:59:42 +12:00
|
|
|
.as_array()
|
|
|
|
.unwrap_or(&Vec::new())
|
2021-05-05 05:30:54 +12:00
|
|
|
.iter()
|
2021-05-16 08:59:42 +12:00
|
|
|
.filter_map(|moderator| {
|
|
|
|
let name = moderator["name"].as_str().unwrap_or_default();
|
|
|
|
if name.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(name.to_string())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
)
|
2021-05-05 05:30:54 +12:00
|
|
|
}
|
|
|
|
|
2020-10-26 09:25:59 +13:00
|
|
|
// SUBREDDIT
|
2021-01-15 06:53:54 +13:00
|
|
|
async fn subreddit(sub: &str) -> Result<Subreddit, String> {
|
2020-11-19 15:50:59 +13:00
|
|
|
// Build the Reddit JSON API url
|
2021-01-12 13:44:31 +13:00
|
|
|
let path: String = format!("/r/{}/about.json?raw_json=1", sub);
|
2020-10-26 09:25:59 +13:00
|
|
|
|
2021-01-02 12:28:13 +13:00
|
|
|
// Send a request to the url
|
2021-03-18 11:30:33 +13:00
|
|
|
match json(path).await {
|
2021-01-02 12:28:13 +13:00
|
|
|
// If success, receive JSON in response
|
2021-01-02 19:21:43 +13:00
|
|
|
Ok(res) => {
|
|
|
|
// Metadata regarding the subreddit
|
2021-03-10 04:22:17 +13:00
|
|
|
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 17:42:18 +13:00
|
|
|
|
2021-01-02 19:21:43 +13:00
|
|
|
// Fetch subreddit icon either from the community_icon or icon_img value
|
2021-05-10 03:40:49 +12:00
|
|
|
let community_icon: &str = res["data"]["community_icon"].as_str().unwrap();
|
2021-02-14 12:02:38 +13:00
|
|
|
let icon = if community_icon.is_empty() { val(&res, "icon_img") } else { community_icon.to_string() };
|
2020-10-26 09:25:59 +13:00
|
|
|
|
2021-01-02 19:21:43 +13:00
|
|
|
let sub = Subreddit {
|
2021-03-12 17:15:26 +13:00
|
|
|
name: esc!(&res, "display_name"),
|
|
|
|
title: esc!(&res, "title"),
|
|
|
|
description: esc!(&res, "public_description"),
|
2021-02-10 18:56:38 +13:00
|
|
|
info: rewrite_urls(&val(&res, "description_html").replace("\\", "")),
|
2021-05-05 05:30:54 +12:00
|
|
|
moderators: moderators_list(sub).await?,
|
2021-02-21 10:59:16 +13:00
|
|
|
icon: format_url(&icon),
|
2021-01-02 19:21:43 +13:00
|
|
|
members: format_num(members),
|
|
|
|
active: format_num(active),
|
|
|
|
wiki: res["data"]["wiki_enabled"].as_bool().unwrap_or_default(),
|
|
|
|
};
|
2020-12-24 17:36:49 +13:00
|
|
|
|
2021-01-02 19:21:43 +13:00
|
|
|
Ok(sub)
|
|
|
|
}
|
|
|
|
// If the Reddit API returns an error, exit this function
|
|
|
|
Err(msg) => return Err(msg),
|
|
|
|
}
|
2020-11-30 15:50:29 +13:00
|
|
|
}
|