Invidious is an alternative front-end to YouTube
Support 'sort_by' in reddit /api/v1/comments
Omar Roth80c1ebd76890
diff --git a/src/invidious.cr b/src/invidious.crindex 2d566ac0..d1388288 100644--- a/src/invidious.cr+++ b/src/invidious.cr@@ -1617,7 +1617,7 @@ get "/subscription_ajax" do |env|}post_url = "/subscription_ajax?#{action}=1&c=#{channel_id}"- # Sync subscription with YouTube+ # Sync subscriptions with YouTubeclient.post(post_url, headers, form: post_req)email = user.emailelse@@ -2778,12 +2778,12 @@ get "/api/v1/comments/:id" do |env|format = env.params.query["format"]?format ||= "json"- sort_by = env.params.query["sort_by"]?.try &.downcase- sort_by ||= "top"-continuation = env.params.query["continuation"]?+ sort_by = env.params.query["sort_by"]?.try &.downcaseif source == "youtube"+ sort_by ||= "top"+begincomments = fetch_youtube_comments(id, PG_DB, continuation, proxies, format, locale, thin_mode, region, sort_by: sort_by)rescue ex@@ -2794,8 +2794,10 @@ get "/api/v1/comments/:id" do |env|next commentselsif source == "reddit"+ sort_by ||= "confidence"+begin- comments, reddit_thread = fetch_reddit_comments(id)+ 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")diff --git a/src/invidious/comments.cr b/src/invidious/comments.crindex cd96565b..645ca2a8 100644--- a/src/invidious/comments.cr+++ b/src/invidious/comments.cr@@ -249,7 +249,7 @@ def fetch_youtube_comments(id, db, continuation, proxies, format, locale, thin_mreturn commentsend-def fetch_reddit_comments(id)+def fetch_reddit_comments(id, sort_by = "confidence")client = make_client(REDDIT_URL)headers = HTTP::Headers{"User-Agent" => "web:invidious:v#{CURRENT_VERSION} (by /u/omarroth)"}@@ -259,12 +259,16 @@ def fetch_reddit_comments(id)if search_results.status_code == 200search_results = RedditThing.from_json(search_results.body)+ # For videos that have more than one thread, choose the one with the highest scorethread = 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=top", headers).body+ result = client.get("/r/#{thread.subreddit}/comments/#{thread.id}.json?limit=100&sort=#{sort_by}", headers).bodyresult = Array(RedditThing).from_json(result)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.+result = client.get(search_results.headers["Location"], headers).bodyresult = Array(RedditThing).from_json(result)