chore: Completely remove Reddit from Invidious (#6087)

* chore: Completely remove Reddit from Invidious

* chore: remove reddit from config.example.yml

* fix comment preferences for nojs

* Add fallback for people that used reddit in the comments API and their preferences

* add missing TODO

* chore: remove unused swap_comments JS function from watch.js

* remove blank line
This commit is contained in:
Fijxu
2026-10-23 19:05:40 -03:00
committed by GitHub
parent 3d23fdccbb
commit 66fed560e1
71 changed files with 41 additions and 584 deletions

View File

@@ -1,76 +0,0 @@
module Invidious::Comments
extend self
def replace_links(html)
# Check if the document is empty
# Prevents edge-case bug with Reddit comments, see issue #3115
if html.nil? || html.empty?
return html
end
html = XML.parse_html(html)
html.xpath_nodes(%q(//a)).each do |anchor|
url = URI.parse(anchor["href"])
if url.host.nil? || url.host.not_nil!.ends_with?("youtube.com") || url.host.not_nil!.ends_with?("youtu.be")
if url.host.try &.ends_with? "youtu.be"
anchor["href"] = "/watch?v=#{url.path.lstrip('/')}#{url.query_params}"
else
if url.path == "/redirect"
params = HTTP::Params.parse(url.query.not_nil!)
anchor["href"] = params["q"]?
else
anchor["href"] = url.request_target
end
end
elsif url.to_s == "#"
begin
length_seconds = decode_length_seconds(anchor.content)
rescue
length_seconds = decode_time(anchor.content)
end
if length_seconds > 0
anchor["href"] = "javascript:void(0)"
anchor["onclick"] = "player.currentTime(#{length_seconds})"
else
anchor["href"] = url.request_target
end
end
end
html = html.xpath_node(%q(//body)).not_nil!
if node = html.xpath_node(%q(./p))
html = node
end
return html.to_xml(options: XML::SaveOptions::NO_DECL)
end
def fill_links(html, scheme, host)
# Check if the document is empty
# Prevents edge-case bug with Reddit comments, see issue #3115
if html.nil? || html.empty?
return html
end
html = XML.parse_html(html)
html.xpath_nodes("//a").each do |match|
url = URI.parse(match["href"])
# Reddit links don't have host
if !url.host && !match["href"].starts_with?("javascript") && !url.to_s.ends_with? "#"
url.scheme = scheme
url.host = host
match["href"] = url
end
end
if host == "www.youtube.com"
html = html.xpath_node(%q(//body/p)).not_nil!
end
return html.to_xml(options: XML::SaveOptions::NO_DECL)
end
end

View File

@@ -1,42 +0,0 @@
module Invidious::Comments
extend self
private REDDIT_URL = URI.parse("https://www.reddit.com")
def fetch_reddit(id, sort_by = "confidence")
client = make_client(REDDIT_URL)
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 = 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
threads = search_results.data.as(RedditListing).children
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)
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.
result = client.get(search_results.headers["Location"], headers).body
result = Array(RedditThing).from_json(result)
thread = result[0].data.as(RedditListing).children[0].data.as(RedditLink)
else
raise NotFoundException.new("Comments not found.")
end
client.close
comments = result[1]?.try(&.data.as(RedditListing).children)
comments ||= [] of RedditThing
return comments, thread
end
end

View File

@@ -1,57 +0,0 @@
class RedditThing
include JSON::Serializable
property kind : String
property data : RedditComment | RedditLink | RedditMore | RedditListing
end
class RedditComment
include JSON::Serializable
property author : String
property body_html : String
property replies : RedditThing | String
property score : Int32
property depth : Int32
property permalink : String
@[JSON::Field(converter: RedditComment::TimeConverter)]
property created_utc : Time
module TimeConverter
def self.from_json(value : JSON::PullParser) : Time
Time.unix(value.read_float.to_i)
end
def self.to_json(value : Time, json : JSON::Builder)
json.number(value.to_unix)
end
end
end
struct RedditLink
include JSON::Serializable
property author : String
property score : Int32
property subreddit : String
property num_comments : Int32
property id : String
property permalink : String
property title : String
end
struct RedditMore
include JSON::Serializable
property children : Array(String)
property count : Int32
property depth : Int32
end
class RedditListing
include JSON::Serializable
property children : Array(RedditThing)
property modhash : String
end

View File

@@ -50,3 +50,9 @@ class InvalidVideoID < InfoException
return "Invalid video ID '#{id}'"
end
end
class RedditIsDeprecated < InfoException
def message
return "Reddit comments have been removed from Invidious."
end
end

View File

@@ -1,50 +0,0 @@
module Invidious::Frontend::Comments
extend self
def template_reddit(root, locale)
String.build do |html|
root.each do |child|
if child.data.is_a?(RedditComment)
child = child.data.as(RedditComment)
body_html = HTML.unescape(child.body_html)
replies_html = ""
if child.replies.is_a?(RedditThing)
replies = child.replies.as(RedditThing)
replies_html = self.template_reddit(replies.data.as(RedditListing).children, locale)
end
if child.depth > 0
html << <<-END_HTML
<div class="pure-g">
<div class="pure-u-1-24">
</div>
<div class="pure-u-23-24">
END_HTML
else
html << <<-END_HTML
<div class="pure-g">
<div class="pure-u-1">
END_HTML
end
html << <<-END_HTML
<p>
<a href="javascript:void(0)" data-onclick="toggle_parent">[ − ]</a>
<b><a href="https://www.reddit.com/user/#{child.author}">#{child.author}</a></b>
#{I18n.translate_count(locale, "comments_points_count", child.score, I18n::NumberFormatting::Separator)}
<span title="#{child.created_utc.to_s("%a %B %-d %T %Y UTC")}">#{I18n.translate(locale, "`x` ago", recode_date(child.created_utc, locale))}</span>
<a href="https://www.reddit.com#{child.permalink}" title="#{I18n.translate(locale, "permalink")}">#{I18n.translate(locale, "permalink")}</a>
</p>
<div>
#{body_html}
#{replies_html}
</div>
</div>
</div>
END_HTML
end
end
end
end
end

View File

@@ -346,6 +346,17 @@ module Invidious::Routes::API::V1::Videos
continuation = env.params.query["continuation"]?
sort_by = env.params.query["sort_by"]?.try &.downcase
# 2026-10-18 TODO: Make a mechanism to remove unused
# preferences from PREFS cookie of the clients
# when some preferences are changed, like this one
# which is a Reedit comments deprecation that appears
# on preferences.
#
# Fallback for API users that still use Reddit as a source
if source == "reddit"
return error_json(410, RedditIsDeprecated.new)
end
if source == "youtube"
sort_by ||= "top"
@@ -358,37 +369,6 @@ module Invidious::Routes::API::V1::Videos
end
return comments
elsif source == "reddit"
sort_by ||= "confidence"
begin
comments, reddit_thread = Comments.fetch_reddit(id, sort_by: sort_by)
rescue
comments = nil
reddit_thread = nil
end
if !reddit_thread || !comments
return error_json(404, "No reddit threads found")
end
if format == "json"
reddit_thread = JSON.parse(reddit_thread.to_json).as_h
reddit_thread["comments"] = JSON.parse(comments.to_json)
return reddit_thread.to_json
else
content_html = Frontend::Comments.template_reddit(comments, locale)
content_html = Comments.fill_links(content_html, "https", "www.reddit.com")
content_html = Comments.replace_links(content_html)
response = {
"title" => reddit_thread.title,
"permalink" => reddit_thread.permalink,
"contentHtml" => content_html,
}
return response.to_json
end
end
end

View File

@@ -73,36 +73,22 @@ module Invidious::Routes::Watch
if nojs
if preferences
source = video.comments? ? preferences.comments[0] : "reddit"
source = preferences.comments[0]
# 2026-10-18 TODO: Make a mechanism to remove unused
# preferences from PREFS cookie of the clients
# when some preferences are changed, like this one
# which is a Reedit comments deprecation that appears
# on preferences.
#
# Fallback for people that have reddit in their prefs
source = preferences.comments[1] if source == "reddit"
if source.empty?
source = preferences.comments[1]
end
if source == "youtube"
begin
comment_html = JSON.parse(Comments.fetch_youtube(id, nil, "html", locale, preferences.thin_mode, region))["contentHtml"]
rescue
if preferences.comments[1] == "reddit"
comments, reddit_thread = Comments.fetch_reddit(id)
comment_html = Frontend::Comments.template_reddit(comments, locale)
comment_html = Comments.fill_links(comment_html, "https", "www.reddit.com")
comment_html = Comments.replace_links(comment_html)
end
end
elsif source == "reddit"
begin
comments, reddit_thread = Comments.fetch_reddit(id)
comment_html = Frontend::Comments.template_reddit(comments, locale)
comment_html = Comments.fill_links(comment_html, "https", "www.reddit.com")
comment_html = Comments.replace_links(comment_html)
rescue
if preferences.comments[1] == "youtube"
comment_html = JSON.parse(Comments.fetch_youtube(id, nil, "html", locale, preferences.thin_mode, region))["contentHtml"]
end
end
comment_html = JSON.parse(Comments.fetch_youtube(id, nil, "html", locale, preferences.thin_mode, region))["contentHtml"]
end
else
comment_html = JSON.parse(Comments.fetch_youtube(id, nil, "html", locale, preferences.thin_mode, region))["contentHtml"]

View File

@@ -30,8 +30,6 @@
{
"id" => id,
"youtube_comments_text" => HTML.escape(I18n.translate(locale, "View YouTube comments")),
"reddit_comments_text" => "",
"reddit_permalink_text" => "",
"comments_text" => HTML.escape(I18n.translate(locale, "View `x` comments", "{commentCount}")),
"hide_replies_text" => HTML.escape(I18n.translate(locale, "Hide replies")),
"show_replies_text" => HTML.escape(I18n.translate(locale, "Show replies")),

View File

@@ -83,7 +83,7 @@
<label for="comments[0]"><%= I18n.translate(locale, "preferences_comments_label") %></label>
<% preferences.comments.each_with_index do |comments, index| %>
<select name="comments[<%= index %>]" id="comments[<%= index %>]">
<% {"", "youtube", "reddit"}.each do |option| %>
<% {"", "youtube"}.each do |option| %>
<option value="<%= option %>" <% if preferences.comments[index] == option %> selected <% end %>><%= I18n.translate(locale, option.blank? ? "none" : option) %></option>
<% end %>
</select>

View File

@@ -43,11 +43,6 @@ we're going to need to do it here in order to allow for translations.
}
</style>
<%
# Disable the try reddit link if javascript is disabled
%>
<noscript><style> #try-reddit-comments-link { display: none } </style></noscript>
<% end %>
<script id="video_data" type="application/json">
@@ -60,10 +55,6 @@ we're going to need to do it here in order to allow for translations.
"play_next" => !video.related_videos.empty? && !plid && params.continue,
"next_video" => video.related_videos.select { |rv| rv["id"]? }[0]?.try &.["id"],
"youtube_comments_text" => HTML.escape(I18n.translate(locale, "View YouTube comments")),
"comments_youtube_disabled_text" => HTML.escape(I18n.translate(locale, "comments_youtube_disabled_text")),
"comments_youtube_disabled_try_reddit" => HTML.escape(I18n.translate(locale, "comments_youtube_disabled_try_reddit")),
"reddit_comments_text" => HTML.escape(I18n.translate(locale, "View Reddit comments")),
"reddit_permalink_text" => HTML.escape(I18n.translate(locale, "View more comments on Reddit")),
"comments_text" => HTML.escape(I18n.translate(locale, "View `x` comments", "{commentCount}")),
"hide_replies_text" => HTML.escape(I18n.translate(locale, "Hide replies")),
"show_replies_text" => HTML.escape(I18n.translate(locale, "Show replies")),
@@ -74,7 +65,6 @@ we're going to need to do it here in order to allow for translations.
"comments_enabled" => video.comments?,
"projection_type" => video.projection_type,
"local_disabled" => CONFIG.disabled?("local"),
"support_reddit" => true,
"live_now" => video.live_now
}.to_pretty_json
%>
@@ -324,21 +314,8 @@ we're going to need to do it here in order to allow for translations.
<% if !nojs %>
<div id="comments-turned-off-on-video-message" class="h-box v-box">
<p><b><%= HTML.escape(I18n.translate(locale, "comments_youtube_disabled_text")) %></b></p>
<p><b><button data-comments="reddit" id="try-reddit-comments-link" class="simulated_a">
<%= HTML.escape(I18n.translate(locale, "comments_youtube_disabled_try_reddit")) %>
</button></b></p>
</div>
<% end %>
<noscript>
<% if nojs %>
<%= comment_html %>
<% else %>
<a href="/watch?<%= env.params.query %>&nojs=1">
<%= HTML.escape(I18n.translate(locale, "comments_youtube_disabled_try_reddit_no_js")) %>
</a>
<% end %>
</noscript>
<% end %>
<% end %>
</div>