Skip to content

Commit 84cc732

Browse files
committed
search functions: Don't return result count
This is useless, as the items count can be directly acessed using the '.size' method, so use that instead when needed.
1 parent 971b6ec commit 84cc732

File tree

7 files changed

+18
-23
lines changed

7 files changed

+18
-23
lines changed

src/invidious/routes/api/v1/channels.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ module Invidious::Routes::API::V1::Channels
254254
page = env.params.query["page"]?.try &.to_i?
255255
page ||= 1
256256

257-
count, search_results = channel_search(query, page, ucid)
257+
search_results = channel_search(query, page, ucid)
258258
JSON.build do |json|
259259
json.array do
260260
search_results.each do |item|

src/invidious/routes/api/v1/search.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ module Invidious::Routes::API::V1::Search
3232
return error_json(400, ex)
3333
end
3434

35-
count, search_results = search(query, search_params, region).as(Tuple)
35+
search_results = search(query, search_params, region)
3636
JSON.build do |json|
3737
json.array do
3838
search_results.each do |item|

src/invidious/routes/playlists.cr

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,15 +247,13 @@ module Invidious::Routes::Playlists
247247
query = env.params.query["q"]?
248248
if query
249249
begin
250-
search_query, count, items, operators = process_search_query(query, page, user, region: nil)
250+
search_query, items, operators = process_search_query(query, page, user, region: nil)
251251
videos = items.select(SearchVideo).map(&.as(SearchVideo))
252252
rescue ex
253253
videos = [] of SearchVideo
254-
count = 0
255254
end
256255
else
257256
videos = [] of SearchVideo
258-
count = 0
259257
end
260258

261259
env.set "add_playlist_items", plid

src/invidious/routes/search.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ module Invidious::Routes::Search
5454
user = env.get? "user"
5555

5656
begin
57-
search_query, count, videos, operators = process_search_query(query, page, user, region: region)
57+
search_query, videos, operators = process_search_query(query, page, user, region: region)
5858
rescue ex : ChannelSearchException
5959
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'.")
6060
rescue ex

src/invidious/search.cr

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class ChannelSearchException < InfoException
55
end
66
end
77

8-
def channel_search(query, page, channel)
8+
def channel_search(query, page, channel) : Array(SearchItem)
99
response = YT_POOL.client &.get("/channel/#{channel}")
1010

1111
if response.status_code == 404
@@ -24,25 +24,24 @@ def channel_search(query, page, channel)
2424
continuation_items = response_json["onResponseReceivedActions"]?
2525
.try &.[0]["appendContinuationItemsAction"]["continuationItems"]
2626

27-
return 0, [] of SearchItem if !continuation_items
27+
return [] of SearchItem if !continuation_items
2828

2929
items = [] of SearchItem
3030
continuation_items.as_a.select(&.as_h.has_key?("itemSectionRenderer")).each { |item|
3131
extract_item(item["itemSectionRenderer"]["contents"].as_a[0])
3232
.try { |t| items << t }
3333
}
3434

35-
return items.size, items
35+
return items
3636
end
3737

38-
def search(query, search_params = produce_search_params(content_type: "all"), region = nil)
39-
return 0, [] of SearchItem if query.empty?
38+
def search(query, search_params = produce_search_params(content_type: "all"), region = nil) : Array(SearchItem)
39+
return [] of SearchItem if query.empty?
4040

4141
client_config = YoutubeAPI::ClientConfig.new(region: region)
4242
initial_data = YoutubeAPI.search(query, search_params, client_config: client_config)
43-
items = extract_items(initial_data)
4443

45-
return items.size, items
44+
return extract_items(initial_data)
4645
end
4746

4847
def produce_search_params(page = 1, sort : String = "relevance", date : String = "", content_type : String = "",
@@ -217,7 +216,7 @@ def process_search_query(query, page, user, region)
217216
search_query = (query.split(" ") - operators).join(" ")
218217

219218
if channel
220-
count, items = channel_search(search_query, page, channel)
219+
items = channel_search(search_query, page, channel)
221220
elsif subscriptions
222221
if view_name
223222
items = PG_DB.query_all("SELECT id,title,published,updated,ucid,author,length_seconds FROM (
@@ -227,16 +226,14 @@ def process_search_query(query, page, user, region)
227226
as document
228227
FROM #{view_name}
229228
) v_search WHERE v_search.document @@ plainto_tsquery($1) LIMIT 20 OFFSET $2;", search_query, (page - 1) * 20, as: ChannelVideo)
230-
count = items.size
231229
else
232230
items = [] of ChannelVideo
233-
count = 0
234231
end
235232
else
236233
search_params = produce_search_params(page: page, sort: sort, date: date, content_type: content_type,
237234
duration: duration, features: features)
238235

239-
count, items = search(search_query, search_params, region).as(Tuple)
236+
items = search(search_query, search_params, region)
240237
end
241238

242239
# Light processing to flatten search results out of Categories.
@@ -254,5 +251,5 @@ def process_search_query(query, page, user, region)
254251
end
255252
end
256253

257-
{search_query, items_without_category.size, items_without_category, operators}
254+
{search_query, items_without_category, operators}
258255
end

src/invidious/views/add_playlist_items.ecr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
</div>
4949
<div class="pure-u-1 pure-u-lg-3-5"></div>
5050
<div class="pure-u-1 pure-u-lg-1-5" style="text-align:right">
51-
<% if count >= 20 %>
51+
<% if videos.size >= 20 %>
5252
<a href="/add_playlist_items?list=<%= plid %>&q=<%= URI.encode_www_form(query.not_nil!) %>&page=<%= page + 1 %>">
5353
<%= translate(locale, "Next page") %>
5454
</a>

src/invidious/views/search.ecr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<% search_query_encoded = env.get?("search").try { |x| URI.encode_www_form(x.as(String), space_to_plus: true) } %>
66

77
<!-- Search redirection and filtering UI -->
8-
<% if count == 0 %>
8+
<% if videos.size == 0 %>
99
<h3 style="text-align: center">
1010
<a href="/redirect?referer=<%= env.get?("current_page") %>"><%= translate(locale, "Broken? Try another Invidious Instance!") %></a>
1111
</h3>
@@ -98,7 +98,7 @@
9898
</details>
9999
<% end %>
100100

101-
<% if count == 0 %>
101+
<% if videos.size == 0 %>
102102
<hr style="margin: 0;"/>
103103
<% else %>
104104
<hr/>
@@ -114,7 +114,7 @@
114114
</div>
115115
<div class="pure-u-1 pure-u-lg-3-5"></div>
116116
<div class="pure-u-1 pure-u-lg-1-5" style="text-align:right">
117-
<% if count >= 20 %>
117+
<% if videos.size >= 20 %>
118118
<a href="/search?q=<%= search_query_encoded %>&page=<%= page + 1 %>">
119119
<%= translate(locale, "Next page") %>
120120
</a>
@@ -138,7 +138,7 @@
138138
</div>
139139
<div class="pure-u-1 pure-u-lg-3-5"></div>
140140
<div class="pure-u-1 pure-u-lg-1-5" style="text-align:right">
141-
<% if count >= 20 %>
141+
<% if videos.size >= 20 %>
142142
<a href="/search?q=<%= search_query_encoded %>&page=<%= page + 1 %>">
143143
<%= translate(locale, "Next page") %>
144144
</a>

0 commit comments

Comments
 (0)