2020-10-26 09:25:59 +13:00
|
|
|
// CRATES
|
2021-03-18 11:30:33 +13:00
|
|
|
use crate::client::json;
|
2023-06-04 05:32:46 +12:00
|
|
|
use crate::config::get_setting;
|
2021-03-18 11:30:33 +13:00
|
|
|
use crate::server::RequestExt;
|
2021-05-17 03:53:39 +12:00
|
|
|
use crate::subreddit::{can_access_quarantine, quarantine};
|
2021-11-25 15:08:27 +13:00
|
|
|
use crate::utils::{
|
2023-01-03 22:39:45 +13:00
|
|
|
error, format_num, get_filters, nsfw_landing, param, parse_post, rewrite_urls, setting, template, time, val, Author, Awards, Comment, Flair, FlairPart, Post, Preferences,
|
2021-11-25 15:08:27 +13:00
|
|
|
};
|
2021-03-18 11:30:33 +13:00
|
|
|
use hyper::{Body, Request, Response};
|
2020-12-15 13:35:04 +13:00
|
|
|
|
2020-10-26 09:25:59 +13:00
|
|
|
use askama::Template;
|
2023-03-25 13:41:26 +13:00
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
use regex::Regex;
|
2021-11-26 17:02:04 +13:00
|
|
|
use std::collections::HashSet;
|
2020-11-30 15:50:29 +13:00
|
|
|
|
2020-10-26 09:25:59 +13:00
|
|
|
// STRUCTS
|
|
|
|
#[derive(Template)]
|
2022-05-21 17:28:31 +12:00
|
|
|
#[template(path = "post.html")]
|
2020-10-26 09:25:59 +13:00
|
|
|
struct PostTemplate {
|
|
|
|
comments: Vec<Comment>,
|
|
|
|
post: Post,
|
2020-10-26 16:57:19 +13:00
|
|
|
sort: String,
|
2021-01-11 10:20:47 +13:00
|
|
|
prefs: Preferences,
|
2021-02-13 06:16:59 +13:00
|
|
|
single_thread: bool,
|
2021-11-15 15:39:33 +13:00
|
|
|
url: String,
|
2023-03-25 13:41:26 +13:00
|
|
|
url_without_query: String,
|
|
|
|
comment_query: String,
|
2020-10-26 09:25:59 +13:00
|
|
|
}
|
|
|
|
|
2023-03-25 13:41:26 +13:00
|
|
|
static COMMENT_SEARCH_CAPTURE: Lazy<Regex> = Lazy::new(|| Regex::new(r#"\?q=(.*)&type=comment"#).unwrap());
|
|
|
|
|
2021-03-18 11:30:33 +13:00
|
|
|
pub async fn item(req: Request<Body>) -> Result<Response<Body>, String> {
|
2021-01-08 05:38:05 +13:00
|
|
|
// Build Reddit API path
|
2021-03-18 11:30:33 +13:00
|
|
|
let mut path: String = format!("{}.json?{}&raw_json=1", req.uri().path(), req.uri().query().unwrap_or_default());
|
2021-05-17 03:53:39 +12:00
|
|
|
let sub = req.param("sub").unwrap_or_default();
|
|
|
|
let quarantined = can_access_quarantine(&req, &sub);
|
2023-03-25 13:41:26 +13:00
|
|
|
let url = req.uri().to_string();
|
2021-01-08 07:32:55 +13:00
|
|
|
|
|
|
|
// Set sort to sort query parameter
|
2021-05-17 03:53:39 +12:00
|
|
|
let sort = param(&path, "sort").unwrap_or_else(|| {
|
|
|
|
// Grab default comment sort method from Cookies
|
|
|
|
let default_sort = setting(&req, "comment_sort");
|
2021-01-08 07:32:55 +13:00
|
|
|
|
2021-05-17 03:53:39 +12:00
|
|
|
// If there's no sort query but there's a default sort, set sort to default_sort
|
2021-05-17 04:11:38 +12:00
|
|
|
if default_sort.is_empty() {
|
|
|
|
String::new()
|
|
|
|
} else {
|
2021-05-17 03:53:39 +12:00
|
|
|
path = format!("{}.json?{}&sort={}&raw_json=1", req.uri().path(), req.uri().query().unwrap_or_default(), default_sort);
|
|
|
|
default_sort
|
|
|
|
}
|
|
|
|
});
|
2021-01-01 12:54:13 +13:00
|
|
|
|
2020-12-22 18:40:06 +13:00
|
|
|
// Log the post ID being fetched in debug mode
|
|
|
|
#[cfg(debug_assertions)]
|
2021-03-18 11:30:33 +13:00
|
|
|
dbg!(req.param("id").unwrap_or_default());
|
2021-02-14 12:02:38 +13:00
|
|
|
|
2021-03-26 17:41:58 +13:00
|
|
|
let single_thread = req.param("comment_id").is_some();
|
2021-02-13 06:16:59 +13:00
|
|
|
let highlighted_comment = &req.param("comment_id").unwrap_or_default();
|
2021-01-02 19:21:43 +13:00
|
|
|
|
2020-11-21 18:04:35 +13:00
|
|
|
// Send a request to the url, receive JSON in response
|
2021-05-17 03:53:39 +12:00
|
|
|
match json(path, quarantined).await {
|
2021-01-01 12:54:13 +13:00
|
|
|
// Otherwise, grab the JSON output from the request
|
2021-05-21 07:24:06 +12:00
|
|
|
Ok(response) => {
|
2021-01-02 12:28:13 +13:00
|
|
|
// Parse the JSON into Post and Comment structs
|
2022-11-10 05:16:51 +13:00
|
|
|
let post = parse_post(&response[0]["data"]["children"][0]).await;
|
2023-01-03 22:39:45 +13:00
|
|
|
|
2023-03-24 07:09:33 +13:00
|
|
|
let req_url = req.uri().to_string();
|
2023-01-03 22:39:45 +13:00
|
|
|
// Return landing page if this post if this Reddit deems this post
|
|
|
|
// NSFW, but we have also disabled the display of NSFW content
|
|
|
|
// or if the instance is SFW-only.
|
2023-03-23 19:18:35 +13:00
|
|
|
if post.nsfw && crate::utils::should_be_nsfw_gated(&req, &req_url) {
|
|
|
|
return Ok(nsfw_landing(req, req_url).await.unwrap_or_default());
|
2023-01-03 22:39:45 +13:00
|
|
|
}
|
|
|
|
|
2023-03-25 13:41:26 +13:00
|
|
|
let query = match COMMENT_SEARCH_CAPTURE.captures(&url) {
|
2023-04-20 02:37:47 +12:00
|
|
|
Some(captures) => captures.get(1).unwrap().as_str().replace("%20", " ").replace('+', " "),
|
2023-03-25 13:41:26 +13:00
|
|
|
None => String::new(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let comments = match query.as_str() {
|
|
|
|
"" => parse_comments(&response[1], &post.permalink, &post.author.name, highlighted_comment, &get_filters(&req), &req),
|
|
|
|
_ => query_comments(&response[1], &post.permalink, &post.author.name, highlighted_comment, &get_filters(&req), &query, &req),
|
|
|
|
};
|
2021-01-02 12:28:13 +13:00
|
|
|
|
|
|
|
// Use the Post and Comment structs to generate a website to show users
|
2021-02-10 06:38:52 +13:00
|
|
|
template(PostTemplate {
|
2021-01-11 10:20:47 +13:00
|
|
|
comments,
|
|
|
|
post,
|
2023-03-25 13:41:26 +13:00
|
|
|
url_without_query: url.clone().trim_end_matches(&format!("?q={query}&type=comment")).to_string(),
|
2021-01-11 10:20:47 +13:00
|
|
|
sort,
|
2023-01-02 15:39:38 +13:00
|
|
|
prefs: Preferences::new(&req),
|
2021-03-26 17:41:58 +13:00
|
|
|
single_thread,
|
2023-03-23 19:18:35 +13:00
|
|
|
url: req_url,
|
2023-03-25 13:41:26 +13:00
|
|
|
comment_query: query,
|
2021-02-10 06:38:52 +13:00
|
|
|
})
|
2021-01-02 19:21:43 +13:00
|
|
|
}
|
2021-01-02 12:28:13 +13:00
|
|
|
// If the Reddit API returns an error, exit and send error page to user
|
2021-05-17 03:53:39 +12:00
|
|
|
Err(msg) => {
|
2023-02-26 20:40:32 +13:00
|
|
|
if msg == "quarantined" || msg == "gated" {
|
2021-05-17 03:53:39 +12:00
|
|
|
let sub = req.param("sub").unwrap_or_default();
|
2023-02-26 20:40:32 +13:00
|
|
|
quarantine(req, sub, msg)
|
2021-05-17 03:53:39 +12:00
|
|
|
} else {
|
|
|
|
error(req, msg).await
|
|
|
|
}
|
|
|
|
}
|
2020-10-26 09:25:59 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// COMMENTS
|
2023-03-25 13:41:26 +13:00
|
|
|
|
2023-01-02 15:39:38 +13:00
|
|
|
fn parse_comments(json: &serde_json::Value, post_link: &str, post_author: &str, highlighted_comment: &str, filters: &HashSet<String>, req: &Request<Body>) -> Vec<Comment> {
|
2021-05-21 07:24:06 +12:00
|
|
|
// Parse the comment JSON into a Vector of Comments
|
|
|
|
let comments = json["data"]["children"].as_array().map_or(Vec::new(), std::borrow::ToOwned::to_owned);
|
2020-10-26 16:57:19 +13:00
|
|
|
|
2020-12-20 16:54:46 +13:00
|
|
|
// For each comment, retrieve the values to build a Comment object
|
2021-05-21 07:24:06 +12:00
|
|
|
comments
|
|
|
|
.into_iter()
|
|
|
|
.map(|comment| {
|
|
|
|
let data = &comment["data"];
|
|
|
|
let replies: Vec<Comment> = if data["replies"].is_object() {
|
2023-01-02 15:39:38 +13:00
|
|
|
parse_comments(&data["replies"], post_link, post_author, highlighted_comment, filters, req)
|
2021-02-01 11:10:13 +13:00
|
|
|
} else {
|
2021-05-21 07:24:06 +12:00
|
|
|
Vec::new()
|
|
|
|
};
|
2023-03-25 13:41:26 +13:00
|
|
|
build_comment(&comment, data, replies, post_link, post_author, highlighted_comment, filters, req)
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
2021-05-21 07:24:06 +12:00
|
|
|
|
2023-03-25 13:41:26 +13:00
|
|
|
fn query_comments(
|
|
|
|
json: &serde_json::Value,
|
|
|
|
post_link: &str,
|
|
|
|
post_author: &str,
|
|
|
|
highlighted_comment: &str,
|
|
|
|
filters: &HashSet<String>,
|
|
|
|
query: &str,
|
|
|
|
req: &Request<Body>,
|
|
|
|
) -> Vec<Comment> {
|
|
|
|
let comments = json["data"]["children"].as_array().map_or(Vec::new(), std::borrow::ToOwned::to_owned);
|
|
|
|
let mut results = Vec::new();
|
2021-11-25 15:08:27 +13:00
|
|
|
|
2023-03-25 13:41:26 +13:00
|
|
|
comments.into_iter().for_each(|comment| {
|
|
|
|
let data = &comment["data"];
|
2021-05-21 07:24:06 +12:00
|
|
|
|
2023-03-25 13:41:26 +13:00
|
|
|
// If this comment contains replies, handle those too
|
|
|
|
if data["replies"].is_object() {
|
|
|
|
results.append(&mut query_comments(&data["replies"], post_link, post_author, highlighted_comment, filters, query, req))
|
|
|
|
}
|
2021-05-21 07:24:06 +12:00
|
|
|
|
2023-03-27 07:52:02 +13:00
|
|
|
let c = build_comment(&comment, data, Vec::new(), post_link, post_author, highlighted_comment, filters, req);
|
2023-03-25 13:41:26 +13:00
|
|
|
if c.body.to_lowercase().contains(&query.to_lowercase()) {
|
|
|
|
results.push(c);
|
|
|
|
}
|
|
|
|
});
|
2021-12-28 16:40:35 +13:00
|
|
|
|
2023-03-25 13:41:26 +13:00
|
|
|
results
|
|
|
|
}
|
2023-04-20 02:37:47 +12:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2023-03-25 13:41:26 +13:00
|
|
|
fn build_comment(
|
|
|
|
comment: &serde_json::Value,
|
|
|
|
data: &serde_json::Value,
|
|
|
|
replies: Vec<Comment>,
|
|
|
|
post_link: &str,
|
|
|
|
post_author: &str,
|
|
|
|
highlighted_comment: &str,
|
|
|
|
filters: &HashSet<String>,
|
|
|
|
req: &Request<Body>,
|
|
|
|
) -> Comment {
|
2023-04-20 02:37:47 +12:00
|
|
|
let id = val(comment, "id");
|
2023-03-25 13:41:26 +13:00
|
|
|
|
2023-04-20 02:37:47 +12:00
|
|
|
let body = if (val(comment, "author") == "[deleted]" && val(comment, "body") == "[removed]") || val(comment, "body") == "[ Removed by Reddit ]" {
|
2023-03-25 13:41:26 +13:00
|
|
|
format!(
|
2023-06-04 05:32:46 +12:00
|
|
|
"<div class=\"md\"><p>[removed] — <a href=\"https://{}{}{}\">view removed comment</a></p></div>",
|
|
|
|
get_setting("LIBREDDIT_PUSHSHIFT_FRONTEND").unwrap_or(String::from(crate::config::DEFAULT_PUSHSHIFT_FRONTEND)),
|
|
|
|
post_link,
|
|
|
|
id
|
2023-03-25 13:41:26 +13:00
|
|
|
)
|
|
|
|
} else {
|
2023-04-20 02:37:47 +12:00
|
|
|
rewrite_urls(&val(comment, "body_html"))
|
2023-03-25 13:41:26 +13:00
|
|
|
};
|
|
|
|
let kind = comment["kind"].as_str().unwrap_or_default().to_string();
|
|
|
|
|
|
|
|
let unix_time = data["created_utc"].as_f64().unwrap_or_default();
|
|
|
|
let (rel_time, created) = time(unix_time);
|
|
|
|
|
|
|
|
let edited = data["edited"].as_f64().map_or((String::new(), String::new()), time);
|
|
|
|
|
|
|
|
let score = data["score"].as_i64().unwrap_or(0);
|
|
|
|
|
|
|
|
// The JSON API only provides comments up to some threshold.
|
|
|
|
// Further comments have to be loaded by subsequent requests.
|
|
|
|
// The "kind" value will be "more" and the "count"
|
|
|
|
// shows how many more (sub-)comments exist in the respective nesting level.
|
|
|
|
// Note that in certain (seemingly random) cases, the count is simply wrong.
|
|
|
|
let more_count = data["count"].as_i64().unwrap_or_default();
|
|
|
|
|
|
|
|
let awards: Awards = Awards::parse(&data["all_awardings"]);
|
|
|
|
|
2023-04-20 02:37:47 +12:00
|
|
|
let parent_kind_and_id = val(comment, "parent_id");
|
2023-03-25 13:41:26 +13:00
|
|
|
let parent_info = parent_kind_and_id.split('_').collect::<Vec<&str>>();
|
|
|
|
|
|
|
|
let highlighted = id == highlighted_comment;
|
|
|
|
|
|
|
|
let author = Author {
|
2023-04-20 02:37:47 +12:00
|
|
|
name: val(comment, "author"),
|
2023-03-25 13:41:26 +13:00
|
|
|
flair: Flair {
|
|
|
|
flair_parts: FlairPart::parse(
|
|
|
|
data["author_flair_type"].as_str().unwrap_or_default(),
|
|
|
|
data["author_flair_richtext"].as_array(),
|
|
|
|
data["author_flair_text"].as_str(),
|
|
|
|
),
|
2023-04-20 02:37:47 +12:00
|
|
|
text: val(comment, "link_flair_text"),
|
|
|
|
background_color: val(comment, "author_flair_background_color"),
|
|
|
|
foreground_color: val(comment, "author_flair_text_color"),
|
2023-03-25 13:41:26 +13:00
|
|
|
},
|
2023-04-20 02:37:47 +12:00
|
|
|
distinguished: val(comment, "distinguished"),
|
2023-03-25 13:41:26 +13:00
|
|
|
};
|
|
|
|
let is_filtered = filters.contains(&["u_", author.name.as_str()].concat());
|
|
|
|
|
|
|
|
// Many subreddits have a default comment posted about the sub's rules etc.
|
|
|
|
// Many libreddit users do not wish to see this kind of comment by default.
|
|
|
|
// Reddit does not tell us which users are "bots", so a good heuristic is to
|
|
|
|
// collapse stickied moderator comments.
|
|
|
|
let is_moderator_comment = data["distinguished"].as_str().unwrap_or_default() == "moderator";
|
|
|
|
let is_stickied = data["stickied"].as_bool().unwrap_or_default();
|
|
|
|
let collapsed = (is_moderator_comment && is_stickied) || is_filtered;
|
|
|
|
|
2023-04-20 02:37:47 +12:00
|
|
|
Comment {
|
2023-03-25 13:41:26 +13:00
|
|
|
id,
|
|
|
|
kind,
|
|
|
|
parent_id: parent_info[1].to_string(),
|
|
|
|
parent_kind: parent_info[0].to_string(),
|
|
|
|
post_link: post_link.to_string(),
|
|
|
|
post_author: post_author.to_string(),
|
|
|
|
body,
|
|
|
|
author,
|
|
|
|
score: if data["score_hidden"].as_bool().unwrap_or_default() {
|
|
|
|
("\u{2022}".to_string(), "Hidden".to_string())
|
|
|
|
} else {
|
|
|
|
format_num(score)
|
|
|
|
},
|
|
|
|
rel_time,
|
|
|
|
created,
|
|
|
|
edited,
|
|
|
|
replies,
|
|
|
|
highlighted,
|
|
|
|
awards,
|
|
|
|
collapsed,
|
|
|
|
is_filtered,
|
|
|
|
more_count,
|
2023-04-20 02:37:47 +12:00
|
|
|
prefs: Preferences::new(req),
|
|
|
|
}
|
2021-12-28 07:15:25 +13:00
|
|
|
}
|