From da422fae05da70740be881c12f82c955f9fb14b7 Mon Sep 17 00:00:00 2001 From: Richard Lora Date: Sun, 30 Jun 2024 18:05:26 -0400 Subject: [PATCH] feat(invidious): specific pages are disabled with the use of an array --- config/config.example.yml | 27 ++++++------------------ src/invidious.cr | 2 +- src/invidious/config.cr | 9 +++++--- src/invidious/routes/api/v1/feeds.cr | 4 ++-- src/invidious/routes/api/v1/search.cr | 2 +- src/invidious/routes/feeds.cr | 4 ++-- src/invidious/routes/preferences.cr | 19 ++++++----------- src/invidious/routes/search.cr | 4 ++-- src/invidious/views/user/preferences.ecr | 6 +++--- 9 files changed, 30 insertions(+), 47 deletions(-) diff --git a/config/config.example.yml b/config/config.example.yml index 9f0c252a5..8749ec572 100644 --- a/config/config.example.yml +++ b/config/config.example.yml @@ -203,28 +203,13 @@ https_only: false # ----------------------------- ## -## Enable/Disable the "Popular" tab on the main page. +## Enable/Disable specific pages on the main page. +## Example: +## pages_enabled: +## trending: true +## popular: true +## search: true ## -## Accepted values: true, false -## Default: true -## -#popular_enabled: true - -## -## Enable/Disable the "Trending" tab on the main page. -## -## Accepted values: true, false -## Default: true -## -#trending_enabled: true - -## -## Enable/Disable "Search" on the main page. -## -## Accepted values: true, false -## Default: true -## -#search_enabled: true ## ## Enable/Disable statstics (available at /api/v1/stats). diff --git a/src/invidious.cr b/src/invidious.cr index e0bd01015..d0a02a3dd 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -176,7 +176,7 @@ if (CONFIG.use_pubsub_feeds.is_a?(Bool) && CONFIG.use_pubsub_feeds.as(Bool)) || Invidious::Jobs.register Invidious::Jobs::SubscribeToFeedsJob.new(PG_DB, HMAC_KEY) end -if CONFIG.popular_enabled +if CONFIG.page_enabled?("popular") Invidious::Jobs.register Invidious::Jobs::PullPopularVideosJob.new(PG_DB) end diff --git a/src/invidious/config.cr b/src/invidious/config.cr index f6146cd78..085aeef89 100644 --- a/src/invidious/config.cr +++ b/src/invidious/config.cr @@ -90,9 +90,7 @@ class Config property domain : String? # Subscribe to channels using PubSubHubbub (requires domain, hmac_key) property use_pubsub_feeds : Bool | Int32 = false - property popular_enabled : Bool = true - property trending_enabled : Bool = true - property search_enabled : Bool = true + property pages_enabled : Hash(String, Bool) = {"trending" => true, "popular" => true, "search" => true} property captcha_enabled : Bool = true property login_enabled : Bool = true property registration_enabled : Bool = true @@ -154,6 +152,11 @@ class Config end end + def page_enabled?(page : String) : Bool + @pages_enabled[page]? || false + end + + def self.load # Load config from file or YAML string env var env_config_file = "INVIDIOUS_CONFIG_FILE" diff --git a/src/invidious/routes/api/v1/feeds.cr b/src/invidious/routes/api/v1/feeds.cr index 3a4835260..bfec4fb6f 100644 --- a/src/invidious/routes/api/v1/feeds.cr +++ b/src/invidious/routes/api/v1/feeds.cr @@ -4,7 +4,7 @@ module Invidious::Routes::API::V1::Feeds env.response.content_type = "application/json" - if !CONFIG.trending_enabled + if !CONFIG.page_enabled?("trending") error_message = {"error" => "Administrator has disabled this endpoint."}.to_json haltf env, 403, error_message end @@ -34,7 +34,7 @@ module Invidious::Routes::API::V1::Feeds env.response.content_type = "application/json" - if !CONFIG.popular_enabled + if !CONFIG.page_enabled?("popular") error_message = {"error" => "Administrator has disabled this endpoint."}.to_json haltf env, 403, error_message end diff --git a/src/invidious/routes/api/v1/search.cr b/src/invidious/routes/api/v1/search.cr index 3c44a989d..48df3a2ba 100644 --- a/src/invidious/routes/api/v1/search.cr +++ b/src/invidious/routes/api/v1/search.cr @@ -5,7 +5,7 @@ module Invidious::Routes::API::V1::Search env.response.content_type = "application/json" - if !CONFIG.search_enabled + if !CONFIG.page_enabled?("search") error_message = {"error" => "Administrator has disabled this endpoint."}.to_json haltf env, 403, error_message end diff --git a/src/invidious/routes/feeds.cr b/src/invidious/routes/feeds.cr index 693becb83..114b9edc7 100644 --- a/src/invidious/routes/feeds.cr +++ b/src/invidious/routes/feeds.cr @@ -34,7 +34,7 @@ module Invidious::Routes::Feeds def self.popular(env) locale = env.get("preferences").as(Preferences).locale - if CONFIG.popular_enabled + if CONFIG.page_enabled?("popular") templated "feeds/popular" else message = translate(locale, "The Popular feed has been disabled by the administrator.") @@ -45,7 +45,7 @@ module Invidious::Routes::Feeds def self.trending(env) locale = env.get("preferences").as(Preferences).locale - if CONFIG.trending_enabled + if CONFIG.page_enabled?("trending") trending_type = env.params.query["type"]? trending_type ||= "Default" diff --git a/src/invidious/routes/preferences.cr b/src/invidious/routes/preferences.cr index 9aee0d8c9..9106d4cf3 100644 --- a/src/invidious/routes/preferences.cr +++ b/src/invidious/routes/preferences.cr @@ -194,17 +194,12 @@ module Invidious::Routes::PreferencesRoute end CONFIG.default_user_preferences.feed_menu = admin_feed_menu - popular_enabled = env.params.body["popular_enabled"]?.try &.as(String) - popular_enabled ||= "off" - CONFIG.popular_enabled = popular_enabled == "on" - - trending_enabled = env.params.body["trending_enabled"]?.try &.as(String) - trending_enabled ||= "off" - CONFIG.trending_enabled = trending_enabled == "on" - - search_enabled = env.params.body["search_enabled"]?.try &.as(String) - search_enabled ||= "off" - CONFIG.search_enabled = search_enabled == "on" + pages_enabled = { + "popular" => (env.params.body["popular_enabled"]?.try &.as(String) || "off") == "on", + "trending" => (env.params.body["trending_enabled"]?.try &.as(String) || "off") == "on", + "search" => (env.params.body["search_enabled"]?.try &.as(String) || "off") == "on" + } + CONFIG.pages_enabled = pages_enabled captcha_enabled = env.params.body["captcha_enabled"]?.try &.as(String) captcha_enabled ||= "off" @@ -355,4 +350,4 @@ module Invidious::Routes::PreferencesRoute env.redirect referer end -end +end \ No newline at end of file diff --git a/src/invidious/routes/search.cr b/src/invidious/routes/search.cr index ae7d98336..f825ffc7b 100644 --- a/src/invidious/routes/search.cr +++ b/src/invidious/routes/search.cr @@ -40,7 +40,7 @@ module Invidious::Routes::Search prefs = env.get("preferences").as(Preferences) locale = prefs.locale - if CONFIG.search_enabled + if CONFIG.page_enabled?("search") region = env.params.query["region"]? || prefs.region query = Invidious::Search::Query.new(env.params.query, :regular, region) @@ -115,4 +115,4 @@ module Invidious::Routes::Search templated "hashtag" end -end +end \ No newline at end of file diff --git a/src/invidious/views/user/preferences.ecr b/src/invidious/views/user/preferences.ecr index 4cda84212..564eca9e8 100644 --- a/src/invidious/views/user/preferences.ecr +++ b/src/invidious/views/user/preferences.ecr @@ -284,17 +284,17 @@
- checked<% end %>> + checked<% end %>>
- checked<% end %>> + checked<% end %>>
- checked<% end %>> + checked<% end %>>