From f48083da3375234696c6cd66fda671a2731716a8 Mon Sep 17 00:00:00 2001 From: broquemonsieur Date: Sun, 6 Aug 2023 00:59:53 -0700 Subject: [PATCH] Add end marker continuation utility --- assets/js/player.js | 9 ++++- assets/js/watch.js | 1 + config/sql/compilations.sql | 4 +- src/invidious/compilations.cr | 40 +++++++++++++------ src/invidious/database/compilations.cr | 8 ++-- .../0011_create_compilations_table.cr | 4 +- src/invidious/routes/compilations.cr | 10 +++-- src/invidious/views/compilation.ecr | 2 +- 8 files changed, 53 insertions(+), 25 deletions(-) diff --git a/assets/js/player.js b/assets/js/player.js index 44640402..bd250119 100644 --- a/assets/js/player.js +++ b/assets/js/player.js @@ -209,8 +209,13 @@ if (video_data.params.video_start > 0 || video_data.params.video_end > 0) { player.markers({ onMarkerReached: function (marker) { - if (marker.text === 'End') - player.loop() ? player.markers.prev('Start') : player.pause(); + if (marker.text === 'End') { + if (video_data.ending_timestamp_seconds) { + player.currentTime(player.duration()); + } else { + player.loop() ? player.markers.prev('Start') : player.pause(); + } + } }, markers: markers }); diff --git a/assets/js/watch.js b/assets/js/watch.js index debf610b..dbfdf5be 100644 --- a/assets/js/watch.js +++ b/assets/js/watch.js @@ -140,6 +140,7 @@ function get_compilation(compid) { if (video_data.params.local !== video_data.preferences.local) url.searchParams.set('local', video_data.params.local); url.searchParams.set('t',video_data.starting_timestamp_seconds); + url.searchParams.set('end',video_data.ending_timestamp_seconds); location.assign(url.pathname + url.search); }); diff --git a/config/sql/compilations.sql b/config/sql/compilations.sql index 5802c9a8..60587e44 100644 --- a/config/sql/compilations.sql +++ b/config/sql/compilations.sql @@ -23,7 +23,9 @@ CREATE TABLE IF NOT EXISTS public.compilations updated timestamptz, privacy compilation_privacy, index int8[], - first_video_id text + first_video_id text, + first_video_starting_timestamp_seconds integer, + first_video_ending_timestamp_seconds integer ); GRANT ALL ON public.compilations TO current_user; diff --git a/src/invidious/compilations.cr b/src/invidious/compilations.cr index 2dda46e2..35d7066e 100644 --- a/src/invidious/compilations.cr +++ b/src/invidious/compilations.cr @@ -96,6 +96,8 @@ struct Compilation property updated : Time property thumbnail : String? property first_video_id : String + property first_video_starting_timestamp_seconds : Int32 + property first_video_ending_timestamp_seconds : Int32 def to_json(offset, json : JSON::Builder, video_id : String? = nil) json.object do @@ -171,6 +173,8 @@ struct InvidiousCompilation property privacy : CompilationPrivacy = CompilationPrivacy::Private property index : Array(Int64) property first_video_id : String + property first_video_starting_timestamp_seconds : Int32 + property first_video_ending_timestamp_seconds : Int32 @[DB::Field(ignore: true)] property thumbnail_id : String? @@ -250,16 +254,18 @@ def create_compilation(title, privacy, user) LOGGER.info("generated compilation id") compilation = InvidiousCompilation.new({ - title: title.byte_slice(0, 150), - id: compid, - author: user.email, - description: "", # Max 5000 characters - video_count: 0, - created: Time.utc, - updated: Time.utc, - privacy: privacy, - index: [] of Int64, - first_video_id: "" + title: title.byte_slice(0, 150), + id: compid, + author: user.email, + description: "", # Max 5000 characters + video_count: 0, + created: Time.utc, + updated: Time.utc, + privacy: privacy, + index: [] of Int64, + first_video_id: "", + first_video_starting_timestamp_seconds: 0, + first_video_ending_timestamp_seconds: 0 }) LOGGER.info("Creating compilation db") @@ -280,7 +286,9 @@ def subscribe_compilation(user, compilation) updated: compilation.updated, privacy: CompilationPrivacy::Private, index: [] of Int64, - first_video_id: "" + first_video_id: "", + first_video_starting_timestamp_seconds: 0, + first_video_ending_timestamp_seconds: 0 }) Invidious::Database::Compilations.insert(compilation) @@ -326,6 +334,7 @@ end def get_compilation(compid : String) #if compid.starts_with? "IVCMP" if compilation = Invidious::Database::Compilations.select(id: compid) + update_first_video_params(compid) return compilation else raise NotFoundException.new("Compilation does not exist.") @@ -333,13 +342,18 @@ def get_compilation(compid : String) #end end -def update_first_video_id(compid : String) +def update_first_video_params(compid : String) if compilation = Invidious::Database::Compilations.select(id: compid) compilation_index_array = compilation.index first_index = compilation_index_array[0] first_id = Invidious::Database::CompilationVideos.select_id_from_index(first_index) if !first_id.nil? - Invidious::Database::Compilations.update_first_video_id(compid, first_id) + timestamps = Invidious::Database::CompilationVideos.select_timestamps(compid, first_id) + if (!timestamps.nil?) + starting_timestamp_seconds=timestamps[0] + ending_timestamp_seconds=timestamps[1] + Invidious::Database::Compilations.update_first_video_params(compid, first_id, starting_timestamp_seconds, ending_timestamp_seconds) + end end else raise NotFoundException.new("Compilation does not exist.") diff --git a/src/invidious/database/compilations.cr b/src/invidious/database/compilations.cr index 1cdd1479..2f3d7072 100644 --- a/src/invidious/database/compilations.cr +++ b/src/invidious/database/compilations.cr @@ -90,14 +90,16 @@ module Invidious::Database::Compilations PG_DB.exec(request, id, index) end - def update_first_video_id(id : String, first_video_id : String) + def update_first_video_params(id : String, first_video_id : String, starting_timestamp_seconds : Int32, ending_timestamp_seconds : Int32) request = <<-SQL UPDATE compilations - SET first_video_id = $2 + SET first_video_id = $2, + first_video_starting_timestamp_seconds = $3, + first_video_ending_timestamp_seconds = $4 WHERE id = $1 SQL - PG_DB.exec(request, id, first_video_id) + PG_DB.exec(request, id, first_video_id, starting_timestamp_seconds, ending_timestamp_seconds) end # ------------------- # Select diff --git a/src/invidious/database/migrations/0011_create_compilations_table.cr b/src/invidious/database/migrations/0011_create_compilations_table.cr index 4bf5a814..643cd506 100644 --- a/src/invidious/database/migrations/0011_create_compilations_table.cr +++ b/src/invidious/database/migrations/0011_create_compilations_table.cr @@ -25,7 +25,9 @@ module Invidious::Database::Migrations updated timestamptz, privacy compilation_privacy, index int8[], - first_video_id text + first_video_id text, + first_video_starting_timestamp_seconds integer, + first_video_ending_timestamp_seconds integer ); SQL diff --git a/src/invidious/routes/compilations.cr b/src/invidious/routes/compilations.cr index 4e0b210d..bd1db90d 100644 --- a/src/invidious/routes/compilations.cr +++ b/src/invidious/routes/compilations.cr @@ -255,7 +255,7 @@ module Invidious::Routes::Compilations 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) + end_timestamp = env.params.body[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? @@ -267,6 +267,8 @@ module Invidious::Routes::Compilations end + update_first_video_params(compid) + env.redirect "/compilation?list=#{compid}" end @@ -431,12 +433,12 @@ module Invidious::Routes::Compilations Invidious::Database::CompilationVideos.insert(compilation_video) Invidious::Database::Compilations.update_video_added(compilation_id, compilation_video.index) - update_first_video_id(compilation_id) + update_first_video_params(compilation_id) when "action_remove_video" index = env.params.query["set_video_id"] Invidious::Database::CompilationVideos.delete(index) Invidious::Database::Compilations.update_video_removed(compilation_id, index) - update_first_video_id(compilation_id) + update_first_video_params(compilation_id) when "action_move_video_before" # TODO: Compilation stub #video_index = compilation.index @@ -461,7 +463,7 @@ module Invidious::Routes::Compilations compilation_index_array.insert(compilation_index_array_position-1,compilation_video[0].index) Invidious::Database::Compilations.move_video_before(compilation_id, compilation_index_array) end - update_first_video_id(compilation_id) + update_first_video_params(compilation_id) else return error_json(400, "Unsupported action #{action}") end diff --git a/src/invidious/views/compilation.ecr b/src/invidious/views/compilation.ecr index b86fac39..7fa900a1 100644 --- a/src/invidious/views/compilation.ecr +++ b/src/invidious/views/compilation.ecr @@ -13,7 +13,7 @@ <%- if compilation.is_a?(InvidiousCompilation) && compilation.author == user.try &.email -%> <%- if compilation.index.size > 0 -%>
- +  <%= translate(locale, "compilation_button_play") %>