diff --git a/src/invidious/database/compilations.cr b/src/invidious/database/compilations.cr index a3372ac7..b5bf8bf3 100644 --- a/src/invidious/database/compilations.cr +++ b/src/invidious/database/compilations.cr @@ -248,4 +248,15 @@ module Invidious::Database::CompilationVideos return PG_DB.query_all(request, compid, index, limit, as: String) end + + # ------------------- + # Update + # ------------------- + + def update_start_timestamp(compid : String, index : VideoIndex, starting_timestamp_seconds : Int64) + request = <<-SQL + UPDATE compilation_videos + SET starting_timestamp_seconds = starting_timestamp_seconds + WHERE id = $2 + end end diff --git a/src/invidious/routes/compilations.cr b/src/invidious/routes/compilations.cr index 253ac22e..a76e72b5 100644 --- a/src/invidious/routes/compilations.cr +++ b/src/invidious/routes/compilations.cr @@ -196,6 +196,63 @@ module Invidious::Routes::Compilations env.redirect "/compilation?list=#{compid}" end + def self.adjust_timestamps(env) + LOGGER.info("Handle POST request for edit compilation") + env.response.content_type = "application/json" + user = env.get("user").as(User) + + compid = env.params.url["compid"]? + if !compid || compid.empty? + return error_json(400, "A compilation ID is required") + end + + compilation = Invidious::Database::Compilations.select(id: compid) + if !compilation || compilation.author != user.email && compilation.privacy.private? + return error_json(404, "Compilation does not exist.") + end + + if compilation.author != user.email + return error_json(403, "Invalid user") + end + + title = env.params.json["title"].try &.as(String).delete("<>").byte_slice(0, 150) || compilation.title + privacy = env.params.json["privacy"]?.try { |p| CompilationPrivacy.parse(p.as(String).downcase) } || compilation.privacy + + if title != compilation.title || + privacy != compilation.privacy + updated = Time.utc + else + updated = compilation.updated + end + + {1...Invidious::Database::Compilations.count_owned_by(user.email)} each do |index| + start_timestamp = env.params.json["_start_timestamp"]?.try &.as(String).byte_slice(0, 150) || compilation.title + + (1..Invidious::Database::Compilations.count_owned_by(user.email)).each do |index| { + compilation_video = Invidious::Database::CompilationVideos.select(order_index: index) + start_timestamp = env.params.json[index+"_start_timestamp"]?.try &.as(String).byte_slice(0, 8) + if !start_timestamp.empty? + start_timestamp_seconds = decode_length_seconds(start_timestamp) + if !start_timestamp_seconds.empty + if start_timestamp_seconds >= 0 && start_timestamp_seconds <= compilation_video + Invidious::Database::CompilationVideos.update_start_timestamp(compid, compilation_video.index, start_timestamp_seconds) + end + end + end + + end_timestamp = env.params.json[index+"_end_timestamp"]?.try &.as(String).byte_slice(0, 8) + if !end_timestamp.empty? + end_timestamp_seconds = decode_length_seconds(end_timestamp) + if !end_timestamp_seconds.empty + if end_timestamp_seconds >= 0 && end_timestamp_seconds <= compilation_video + Invidious::Database::CompilationVideos.update_end_timestamp(compid, compilation_video.index, end_timestamp_seconds) + end + end + end + } + + + def self.add_compilation_items_page(env) LOGGER.info("13. add_compilation_items") prefs = env.get("preferences").as(Preferences) @@ -305,6 +362,7 @@ module Invidious::Routes::Compilations case action when "action_edit_compilation" # TODO: Compilation stub + LOGGER.info("Begin handling of Compilation edit") when "action_add_video" if compilation.index.size >= CONFIG.compilation_length_limit if redirect diff --git a/src/invidious/routing.cr b/src/invidious/routing.cr index dc7aadd8..b5cb0527 100644 --- a/src/invidious/routing.cr +++ b/src/invidious/routing.cr @@ -88,7 +88,7 @@ module Invidious::Routing post "/compilation_ajax", Routes::Compilations, :compilation_ajax get "/add_compilation_items", Routes::Compilations, :add_compilation_items_page get "/edit_compilation", Routes::Compilations, :edit - post "/edit_compilation", Routes::Compilations, :update + post "/edit_compilation", Routes::Compilations, :adjust_timestamps end def register_iv_playlist_routes diff --git a/src/invidious/views/components/item.ecr b/src/invidious/views/components/item.ecr index b0f50fe2..63f44e8c 100644 --- a/src/invidious/views/components/item.ecr +++ b/src/invidious/views/components/item.ecr @@ -87,6 +87,20 @@
+ <% when InvidiousCompilation %> + <% link_url = "/compilation?list=#{item.id}" %> + + <% if !env.get("preferences").as(Preferences).thin_mode %> +<%= translate_count(locale, "generic_videos_count", item.video_count, NumberFormatting::Separator) %>
+<%= HTML.escape(item.title) %>
+ + +<%= HTML.escape(item.author) %><% if !item.is_a?(InvidiousCompilation) && !item.is_a?(InvidiousPlaylist) && !item.author_verified.nil? && item.author_verified %> <% end %>
+ <% when Category %> <% else %> <%- @@ -97,42 +111,7 @@ elsif item.is_a?(MixVideo) link_url = "/watch?v=#{item.id}&list=#{item.rdid}" endpoint_params = "?v=#{item.id}&list=#{item.rdid}" - elsif item.is_a?(InvidiousCompilation) - <% when InvidiousCompilation %> - <% url = "/compilation?list=#{item.id}" %> - - <% if !env.get("preferences").as(Preferences).thin_mode %> -<%= translate_count(locale, "generic_videos_count", item.video_count, NumberFormatting::Separator) %>
-<%= HTML.escape(item.title) %>
- elsif item.is_a?(CompilationVideo) - <% when CompilationVideo %> -<%= HTML.escape(item.author) %><% if !item.is_a?(InvidiousCompilation) && !item.is_a?(InvidiousPlaylist) && !item.author_verified.nil? && item.author_verified %> <% end %>
- + # elsif item.is_a?(CompilationVideo) else link_url = "/watch?v=#{item.id}" endpoint_params = "?v=#{item.id}"