From d7a53d0159c6d1c58c2008f61737311badbf5782 Mon Sep 17 00:00:00 2001 From: broquemonsieur Date: Thu, 3 Aug 2023 02:33:07 -0700 Subject: [PATCH] Finish vetting changes for autoplay stability --- src/invidious/compilations.cr | 20 +++++++++---------- src/invidious/database/compilations.cr | 6 +++--- src/invidious/helpers/utils.cr | 4 +++- src/invidious/routes/compilations.cr | 20 ++++++++++++------- src/invidious/views/add_compilation_items.ecr | 8 +------- src/invidious/views/components/item.ecr | 11 +++++----- src/invidious/views/user/preferences.ecr | 4 ++-- src/invidious/views/watch.ecr | 1 + 8 files changed, 39 insertions(+), 35 deletions(-) diff --git a/src/invidious/compilations.cr b/src/invidious/compilations.cr index 5710e6b2..2dda46e2 100644 --- a/src/invidious/compilations.cr +++ b/src/invidious/compilations.cr @@ -423,17 +423,17 @@ def extract_compilation_videos(initial_data : Hash(String, JSON::Any)) end videos << CompilationVideo.new({ - title: title, - id: video_id, - author: author, - ucid: ucid, - length_seconds: length_seconds, + title: title, + id: video_id, + author: author, + ucid: ucid, + length_seconds: length_seconds, starting_timestamp_seconds: starting_timestamp_seconds, - ending_timestamp_seconds: ending_timestamp_seconds, - published: Time.utc, - compid: compid, - index: index, - order_index: order_index + ending_timestamp_seconds: ending_timestamp_seconds, + published: Time.utc, + compid: compid, + index: index, + order_index: order_index }) end end diff --git a/src/invidious/database/compilations.cr b/src/invidious/database/compilations.cr index daf597b0..8ccc532f 100644 --- a/src/invidious/database/compilations.cr +++ b/src/invidious/database/compilations.cr @@ -334,13 +334,13 @@ module Invidious::Database::CompilationVideos PG_DB.exec(request, id, starting_timestamp_seconds) end - def update_end_timestamp(id : String, ending_timestamp_seconds : Int64) + def update_end_timestamp(id : String, ending_timestamp_seconds : Int32) request = <<-SQL UPDATE compilation_videos - SET ending_timestamp_seconds = ending_timestamp_seconds + SET ending_timestamp_seconds = $2 WHERE id = $1 SQL - PG_DB.exec(request, id) + PG_DB.exec(request, id, ending_timestamp_seconds) end end diff --git a/src/invidious/helpers/utils.cr b/src/invidious/helpers/utils.cr index a006d602..8441ea8f 100644 --- a/src/invidious/helpers/utils.cr +++ b/src/invidious/helpers/utils.cr @@ -35,8 +35,10 @@ def decode_length_seconds(string) end def recode_length_seconds(time) - if time <= 0 + if time < 0 return "" + elsif time == 0 + return "0:00" else time = time.seconds text = "#{time.minutes.to_s.rjust(2, '0')}:#{time.seconds.to_s.rjust(2, '0')}" diff --git a/src/invidious/routes/compilations.cr b/src/invidious/routes/compilations.cr index c592eb70..4e0b210d 100644 --- a/src/invidious/routes/compilations.cr +++ b/src/invidious/routes/compilations.cr @@ -244,7 +244,6 @@ module Invidious::Routes::Compilations compilation_video_index = compilation.index[index] compilation_video = Invidious::Database::CompilationVideos.select_video(compid, compilation.index, compilation_video_index, 0, 1) json_timestamp_query_start = compilation_video_index.to_s + "_start_timestamp" - start_timestamp = env.params.body[json_timestamp_query_start]?.try &.as(String).byte_slice(0, 8) if !start_timestamp.nil? && !compilation_video[0].id.nil? start_timestamp_seconds = decode_length_seconds(start_timestamp) @@ -254,14 +253,14 @@ module Invidious::Routes::Compilations end end end - + compilation_video = Invidious::Database::CompilationVideos.select_video(compid, compilation.index, compilation_video_index, 0, 1) json_timestamp_query_end = compilation_video_index.to_s + "_end_timestamp" end_timestamp = env.params.json[json_timestamp_query_end]?.try &.as(String).byte_slice(0, 8) if !end_timestamp.nil? && !compilation_video[0].id.nil? end_timestamp_seconds = decode_length_seconds(end_timestamp) if !end_timestamp_seconds.nil? - if end_timestamp_seconds >= 0 && end_timestamp_seconds <= compilation_video[0].ending_timestamp_seconds && end_timestamp_seconds > compilation_video[0].starting_timestamp_seconds - Invidious::Database::CompilationVideos.update_end_timestamp(compilation_video[0].id, end_timestamp_seconds) + if end_timestamp_seconds >= 0 && end_timestamp_seconds <= compilation_video[0].length_seconds && end_timestamp_seconds > compilation_video[0].starting_timestamp_seconds + Invidious::Database::CompilationVideos.update_end_timestamp(compilation_video[0].id, end_timestamp_seconds.to_i) end end end @@ -303,11 +302,18 @@ module Invidious::Routes::Compilations begin query = Invidious::Search::Query.new(env.params.query, :compilation, region) - videos = query.process.select(SearchVideo).map(&.as(SearchVideo)) + items = query.process.select(SearchVideo).map(&.as(SearchVideo)) rescue ex - videos = [] of SearchVideo + items = [] of SearchVideo end + query_encoded = URI.encode_www_form(query.try &.text || "", space_to_plus: true) + page_nav_html = Frontend::Pagination.nav_numeric(locale, + base_url: "/add_compilation_items?list=#{compilation.id}&q=#{query_encoded}", + current_page: page, + show_next: (items.size >= 20) + ) + env.set "add_compilation_items", compid templated "add_compilation_items" end @@ -414,7 +420,7 @@ module Invidious::Routes::Compilations author: video.author, ucid: video.ucid, length_seconds: video.length_seconds, - starting_timestamp_seconds: video.length_seconds, + starting_timestamp_seconds: 0, ending_timestamp_seconds: video.length_seconds, published: video.published, compid: compilation_id, diff --git a/src/invidious/views/add_compilation_items.ecr b/src/invidious/views/add_compilation_items.ecr index 28494d98..3fbb5d96 100644 --- a/src/invidious/views/add_compilation_items.ecr +++ b/src/invidious/views/add_compilation_items.ecr @@ -31,10 +31,4 @@ -
- <% videos.each_slice(4) do |slice| %> - <% slice.each do |item| %> - <%= rendered "components/item" %> - <% end %> - <% end %> -
\ No newline at end of file +<%= rendered "components/items_paginated" %> \ No newline at end of file diff --git a/src/invidious/views/components/item.ecr b/src/invidious/views/components/item.ecr index e7da1764..4e960d8d 100644 --- a/src/invidious/views/components/item.ecr +++ b/src/invidious/views/components/item.ecr @@ -138,12 +138,13 @@ - <% elsif compid_form = env.get? "add_compilation_items" %> -
" method="post"> + <%- elsif compid_form = env.get?("add_compilation_items") -%> + <%- form_parameters = "action_add_video=1&video_id=#{item.id}&compilation_id=#{compid_form}&referer=#{env.get("current_page")}" -%> + "> -

