Run Crystal formatting tool

This commit is contained in:
broquemonsieur 2023-08-06 16:53:56 -07:00
parent 205c5493db
commit 93ac7cdeb4
10 changed files with 64 additions and 65 deletions

View File

@ -19,7 +19,7 @@ struct CompilationVideo
xml.element("yt:videoId") { xml.text self.id } xml.element("yt:videoId") { xml.text self.id }
xml.element("yt:channelId") { xml.text self.ucid } xml.element("yt:channelId") { xml.text self.ucid }
xml.element("title") { xml.text self.title } xml.element("title") { xml.text self.title }
xml.element("orderIndex") {xml.text self.order_index } xml.element("orderIndex") { xml.text self.order_index }
xml.element("link", rel: "alternate", href: "#{HOST_URL}/watch?v=#{self.id}") xml.element("link", rel: "alternate", href: "#{HOST_URL}/watch?v=#{self.id}")
xml.element("author") do xml.element("author") do
@ -263,7 +263,7 @@ def create_compilation(title, privacy, user)
index: [] of Int64, index: [] of Int64,
first_video_id: "", first_video_id: "",
first_video_starting_timestamp_seconds: 0, first_video_starting_timestamp_seconds: 0,
first_video_ending_timestamp_seconds: 0 first_video_ending_timestamp_seconds: 0,
}) })
Invidious::Database::Compilations.insert(compilation) Invidious::Database::Compilations.insert(compilation)
@ -273,18 +273,18 @@ end
def subscribe_compilation(user, compilation) def subscribe_compilation(user, compilation)
compilation = InvidiousCompilation.new({ compilation = InvidiousCompilation.new({
title: compilation.title.byte_slice(0, 150), title: compilation.title.byte_slice(0, 150),
id: compilation.id, id: compilation.id,
author: user.email, author: user.email,
description: "", # Max 5000 characters description: "", # Max 5000 characters
video_count: compilation.video_count, video_count: compilation.video_count,
created: Time.utc, created: Time.utc,
updated: compilation.updated, updated: compilation.updated,
privacy: CompilationPrivacy::Private, privacy: CompilationPrivacy::Private,
index: [] of Int64, index: [] of Int64,
first_video_id: "", first_video_id: "",
first_video_starting_timestamp_seconds: 0, first_video_starting_timestamp_seconds: 0,
first_video_ending_timestamp_seconds: 0 first_video_ending_timestamp_seconds: 0,
}) })
Invidious::Database::Compilations.insert(compilation) Invidious::Database::Compilations.insert(compilation)
@ -339,21 +339,21 @@ end
def update_first_video_params(compid : String) def update_first_video_params(compid : String)
if compilation = Invidious::Database::Compilations.select(id: compid) if compilation = Invidious::Database::Compilations.select(id: compid)
compilation_index_array = compilation.index compilation_index_array = compilation.index
if (compilation_index_array.size > 0) if (compilation_index_array.size > 0)
first_index = compilation_index_array[0] first_index = compilation_index_array[0]
first_id = Invidious::Database::CompilationVideos.select_id_from_index(first_index) first_id = Invidious::Database::CompilationVideos.select_id_from_index(first_index)
if !first_id.nil? if !first_id.nil?
timestamps = Invidious::Database::CompilationVideos.select_timestamps(compid, first_id) timestamps = Invidious::Database::CompilationVideos.select_timestamps(compid, first_id)
if (!timestamps.nil?) if (!timestamps.nil?)
starting_timestamp_seconds=timestamps[0] starting_timestamp_seconds = timestamps[0]
ending_timestamp_seconds=timestamps[1] ending_timestamp_seconds = timestamps[1]
Invidious::Database::Compilations.update_first_video_params(compid, first_id, starting_timestamp_seconds, ending_timestamp_seconds) Invidious::Database::Compilations.update_first_video_params(compid, first_id, starting_timestamp_seconds, ending_timestamp_seconds)
end end
end end
end end
else else
raise NotFoundException.new("Compilation does not exist.") raise NotFoundException.new("Compilation does not exist.")
end end
end end
def get_compilation_videos(compilation : InvidiousCompilation | Compilation, offset : Int32, video_id = nil) def get_compilation_videos(compilation : InvidiousCompilation | Compilation, offset : Int32, video_id = nil)
@ -368,7 +368,7 @@ def get_compilation_videos(compilation : InvidiousCompilation | Compilation, off
else else
if video_id if video_id
initial_data = YoutubeAPI.next({ initial_data = YoutubeAPI.next({
"videoId" => video_id, "videoId" => video_id,
"compilationId" => compilation.id, "compilationId" => compilation.id,
}) })
offset = initial_data.dig?("contents", "twoColumnWatchNextResults", "compilation", "compilation", "currentIndex").try &.as_i || offset offset = initial_data.dig?("contents", "twoColumnWatchNextResults", "compilation", "compilation", "currentIndex").try &.as_i || offset
@ -441,7 +441,7 @@ def extract_compilation_videos(initial_data : Hash(String, JSON::Any))
published: Time.utc, published: Time.utc,
compid: compid, compid: compid,
index: index, index: index,
order_index: order_index order_index: order_index,
}) })
end end
end end

