From 412ce8f1f3ab7ef91f8a40182aee2a039bd7783b Mon Sep 17 00:00:00 2001 From: Matthew Esposito Date: Wed, 8 Mar 2023 23:53:23 -0500 Subject: [PATCH] Fix default subscriptions (#732) Co-authored-by: Daniel Valentine --- README.md | 2 ++ app.json | 3 +++ src/client.rs | 19 +++++++++++++------ src/config.rs | 10 ++++++++++ src/instance_info.rs | 11 +++++++---- src/main.rs | 2 +- 6 files changed, 36 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index bbcc8df..a0d4786 100644 --- a/README.md +++ b/README.md @@ -231,6 +231,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` | +| `SUBSCRIPTIONS` | `+`-delimited list of subreddits (`sub1+sub2+sub3+...`) | _(none)_ | | `HIDE_AWARDS` | `["on", "off"]` | `off` | `DISABLE_VISIT_REDDIT_CONFIRMATION` | `["on", "off"]` | `off` | diff --git a/app.json b/app.json index da5c1f9..b4e0f3d 100644 --- a/app.json +++ b/app.json @@ -50,6 +50,9 @@ "LIBREDDIT_BANNER": { "required": false }, + "LIBREDDIT_DEFAULT_SUBSCRIPTIONS": { + "required": false + }, "LIBREDDIT_DEFAULT_DISABLE_VISIT_REDDIT_CONFIRMATION": { "required": false } diff --git a/src/client.rs b/src/client.rs index b1d15a6..4c174cd 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,20 +1,20 @@ use cached::proc_macro::cached; use futures_lite::{future::Boxed, FutureExt}; -use hyper::{body, body::Buf, client, header, Body, Method, Request, Response, Uri, Client}; +use hyper::client::HttpConnector; +use hyper::{body, body::Buf, client, header, Body, Client, Method, Request, Response, Uri}; +use hyper_rustls::HttpsConnector; use libflate::gzip; +use once_cell::sync::Lazy; use percent_encoding::{percent_encode, CONTROLS}; use serde_json::Value; use std::{io, result::Result}; -use hyper::client::HttpConnector; -use hyper_rustls::HttpsConnector; -use once_cell::sync::Lazy; use crate::dbg_msg; use crate::server::RequestExt; const REDDIT_URL_BASE: &str = "https://www.reddit.com"; -static CLIENT: Lazy>> = Lazy::new(||{ +static CLIENT: Lazy>> = Lazy::new(|| { let https = hyper_rustls::HttpsConnectorBuilder::new().with_native_roots().https_only().enable_http1().build(); client::Client::builder().build(https) }); @@ -142,7 +142,14 @@ fn request(method: &'static Method, path: String, redirect: bool, quarantine: bo .header("Accept-Encoding", if method == Method::GET { "gzip" } else { "identity" }) .header("Accept-Language", "en-US,en;q=0.5") .header("Connection", "keep-alive") - .header("Cookie", if quarantine { "_options=%7B%22pref_quarantine_optin%22%3A%20true%2C%20%22pref_gated_sr_optin%22%3A%20true%7D" } else { "" }) + .header( + "Cookie", + if quarantine { + "_options=%7B%22pref_quarantine_optin%22%3A%20true%2C%20%22pref_gated_sr_optin%22%3A%20true%7D" + } else { + "" + }, + ) .body(Body::empty()); async move { diff --git a/src/config.rs b/src/config.rs index 9884c08..b552504 100644 --- a/src/config.rs +++ b/src/config.rs @@ -52,6 +52,9 @@ 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_DEFAULT_DISABLE_VISIT_REDDIT_CONFIRMATION")] pub(crate) default_disable_visit_reddit_confirmation: Option, @@ -84,6 +87,7 @@ 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"), default_disable_visit_reddit_confirmation: parse("LIBREDDIT_DEFAULT_DISABLE_VISIT_REDDIT_CONFIRMATION"), banner: parse("LIBREDDIT_BANNER"), } @@ -104,6 +108,7 @@ 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_DEFAULT_DISABLE_VISIT_REDDIT_CONFIRMATION" => config.default_disable_visit_reddit_confirmation.clone(), "LIBREDDIT_BANNER" => config.banner.clone(), _ => None, @@ -147,3 +152,8 @@ fn test_alt_env_config_precedence() { write("libreddit.toml", config_to_write).unwrap(); assert_eq!(get_setting("LIBREDDIT_DEFAULT_COMMENT_SORT"), Some("top".into())) } +#[test] +#[sealed_test(env = [("LIBREDDIT_DEFAULT_SUBSCRIPTIONS", "news+bestof")])] +fn test_default_subscriptions() { + assert_eq!(get_setting("LIBREDDIT_DEFAULT_SUBSCRIPTIONS"), Some("news+bestof".into())); +} diff --git a/src/instance_info.rs b/src/instance_info.rs index 19b93f1..f61796c 100644 --- a/src/instance_info.rs +++ b/src/instance_info.rs @@ -139,6 +139,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 +154,10 @@ impl InstanceInfo { Deploy date: {}\n Deploy timestamp: {}\n Compile mode: {}\n + SFW only: {:?}\n Config:\n Banner: {:?}\n Hide awards: {:?}\n - SFW only: {:?}\n Default theme: {:?}\n Default front page: {:?}\n Default layout: {:?}\n @@ -166,15 +167,16 @@ 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.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 +186,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);