From 91a8278a0bc3535e0de7ffce81aa4ec09946d186 Mon Sep 17 00:00:00 2001 From: Matthew Esposito Date: Mon, 30 Jan 2023 12:10:11 -0500 Subject: [PATCH] Fix default subscriptions, add option for pushshift frontend --- README.md | 4 +++- app.json | 8 +++++++- src/config.rs | 15 +++++++++++++++ src/instance_info.rs | 14 ++++++++++---- src/main.rs | 2 +- src/post.rs | 11 +++++++++-- src/utils.rs | 8 +++++++- 7 files changed, 52 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 3fec4cc..c81aaeb 100644 --- a/README.md +++ b/README.md @@ -191,6 +191,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. | +| `PUSHSHIFT_FRONTEND` | String | `www.unddit.com` | Allows the server to set the Pushshift frontend to be used with "removed" links. ## Default User Settings @@ -209,7 +210,8 @@ Assign a default value for each user-modifiable setting by passing environment v | `USE_HLS` | `["on", "off"]` | `off` | | `HIDE_HLS_NOTIFICATION` | `["on", "off"]` | `off` | | `AUTOPLAY_VIDEOS` | `["on", "off"]` | `off` | -| `HIDE_AWARDS` | `["on", "off"]` | `off` +| `HIDE_AWARDS` | `["on", "off"]` | `off` | +| `SUBSCRIPTIONS` | Array of subreddit names (`["sub1", "sub2"]`) | `[]` | You can also configure Libreddit with a configuration file. An example `libreddit.toml` can be found below: diff --git a/app.json b/app.json index 33a41fa..96d8424 100644 --- a/app.json +++ b/app.json @@ -46,9 +46,15 @@ }, "LIBREDDIT_DEFAULT_HIDE_AWARDS": { "required": false - } + }, "LIBREDDIT_BANNER": { "required": false + }, + "LIBREDDIT_DEFAULT_SUBSCRIPTIONS": { + "required": false + }, + "LIBREDDIT_PUSHSHIFT_FRONTEND": { + "required": false } } } diff --git a/src/config.rs b/src/config.rs index 90478b9..fb01db6 100644 --- a/src/config.rs +++ b/src/config.rs @@ -52,8 +52,14 @@ pub struct Config { #[serde(rename = "LIBREDDIT_DEFAULT_HIDE_AWARDS")] pub(crate) default_hide_awards: Option, + #[serde(rename = "LIBREDDIT_DEFAULT_SUBSCRIPTIONS")] + pub(crate) default_subscriptions: Option, + #[serde(rename = "LIBREDDIT_BANNER")] pub(crate) banner: Option, + + #[serde(rename = "LIBREDDIT_PUSHSHIFT_FRONTEND")] + pub(crate) pushshift: String, } impl Config { @@ -68,6 +74,11 @@ 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) }; + + // 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). + let default_pushshift_frontend = String::from("www.unddit.com"); + Self { sfw_only: parse("LIBREDDIT_SFW_ONLY"), default_theme: parse("LIBREDDIT_DEFAULT_THEME"), @@ -81,7 +92,9 @@ impl Config { default_use_hls: parse("LIBREDDIT_DEFAULT_USE_HLS"), default_hide_hls_notification: parse("LIBREDDIT_DEFAULT_HIDE_HLS"), default_hide_awards: parse("LIBREDDIT_DEFAULT_HIDE_AWARDS"), + default_subscriptions: parse("LIBREDDIT_DEFAULT_SUBSCRIPTIONS"), banner: parse("LIBREDDIT_BANNER"), + pushshift: parse("LIBREDDIT_PUSHSHIFT_FRONTEND").unwrap_or(default_pushshift_frontend), } } } @@ -100,7 +113,9 @@ fn get_setting_from_config(name: &str, config: &Config) -> Option { "LIBREDDIT_DEFAULT_HIDE_HLS_NOTIFICATION" => config.default_hide_hls_notification.clone(), "LIBREDDIT_DEFAULT_WIDE" => config.default_wide.clone(), "LIBREDDIT_DEFAULT_HIDE_AWARDS" => config.default_hide_awards.clone(), + "LIBREDDIT_DEFAULT_SUBSCRIPTIONS" => config.default_subscriptions.clone(), "LIBREDDIT_BANNER" => config.banner.clone(), + "LIBREDDIT_PUSHSHIFT_FRONTEND" => Some(config.pushshift.clone()), _ => None, } } diff --git a/src/instance_info.rs b/src/instance_info.rs index 19b93f1..479fba5 100644 --- a/src/instance_info.rs +++ b/src/instance_info.rs @@ -122,6 +122,7 @@ 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(&Some(self.config.pushshift.clone()))], ]) .with_header_row(["Settings"]), ); @@ -139,6 +140,7 @@ impl InstanceInfo { ["Blur NSFW", &convert(&self.config.default_blur_nsfw)], ["Use HLS", &convert(&self.config.default_use_hls)], ["Hide HLS notification", &convert(&self.config.default_hide_hls_notification)], + ["Subscriptions", &convert(&self.config.default_subscriptions)], ]) .with_header_row(["Default preferences"]), ); @@ -153,10 +155,11 @@ impl InstanceInfo { Deploy date: {}\n Deploy timestamp: {}\n Compile mode: {}\n + SFW only: {:?}\n + Pushshift frontend: {:?}\n Config:\n Banner: {:?}\n Hide awards: {:?}\n - SFW only: {:?}\n Default theme: {:?}\n Default front page: {:?}\n Default layout: {:?}\n @@ -166,15 +169,17 @@ impl InstanceInfo { Default show NSFW: {:?}\n Default blur NSFW: {:?}\n Default use HLS: {:?}\n - Default hide HLS notification: {:?}\n", + Default hide HLS notification: {:?}\n + Default subscriptions: {:?}\n", self.crate_version, self.git_commit, self.deploy_date, self.deploy_unix_ts, self.compile_mode, + self.config.sfw_only, + self.config.pushshift, self.config.banner, self.config.default_hide_awards, - self.config.sfw_only, self.config.default_theme, self.config.default_front_page, self.config.default_layout, @@ -184,7 +189,8 @@ impl InstanceInfo { self.config.default_show_nsfw, self.config.default_blur_nsfw, self.config.default_use_hls, - self.config.default_hide_hls_notification + self.config.default_hide_hls_notification, + self.config.default_subscriptions, ) } StringType::Html => self.to_table(), diff --git a/src/main.rs b/src/main.rs index 7489aa0..4f5d3d2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -161,7 +161,7 @@ async fn main() { let mut app = server::Server::new(); // Force evaluation of statics. In instance_info case, we need to evaluate - // the timestamp so deploy date is accurate - in config case, we need to + // the timestamp so deploy date is accurate - in config case, we need to // evaluate the configuration to avoid paying penalty at first request. Lazy::force(&config::CONFIG); diff --git a/src/post.rs b/src/post.rs index 3031000..970dc89 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::{ @@ -124,8 +125,14 @@ fn parse_comments(json: &serde_json::Value, post_link: &str, post_author: &str, 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

", + // Safe to unwrap: The get_setting function only returns an option by design, + // for this specific setting, it is a String, not an Option. See + // get_setting_from_config() in config.rs - when requesting this specific + // setting, it wraps it in a Some, just to match the type signature. + get_setting("LIBREDDIT_PUSHSHIFT_FRONTEND").unwrap(), + post_link, + id ) } else { rewrite_urls(&val(&comment, "body_html")) diff --git a/src/utils.rs b/src/utils.rs index daee4a9..e56214b 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,3 +1,4 @@ +use crate::config::get_setting; // // CRATES // @@ -592,7 +593,12 @@ 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

", + // Safe to unwrap: The get_setting function only returns an option by design, + // for this specific setting, it is a String, not an Option. See + // get_setting_from_config() in config.rs - when requesting this specific + // setting, it wraps it in a Some, just to match the type signature. + get_setting("LIBREDDIT_PUSHSHIFT_FRONTEND").unwrap(), permalink ) } else {