View File

@ -88,7 +88,7 @@ module Invidious::Database::Compilations
SQL SQL
PG_DB.exec(request, id, index) PG_DB.exec(request, id, index)
end end
def update_first_video_params(id : String, first_video_id : String, starting_timestamp_seconds : Int32, ending_timestamp_seconds : Int32) def update_first_video_params(id : String, first_video_id : String, starting_timestamp_seconds : Int32, ending_timestamp_seconds : Int32)
request = <<-SQL request = <<-SQL
@ -100,7 +100,8 @@ module Invidious::Database::Compilations
SQL SQL
PG_DB.exec(request, id, first_video_id, starting_timestamp_seconds, ending_timestamp_seconds) PG_DB.exec(request, id, first_video_id, starting_timestamp_seconds, ending_timestamp_seconds)
end end
# ------------------- # -------------------
# Select # Select
# ------------------- # -------------------
@ -131,7 +132,7 @@ module Invidious::Database::Compilations
SQL SQL
PG_DB.query_one?(request, id, as: Array(Int64)) PG_DB.query_one?(request, id, as: Array(Int64))
end end
# ------------------- # -------------------
# Select (filtered) # Select (filtered)
@ -261,15 +262,13 @@ module Invidious::Database::CompilationVideos
end end
def select_timestamps(compid : String, vid : String) def select_timestamps(compid : String, vid : String)
request = <<-SQL request = <<-SQL
SELECT starting_timestamp_seconds,ending_timestamp_seconds FROM compilation_videos SELECT starting_timestamp_seconds,ending_timestamp_seconds FROM compilation_videos
WHERE compid = $1 AND id = $2 WHERE compid = $1 AND id = $2
LIMIT 1 LIMIT 1
SQL SQL
return PG_DB.query_one?(request, compid, vid, as: {Int32,Int32}) return PG_DB.query_one?(request, compid, vid, as: {Int32, Int32})
end end
def select_id_from_order_index(order_index : Int32) def select_id_from_order_index(order_index : Int32)

View File

@ -38,7 +38,7 @@ def recode_length_seconds(time)
if time < 0 if time < 0
return "" return ""
elsif time == 0 elsif time == 0
return "0:00" return "0:00"
else else
time = time.seconds time = time.seconds
text = "#{time.minutes.to_s.rjust(2, '0')}:#{time.seconds.to_s.rjust(2, '0')}" text = "#{time.minutes.to_s.rjust(2, '0')}:#{time.seconds.to_s.rjust(2, '0')}"

View File

