From 89fd35e02d0e8de585c3f5d619fb45986ac1173a Mon Sep 17 00:00:00 2001 From: Andrew Zhao Date: Thu, 18 Mar 2021 01:23:32 -0400 Subject: [PATCH] fix comment replies --- assets/js/handlers.js | 3 ++- assets/js/watch.js | 7 +++++-- src/invidious.cr | 5 ++++- src/invidious/comments.cr | 17 +++++++++++------ 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/assets/js/handlers.js b/assets/js/handlers.js index 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.js index 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.cr index 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 ex next error_json(500, ex) end diff --git a/src/invidious/comments.cr b/src/invidious/comments.cr index 20e64a08..e7e87203 100644 --- a/src/invidious/comments.cr +++ b/src/invidious/comments.cr @@ -56,7 +56,7 @@ class RedditListing property modhash : String end -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, so if 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, thread end -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_a root.each do |child| @@ -292,7 +297,7 @@ def template_youtube_comments(comments, locale, thin_mode)

#{translate(locale, "View `x` replies", number_with_separator(child["replies"]["replyCount"]))} + data-onclick="get_youtube_replies" data-load-replies>#{translate(locale, "View `x` replies", number_with_separator(child["replies"]["replyCount"]))}

@@ -412,7 +417,7 @@ def template_youtube_comments(comments, locale, thin_mode)

#{translate(locale, "Load more")} + data-onclick="get_youtube_replies" data-load-more #{"data-load-replies" if is_replies}>#{translate(locale, "Load more")}