- -

+
<%- elsif item.is_a?(PlaylistVideo) && (plid_form = env.get?("remove_playlist_items")) -%> <%- form_parameters = "action_remove_video=1&set_video_id=#{item.index}&playlist_id=#{plid_form}&referer=#{env.get("current_page")}" -%> diff --git a/src/invidious/views/user/preferences.ecr b/src/invidious/views/user/preferences.ecr index 93f03a05..1ef99cd2 100644 --- a/src/invidious/views/user/preferences.ecr +++ b/src/invidious/views/user/preferences.ecr @@ -174,7 +174,7 @@ @@ -184,7 +184,7 @@ <% (feed_options.size - 1).times do |index| %> <% end %> diff --git a/src/invidious/views/watch.ecr b/src/invidious/views/watch.ecr index b861422e..890732e4 100644 --- a/src/invidious/views/watch.ecr +++ b/src/invidious/views/watch.ecr @@ -50,6 +50,7 @@ we're going to need to do it here in order to allow for translations. "id" => video.id, "index" => continuation, "plid" => plid, + "compid" => compid, "length_seconds" => video.length_seconds.to_f, "play_next" => !video.related_videos.empty? && !plid && params.continue, "next_video" => video.related_videos.select { |rv| rv["id"]? }[0]?.try &.["id"],