Invidious is an alternative front-end to YouTube
fix comment replies
Andrew Zhao89fd35e02d0e
diff --git a/assets/js/handlers.js b/assets/js/handlers.jsindex b3da8d9b..1498f39a 100644--- a/assets/js/handlers.js+++ b/assets/js/handlers.js@@ -22,7 +22,8 @@break;case 'get_youtube_replies':var load_more = e.getAttribute('data-load-more') !== null;- get_youtube_replies(e, load_more);+ var load_replies = e.getAttribute('data-load-replies') !== null;+ get_youtube_replies(e, load_more, load_replies);break;case 'toggle_parent':toggle_parent(e);diff --git a/assets/js/watch.js b/assets/js/watch.jsindex eb493bf3..3909edd4 100644--- a/assets/js/watch.js+++ b/assets/js/watch.js@@ -359,7 +359,7 @@ function get_youtube_comments(retries) {xhr.send();}-function get_youtube_replies(target, load_more) {+function get_youtube_replies(target, load_more, load_replies) {var continuation = target.getAttribute('data-continuation');var body = target.parentNode.parentNode;@@ -371,7 +371,10 @@ function get_youtube_replies(target, load_more) {'?format=html' +'&hl=' + video_data.preferences.locale +'&thin_mode=' + video_data.preferences.thin_mode +- '&continuation=' + continuation;+ '&continuation=' + continuation+ if (load_replies) {+ url += '&action=action_get_comment_replies';+ }var xhr = new XMLHttpRequest();xhr.responseType = 'json';xhr.timeout = 10000;diff --git a/src/invidious.cr b/src/invidious.crindex 89d99ecc..8d579f92 100644--- a/src/invidious.cr+++ b/src/invidious.cr@@ -2054,6 +2054,9 @@ get "/api/v1/comments/:id" do |env|format = env.params.query["format"]?format ||= "json"+ action = env.params.query["action"]?+ action ||= "action_get_comments"+continuation = env.params.query["continuation"]?sort_by = env.params.query["sort_by"]?.try &.downcase@@ -2061,7 +2064,7 @@ get "/api/v1/comments/:id" do |env|sort_by ||= "top"begin- comments = fetch_youtube_comments(id, PG_DB, continuation, format, locale, thin_mode, region, sort_by: sort_by)+ comments = fetch_youtube_comments(id, PG_DB, continuation, format, locale, thin_mode, region, sort_by: sort_by, action: action)rescue exnext error_json(500, ex)enddiff --git a/src/invidious/comments.cr b/src/invidious/comments.crindex 20e64a08..e7e87203 100644--- a/src/invidious/comments.cr+++ b/src/invidious/comments.cr@@ -56,7 +56,7 @@ class RedditListingproperty modhash : Stringend-def fetch_youtube_comments(id, db, cursor, format, locale, thin_mode, region, sort_by = "top")+def fetch_youtube_comments(id, db, cursor, format, locale, thin_mode, region, sort_by = "top", action = "action_get_comments")video = get_video(id, db, region: region)session_token = video.session_token@@ -88,9 +88,14 @@ def fetch_youtube_comments(id, db, cursor, format, locale, thin_mode, region, so"cookie" => video.cookie,}- response = YT_POOL.client(region, &.post("/comment_service_ajax?action_get_comments=1&hl=en&gl=US&pbj=1", headers, form: post_req))+ response = YT_POOL.client(region, &.post("/comment_service_ajax?#{action}=1&hl=en&gl=US&pbj=1", headers, form: post_req))response = JSON.parse(response.body)+ # For some reason youtube puts it in an array for comment_replies but otherwise it's the same+ if action == "action_get_comment_replies"+ response = response[1]+ end+if !response["response"]["continuationContents"]?raise InfoException.new("Could not fetch comments")end@@ -228,7 +233,7 @@ def fetch_youtube_comments(id, db, cursor, format, locale, thin_mode, region, soif format == "html"response = JSON.parse(response)- content_html = template_youtube_comments(response, locale, thin_mode)+ content_html = template_youtube_comments(response, locale, thin_mode, action == "action_get_comment_replies")response = JSON.build do |json|json.object do@@ -281,7 +286,7 @@ def fetch_reddit_comments(id, sort_by = "confidence")return comments, threadend-def template_youtube_comments(comments, locale, thin_mode)+def template_youtube_comments(comments, locale, thin_mode, is_replies = false)String.build do |html|root = comments["comments"].as_aroot.each do |child|@@ -292,7 +297,7 @@ def template_youtube_comments(comments, locale, thin_mode)<div class="pure-u-23-24"><p><a href="javascript:void(0)" data-continuation="#{child["replies"]["continuation"]}"- data-onclick="get_youtube_replies">#{translate(locale, "View `x` replies", number_with_separator(child["replies"]["replyCount"]))}</a>+ data-onclick="get_youtube_replies" data-load-replies>#{translate(locale, "View `x` replies", number_with_separator(child["replies"]["replyCount"]))}</a></p></div></div>@@ -412,7 +417,7 @@ def template_youtube_comments(comments, locale, thin_mode)<div class="pure-u-1"><p><a href="javascript:void(0)" data-continuation="#{comments["continuation"]}"- data-onclick="get_youtube_replies" data-load-more>#{translate(locale, "Load more")}</a>+ data-onclick="get_youtube_replies" data-load-more #{"data-load-replies" if is_replies}>#{translate(locale, "Load more")}</a></p></div></div>