Procházet zdrojové kódy

Fix default subscriptions (#732)

Co-authored-by: Daniel Valentine <daniel@vielle.ws>
Matthew Esposito před 2 roky
rodič
revize
412ce8f1f3
6 změnil soubory, kde provedl 36 přidání a 11 odebrání
  1. 2 0
      README.md
  2. 3 0
      app.json
  3. 13 6
      src/client.rs
  4. 10 0
      src/config.rs
  5. 7 4
      src/instance_info.rs
  6. 1 1
      src/main.rs

+ 2 - 0
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`         |
 

+ 3 - 0
app.json

@@ -50,6 +50,9 @@
     "LIBREDDIT_BANNER": {
       "required": false
     },
+    "LIBREDDIT_DEFAULT_SUBSCRIPTIONS": {
+      "required": false
+    },
     "LIBREDDIT_DEFAULT_DISABLE_VISIT_REDDIT_CONFIRMATION": {
       "required": false
     }

+ 13 - 6
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<Client<HttpsConnector<HttpConnector>>> = Lazy::new(||{
+static CLIENT: Lazy<Client<HttpsConnector<HttpConnector>>> = 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 {

+ 10 - 0
src/config.rs

@@ -52,6 +52,9 @@ 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_DEFAULT_DISABLE_VISIT_REDDIT_CONFIRMATION")]
 	pub(crate) default_disable_visit_reddit_confirmation: Option<String>,
 
@@ -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<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_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()));
+}

+ 7 - 4
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(),

+ 1 - 1
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);