@ -243,10 +243,10 @@ module Invidious::Routes::API::V1::Authenticated
env.response.headers["Location"] = "#{HOST_URL}/api/v1/auth/compilations/#{compilation.id}" env.response.headers["Location"] = "#{HOST_URL}/api/v1/auth/compilations/#{compilation.id}"
env.response.status_code = 201 env.response.status_code = 201
{ {
"title" => title, "title" => title,
"compilationId" => compilation.id, "compilationId" => compilation.id,
}.to_json }.to_json
end end
def self.create_playlist(env) def self.create_playlist(env)
env.response.content_type = "application/json" env.response.content_type = "application/json"

View File

@ -68,15 +68,14 @@ module Invidious::Routes::API::V1::Misc
response = { response = {
"compilationHtml" => compilation_html, "compilationHtml" => compilation_html,
"index" => index, "index" => index,
"nextVideo" => next_video, "nextVideo" => next_video,
}.to_json }.to_json
end end
response response
end end
# APIv1 currently uses the same logic for both # APIv1 currently uses the same logic for both
# user playlists and Invidious playlists. This means that we can't # user playlists and Invidious playlists. This means that we can't
# reasonably split them yet. This should be addressed in APIv2 # reasonably split them yet. This should be addressed in APIv2

View File

@ -22,7 +22,7 @@ module Invidious::Routes::Compilations
user = env.get? "user" user = env.get? "user"
sid = env.get? "sid" sid = env.get? "sid"
referer = get_referer(env) referer = get_referer(env)
return env.redirect "/" if user.nil? return env.redirect "/" if user.nil?
@ -49,7 +49,7 @@ module Invidious::Routes::Compilations
if Invidious::Database::Compilations.count_owned_by(user.email) >= 100 if Invidious::Database::Compilations.count_owned_by(user.email) >= 100
return error_template(400, "User cannot have more than 100 compilations.") return error_template(400, "User cannot have more than 100 compilations.")
end end
compilation = create_compilation(title, privacy, user) compilation = create_compilation(title, privacy, user)
env.redirect "/compilation?list=#{compilation.id}" env.redirect "/compilation?list=#{compilation.id}"
@ -218,7 +218,6 @@ module Invidious::Routes::Compilations
return error_template(400, ex) return error_template(400, ex)
end end
if !compid || compid.empty? if !compid || compid.empty?
return error_json(400, "A compilation ID is required") return error_json(400, "A compilation ID is required")
end end
@ -245,7 +244,7 @@ module Invidious::Routes::Compilations
if !start_timestamp.nil? && !compilation_video[0].id.nil? if !start_timestamp.nil? && !compilation_video[0].id.nil?
start_timestamp_seconds = decode_length_seconds(start_timestamp) start_timestamp_seconds = decode_length_seconds(start_timestamp)
if !start_timestamp_seconds.nil? if !start_timestamp_seconds.nil?
if start_timestamp_seconds >= 0 && start_timestamp_seconds <= compilation_video[0].length_seconds if start_timestamp_seconds >= 0 && start_timestamp_seconds <= compilation_video[0].length_seconds
Invidious::Database::CompilationVideos.update_start_timestamp(compilation_video[0].id, start_timestamp_seconds.to_i) Invidious::Database::CompilationVideos.update_start_timestamp(compilation_video[0].id, start_timestamp_seconds.to_i)
end end
end end
@ -261,14 +260,12 @@ module Invidious::Routes::Compilations
end end
end end
end end
end end
update_first_video_params(compid) update_first_video_params(compid)
env.redirect "/compilation?list=#{compid}" env.redirect "/compilation?list=#{compid}"
end end
def self.add_compilation_items_page(env) def self.add_compilation_items_page(env)
prefs = env.get("preferences").as(Preferences) prefs = env.get("preferences").as(Preferences)
@ -314,7 +311,7 @@ module Invidious::Routes::Compilations
env.set "add_compilation_items", compid env.set "add_compilation_items", compid
templated "add_compilation_items" templated "add_compilation_items"
end end
def self.compilation_ajax(env) def self.compilation_ajax(env)
locale = env.get("preferences").as(Preferences).locale locale = env.get("preferences").as(Preferences).locale
@ -326,7 +323,7 @@ module Invidious::Routes::Compilations
redirect = env.params.query["redirect"]? redirect = env.params.query["redirect"]?
redirect ||= "true" redirect ||= "true"
redirect = redirect == "true" redirect = redirect == "true"
if !user if !user
if redirect if redirect
return env.redirect referer return env.redirect referer
else else
@ -362,7 +359,7 @@ module Invidious::Routes::Compilations
elsif env.params.query["action_move_video_before"]? elsif env.params.query["action_move_video_before"]?
action = "action_move_video_before" action = "action_move_video_before"
elsif env.params.query["action_move_video_after"]? elsif env.params.query["action_move_video_after"]?
action = "action_move_video_after" action = "action_move_video_after"
else else
return env.redirect referer return env.redirect referer
end end
@ -422,7 +419,7 @@ module Invidious::Routes::Compilations
compid: compilation_id, compid: compilation_id,
live_now: video.live_now, live_now: video.live_now,
index: Random::Secure.rand(0_i64..Int64::MAX), index: Random::Secure.rand(0_i64..Int64::MAX),
order_index: compilation.index.size order_index: compilation.index.size,
}) })
Invidious::Database::CompilationVideos.insert(compilation_video) Invidious::Database::CompilationVideos.insert(compilation_video)
@ -451,7 +448,7 @@ module Invidious::Routes::Compilations
compilation_index_array_position = compilation_index_array.index(compilation_video[0].index) compilation_index_array_position = compilation_index_array.index(compilation_video[0].index)
if !compilation_index_array_position.nil? if !compilation_index_array_position.nil?
compilation_index_array.delete_at(compilation_index_array_position) compilation_index_array.delete_at(compilation_index_array_position)
compilation_index_array.insert(compilation_index_array_position-1,compilation_video[0].index) compilation_index_array.insert(compilation_index_array_position - 1, compilation_video[0].index)
Invidious::Database::Compilations.move_video_position(compilation_id, compilation_index_array) Invidious::Database::Compilations.move_video_position(compilation_id, compilation_index_array)
end end
update_first_video_params(compilation_id) update_first_video_params(compilation_id)
@ -473,10 +470,14 @@ module Invidious::Routes::Compilations
compilation_index_array_position = compilation_index_array.index(compilation_video[0].index) compilation_index_array_position = compilation_index_array.index(compilation_video[0].index)
if !compilation_index_array_position.nil? if !compilation_index_array_position.nil?
compilation_index_array.delete_at(compilation_index_array_position) compilation_index_array.delete_at(compilation_index_array_position)
compilation_index_array.insert(compilation_index_array_position+1,compilation_video[0].index) if (compilation_index_array_position == compilation_index_array.size)
compilation_index_array.insert(compilation_index_array_position, compilation_video[0].index)
else
compilation_index_array.insert(compilation_index_array_position + 1, compilation_video[0].index)
end
Invidious::Database::Compilations.move_video_position(compilation_id, compilation_index_array) Invidious::Database::Compilations.move_video_position(compilation_id, compilation_index_array)
end end
update_first_video_params(compilation_id) update_first_video_params(compilation_id)
else else
return error_json(400, "Unsupported action #{action}") return error_json(400, "Unsupported action #{action}")
end end
@ -537,5 +538,5 @@ module Invidious::Routes::Compilations
end end
templated "compilation" templated "compilation"
end end
end end

