Fix default subscriptions, add option for pushshift frontend

This commit is contained in:
Matthew Esposito 2023-01-30 12:10:11 -05:00
parent f405f509c4
commit 91a8278a0b
No known key found for this signature in database
7 changed files with 52 additions and 10 deletions

View File

@ -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:

View File

@ -46,9 +46,15 @@
},
"LIBREDDIT_DEFAULT_HIDE_AWARDS": {
"required": false
}
},
"LIBREDDIT_BANNER": {
"required": false
},
"LIBREDDIT_DEFAULT_SUBSCRIPTIONS": {
"required": false
},
"LIBREDDIT_PUSHSHIFT_FRONTEND": {
"required": false
}
}
}

View File

@ -52,8 +52,14 @@ pub struct Config {
#[serde(rename = "LIBREDDIT_DEFAULT_HIDE_AWARDS")]
pub(crate) default_hide_awards: Option<String>,
#[serde(rename = "LIBREDDIT_DEFAULT_SUBSCRIPTIONS")]
pub(crate) default_subscriptions: Option<String>,
#[serde(rename = "LIBREDDIT_BANNER")]
pub(crate) banner: Option<String>,
#[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<String> { 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<String> {
"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,
}
}

View File

@ -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(),

View File

@ -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);

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::{
@ -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!(
"<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>",
// Safe to unwrap: The get_setting function only returns an option by design,
// for this specific setting, it is a String, not an Option<String>. 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"))

View File

@ -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!(
"<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>",
// Safe to unwrap: The get_setting function only returns an option by design,
// for this specific setting, it is a String, not an Option<String>. 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 {