summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSamantaz Fox <coding@samantaz.fr>2023-05-06 20:12:02 +0200
committerSamantaz Fox <coding@samantaz.fr>2023-05-25 22:53:53 +0200
commitde78848039c2e5e8dea25b6013f3e24797a0b1ce (patch)
tree887dbf5cd4216eb2f612f5250df7dfe1a2f6c8ac /src
parente10f6b6626bfe462861980184b09b7350499c889 (diff)
downloadinvidious-de78848039c2e5e8dea25b6013f3e24797a0b1ce.tar.gz
invidious-de78848039c2e5e8dea25b6013f3e24797a0b1ce.tar.bz2
invidious-de78848039c2e5e8dea25b6013f3e24797a0b1ce.zip
Comments: Move 'template_reddit' function to own file + module
Diffstat (limited to 'src')
-rw-r--r--src/invidious/comments.cr47
-rw-r--r--src/invidious/frontend/comments_reddit.cr50
-rw-r--r--src/invidious/routes/api/v1/videos.cr2
-rw-r--r--src/invidious/routes/watch.cr4
4 files changed, 53 insertions, 50 deletions
diff --git a/src/invidious/comments.cr b/src/invidious/comments.cr
index 8943b1da..6a3aa4c2 100644
--- a/src/invidious/comments.cr
+++ b/src/invidious/comments.cr
@@ -1,50 +1,3 @@
-def template_reddit_comments(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 = template_reddit_comments(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>
- #{translate_count(locale, "comments_points_count", child.score, NumberFormatting::Separator)}
- <span title="#{child.created_utc.to_s(translate(locale, "%a %B %-d %T %Y UTC"))}">#{translate(locale, "`x` ago", recode_date(child.created_utc, locale))}</span>
- <a href="https://www.reddit.com#{child.permalink}" title="#{translate(locale, "permalink")}">#{translate(locale, "permalink")}</a>
- </p>
- <div>
- #{body_html}
- #{replies_html}
- </div>
- </div>
- </div>
- END_HTML
- end
- end
- end
-end
-
def replace_links(html)
# Check if the document is empty
# Prevents edge-case bug with Reddit comments, see issue #3115
diff --git a/src/invidious/frontend/comments_reddit.cr b/src/invidious/frontend/comments_reddit.cr
new file mode 100644
index 00000000..b5647bae
--- /dev/null
+++ b/src/invidious/frontend/comments_reddit.cr
@@ -0,0 +1,50 @@
+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>
+ #{translate_count(locale, "comments_points_count", child.score, NumberFormatting::Separator)}
+ <span title="#{child.created_utc.to_s(translate(locale, "%a %B %-d %T %Y UTC"))}">#{translate(locale, "`x` ago", recode_date(child.created_utc, locale))}</span>
+ <a href="https://www.reddit.com#{child.permalink}" title="#{translate(locale, "permalink")}">#{translate(locale, "permalink")}</a>
+ </p>
+ <div>
+ #{body_html}
+ #{replies_html}
+ </div>
+ </div>
+ </div>
+ END_HTML
+ end
+ end
+ end
+ end
+end
diff --git a/src/invidious/routes/api/v1/videos.cr b/src/invidious/routes/api/v1/videos.cr
index cb1008ac..6feaaef4 100644
--- a/src/invidious/routes/api/v1/videos.cr
+++ b/src/invidious/routes/api/v1/videos.cr
@@ -361,7 +361,7 @@ module Invidious::Routes::API::V1::Videos
return reddit_thread.to_json
else
- content_html = template_reddit_comments(comments, locale)
+ content_html = Frontend::Comments.template_reddit(comments, locale)
content_html = fill_links(content_html, "https", "www.reddit.com")
content_html = replace_links(content_html)
response = {
diff --git a/src/invidious/routes/watch.cr b/src/invidious/routes/watch.cr
index b08e6fbe..6b441a48 100644
--- a/src/invidious/routes/watch.cr
+++ b/src/invidious/routes/watch.cr
@@ -99,7 +99,7 @@ module Invidious::Routes::Watch
rescue ex
if preferences.comments[1] == "reddit"
comments, reddit_thread = Comments.fetch_reddit(id)
- comment_html = template_reddit_comments(comments, locale)
+ comment_html = Frontend::Comments.template_reddit(comments, locale)
comment_html = fill_links(comment_html, "https", "www.reddit.com")
comment_html = replace_links(comment_html)
@@ -108,7 +108,7 @@ module Invidious::Routes::Watch
elsif source == "reddit"
begin
comments, reddit_thread = Comments.fetch_reddit(id)
- comment_html = template_reddit_comments(comments, locale)
+ comment_html = Frontend::Comments.template_reddit(comments, locale)
comment_html = fill_links(comment_html, "https", "www.reddit.com")
comment_html = replace_links(comment_html)