View File

@ -28,7 +28,7 @@ module Invidious::Routes::Misc
env.redirect "/feed/compilations" env.redirect "/feed/compilations"
else else
env.redirect "/feed/popular" env.redirect "/feed/popular"
end end
else else
templated "search_homepage", navbar_search: false templated "search_homepage", navbar_search: false
end end

View File

@ -41,32 +41,32 @@ module Invidious::Routes::Watch
if env.params.query["list"]?.try &.starts_with? "IVPL" if env.params.query["list"]?.try &.starts_with? "IVPL"
plid = env.params.query["list"]?.try &.gsub(/[^a-zA-Z0-9_-]/, "") plid = env.params.query["list"]?.try &.gsub(/[^a-zA-Z0-9_-]/, "")
continuation = process_continuation(env.params.query, plid, id) continuation = process_continuation(env.params.query, plid, id)
elsif env.params.query["list"]?.try &.starts_with? "IVCMP" elsif env.params.query["list"]?.try &.starts_with? "IVCMP"
compid = env.params.query["list"]?.try &.gsub(/[^a-zA-Z0-9_-]/, "") compid = env.params.query["list"]?.try &.gsub(/[^a-zA-Z0-9_-]/, "")
if (!compid.nil?) if (!compid.nil?)
index=Invidious::Database::CompilationVideos.select_index(compid,id) index = Invidious::Database::CompilationVideos.select_index(compid, id)
indices_array=Invidious::Database::Compilations.select_index_array(compid) indices_array = Invidious::Database::Compilations.select_index_array(compid)
if (!indices_array.nil?) if (!indices_array.nil?)
position_of_index=indices_array.index(index) position_of_index = indices_array.index(index)
if (!position_of_index.nil? && position_of_index != indices_array.size - 1) if (!position_of_index.nil? && position_of_index != indices_array.size - 1)
next_index=indices_array[position_of_index+1] next_index = indices_array[position_of_index + 1]
else else
next_index=indices_array[0] next_index = indices_array[0]
end end
if (!next_index.nil?) if (!next_index.nil?)
next_id=Invidious::Database::CompilationVideos.select_id_from_index(next_index) next_id = Invidious::Database::CompilationVideos.select_id_from_index(next_index)
if (!next_id.nil?) if (!next_id.nil?)
timestamps = Invidious::Database::CompilationVideos.select_timestamps(compid, next_id) timestamps = Invidious::Database::CompilationVideos.select_timestamps(compid, next_id)
if (!timestamps.nil?) if (!timestamps.nil?)
starting_timestamp_seconds=timestamps[0] starting_timestamp_seconds = timestamps[0]
ending_timestamp_seconds=timestamps[1] ending_timestamp_seconds = timestamps[1]
end end
end end
end end
end end
end end
continuation = process_continuation(env.params.query, compid, id) continuation = process_continuation(env.params.query, compid, id)
end end
nojs = env.params.query["nojs"]? nojs = env.params.query["nojs"]?

View File

@ -89,7 +89,7 @@ module Invidious::Routing
get "/add_compilation_items", Routes::Compilations, :add_compilation_items_page get "/add_compilation_items", Routes::Compilations, :add_compilation_items_page
get "/edit_compilation", Routes::Compilations, :edit get "/edit_compilation", Routes::Compilations, :edit
post "/edit_compilation", Routes::Compilations, :adjust_timestamps post "/edit_compilation", Routes::Compilations, :adjust_timestamps
end end
def register_iv_playlist_routes def register_iv_playlist_routes
get "/create_playlist", Routes::Playlists, :new get "/create_playlist", Routes::Playlists, :new

View File

@ -81,7 +81,7 @@ module Invidious::Search
# string itself (legacy), and the channel is ignored. # string itself (legacy), and the channel is ignored.
# #
@filters, _, @query, _ = Filters.from_legacy_filters(@raw_query) @filters, _, @query, _ = Filters.from_legacy_filters(@raw_query)
# #
when .subscriptions?, .regular? when .subscriptions?, .regular?
if params["sp"]? if params["sp"]?
# Parse the `sp` URL parameter (youtube compatibility) # Parse the `sp` URL parameter (youtube compatibility)