From b2a738cf13919625e40bfcd31a0551c465a58941 Mon Sep 17 00:00:00 2001
From: matthewmcgarvey <matthewmcgarvey14@gmail.com>
Date: Mon, 17 Jan 2022 12:11:47 -0600
Subject: [PATCH 1/2] Fix loading reddit comments when there are no threads
 found

---
 src/invidious/comments.cr             | 19 +++++++++++--------
 src/invidious/routes/api/v1/videos.cr | 10 ++++------
 2 files changed, 15 insertions(+), 14 deletions(-)

diff --git a/src/invidious/comments.cr b/src/invidious/comments.cr
index 256a294e..5302cc53 100644
--- a/src/invidious/comments.cr
+++ b/src/invidious/comments.cr
@@ -268,18 +268,20 @@ def fetch_reddit_comments(id, sort_by = "confidence")
   headers = HTTP::Headers{"User-Agent" => "web:invidious:v#{CURRENT_VERSION} (by github.com/iv-org/invidious)"}
 
   # TODO: Use something like #479 for a static list of instances to use here
-  query = "(url:3D#{id}%20OR%20url:#{id})%20(site:invidio.us%20OR%20site:youtube.com%20OR%20site:youtu.be)"
-  search_results = client.get("/search.json?q=#{query}", headers)
+  query = URI::Params.encode({q: "(url:3D#{id} OR url:#{id}) AND (site:invidio.us OR site:youtube.com OR site:youtu.be)"})
+  search_results = client.get("/search.json?#{query}", headers)
 
   if search_results.status_code == 200
     search_results = RedditThing.from_json(search_results.body)
 
     # For videos that have more than one thread, choose the one with the highest score
-    thread = search_results.data.as(RedditListing).children.sort_by { |child| child.data.as(RedditLink).score }[-1]
-    thread = thread.data.as(RedditLink)
-
-    result = client.get("/r/#{thread.subreddit}/comments/#{thread.id}.json?limit=100&sort=#{sort_by}", headers).body
-    result = Array(RedditThing).from_json(result)
+    threads = search_results.data.as(RedditListing).children
+    thread = threads.max_by? { |child| child.data.as(RedditLink).score }.try(&.data.as(RedditLink))
+    result = thread.try do |t|
+      body = client.get("/r/#{t.subreddit}/comments/#{t.id}.json?limit=100&sort=#{sort_by}", headers).body
+      Array(RedditThing).from_json(body)
+    end
+    result ||= [] of RedditThing
   elsif search_results.status_code == 302
     # Previously, if there was only one result then the API would redirect to that result.
     # Now, it appears it will still return a listing so this section is likely unnecessary.
@@ -294,7 +296,8 @@ def fetch_reddit_comments(id, sort_by = "confidence")
 
   client.close
 
-  comments = result[1].data.as(RedditListing).children
+  comments = result[1]?.try(&.data.as(RedditListing).children)
+  comments ||= [] of RedditThing
   return comments, thread
 end
 
diff --git a/src/invidious/routes/api/v1/videos.cr b/src/invidious/routes/api/v1/videos.cr
index 4d244e7f..3a013ba0 100644
--- a/src/invidious/routes/api/v1/videos.cr
+++ b/src/invidious/routes/api/v1/videos.cr
@@ -330,18 +330,13 @@ module Invidious::Routes::API::V1::Videos
 
       begin
         comments, reddit_thread = fetch_reddit_comments(id, sort_by: sort_by)
-        content_html = template_reddit_comments(comments, locale)
-
-        content_html = fill_links(content_html, "https", "www.reddit.com")
-        content_html = replace_links(content_html)
       rescue ex
         comments = nil
         reddit_thread = nil
-        content_html = ""
       end
 
       if !reddit_thread || !comments
-        haltf env, 404
+        return error_json(404, "No reddit threads found")
       end
 
       if format == "json"
@@ -350,6 +345,9 @@ module Invidious::Routes::API::V1::Videos
 
         return reddit_thread.to_json
       else
+        content_html = template_reddit_comments(comments, locale)
+        content_html = fill_links(content_html, "https", "www.reddit.com")
+        content_html = replace_links(content_html)
         response = {
           "title"       => reddit_thread.title,
           "permalink"   => reddit_thread.permalink,

From 9233f71549ce397dc82cba90d0379bc461f64e4b Mon Sep 17 00:00:00 2001
From: matthewmcgarvey <matthewmcgarvey14@gmail.com>
Date: Mon, 17 Jan 2022 13:03:36 -0600
Subject: [PATCH 2/2] Use &.methods where possible instead of curly braces

---
 src/invidious/comments.cr | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/invidious/comments.cr b/src/invidious/comments.cr
index 5302cc53..dda92440 100644
--- a/src/invidious/comments.cr
+++ b/src/invidious/comments.cr
@@ -276,7 +276,7 @@ def fetch_reddit_comments(id, sort_by = "confidence")
 
     # For videos that have more than one thread, choose the one with the highest score
     threads = search_results.data.as(RedditListing).children
-    thread = threads.max_by? { |child| child.data.as(RedditLink).score }.try(&.data.as(RedditLink))
+    thread = threads.max_by?(&.data.as(RedditLink).score).try(&.data.as(RedditLink))
     result = thread.try do |t|
       body = client.get("/r/#{t.subreddit}/comments/#{t.id}.json?limit=100&sort=#{sort_by}", headers).body
       Array(RedditThing).from_json(body)