Add timestamp DB handling

This commit is contained in:
broquemonsieur 2023-07-18 20:16:40 -07:00
parent be42dd0a4b
commit 60a184eda3
4 changed files with 85 additions and 37 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -87,6 +87,20 @@
</p>
</a></div>
</div>
<% when InvidiousCompilation %>
<% link_url = "/compilation?list=#{item.id}" %>
<a style="width:100%" href="<%= link_url %>">
<% if !env.get("preferences").as(Preferences).thin_mode %>
<div class="thumbnail">
<img loading="lazy" tabindex="-1" class="thumbnail" src="<%= URI.parse(item.thumbnail || "/").request_target %>" alt="" />
<p class="length"><%= translate_count(locale, "generic_videos_count", item.video_count, NumberFormatting::Separator) %></p>
</div>
<% end %>
<p dir="auto"><%= HTML.escape(item.title) %></p>
</a>
<a href="/channel/<%= item.ucid %>">
<p dir="auto"><b><%= HTML.escape(item.author) %><% if !item.is_a?(InvidiousCompilation) && !item.is_a?(InvidiousPlaylist) && !item.author_verified.nil? && item.author_verified %>&nbsp;<i class="icon ion ion-md-checkmark-circle"></i><% end %></b></p>
</a>
<% 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}" %>
<a style="width:100%" href="<%= url %>">
<% if !env.get("preferences").as(Preferences).thin_mode %>
<div class="thumbnail">
<img loading="lazy" tabindex="-1" class="thumbnail" src="<%= URI.parse(item.thumbnail || "/").request_target %>" alt="" />
<p class="length"><%= translate_count(locale, "generic_videos_count", item.video_count, NumberFormatting::Separator) %></p>
</div>
<% end %>
<p dir="auto"><%= HTML.escape(item.title) %></p>
elsif item.is_a?(CompilationVideo)
<% when CompilationVideo %>
<div style="width:100%; height:50px; border:1px solid black;">
<!-- <a style="width:100%" href="/watch?v=<%= item.id %>&list=<%= item.compid %>&index=<%= item.index %>">
<% if !env.get("preferences").as(Preferences).thin_mode %>
<div class="thumbnail">
<img loading="lazy" tabindex="-1" class="thumbnail" src="/vi/<%= item.id %>/mqdefault.jpg" alt="" />
<% if compid_form = env.get?("remove_compilation_items") %>
<form data-onsubmit="return_false" action="/compilation_ajax?action_remove_video=1&set_video_id=<%= item.index %>&compilation_id=<%= compid_form %>&referer=<%= env.get("current_page") %>" method="post">
<input type="hidden" name="csrf_token" value="<%= HTML.escape(env.get?("csrf_token").try &.as(String) || "") %>">
<p class="watched">
<button type="submit" style="all:unset" data-onclick="remove_compilation_item" data-index="<%= item.index %>" data-compid="<%= compid_form %>"><i class="icon ion-md-trash"></i></button>
</p>
</form>
<% end %>
</div>
<% end %>
<p dir="auto"><%= HTML.escape(item.title) %></p>
</a> -->
</div>
</a>
<a href="/channel/<%= item.ucid %>">
<p dir="auto"><b><%= HTML.escape(item.author) %><% if !item.is_a?(InvidiousCompilation) && !item.is_a?(InvidiousPlaylist) && !item.author_verified.nil? && item.author_verified %>&nbsp;<i class="icon ion ion-md-checkmark-circle"></i><% end %></b></p>
</a>
# elsif item.is_a?(CompilationVideo)
else
link_url = "/watch?v=#{item.id}"
endpoint_params = "?v=#{item.id}"