Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: trending_enabled and search_enabled params added #4674

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions config/config.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions src/invidious/config.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/invidious/routes/api/v1/feeds.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
NorkzYT marked this conversation as resolved.
Show resolved Hide resolved
end

region = env.params.query["region"]?
trending_type = env.params.query["type"]?

Expand Down
5 changes: 5 additions & 0 deletions src/invidious/routes/api/v1/search.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 17 additions & 12 deletions src/invidious/routes/feeds.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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"

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

trending_type = env.params.query["type"]?
trending_type ||= "Default"

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)
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)
Expand Down
8 changes: 8 additions & 0 deletions src/invidious/routes/preferences.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
67 changes: 36 additions & 31 deletions src/invidious/routes/search.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
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}"
else
env.set "search", query.text
end

templated "search"
end

templated "search"
else
message = translate(locale, "Search has been disabled by the administrator.")
templated "message"
end
end

Expand Down
9 changes: 9 additions & 0 deletions src/invidious/views/user/preferences.ecr
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,15 @@
<input name="popular_enabled" id="popular_enabled" type="checkbox" <% if CONFIG.popular_enabled %>checked<% end %>>
</div>

<div class="pure-control-group">
<label for="trending_enabled"><%= translate(locale, "Trending enabled: ") %></label>
<input name="trending_enabled" id="trending_enabled" type="checkbox" <% if CONFIG.trending_enabled %>checked<% end %>>
</div>

<div class="pure-control-group">
<label for="search_enabled"><%= translate(locale, "Search enabled: ") %></label>
<input name="search_enabled" id="search_enabled" type="checkbox" <% if CONFIG.search_enabled %>checked<% end %>>
</div>

<div class="pure-control-group">
<label for="captcha_enabled"><%= translate(locale, "CAPTCHA enabled: ") %></label>
Expand Down