Repository: redlib-org/redlib (language: rust)

Issue:
Hide Popular Option
I’d like to request a way to hide/disable the popular page, I think this would help with creating a more focussed experience (especially given how polarising the homepage can be).




Discussion / hints:
This is a good feature to add, working on it now!

Fix patch:
diff --git a/README.md b/README.md
index 6fabfcc9..5aac1e8f 100644
--- a/README.md
+++ b/README.md
@@ -440,7 +440,7 @@ Assign a default value for each user-modifiable setting by passing environment v
 | `WIDE`                              | `["on", "off"]`                                                                                                                    | `off`         |
 | `POST_SORT`                         | `["hot", "new", "top", "rising", "controversial"]`                                                                                 | `hot`         |
 | `COMMENT_SORT`                      | `["confidence", "top", "new", "controversial", "old"]`                                                                             | `confidence`  |
-| `BLUR_SPOILER`                         | `["on", "off"]`                                                                                                                    | `off`         |
+| `BLUR_SPOILER`                      | `["on", "off"]`                                                                                                                    | `off`         |
 | `SHOW_NSFW`                         | `["on", "off"]`                                                                                                                    | `off`         |
 | `BLUR_NSFW`                         | `["on", "off"]`                                                                                                                    | `off`         |
 | `USE_HLS`                           | `["on", "off"]`                                                                                                                    | `off`         |
@@ -452,3 +452,4 @@ Assign a default value for each user-modifiable setting by passing environment v
 | `HIDE_SCORE`                        | `["on", "off"]`                                                                                                                    | `off`         |
 | `HIDE_SIDEBAR_AND_SUMMARY`          | `["on", "off"]`                                                                                                                    | `off`         |
 | `FIXED_NAVBAR`                      | `["on", "off"]`                                                                                                                    | `on`          |
+| `REMOVE_DEFAULT_FEEDS`              | `["on", "off"]`                                                                                                                    | `off`         |
\ No newline at end of file
diff --git a/src/settings.rs b/src/settings.rs
index 34718c28..4b30da37 100644
--- a/src/settings.rs
+++ b/src/settings.rs
@@ -22,7 +22,7 @@ struct SettingsTemplate {
 
 // CONSTANTS
 
-const PREFS: [&str; 18] = [
+const PREFS: [&str; 19] = [
 	"theme",
 	"front_page",
 	"layout",
@@ -41,6 +41,7 @@ const PREFS: [&str; 18] = [
 	"hide_score",
 	"disable_visit_reddit_confirmation",
 	"video_quality",
+	"remove_default_feeds",
 ];
 
 // FUNCTIONS
diff --git a/src/subreddit.rs b/src/subreddit.rs
index f7fe01d0..d03a9dd4 100644
--- a/src/subreddit.rs
+++ b/src/subreddit.rs
@@ -3,12 +3,13 @@
 use crate::{config, utils};
 // CRATES
 use crate::utils::{
-	catch_random, error, filter_posts, format_num, format_url, get_filters, nsfw_landing, param, redirect, rewrite_urls, setting, template, val, Post, Preferences, Subreddit,
+	catch_random, error, filter_posts, format_num, format_url, get_filters, info, nsfw_landing, param, redirect, rewrite_urls, setting, template, val, Post, Preferences,
+	Subreddit,
 };
 use crate::{client::json, server::RequestExt, server::ResponseExt};
 use cookie::Cookie;
 use hyper::{Body, Request, Response};
-use log::{debug, trace};
+use log::debug;
 use rinja::Template;
 
 use chrono::DateTime;
@@ -66,6 +67,7 @@ pub async fn community(req: Request<Body>) -> Result<Response<Body>, String> {
 	let query = req.uri().query().unwrap_or_default().to_string();
 	let subscribed = setting(&req, "subscriptions");
 	let front_page = setting(&req, "front_page");
+	let remove_default_feeds = setting(&req, "remove_default_feeds") == "on";
 	let post_sort = req.cookie("post_sort").map_or_else(|| "hot".to_string(), |c| c.value().to_string());
 	let sort = req.param("sort").unwrap_or_else(|| req.param("id").unwrap_or(post_sort));
 
@@ -78,6 +80,21 @@ pub async fn community(req: Request<Body>) -> Result<Response<Body>, String> {
 	} else {
 		front_page.clone()
 	});
+
+	if (sub_name == "popular" || sub_name == "all") && remove_default_feeds {
+		if subscribed.is_empty() {
+			return info(req, "Subscribe to some subreddits! (Default feeds disabled in settings)").await;
+		} else {
+			// If there are subscribed subs, but we get here, then the problem is that front_page pref is set to something besides default.
+			// Tell user to go to settings and change front page to default.
+			return info(
+				req,
+				"You have subscribed to some subreddits, but your front page is not set to default. Visit settings and change front page to default.",
+			)
+			.await;
+		}
+	}
+
 	let quarantined = can_access_quarantine(&req, &sub_name) || root;
 
 	// Handle random subreddits
diff --git a/src/utils.rs b/src/utils.rs
index 0aa3159a..2747449c 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -551,6 +551,14 @@ pub struct ErrorTemplate {
 	pub url: String,
 }
 
+#[derive(Template)]
+#[template(path = "info.html")]
+pub struct InfoTemplate {
+	pub msg: String,
+	pub prefs: Preferences,
+	pub url: String,
+}
+
 /// Template for NSFW landing page. The landing page is displayed when a page's
 /// content is wholly NSFW, but a user has not enabled the option to view NSFW
 /// posts.
@@ -636,6 +644,7 @@ pub struct Preferences {
 	pub filters: Vec<String>,
 	pub hide_awards: String,
 	pub hide_score: String,
+	pub remove_default_feeds: String,
 }
 
 fn serialize_vec_with_plus<S>(vec: &[String], serializer: S) -> Result<S::Ok, S::Error>
@@ -682,6 +691,7 @@ impl Preferences {
 			filters: setting(req, "filters").split('+').map(String::from).filter(|s| !s.is_empty()).collect(),
 			hide_awards: setting(req, "hide_awards"),
 			hide_score: setting(req, "hide_score"),
+			remove_default_feeds: setting(req, "remove_default_feeds"),
 		}
 	}
 
@@ -1265,6 +1275,20 @@ pub async fn error(req: Request<Body>, msg: &str) -> Result<Response<Body>, Stri
 	Ok(Response::builder().status(404).header("content-type", "text/html").body(body.into()).unwrap_or_default())
 }
 
+/// Renders a generic info landing page.
+pub async fn info(req: Request<Body>, msg: &str) -> Result<Response<Body>, String> {
+	let url = req.uri().to_string();
+	let body = InfoTemplate {
+		msg: msg.to_string(),
+		prefs: Preferences::new(&req),
+		url,
+	}
+	.render()
+	.unwrap_or_default();
+
+	Ok(Response::builder().status(200).header("content-type", "text/html").body(body.into()).unwrap_or_default())
+}
+
 /// Returns true if the config/env variable `REDLIB_SFW_ONLY` carries the
 /// value `on`.
 ///
@@ -1463,10 +1487,11 @@ mod tests {
 			filters: vec![],
 			hide_awards: "off".to_owned(),
 			hide_score: "off".to_owned(),
+			remove_default_feeds: "off".to_owned(),
 		};
 		let urlencoded = serde_urlencoded::to_string(prefs).expect("Failed to serialize Prefs");
 
-		assert_eq!(urlencoded, "theme=laserwave&front_page=default&layout=compact&wide=on&blur_spoiler=on&show_nsfw=off&blur_nsfw=on&hide_hls_notification=off&video_quality=best&hide_sidebar_and_summary=off&use_hls=on&autoplay_videos=on&fixed_navbar=on&disable_visit_reddit_confirmation=on&comment_sort=confidence&post_sort=top&subscriptions=memes%2Bmildlyinteresting&filters=&hide_awards=off&hide_score=off")
+		assert_eq!(urlencoded, "theme=laserwave&front_page=default&layout=compact&wide=on&blur_spoiler=on&show_nsfw=off&blur_nsfw=on&hide_hls_notification=off&video_quality=best&hide_sidebar_and_summary=off&use_hls=on&autoplay_videos=on&fixed_navbar=on&disable_visit_reddit_confirmation=on&comment_sort=confidence&post_sort=top&subscriptions=memes%2Bmildlyinteresting&filters=&hide_awards=off&hide_score=off&remove_default_feeds=off");
 	}
 }
 
