2020-10-26 09:25:59 +13:00
|
|
|
// CRATES
|
2021-03-12 17:15:26 +13:00
|
|
|
use crate::esc;
|
2021-11-26 17:02:04 +13:00
|
|
|
use crate::utils::{
|
|
|
|
catch_random, error, filter_posts, format_num, format_url, get_filters, 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,
|
2022-03-14 08:06:27 +13:00
|
|
|
redirect_url: String,
|
2021-11-26 17:02:04 +13:00
|
|
|
/// Whether the subreddit itself is filtered.
|
|
|
|
is_filtered: bool,
|
|
|
|
/// Whether all fetched posts are filtered (to differentiate between no posts fetched in the first place,
|
|
|
|
/// and all fetched posts being filtered).
|
|
|
|
all_posts_filtered: bool,
|
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-11-15 15:39:33 +13:00
|
|
|
url: String,
|
2021-01-02 19:21:43 +13:00
|
|
|
}
|
|
|
|
|
2021-05-17 03:53:39 +12:00
|
|
|
#[derive(Template)]
|
|
|
|
#[template(path = "wall.html", escape = "none")]
|
|
|
|
struct WallTemplate {
|
|
|
|
title: String,
|
|
|
|
sub: String,
|
|
|
|
msg: String,
|
|
|
|
prefs: Preferences,
|
|
|
|
url: String,
|
|
|
|
}
|
|
|
|
|
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-17 03:53:39 +12:00
|
|
|
let root = req.uri().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-11-26 17:02:04 +13:00
|
|
|
let sub_name = req.param("sub").unwrap_or(if front_page == "default" || front_page.is_empty() {
|
2021-04-07 05:23:05 +12:00
|
|
|
if subscribed.is_empty() {
|
|
|
|
"popular".to_string()
|
2021-01-31 17:18:57 +13:00
|
|
|
} else {
|
2021-06-12 06:03:36 +12:00
|
|
|
subscribed.clone()
|
2021-04-07 05:23:05 +12:00
|
|
|
}
|
|
|
|
} else {
|
2021-06-12 06:03:36 +12:00
|
|
|
front_page.clone()
|
2021-04-07 05:23:05 +12:00
|
|
|
});
|
2021-11-26 17:02:04 +13:00
|
|
|
let quarantined = can_access_quarantine(&req, &sub_name) || root;
|
2021-04-07 05:23:05 +12:00
|
|
|
|
2021-05-10 03:40:49 +12:00
|
|
|
// Handle random subreddits
|
2021-11-26 17:02:04 +13:00
|
|
|
if let Ok(random) = catch_random(&sub_name, "").await {
|
2021-05-10 03:40:49 +12:00
|
|
|
return Ok(random);
|
|
|
|
}
|
|
|
|
|
2021-11-26 17:02:04 +13:00
|
|
|
if req.param("sub").is_some() && sub_name.starts_with("u_") {
|
|
|
|
return Ok(redirect(["/user/", &sub_name[2..]].concat()));
|
2021-04-07 05:23:05 +12:00
|
|
|
}
|
2021-01-31 17:18:57 +13:00
|
|
|
|
2021-11-26 17:02:04 +13:00
|
|
|
// Request subreddit metadata
|
|
|
|
let sub = if !sub_name.contains('+') && sub_name != subscribed && sub_name != "popular" && sub_name != "all" {
|
|
|
|
// Regular subreddit
|
|
|
|
subreddit(&sub_name, quarantined).await.unwrap_or_default()
|
|
|
|
} else if sub_name == subscribed {
|
|
|
|
// Subscription feed
|
|
|
|
if req.uri().path().starts_with("/r/") {
|
|
|
|
subreddit(&sub_name, quarantined).await.unwrap_or_default()
|
|
|
|
} else {
|
|
|
|
Subreddit::default()
|
|
|
|
}
|
2022-03-15 16:39:39 +13:00
|
|
|
} else {
|
|
|
|
// Multireddit, all, popular
|
2021-11-26 17:02:04 +13:00
|
|
|
Subreddit {
|
|
|
|
name: sub_name.clone(),
|
|
|
|
..Subreddit::default()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let path = format!("/r/{}/{}.json?{}&raw_json=1", sub_name.clone(), sort, req.uri().query().unwrap_or_default());
|
|
|
|
let url = String::from(req.uri().path_and_query().map_or("", |val| val.as_str()));
|
2022-05-16 08:50:17 +12:00
|
|
|
let redirect_url = url[1..].replace('?', "%3F").replace('&', "%26").replace('+',"%2B");
|
2021-11-26 17:02:04 +13:00
|
|
|
let filters = get_filters(&req);
|
|
|
|
|
|
|
|
// If all requested subs are filtered, we don't need to fetch posts.
|
2021-12-27 18:18:20 +13:00
|
|
|
if sub_name.split('+').all(|s| filters.contains(s)) {
|
2021-11-26 17:02:04 +13:00
|
|
|
template(SubredditTemplate {
|
|
|
|
sub,
|
|
|
|
posts: Vec::new(),
|
|
|
|
sort: (sort, param(&path, "t").unwrap_or_default()),
|
|
|
|
ends: (param(&path, "after").unwrap_or_default(), "".to_string()),
|
|
|
|
prefs: Preferences::new(req),
|
|
|
|
url,
|
2022-03-14 08:06:27 +13:00
|
|
|
redirect_url,
|
2021-11-26 17:02:04 +13:00
|
|
|
is_filtered: true,
|
|
|
|
all_posts_filtered: false,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
match Post::fetch(&path, quarantined).await {
|
|
|
|
Ok((mut posts, after)) => {
|
|
|
|
let all_posts_filtered = filter_posts(&mut posts, &filters);
|
|
|
|
|
|
|
|
template(SubredditTemplate {
|
|
|
|
sub,
|
|
|
|
posts,
|
|
|
|
sort: (sort, param(&path, "t").unwrap_or_default()),
|
|
|
|
ends: (param(&path, "after").unwrap_or_default(), after),
|
|
|
|
prefs: Preferences::new(req),
|
|
|
|
url,
|
2022-03-14 08:06:27 +13:00
|
|
|
redirect_url,
|
2021-11-26 17:02:04 +13:00
|
|
|
is_filtered: false,
|
|
|
|
all_posts_filtered,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
Err(msg) => match msg.as_str() {
|
|
|
|
"quarantined" => quarantine(req, sub_name),
|
|
|
|
"private" => error(req, format!("r/{} is a private community", sub_name)).await,
|
|
|
|
"banned" => error(req, format!("r/{} has been banned from Reddit", sub_name)).await,
|
|
|
|
_ => error(req, msg).await,
|
|
|
|
},
|
2021-01-02 19:21:43 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-17 03:53:39 +12:00
|
|
|
pub fn quarantine(req: Request<Body>, sub: String) -> Result<Response<Body>, String> {
|
|
|
|
let wall = WallTemplate {
|
|
|
|
title: format!("r/{} is quarantined", sub),
|
|
|
|
msg: "Please click the button below to continue to this subreddit.".to_string(),
|
|
|
|
url: req.uri().to_string(),
|
|
|
|
sub,
|
|
|
|
prefs: Preferences::new(req),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(
|
|
|
|
Response::builder()
|
|
|
|
.status(403)
|
|
|
|
.header("content-type", "text/html")
|
|
|
|
.body(wall.render().unwrap_or_default().into())
|
|
|
|
.unwrap_or_default(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn add_quarantine_exception(req: Request<Body>) -> Result<Response<Body>, String> {
|
|
|
|
let subreddit = req.param("sub").ok_or("Invalid URL")?;
|
|
|
|
let redir = param(&format!("?{}", req.uri().query().unwrap_or_default()), "redir").ok_or("Invalid URL")?;
|
2021-05-21 07:24:06 +12:00
|
|
|
let mut response = redirect(redir);
|
|
|
|
response.insert_cookie(
|
2021-05-17 03:53:39 +12:00
|
|
|
Cookie::build(&format!("allow_quaran_{}", subreddit.to_lowercase()), "true")
|
|
|
|
.path("/")
|
|
|
|
.http_only(true)
|
|
|
|
.expires(cookie::Expiration::Session)
|
|
|
|
.finish(),
|
|
|
|
);
|
2021-05-21 07:24:06 +12:00
|
|
|
Ok(response)
|
2021-05-17 03:53:39 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn can_access_quarantine(req: &Request<Body>, sub: &str) -> bool {
|
|
|
|
// Determine if the subreddit can be accessed
|
2021-09-10 12:28:55 +12:00
|
|
|
setting(req, &format!("allow_quaran_{}", sub.to_lowercase())).parse().unwrap_or_default()
|
2021-05-17 03:53:39 +12:00
|
|
|
}
|
|
|
|
|
2021-11-26 17:02:04 +13:00
|
|
|
// Sub, filter, unfilter, or unsub by setting subscription cookie using response "Set-Cookie" header
|
|
|
|
pub async fn subscriptions_filters(req: Request<Body>) -> Result<Response<Body>, String> {
|
2021-03-27 16:00:47 +13:00
|
|
|
let sub = req.param("sub").unwrap_or_default();
|
2021-11-26 17:02:04 +13:00
|
|
|
let action: Vec<String> = req.uri().path().split('/').map(String::from).collect();
|
|
|
|
|
2021-05-10 03:40:49 +12:00
|
|
|
// Handle random subreddits
|
|
|
|
if sub == "random" || sub == "randnsfw" {
|
2021-11-26 17:02:04 +13:00
|
|
|
if action.contains(&"filter".to_string()) || action.contains(&"unfilter".to_string()) {
|
|
|
|
return Err("Can't filter random subreddit!".to_string());
|
|
|
|
} else {
|
|
|
|
return Err("Can't subscribe to random subreddit!".to_string());
|
|
|
|
}
|
2021-05-10 03:40:49 +12:00
|
|
|
}
|
|
|
|
|
2021-03-18 11:30:33 +13:00
|
|
|
let query = req.uri().query().unwrap_or_default().to_string();
|
2021-01-30 20:00:00 +13:00
|
|
|
|
2021-11-26 17:02:04 +13:00
|
|
|
let preferences = Preferences::new(req);
|
|
|
|
let mut sub_list = preferences.subscriptions;
|
|
|
|
let mut filters = preferences.filters;
|
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
|
2021-05-17 03:53:39 +12:00
|
|
|
let posts = json(format!("/r/{}/hot.json?raw_json=1", sub), true).await?;
|
2021-05-16 09:51:57 +12:00
|
|
|
let display_lookup: Vec<(String, &str)> = posts["data"]["children"]
|
2021-05-07 07:31:59 +12:00
|
|
|
.as_array()
|
2021-05-16 09:51:57 +12:00
|
|
|
.map(|list| {
|
|
|
|
list
|
|
|
|
.iter()
|
|
|
|
.map(|post| {
|
|
|
|
let display_name = post["data"]["subreddit"].as_str().unwrap_or_default();
|
|
|
|
(display_name.to_lowercase(), display_name)
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>()
|
2021-05-07 07:31:59 +12:00
|
|
|
})
|
2021-05-16 09:51:57 +12:00
|
|
|
.unwrap_or_default();
|
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
|
2022-01-06 13:46:45 +13:00
|
|
|
for part in sub.split('+').filter(|x| x != &"") {
|
2021-05-07 07:11:25 +12:00
|
|
|
// Retrieve display name for the subreddit
|
|
|
|
let display;
|
2021-11-26 17:02:04 +13:00
|
|
|
let part = if part.starts_with("u_") {
|
|
|
|
part
|
|
|
|
} else if let Some(&(_, display)) = display_lookup.iter().find(|x| x.0 == part.to_lowercase()) {
|
|
|
|
// This is already known, doesn't require separate request
|
2021-05-07 07:11:25 +12:00
|
|
|
display
|
|
|
|
} else {
|
|
|
|
// This subreddit display name isn't known, retrieve it
|
|
|
|
let path: String = format!("/r/{}/about.json?raw_json=1", part);
|
2021-05-17 03:53:39 +12:00
|
|
|
display = json(path, true).await?;
|
2021-05-07 07:11:25 +12:00
|
|
|
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());
|
2021-11-26 17:02:04 +13:00
|
|
|
filters.retain(|s| s.to_lowercase() != part.to_lowercase());
|
|
|
|
// Reorder sub names alphabetically
|
2021-09-10 12:28:55 +12:00
|
|
|
sub_list.sort_by_key(|a| a.to_lowercase());
|
2021-11-26 17:02:04 +13:00
|
|
|
filters.sort_by_key(|a| a.to_lowercase());
|
2021-02-13 17:47:54 +13:00
|
|
|
} else if action.contains(&"unsubscribe".to_string()) {
|
|
|
|
// Remove sub name from subscribed list
|
2021-05-20 15:30:10 +12:00
|
|
|
sub_list.retain(|s| s.to_lowercase() != part.to_lowercase());
|
2021-11-26 17:02:04 +13:00
|
|
|
} else if action.contains(&"filter".to_string()) && !filters.contains(&part.to_owned()) {
|
|
|
|
// Add each sub name to the filtered list
|
|
|
|
filters.push(part.to_owned());
|
|
|
|
sub_list.retain(|s| s.to_lowercase() != part.to_lowercase());
|
|
|
|
// Reorder sub names alphabetically
|
|
|
|
filters.sort_by_key(|a| a.to_lowercase());
|
|
|
|
sub_list.sort_by_key(|a| a.to_lowercase());
|
|
|
|
} else if action.contains(&"unfilter".to_string()) {
|
|
|
|
// Remove sub name from filtered list
|
|
|
|
filters.retain(|s| s.to_lowercase() != part.to_lowercase());
|
2021-02-13 17:47:54 +13:00
|
|
|
}
|
2021-01-30 20:00:00 +13:00
|
|
|
}
|
|
|
|
|
2021-02-10 06:38:52 +13:00
|
|
|
// Redirect back to subreddit
|
2021-11-26 17:02:04 +13:00
|
|
|
// check for redirect parameter if unsubscribing/unfiltering from outside sidebar
|
2021-05-17 04:11:38 +12:00
|
|
|
let path = if let Some(redirect_path) = param(&format!("?{}", query), "redirect") {
|
2022-03-14 08:06:27 +13:00
|
|
|
format!("/{}", redirect_path)
|
2021-05-17 04:11:38 +12:00
|
|
|
} else {
|
|
|
|
format!("/r/{}", sub)
|
2021-02-10 06:38:52 +13:00
|
|
|
};
|
|
|
|
|
2021-05-21 07:24:06 +12:00
|
|
|
let mut response = 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-05-21 07:24:06 +12:00
|
|
|
response.remove_cookie("subscriptions".to_string());
|
2021-01-30 20:18:53 +13:00
|
|
|
} else {
|
2021-05-21 07:24:06 +12:00
|
|
|
response.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-11-26 17:02:04 +13:00
|
|
|
if filters.is_empty() {
|
|
|
|
response.remove_cookie("filters".to_string());
|
|
|
|
} else {
|
|
|
|
response.insert_cookie(
|
|
|
|
Cookie::build("filters", filters.join("+"))
|
|
|
|
.path("/")
|
|
|
|
.http_only(true)
|
|
|
|
.expires(OffsetDateTime::now_utc() + Duration::weeks(52))
|
|
|
|
.finish(),
|
|
|
|
);
|
|
|
|
}
|
2021-01-30 20:00:00 +13:00
|
|
|
|
2021-05-21 07:24:06 +12:00
|
|
|
Ok(response)
|
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-17 03:53:39 +12:00
|
|
|
let quarantined = can_access_quarantine(&req, &sub);
|
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-11-15 15:39:33 +13:00
|
|
|
let url = req.uri().to_string();
|
2021-01-02 19:21:43 +13:00
|
|
|
|
2021-05-17 03:53:39 +12:00
|
|
|
match json(path, quarantined).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-11-15 15:51:36 +13:00
|
|
|
url,
|
2021-02-10 06:38:52 +13:00
|
|
|
}),
|
2021-05-17 03:53:39 +12:00
|
|
|
Err(msg) => {
|
|
|
|
if msg == "quarantined" {
|
|
|
|
quarantine(req, sub)
|
|
|
|
} else {
|
|
|
|
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-17 03:53:39 +12:00
|
|
|
let quarantined = can_access_quarantine(&req, &sub);
|
2021-05-21 07:24:06 +12:00
|
|
|
|
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);
|
2021-11-15 15:39:33 +13:00
|
|
|
let url = req.uri().to_string();
|
2021-03-22 15:28:05 +13:00
|
|
|
|
|
|
|
// Send a request to the url
|
2021-05-17 03:53:39 +12:00
|
|
|
match json(path, quarantined).await {
|
2021-03-22 15:28:05 +13:00
|
|
|
// If success, receive JSON in response
|
|
|
|
Ok(response) => template(WikiTemplate {
|
2022-03-27 09:26:30 +13:00
|
|
|
wiki: rewrite_urls(&val(&response, "description_html")),
|
2021-06-12 06:03:36 +12:00
|
|
|
// wiki: format!(
|
|
|
|
// "{}<hr><h1>Moderators</h1><br><ul>{}</ul>",
|
2022-03-27 09:26:30 +13:00
|
|
|
// rewrite_urls(&val(&response, "description_html"),
|
2021-06-12 06:03:36 +12:00
|
|
|
// moderators(&sub, quarantined).await.unwrap_or(vec!["Could not fetch moderators".to_string()]).join(""),
|
|
|
|
// ),
|
2021-03-22 15:28:05 +13:00
|
|
|
sub,
|
|
|
|
page: "Sidebar".to_string(),
|
|
|
|
prefs: Preferences::new(req),
|
2021-11-15 15:51:36 +13:00
|
|
|
url,
|
2021-03-22 15:28:05 +13:00
|
|
|
}),
|
2021-05-17 03:53:39 +12:00
|
|
|
Err(msg) => {
|
|
|
|
if msg == "quarantined" {
|
|
|
|
quarantine(req, sub)
|
|
|
|
} else {
|
|
|
|
error(req, msg).await
|
|
|
|
}
|
|
|
|
}
|
2021-03-22 15:28:05 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-12 06:03:36 +12:00
|
|
|
// pub async fn moderators(sub: &str, quarantined: bool) -> Result<Vec<String>, String> {
|
|
|
|
// // Retrieve and format the html for the moderators list
|
|
|
|
// Ok(
|
|
|
|
// moderators_list(sub, quarantined)
|
|
|
|
// .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, quarantined: bool) -> Result<Vec<String>, String> {
|
|
|
|
// // Build the moderator list URL
|
|
|
|
// let path: String = format!("/r/{}/about/moderators.json?raw_json=1", sub);
|
|
|
|
|
|
|
|
// // Retrieve response
|
|
|
|
// json(path, quarantined).await.map(|response| {
|
|
|
|
// // Traverse json tree and format into list of strings
|
|
|
|
// response["data"]["children"]
|
|
|
|
// .as_array()
|
|
|
|
// .unwrap_or(&Vec::new())
|
|
|
|
// .iter()
|
|
|
|
// .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-05-17 03:53:39 +12:00
|
|
|
async fn subreddit(sub: &str, quarantined: bool) -> 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-05-21 07:24:06 +12:00
|
|
|
let res = json(path, quarantined).await?;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
// Fetch subreddit icon either from the community_icon or icon_img value
|
|
|
|
let community_icon: &str = res["data"]["community_icon"].as_str().unwrap_or_default();
|
|
|
|
let icon = if community_icon.is_empty() { val(&res, "icon_img") } else { community_icon.to_string() };
|
|
|
|
|
|
|
|
Ok(Subreddit {
|
|
|
|
name: esc!(&res, "display_name"),
|
|
|
|
title: esc!(&res, "title"),
|
|
|
|
description: esc!(&res, "public_description"),
|
2022-03-27 09:26:30 +13:00
|
|
|
info: rewrite_urls(&val(&res, "description_html")),
|
2021-06-12 06:03:36 +12:00
|
|
|
// moderators: moderators_list(sub, quarantined).await.unwrap_or_default(),
|
2021-05-21 07:24:06 +12:00
|
|
|
icon: format_url(&icon),
|
|
|
|
members: format_num(members),
|
|
|
|
active: format_num(active),
|
|
|
|
wiki: res["data"]["wiki_enabled"].as_bool().unwrap_or_default(),
|
|
|
|
})
|
2020-11-30 15:50:29 +13:00
|
|
|
}
|