From 60fa552469b5c1d39fd78a3e6a7f5aa133bd3fc7 Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Thu, 7 Oct 2021 13:53:55 +0200 Subject: [PATCH 1/7] Fix an infinite recursion caused by #2228 Changes in the aforementioned PR lead to change the behavior of some old code. The data type of the parameters aren't explicit enough, which makes the compiler use the wrong method because of type infering. --- src/invidious/helpers/serialized_yt_data.cr | 66 ++++++++++----------- 1 file changed, 31 insertions(+), 35 deletions(-) diff --git a/src/invidious/helpers/serialized_yt_data.cr b/src/invidious/helpers/serialized_yt_data.cr index 1ba3f20e..0cac7c49 100644 --- a/src/invidious/helpers/serialized_yt_data.cr +++ b/src/invidious/helpers/serialized_yt_data.cr @@ -58,17 +58,13 @@ struct SearchVideo end end - def to_xml(auto_generated, query_params, xml : XML::Builder | Nil = nil) - if xml - to_xml(HOST_URL, auto_generated, query_params, xml) - else - XML.build do |xml| - to_xml(HOST_URL, auto_generated, query_params, xml) - end + def to_xml(auto_generated, query_params, _xml : Nil) + XML.build do |xml| + to_xml(auto_generated, query_params, xml) end end - def to_json(locale : Hash(String, JSON::Any), json : JSON::Builder) + def to_json(locale : Hash(String, JSON::Any) | Nil, json : JSON::Builder) json.object do json.field "type", "video" json.field "title", self.title @@ -99,16 +95,16 @@ struct SearchVideo end end - def to_json(locale, json : JSON::Builder | Nil = nil) - if json + def to_json(locale : Hash(String, JSON::Any) | Nil, _json : Nil = nil) + JSON.build do |json| to_json(locale, json) - else - JSON.build do |json| - to_json(locale, json) - end end end + def to_json(json : JSON::Builder) + to_json(nil, json) + end + def is_upcoming premiere_timestamp ? true : false end @@ -133,7 +129,7 @@ struct SearchPlaylist property videos : Array(SearchPlaylistVideo) property thumbnail : String? - def to_json(locale, json : JSON::Builder) + def to_json(locale : Hash(String, JSON::Any) | Nil, json : JSON::Builder) json.object do json.field "type", "playlist" json.field "title", self.title @@ -163,15 +159,15 @@ struct SearchPlaylist end end - def to_json(locale, json : JSON::Builder | Nil = nil) - if json + def to_json(locale : Hash(String, JSON::Any) | Nil, _json : Nil = nil) + JSON.build do |json| to_json(locale, json) - else - JSON.build do |json| - to_json(locale, json) - end end end + + def to_json(json : JSON::Builder) + to_json(nil, json) + end end struct SearchChannel @@ -185,7 +181,7 @@ struct SearchChannel property description_html : String property auto_generated : Bool - def to_json(locale, json : JSON::Builder) + def to_json(locale : Hash(String, JSON::Any) | Nil, json : JSON::Builder) json.object do json.field "type", "channel" json.field "author", self.author @@ -215,15 +211,15 @@ struct SearchChannel end end - def to_json(locale, json : JSON::Builder | Nil = nil) - if json + def to_json(locale, _json : Nil = nil) + JSON.build do |json| to_json(locale, json) - else - JSON.build do |json| - to_json(locale, json) - end end end + + def to_json(json : JSON::Builder) + to_json(nil, json) + end end class Category @@ -235,7 +231,7 @@ class Category property description_html : String property badges : Array(Tuple(String, String))? - def to_json(locale, json : JSON::Builder) + def to_json(locale : Hash(String, JSON::Any) | Nil, json : JSON::Builder) json.object do json.field "type", "category" json.field "title", self.title @@ -249,15 +245,15 @@ class Category end end - def to_json(locale, json : JSON::Builder | Nil = nil) - if json + def to_json(locale : Hash(String, JSON::Any) | Nil, _json : Nil = nil) + JSON.build do |json| to_json(locale, json) - else - JSON.build do |json| - to_json(locale, json) - end end end + + def to_json(json : JSON::Builder) + to_json(nil, json) + end end alias SearchItem = SearchVideo | SearchChannel | SearchPlaylist | Category From f65b628bf36d51a46d7d4584004ccc6764c6669e Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Thu, 7 Oct 2021 22:50:32 +0200 Subject: [PATCH 2/7] serialized_yt_data: Remove default nil value in to_json this will ensure that two parameters are passed and that it doesn't collide with 'to_json(builder)' --- src/invidious/helpers/serialized_yt_data.cr | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/invidious/helpers/serialized_yt_data.cr b/src/invidious/helpers/serialized_yt_data.cr index 0cac7c49..bc8fc946 100644 --- a/src/invidious/helpers/serialized_yt_data.cr +++ b/src/invidious/helpers/serialized_yt_data.cr @@ -95,7 +95,7 @@ struct SearchVideo end end - def to_json(locale : Hash(String, JSON::Any) | Nil, _json : Nil = nil) + def to_json(locale : Hash(String, JSON::Any) | Nil, _json : Nil) JSON.build do |json| to_json(locale, json) end @@ -159,7 +159,7 @@ struct SearchPlaylist end end - def to_json(locale : Hash(String, JSON::Any) | Nil, _json : Nil = nil) + def to_json(locale : Hash(String, JSON::Any) | Nil, _json : Nil) JSON.build do |json| to_json(locale, json) end @@ -211,7 +211,7 @@ struct SearchChannel end end - def to_json(locale, _json : Nil = nil) + def to_json(locale, _json : Nil) JSON.build do |json| to_json(locale, json) end @@ -245,7 +245,7 @@ class Category end end - def to_json(locale : Hash(String, JSON::Any) | Nil, _json : Nil = nil) + def to_json(locale : Hash(String, JSON::Any) | Nil, _json : Nil) JSON.build do |json| to_json(locale, json) end From 1cb715ac9f102c74b6611dae703c5a45e7cae94a Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Thu, 7 Oct 2021 22:53:00 +0200 Subject: [PATCH 3/7] serialized_yt_data: force datatype of 'locale' --- src/invidious/helpers/serialized_yt_data.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/invidious/helpers/serialized_yt_data.cr b/src/invidious/helpers/serialized_yt_data.cr index bc8fc946..d4f5da87 100644 --- a/src/invidious/helpers/serialized_yt_data.cr +++ b/src/invidious/helpers/serialized_yt_data.cr @@ -211,7 +211,7 @@ struct SearchChannel end end - def to_json(locale, _json : Nil) + def to_json(locale : Hash(String, JSON::Any) | Nil, _json : Nil) JSON.build do |json| to_json(locale, json) end From 33780f1995d23291b73792caad6cb2ad56952f4e Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Fri, 29 Oct 2021 14:53:06 +0200 Subject: [PATCH 4/7] Also fix 'to_json' in struct Video --- src/invidious/helpers/helpers.cr | 4 ++-- src/invidious/routes/api/v1/videos.cr | 2 +- src/invidious/videos.cr | 17 ++++++++--------- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/invidious/helpers/helpers.cr b/src/invidious/helpers/helpers.cr index 9c053d74..c3b356a9 100644 --- a/src/invidious/helpers/helpers.cr +++ b/src/invidious/helpers/helpers.cr @@ -206,7 +206,7 @@ def create_notification_stream(env, topics, connection_channel) video = get_video(video_id, PG_DB) video.published = published - response = JSON.parse(video.to_json(locale)) + response = JSON.parse(video.to_json(locale, nil)) if fields_text = env.params.query["fields"]? begin @@ -282,7 +282,7 @@ def create_notification_stream(env, topics, connection_channel) video = get_video(video_id, PG_DB) video.published = Time.unix(published) - response = JSON.parse(video.to_json(locale)) + response = JSON.parse(video.to_json(locale, nil)) if fields_text = env.params.query["fields"]? begin diff --git a/src/invidious/routes/api/v1/videos.cr b/src/invidious/routes/api/v1/videos.cr index d483bca6..1edee29c 100644 --- a/src/invidious/routes/api/v1/videos.cr +++ b/src/invidious/routes/api/v1/videos.cr @@ -16,7 +16,7 @@ module Invidious::Routes::API::V1::Videos return error_json(500, ex) end - video.to_json(locale) + video.to_json(locale, nil) end def self.captions(env) diff --git a/src/invidious/videos.cr b/src/invidious/videos.cr index d38a66d8..d3e5800c 100644 --- a/src/invidious/videos.cr +++ b/src/invidious/videos.cr @@ -275,7 +275,7 @@ struct Video end end - def to_json(locale : Hash(String, JSON::Any), json : JSON::Builder) + def to_json(locale : Hash(String, JSON::Any) | Nil, json : JSON::Builder) json.object do json.field "type", "video" @@ -474,14 +474,13 @@ struct Video end end - def to_json(locale, json : JSON::Builder | Nil = nil) - if json - to_json(locale, json) - else - JSON.build do |json| - to_json(locale, json) - end - end + # TODO: remove the locale and follow the crystal convention + def to_json(locale : Hash(String, JSON::Any) | Nil, _json : Nil) + JSON.build { |json| to_json(locale, json) } + end + + def to_json(json : JSON::Builder | Nil = nil) + to_json(nil, json) end def title From 0ec94405ce2b9f421dc85e2d5941e415e9d97f3d Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Fri, 29 Oct 2021 14:53:57 +0200 Subject: [PATCH 5/7] Add TODO comments to other places --- src/invidious/helpers/serialized_yt_data.cr | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/invidious/helpers/serialized_yt_data.cr b/src/invidious/helpers/serialized_yt_data.cr index d4f5da87..f92b7b89 100644 --- a/src/invidious/helpers/serialized_yt_data.cr +++ b/src/invidious/helpers/serialized_yt_data.cr @@ -95,6 +95,7 @@ struct SearchVideo end end + # TODO: remove the locale and follow the crystal convention def to_json(locale : Hash(String, JSON::Any) | Nil, _json : Nil) JSON.build do |json| to_json(locale, json) @@ -159,6 +160,7 @@ struct SearchPlaylist end end + # TODO: remove the locale and follow the crystal convention def to_json(locale : Hash(String, JSON::Any) | Nil, _json : Nil) JSON.build do |json| to_json(locale, json) @@ -211,6 +213,7 @@ struct SearchChannel end end + # TODO: remove the locale and follow the crystal convention def to_json(locale : Hash(String, JSON::Any) | Nil, _json : Nil) JSON.build do |json| to_json(locale, json) @@ -245,6 +248,7 @@ class Category end end + # TODO: remove the locale and follow the crystal convention def to_json(locale : Hash(String, JSON::Any) | Nil, _json : Nil) JSON.build do |json| to_json(locale, json) From 86f75758a76427dd7564ef17bc6a33b6d7ce2e62 Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Fri, 29 Oct 2021 14:59:53 +0200 Subject: [PATCH 6/7] Fix 'to_json' in struct PlaylistVideo --- src/invidious/playlists.cr | 16 +++++----------- src/invidious/routes/api/v1/authenticated.cr | 5 ++++- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/invidious/playlists.cr b/src/invidious/playlists.cr index 443d19d7..e25180c1 100644 --- a/src/invidious/playlists.cr +++ b/src/invidious/playlists.cr @@ -57,7 +57,7 @@ struct PlaylistVideo end end - def to_json(locale, json : JSON::Builder, index : Int32?) + def to_json(json : JSON::Builder, index : Int32? = nil) json.object do json.field "title", self.title json.field "videoId", self.id @@ -81,14 +81,8 @@ struct PlaylistVideo end end - def to_json(locale, json : JSON::Builder? = nil, index : Int32? = nil) - if json - to_json(locale, json, index: index) - else - JSON.build do |json| - to_json(locale, json, index: index) - end - end + def to_json(_json : Nil, index : Int32? = nil) + JSON.build { |json| to_json(json, index: index) } end end @@ -144,7 +138,7 @@ struct Playlist json.array do videos = get_playlist_videos(PG_DB, self, offset: offset, locale: locale, video_id: video_id) videos.each do |video| - video.to_json(locale, json) + video.to_json(json) end end end @@ -224,7 +218,7 @@ struct InvidiousPlaylist videos = get_playlist_videos(PG_DB, self, offset: offset, locale: locale, video_id: video_id) videos.each_with_index do |video, index| - video.to_json(locale, json, offset + index) + video.to_json(json, offset + index) end end end diff --git a/src/invidious/routes/api/v1/authenticated.cr b/src/invidious/routes/api/v1/authenticated.cr index 7950b302..cdd9e2f6 100644 --- a/src/invidious/routes/api/v1/authenticated.cr +++ b/src/invidious/routes/api/v1/authenticated.cr @@ -274,7 +274,10 @@ module Invidious::Routes::API::V1::Authenticated env.response.headers["Location"] = "#{HOST_URL}/api/v1/auth/playlists/#{plid}/videos/#{playlist_video.index.to_u64.to_s(16).upcase}" env.response.status_code = 201 - playlist_video.to_json(locale, index: playlist.index.size) + + JSON.build do |json| + playlist_video.to_json(json, index: playlist.index.size) + end end def self.delete_video_in_playlist(env) From 6cf0ff6b49ee6b189eb5a308089dc86d42f79019 Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Fri, 29 Oct 2021 16:01:52 +0200 Subject: [PATCH 7/7] Remove useless auto_generated param from PlaylistVideo#to_xml given the variables available in this function's context, 'author' and 'ucid' provide the same data 'self.author' and 'self.ucid', respectively. Given that fact, the variable `auto_generated` has no impact on the logic of this function, and hence can be safely removed. this greatly simplifies the code and makes it perfectly compatible with crystal's calling convention for '#to_xml' methods. --- src/invidious/playlists.cr | 21 +++++---------------- src/invidious/routes/feeds.cr | 4 +--- 2 files changed, 6 insertions(+), 19 deletions(-) diff --git a/src/invidious/playlists.cr b/src/invidious/playlists.cr index e25180c1..f37667b5 100644 --- a/src/invidious/playlists.cr +++ b/src/invidious/playlists.cr @@ -11,7 +11,7 @@ struct PlaylistVideo property index : Int64 property live_now : Bool - def to_xml(auto_generated, xml : XML::Builder) + def to_xml(xml : XML::Builder) xml.element("entry") do xml.element("id") { xml.text "yt:video:#{self.id}" } xml.element("yt:videoId") { xml.text self.id } @@ -20,13 +20,8 @@ struct PlaylistVideo xml.element("link", rel: "alternate", href: "#{HOST_URL}/watch?v=#{self.id}") xml.element("author") do - if auto_generated - xml.element("name") { xml.text self.author } - xml.element("uri") { xml.text "#{HOST_URL}/channel/#{self.ucid}" } - else - xml.element("name") { xml.text author } - xml.element("uri") { xml.text "#{HOST_URL}/channel/#{ucid}" } - end + xml.element("name") { xml.text self.author } + xml.element("uri") { xml.text "#{HOST_URL}/channel/#{self.ucid}" } end xml.element("content", type: "xhtml") do @@ -47,14 +42,8 @@ struct PlaylistVideo end end - def to_xml(auto_generated, xml : XML::Builder? = nil) - if xml - to_xml(auto_generated, xml) - else - XML.build do |xml| - to_xml(auto_generated, xml) - end - end + def to_xml(_xml : Nil = nil) + XML.build { |xml| to_xml(xml) } end def to_json(json : JSON::Builder, index : Int32? = nil) diff --git a/src/invidious/routes/feeds.cr b/src/invidious/routes/feeds.cr index 40c41dc1..f4a8467b 100644 --- a/src/invidious/routes/feeds.cr +++ b/src/invidious/routes/feeds.cr @@ -281,9 +281,7 @@ module Invidious::Routes::Feeds xml.element("name") { xml.text playlist.author } end - videos.each do |video| - video.to_xml(false, xml) - end + videos.each &.to_xml(xml) end end else