diff --git a/templates/info.html b/templates/info.html
new file mode 100644
index 00000000..a14c1709
--- /dev/null
+++ b/templates/info.html
@@ -0,0 +1,20 @@
+{% extends "base.html" %}
+{% import "utils.html" as utils %}
+
+{% block title %}Info: {{ msg }}{% endblock %}
+{% block sortstyle %}{% endblock %}
+
+{% block subscriptions %}
+	{% call utils::sub_list("") %}
+{% endblock %}
+
+{% block search %}
+	{% call utils::search("".to_owned(), "") %}
+{% endblock %}
+
+{% block content %}
+<div id="error">
+	<h2>{{ msg }}</h2>
+	<br />
+</div>
+{% endblock %}
diff --git a/templates/settings.html b/templates/settings.html
index fef91cf3..79953121 100644
--- a/templates/settings.html
+++ b/templates/settings.html
@@ -26,6 +26,11 @@
 			</fieldset>
 			<fieldset>
 				<legend>Interface</legend>
+				<div class="prefs-group">
+					<label for="remove_default_feeds">Remove default feeds</label>
+					<input type="hidden" value="off" name="remove_default_feeds">
+					<input type="checkbox" name="remove_default_feeds" id="remove_default_feeds" {% if prefs.remove_default_feeds == "on" %}checked{% endif %}>
+				</div>
 				<div class="prefs-group">
 					<label for="front_page">Front page:</label>
 					<select name="front_page" id="front_page"> 
diff --git a/templates/utils.html b/templates/utils.html
index e0b3589c..4cee9798 100644
--- a/templates/utils.html
+++ b/templates/utils.html
@@ -43,8 +43,10 @@
 		<div id="feed_list">
 			<p>MAIN FEEDS</p>
 			<a href="/">Home</a>
-			<a href="/r/popular">Popular</a>
-			<a href="/r/all">All</a>
+			{% if prefs.remove_default_feeds != "on" %}
+				<a href="/r/popular">Popular</a>
+				<a href="/r/all">All</a>
+			{% endif %}
 			{% if prefs.subscriptions.len() > 0 %}
 				<p>REDDIT FEEDS</p>
 				{% for sub in prefs.subscriptions %}


Test patch: