diff --git a/locales/en-US.json b/locales/en-US.json index 5b2ef8d0e..7fe7eeae8 100644 --- a/locales/en-US.json +++ b/locales/en-US.json @@ -236,6 +236,7 @@ "This channel does not exist.": "This channel does not exist.", "Could not get channel info.": "Could not get channel info.", "Could not fetch comments": "Could not fetch comments", + "Comments are turned off.": "Comments are turned off.", "comments_view_x_replies": "View {{count}} reply", "comments_view_x_replies_plural": "View {{count}} replies", "`x` ago": "`x` ago", diff --git a/spec/invidious/comments/youtube_spec.cr b/spec/invidious/comments/youtube_spec.cr new file mode 100644 index 000000000..162e0f5b5 --- /dev/null +++ b/spec/invidious/comments/youtube_spec.cr @@ -0,0 +1,57 @@ +require "../../parsers_helper" +require "../../../src/invidious/helpers/helpers" +require "../../../src/invidious/helpers/i18next" +require "../../../src/invidious/helpers/i18n" +require "../../../src/invidious/yt_backend/youtube_api" +require "../../../src/invidious/frontend/comments_youtube" +require "../../../src/invidious/comments/youtube" + +Spectator.describe Invidious::Comments do + describe ".parse_youtube" do + it "shows a message when comments are disabled" do + response = JSON.parse({ + "onResponseReceivedEndpoints" => [ + { + "reloadContinuationItemsCommand" => { + "slot" => "RELOAD_CONTINUATION_SLOT_HEADER", + "continuationItems" => [ + { + "commentsHeaderRenderer" => { + "countText" => {"simpleText" => "0 Comments"}, + }, + }, + ], + }, + }, + { + "reloadContinuationItemsCommand" => { + "slot" => "RELOAD_CONTINUATION_SLOT_BODY", + }, + }, + ], + }.to_json) + + parsed = JSON.parse(Invidious::Comments.parse_youtube("video-id", response, "html", "en-US", false)) + + expect(parsed["contentHtml"].as_s).to contain("Comments are turned off.") + expect(parsed["commentCount"].as_i).to eq(0) + end + + it "marks disabled comments in JSON responses" do + response = JSON.parse({ + "onResponseReceivedEndpoints" => [ + { + "reloadContinuationItemsCommand" => { + "slot" => "RELOAD_CONTINUATION_SLOT_BODY", + }, + }, + ], + }.to_json) + + parsed = JSON.parse(Invidious::Comments.parse_youtube("video-id", response, "json", "en-US", false)) + + expect(parsed["comments"].as_a).to be_empty + expect(parsed["commentsDisabled"].as_bool).to be_true + end + end +end diff --git a/src/invidious/comments/youtube.cr b/src/invidious/comments/youtube.cr index 78a569ab0..a33be1598 100644 --- a/src/invidious/comments/youtube.cr +++ b/src/invidious/comments/youtube.cr @@ -94,9 +94,13 @@ module Invidious::Comments if !contents if format == "json" - return {"comments" => [] of String}.to_json + return { + "comments" => [] of String, + "commentsDisabled" => true, + }.to_json else - return {"contentHtml" => "", "commentCount" => 0}.to_json + message = HTML.escape(I18n.translate(locale, "Comments are turned off.")) + return {"contentHtml" => "
#{message}
", "commentCount" => 0}.to_json end end