Merge branch 'master' into reddit-stats

This commit is contained in:
Matthew Esposito 2023-06-09 17:31:12 -04:00 committed by GitHub
commit a39bb9d502
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 409 additions and 367 deletions

View file

@ -9,12 +9,16 @@ use std::{env::var, fs::read_to_string};
// first request) and contains the instance settings.
pub(crate) static CONFIG: Lazy<Config> = Lazy::new(Config::load);
// This serves as the frontend for the Pushshift API - on removed comments, this URL will
// be the base of a link, to display removed content (on another site).
pub(crate) const DEFAULT_PUSHSHIFT_FRONTEND: &str = "www.unddit.com";
/// Stores the configuration parsed from the environment variables and the
/// config file. `Config::Default()` contains None for each setting.
/// When adding more config settings, add it to `Config::load`,
/// `get_setting_from_config`, both below, as well as
/// instance_info::InstanceInfo.to_string(), README.md and app.json.
#[derive(Default, Serialize, Deserialize, Clone)]
#[derive(Default, Serialize, Deserialize, Clone, Debug)]
pub struct Config {
#[serde(rename = "LIBREDDIT_SFW_ONLY")]
pub(crate) sfw_only: Option<String>,
@ -66,6 +70,9 @@ pub struct Config {
#[serde(rename = "LIBREDDIT_DISABLE_STATS_COLLECTION")]
pub(crate) disable_stats_collection: Option<String>,
#[serde(rename = "LIBREDDIT_PUSHSHIFT_FRONTEND")]
pub(crate) pushshift: Option<String>,
}
impl Config {
@ -80,6 +87,7 @@ impl Config {
// environment variables with "LIBREDDIT", then check the config, then if
// both are `None`, return a `None` via the `map_or_else` function
let parse = |key: &str| -> Option<String> { var(key).ok().map_or_else(|| get_setting_from_config(key, &config), Some) };
Self {
sfw_only: parse("LIBREDDIT_SFW_ONLY"),
default_theme: parse("LIBREDDIT_DEFAULT_THEME"),
@ -98,6 +106,7 @@ impl Config {
banner: parse("LIBREDDIT_BANNER"),
robots_disable_indexing: parse("LIBREDDIT_ROBOTS_DISABLE_INDEXING"),
disable_stats_collection: parse("LIBREDDIT_DISABLE_STATS_COLLECTION"),
pushshift: parse("LIBREDDIT_PUSHSHIFT_FRONTEND"),
}
}
}
@ -121,6 +130,7 @@ fn get_setting_from_config(name: &str, config: &Config) -> Option<String> {
"LIBREDDIT_BANNER" => config.banner.clone(),
"LIBREDDIT_ROBOTS_DISABLE_INDEXING" => config.robots_disable_indexing.clone(),
"LIBREDDIT_DISABLE_STATS_COLLECTION" => config.disable_stats_collection.clone(),
"LIBREDDIT_PUSHSHIFT_FRONTEND" => config.pushshift.clone(),
_ => None,
}
}
@ -133,6 +143,13 @@ pub(crate) fn get_setting(name: &str) -> Option<String> {
#[cfg(test)]
use {sealed_test::prelude::*, std::fs::write};
#[test]
fn test_deserialize() {
// Must handle empty input
let result = toml::from_str::<Config>("");
assert!(result.is_ok(), "Error: {}", result.unwrap_err());
}
#[test]
#[sealed_test(env = [("LIBREDDIT_SFW_ONLY", "on")])]
fn test_env_var() {

View file

@ -131,6 +131,8 @@ impl InstanceInfo {
["Disable stats collection", &convert(&self.config.disable_stats_collection)],
["Reddit request count", &self.reddit_requests.load(SeqCst).to_string()],
["Total request count", &self.total_requests.load(SeqCst).to_string()],
["Pushshift frontend", &convert(&self.config.pushshift)],
//TODO: fallback to crate::config::DEFAULT_PUSHSHIFT_FRONTEND
])
.with_header_row(["Settings"]),
);
@ -167,6 +169,7 @@ impl InstanceInfo {
Disable stats collection: {:?}\n
Reddit request count: {}\n
Total request count: {}\n
Pushshift frontend: {:?}\n
Config:\n
Banner: {:?}\n
Hide awards: {:?}\n
@ -190,6 +193,7 @@ impl InstanceInfo {
self.config.disable_stats_collection,
self.reddit_requests.load(SeqCst),
self.total_requests.load(SeqCst),
self.config.pushshift,
self.config.banner,
self.config.default_hide_awards,
self.config.default_theme,

View file

@ -1,5 +1,6 @@
// CRATES
use crate::client::json;
use crate::config::get_setting;
use crate::server::RequestExt;
use crate::subreddit::{can_access_quarantine, quarantine};
use crate::utils::{
@ -169,8 +170,10 @@ fn build_comment(
let body = if (val(comment, "author") == "[deleted]" && val(comment, "body") == "[removed]") || val(comment, "body") == "[ Removed by Reddit ]" {
format!(
"<div class=\"md\"><p>[removed] — <a href=\"https://www.unddit.com{}{}\">view removed comment</a></p></div>",
post_link, id
"<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
)
} else {
rewrite_urls(&val(comment, "body_html"))

View file

@ -1,3 +1,4 @@
use crate::config::get_setting;
//
// CRATES
//
@ -662,7 +663,8 @@ pub async fn parse_post(post: &serde_json::Value) -> Post {
let body = if val(post, "removed_by_category") == "moderator" {
format!(
"<div class=\"md\"><p>[removed] — <a href=\"https://www.unddit.com{}\">view removed post</a></p></div>",
"<div class=\"md\"><p>[removed] — <a href=\"https://{}{}\">view removed post</a></p></div>",
get_setting("LIBREDDIT_PUSHSHIFT_FRONTEND").unwrap_or(String::from(crate::config::DEFAULT_PUSHSHIFT_FRONTEND)),
permalink
)
} else {