From 6b8189b46561c0614a2db87a0a722e268b4eca31 Mon Sep 17 00:00:00 2001
From: Samantaz Fox <coding@samantaz.fr>
Date: Sun, 17 Apr 2022 15:53:10 +0200
Subject: [PATCH] Unify parameters of the different #to_xml methods

---
 src/invidious/channels/channels.cr          | 12 ++----------
 src/invidious/helpers/serialized_yt_data.cr | 12 ++----------
 src/invidious/playlists.cr                  |  8 ++------
 src/invidious/rss_atom.cr                   |  9 +--------
 4 files changed, 7 insertions(+), 34 deletions(-)

diff --git a/src/invidious/channels/channels.cr b/src/invidious/channels/channels.cr
index fb104779..9315d165 100644
--- a/src/invidious/channels/channels.cr
+++ b/src/invidious/channels/channels.cr
@@ -50,9 +50,7 @@ struct ChannelVideo
     end
   end
 
-  def to_xml(locale, query_params, xml : XML::Builder)
-    query_params["v"] = self.id
-
+  def to_xml(xml : XML::Builder, query_params : HTTP::Params)
     xml.element("entry") do
       xml.element("id") { xml.text "ni://invidious/sha-256;" + sha256("video/#{self.id}") }
       xml.element("title") { xml.text self.title }
@@ -65,7 +63,7 @@ struct ChannelVideo
 
       xml.element("content", type: "xhtml") do
         xml.element("div", xmlns: "http://www.w3.org/1999/xhtml") do
-          xml.element("a", href: "#{HOST_URL}/watch?#{query_params}") do
+          xml.element("a", href: "#{HOST_URL}/watch?v=#{self.id}&#{query_params}") do
             xml.element("img", src: "#{HOST_URL}/vi/#{self.id}/mqdefault.jpg")
           end
         end
@@ -82,12 +80,6 @@ struct ChannelVideo
     end
   end
 
-  def to_xml(locale, _xml : Nil = nil)
-    XML.build do |xml|
-      to_xml(locale, xml)
-    end
-  end
-
   def to_tuple
     {% begin %}
       {
diff --git a/src/invidious/helpers/serialized_yt_data.cr b/src/invidious/helpers/serialized_yt_data.cr
index 082fe97c..fc91031f 100644
--- a/src/invidious/helpers/serialized_yt_data.cr
+++ b/src/invidious/helpers/serialized_yt_data.cr
@@ -14,9 +14,7 @@ struct SearchVideo
   property premiere_timestamp : Time?
   property author_verified : Bool
 
-  def to_xml(auto_generated, query_params, xml : XML::Builder)
-    query_params["v"] = self.id
-
+  def to_xml(xml : XML::Builder, query_params : HTTP::Params)
     xml.element("entry") do
       xml.element("id") { xml.text "ni://invidious/sha-256;" + sha256("video/#{self.id}") }
       xml.element("title") { xml.text self.title }
@@ -29,7 +27,7 @@ struct SearchVideo
 
       xml.element("content", type: "xhtml") do
         xml.element("div", xmlns: "http://www.w3.org/1999/xhtml") do
-          xml.element("a", href: "#{HOST_URL}/watch?#{query_params}") do
+          xml.element("a", href: "#{HOST_URL}/watch?v=#{self.id}&#{query_params}") do
             xml.element("img", src: "#{HOST_URL}/vi/#{self.id}/mqdefault.jpg")
           end
 
@@ -54,12 +52,6 @@ struct SearchVideo
     end
   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 : String?, json : JSON::Builder)
     json.object do
       json.field "type", "video"
diff --git a/src/invidious/playlists.cr b/src/invidious/playlists.cr
index a4a9d1b6..839ba41e 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(xml : XML::Builder)
+  def to_xml(xml : XML::Builder, query_params : HTTP::Params)
     xml.element("entry") do
       xml.element("id") { xml.text "ni://invidious/sha-256;" + sha256("video/#{self.id}") }
       xml.element("title") { xml.text self.title }
@@ -24,7 +24,7 @@ struct PlaylistVideo
 
       xml.element("content", type: "xhtml") do
         xml.element("div", xmlns: "http://www.w3.org/1999/xhtml") do
-          xml.element("a", href: "#{HOST_URL}/watch?v=#{self.id}") do
+          xml.element("a", href: "#{HOST_URL}/watch?v=#{self.id}&#{query_params}") do
             xml.element("img", src: "#{HOST_URL}/vi/#{self.id}/mqdefault.jpg")
           end
         end
@@ -40,10 +40,6 @@ struct PlaylistVideo
     end
   end
 
-  def to_xml(_xml : Nil = nil)
-    XML.build { |xml| to_xml(xml) }
-  end
-
   def to_json(json : JSON::Builder, index : Int32? = nil)
     json.object do
       json.field "title", self.title
diff --git a/src/invidious/rss_atom.cr b/src/invidious/rss_atom.cr
index 305238e1..35f0c90f 100644
--- a/src/invidious/rss_atom.cr
+++ b/src/invidious/rss_atom.cr
@@ -110,14 +110,7 @@ module Invidious::RssAtom
         end
 
         # Video entries
-        # TODO: Unify `.to_xml` methods
-        videos.each do |video|
-          case video
-          when .is_a?(PlaylistVideo) then video.to_xml(xml)
-          when .is_a?(ChannelVideo)  then video.to_xml(locale, params, xml)
-          when .is_a?(SearchVideo)   then video.to_xml(false, params, xml)
-          end
-        end
+        videos.each { |video| video.to_xml(xml, params) }
       end
     end
   end