From e613e3c08c0b807e012ce08e41afa565d57ed39d Mon Sep 17 00:00:00 2001 From: Richard Lora Date: Tue, 30 Apr 2024 16:34:40 -0400 Subject: [PATCH 1/4] feat: optional `trending_enabled` and `search_enabled` params added --- config/config.example.yml | 16 ++++++ src/invidious/config.cr | 2 + src/invidious/routes/api/v1/feeds.cr | 5 ++ src/invidious/routes/api/v1/search.cr | 5 ++ src/invidious/routes/feeds.cr | 25 +++++---- src/invidious/routes/preferences.cr | 8 +++ src/invidious/routes/search.cr | 67 +++++++++++++----------- src/invidious/views/user/preferences.ecr | 9 ++++ 8 files changed, 96 insertions(+), 41 deletions(-) diff --git a/config/config.example.yml b/config/config.example.yml index 38085a20..9f0c252a 100644 --- a/config/config.example.yml +++ b/config/config.example.yml @@ -210,6 +210,22 @@ https_only: false ## #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). ## The following data is available: diff --git a/src/invidious/config.cr b/src/invidious/config.cr index 09c2168b..f6146cd7 100644 --- a/src/invidious/config.cr +++ b/src/invidious/config.cr @@ -91,6 +91,8 @@ class Config # 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 captcha_enabled : Bool = true property login_enabled : Bool = true property registration_enabled : Bool = true diff --git a/src/invidious/routes/api/v1/feeds.cr b/src/invidious/routes/api/v1/feeds.cr index 41865f34..f6feab5f 100644 --- a/src/invidious/routes/api/v1/feeds.cr +++ b/src/invidious/routes/api/v1/feeds.cr @@ -4,6 +4,11 @@ module Invidious::Routes::API::V1::Feeds env.response.content_type = "application/json" + if !CONFIG.trending_enabled + error_message = {"error" => "Administrator has disabled this endpoint."}.to_json + haltf env, 400, error_message + end + region = env.params.query["region"]? trending_type = env.params.query["type"]? diff --git a/src/invidious/routes/api/v1/search.cr b/src/invidious/routes/api/v1/search.cr index 2922b060..526914b0 100644 --- a/src/invidious/routes/api/v1/search.cr +++ b/src/invidious/routes/api/v1/search.cr @@ -5,6 +5,11 @@ module Invidious::Routes::API::V1::Search env.response.content_type = "application/json" + if !CONFIG.search_enabled + error_message = {"error" => "Administrator has disabled this endpoint."}.to_json + haltf env, 400, error_message + end + query = Invidious::Search::Query.new(env.params.query, :regular, region) begin diff --git a/src/invidious/routes/feeds.cr b/src/invidious/routes/feeds.cr index e20a7139..22a23a7e 100644 --- a/src/invidious/routes/feeds.cr +++ b/src/invidious/routes/feeds.cr @@ -44,20 +44,25 @@ module Invidious::Routes::Feeds def self.trending(env) locale = env.get("preferences").as(Preferences).locale + + if CONFIG.trending_enabled + trending_type = env.params.query["type"]? + trending_type ||= "Default" - trending_type = env.params.query["type"]? - trending_type ||= "Default" + region = env.params.query["region"]? + region ||= env.get("preferences").as(Preferences).region - region = env.params.query["region"]? - region ||= env.get("preferences").as(Preferences).region + begin + trending, plid = fetch_trending(trending_type, region, locale) + rescue ex + return error_template(500, ex) + end - begin - trending, plid = fetch_trending(trending_type, region, locale) - rescue ex - return error_template(500, ex) + templated "feeds/trending" + else + message = translate(locale, "The Trending feed has been disabled by the administrator.") + templated "message" end - - templated "feeds/trending" end def self.subscriptions(env) diff --git a/src/invidious/routes/preferences.cr b/src/invidious/routes/preferences.cr index 112535bd..9aee0d8c 100644 --- a/src/invidious/routes/preferences.cr +++ b/src/invidious/routes/preferences.cr @@ -198,6 +198,14 @@ module Invidious::Routes::PreferencesRoute 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" + captcha_enabled = env.params.body["captcha_enabled"]?.try &.as(String) captcha_enabled ||= "off" CONFIG.captcha_enabled = captcha_enabled == "on" diff --git a/src/invidious/routes/search.cr b/src/invidious/routes/search.cr index 5be33533..ae7d9833 100644 --- a/src/invidious/routes/search.cr +++ b/src/invidious/routes/search.cr @@ -40,41 +40,46 @@ module Invidious::Routes::Search prefs = env.get("preferences").as(Preferences) locale = prefs.locale - region = env.params.query["region"]? || prefs.region + if CONFIG.search_enabled + region = env.params.query["region"]? || prefs.region - query = Invidious::Search::Query.new(env.params.query, :regular, region) + query = Invidious::Search::Query.new(env.params.query, :regular, region) - if query.empty? - # Display the full page search box implemented in #1977 - env.set "search", "" - templated "search_homepage", navbar_search: false - else - user = env.get? "user" - - begin - items = query.process - rescue ex : ChannelSearchException - return error_template(404, "Unable to find channel with id of '#{HTML.escape(ex.channel)}'. Are you sure that's an actual channel id? It should look like 'UC4QobU6STFB0P71PMvOGN5A'.") - rescue ex - return error_template(500, ex) - end - - redirect_url = Invidious::Frontend::Misc.redirect_url(env) - - # Pagination - page_nav_html = Frontend::Pagination.nav_numeric(locale, - base_url: "/search?#{query.to_http_params}", - current_page: query.page, - show_next: (items.size >= 20) - ) - - if query.type == Invidious::Search::Query::Type::Channel - env.set "search", "channel:#{query.channel} #{query.text}" + if query.empty? + # Display the full page search box implemented in #1977 + env.set "search", "" + templated "search_homepage", navbar_search: false else - env.set "search", query.text - end + user = env.get? "user" - templated "search" + begin + items = query.process + rescue ex : ChannelSearchException + return error_template(404, "Unable to find channel with id of '#{HTML.escape(ex.channel)}'. Are you sure that's an actual channel id? It should look like 'UC4QobU6STFB0P71PMvOGN5A'.") + rescue ex + return error_template(500, ex) + end + + redirect_url = Invidious::Frontend::Misc.redirect_url(env) + + # Pagination + page_nav_html = Frontend::Pagination.nav_numeric(locale, + base_url: "/search?#{query.to_http_params}", + current_page: query.page, + show_next: (items.size >= 20) + ) + + if query.type == Invidious::Search::Query::Type::Channel + env.set "search", "channel:#{query.channel} #{query.text}" + else + env.set "search", query.text + end + + templated "search" + end + else + message = translate(locale, "Search has been disabled by the administrator.") + templated "message" end end diff --git a/src/invidious/views/user/preferences.ecr b/src/invidious/views/user/preferences.ecr index 55349c5a..4cda8421 100644 --- a/src/invidious/views/user/preferences.ecr +++ b/src/invidious/views/user/preferences.ecr @@ -287,6 +287,15 @@ checked<% end %>> +
+ + checked<% end %>> +
+ +
+ + checked<% end %>> +
From 5fa293541b05aa9fc0be404e5e872cb991cbe423 Mon Sep 17 00:00:00 2001 From: Richard Lora Date: Fri, 3 May 2024 10:36:43 -0400 Subject: [PATCH 2/4] fix(feeds.cr): http status code --- src/invidious/routes/api/v1/feeds.cr | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/invidious/routes/api/v1/feeds.cr b/src/invidious/routes/api/v1/feeds.cr index f6feab5f..3a483526 100644 --- a/src/invidious/routes/api/v1/feeds.cr +++ b/src/invidious/routes/api/v1/feeds.cr @@ -6,7 +6,7 @@ module Invidious::Routes::API::V1::Feeds if !CONFIG.trending_enabled error_message = {"error" => "Administrator has disabled this endpoint."}.to_json - haltf env, 400, error_message + haltf env, 403, error_message end region = env.params.query["region"]? @@ -36,7 +36,7 @@ module Invidious::Routes::API::V1::Feeds if !CONFIG.popular_enabled error_message = {"error" => "Administrator has disabled this endpoint."}.to_json - haltf env, 400, error_message + haltf env, 403, error_message end JSON.build do |json| From 3abf21625cc88d92815814a6217d7a173eec1336 Mon Sep 17 00:00:00 2001 From: Richard Lora Date: Fri, 3 May 2024 10:38:57 -0400 Subject: [PATCH 3/4] fix(search.cr): http status code --- src/invidious/routes/api/v1/search.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/invidious/routes/api/v1/search.cr b/src/invidious/routes/api/v1/search.cr index 526914b0..3c44a989 100644 --- a/src/invidious/routes/api/v1/search.cr +++ b/src/invidious/routes/api/v1/search.cr @@ -7,7 +7,7 @@ module Invidious::Routes::API::V1::Search if !CONFIG.search_enabled error_message = {"error" => "Administrator has disabled this endpoint."}.to_json - haltf env, 400, error_message + haltf env, 403, error_message end query = Invidious::Search::Query.new(env.params.query, :regular, region) From e0f20b06418e1a93ae6c0e27e8e2871970c92803 Mon Sep 17 00:00:00 2001 From: Richard Lora Date: Fri, 3 May 2024 14:00:28 -0400 Subject: [PATCH 4/4] style(feeds.cr): fix code formatting --- src/invidious/routes/feeds.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/invidious/routes/feeds.cr b/src/invidious/routes/feeds.cr index 22a23a7e..693becb8 100644 --- a/src/invidious/routes/feeds.cr +++ b/src/invidious/routes/feeds.cr @@ -44,7 +44,7 @@ module Invidious::Routes::Feeds def self.trending(env) locale = env.get("preferences").as(Preferences).locale - + if CONFIG.trending_enabled trending_type = env.params.query["type"]? trending_type ||= "Default"