From 97f0f6905912128ae45ac96313fe6e2268324b13 Mon Sep 17 00:00:00 2001 From: Nazar <63452145+Tokarak@users.noreply.github.com> Date: Sat, 3 Jun 2023 19:32:46 +0200 Subject: [PATCH] Rebase #811 (#812) Co-authored-by: Matthew Esposito --- README.md | 1 + app.json | 3 +++ src/config.rs | 19 ++++++++++++++++++- src/instance_info.rs | 4 ++++ src/post.rs | 7 +++++-- src/utils.rs | 4 +++- 6 files changed, 34 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3b13d49..0330486 100644 --- a/README.md +++ b/README.md @@ -214,6 +214,7 @@ Assign a default value for each instance-specific setting by passing environment | `SFW_ONLY` | `["on", "off"]` | `off` | Enables SFW-only mode for the instance, i.e. all NSFW content is filtered. | | `BANNER` | String | (empty) | Allows the server to set a banner to be displayed. Currently this is displayed on the instance info page. | | `ROBOTS_DISABLE_INDEXING` | `["on", "off"]` | `off` | Disables indexing of the instance by search engines. | +| `PUSHSHIFT_FRONTEND` | String | `www.unddit.com` | Allows the server to set the Pushshift frontend to be used with "removed" links. ## Default User Settings diff --git a/app.json b/app.json index 0da7058..c6b60c5 100644 --- a/app.json +++ b/app.json @@ -58,6 +58,9 @@ }, "LIBREDDIT_DEFAULT_DISABLE_VISIT_REDDIT_CONFIRMATION": { "required": false + }, + "LIBREDDIT_PUSHSHIFT_FRONTEND": { + "required": false } } } diff --git a/src/config.rs b/src/config.rs index 4107582..602a012 100644 --- a/src/config.rs +++ b/src/config.rs @@ -9,12 +9,16 @@ use std::{env::var, fs::read_to_string}; // first request) and contains the instance settings. pub(crate) static CONFIG: Lazy = 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, @@ -63,6 +67,9 @@ pub struct Config { #[serde(rename = "LIBREDDIT_ROBOTS_DISABLE_INDEXING")] pub(crate) robots_disable_indexing: Option, + + #[serde(rename = "LIBREDDIT_PUSHSHIFT_FRONTEND")] + pub(crate) pushshift: Option, } impl Config { @@ -77,6 +84,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 { 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"), @@ -94,6 +102,7 @@ impl Config { default_disable_visit_reddit_confirmation: parse("LIBREDDIT_DEFAULT_DISABLE_VISIT_REDDIT_CONFIRMATION"), banner: parse("LIBREDDIT_BANNER"), robots_disable_indexing: parse("LIBREDDIT_ROBOTS_DISABLE_INDEXING"), + pushshift: parse("LIBREDDIT_PUSHSHIFT_FRONTEND"), } } } @@ -116,6 +125,7 @@ fn get_setting_from_config(name: &str, config: &Config) -> Option { "LIBREDDIT_DEFAULT_DISABLE_VISIT_REDDIT_CONFIRMATION" => config.default_disable_visit_reddit_confirmation.clone(), "LIBREDDIT_BANNER" => config.banner.clone(), "LIBREDDIT_ROBOTS_DISABLE_INDEXING" => config.robots_disable_indexing.clone(), + "LIBREDDIT_PUSHSHIFT_FRONTEND" => config.pushshift.clone(), _ => None, } } @@ -128,6 +138,13 @@ pub(crate) fn get_setting(name: &str) -> Option { #[cfg(test)] use {sealed_test::prelude::*, std::fs::write}; +#[test] +fn test_deserialize() { + // Must handle empty input + let result = toml::from_str::(""); + assert!(result.is_ok(), "Error: {}", result.unwrap_err()); +} + #[test] #[sealed_test(env = [("LIBREDDIT_SFW_ONLY", "on")])] fn test_env_var() { diff --git a/src/instance_info.rs b/src/instance_info.rs index f61796c..daa06e2 100644 --- a/src/instance_info.rs +++ b/src/instance_info.rs @@ -122,6 +122,8 @@ impl InstanceInfo { ["Deploy timestamp", &self.deploy_unix_ts.to_string()], ["Compile mode", &self.compile_mode], ["SFW only", &convert(&self.config.sfw_only)], + ["Pushshift frontend", &convert(&self.config.pushshift)], + //TODO: fallback to crate::config::DEFAULT_PUSHSHIFT_FRONTEND ]) .with_header_row(["Settings"]), ); @@ -155,6 +157,7 @@ impl InstanceInfo { Deploy timestamp: {}\n Compile mode: {}\n SFW only: {:?}\n + Pushshift frontend: {:?}\n Config:\n Banner: {:?}\n Hide awards: {:?}\n @@ -175,6 +178,7 @@ impl InstanceInfo { self.deploy_unix_ts, self.compile_mode, self.config.sfw_only, + self.config.pushshift, self.config.banner, self.config.default_hide_awards, self.config.default_theme, diff --git a/src/post.rs b/src/post.rs index 0f97b2e..98dcac3 100644 --- a/src/post.rs +++ b/src/post.rs @@ -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!( - "

[removed] — view removed comment

", - post_link, id + "

[removed] — view removed comment

", + get_setting("LIBREDDIT_PUSHSHIFT_FRONTEND").unwrap_or(String::from(crate::config::DEFAULT_PUSHSHIFT_FRONTEND)), + post_link, + id ) } else { rewrite_urls(&val(comment, "body_html")) diff --git a/src/utils.rs b/src/utils.rs index a084af7..5cf4b7d 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -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!( - "

[removed] — view removed post

", + "

[removed] — view removed post

", + get_setting("LIBREDDIT_PUSHSHIFT_FRONTEND").unwrap_or(String::from(crate::config::DEFAULT_PUSHSHIFT_FRONTEND)), permalink ) } else {