Move DB queries related to playlists in a separate module (3/3)

This commit is contained in:
Samantaz Fox 2021-12-06 17:02:15 +01:00
parent 7691f53520
commit 85cf27119c
No known key found for this signature in database
GPG Key ID: F42821059186176E
5 changed files with 94 additions and 10 deletions

View File

@ -656,7 +656,7 @@ get "/subscription_manager" do |env|
if format == "json" if format == "json"
env.response.content_type = "application/json" env.response.content_type = "application/json"
env.response.headers["content-disposition"] = "attachment" env.response.headers["content-disposition"] = "attachment"
playlists = PG_DB.query_all("SELECT * FROM playlists WHERE author = $1 AND id LIKE 'IV%' ORDER BY created", user.email, as: InvidiousPlaylist) playlists = Invidious::Database::Playlists.select_like_iv(user.email)
next JSON.build do |json| next JSON.build do |json|
json.object do json.object do
@ -672,7 +672,7 @@ get "/subscription_manager" do |env|
json.field "privacy", playlist.privacy.to_s json.field "privacy", playlist.privacy.to_s
json.field "videos" do json.field "videos" do
json.array do json.array do
PG_DB.query_all("SELECT id FROM playlist_videos WHERE plid = $1 ORDER BY array_position($2, index) LIMIT 500", playlist.id, playlist.index, as: String).each do |video_id| Invidious::Database::PlaylistVideos.select_ids(playlist.id, playlist.index, limit: 500).each do |video_id|
json.string video_id json.string video_id
end end
end end

View File

@ -117,6 +117,39 @@ module Invidious::Database::Playlists
return PG_DB.query_all(request, author, as: InvidiousPlaylist) return PG_DB.query_all(request, author, as: InvidiousPlaylist)
end end
# -------------------
# Salect (filtered)
# -------------------
def select_like_iv(email : String) : Array(InvidiousPlaylist)
request = <<-SQL
SELECT * FROM playlists
WHERE author = $1 AND id LIKE 'IV%'
ORDER BY created
SQL
PG_DB.query_all(request, email, as: InvidiousPlaylist)
end
def select_not_like_iv(email : String) : Array(InvidiousPlaylist)
request = <<-SQL
SELECT * FROM playlists
WHERE author = $1 AND id NOT LIKE 'IV%'
ORDER BY created
SQL
PG_DB.query_all(request, email, as: InvidiousPlaylist)
end
def select_user_created_playlists(email : String) : Array({String, String})
request = <<-SQL
SELECT id,title FROM playlists
WHERE author = $1 AND id LIKE 'IV%'
SQL
PG_DB.query_all(request, email, as: {String, String})
end
# ------------------- # -------------------
# Misc checks # Misc checks
# ------------------- # -------------------
@ -148,6 +181,8 @@ end
module Invidious::Database::PlaylistVideos module Invidious::Database::PlaylistVideos
extend self extend self
private alias VideoIndex = Int64 | Array(Int64)
# ------------------- # -------------------
# Insert / Delete # Insert / Delete
# ------------------- # -------------------
@ -171,4 +206,52 @@ module Invidious::Database::PlaylistVideos
PG_DB.exec(request, index) PG_DB.exec(request, index)
end end
# -------------------
# Salect
# -------------------
def select(plid : String, index : VideoIndex, offset, limit = 100) : Array(PlaylistVideo)
request = <<-SQL
SELECT * FROM playlist_videos
WHERE plid = $1
ORDER BY array_position($2, index)
LIMIT $3
OFFSET $4
SQL
return PG_DB.query_all(request, plid, index, limit, offset, as: PlaylistVideo)
end
def select_index(plid : String, vid : String) : Int64?
request = <<-SQL
SELECT index FROM playlist_videos
WHERE plid = $1 AND id = $2
LIMIT 1
SQL
return PG_DB.query_one?(request, plid, vid, as: Int64)
end
def select_one_id(plid : String, index : VideoIndex) : String?
request = <<-SQL
SELECT id FROM playlist_videos
WHERE plid = $1
ORDER BY array_position($2, index)
LIMIT 1
SQL
return PG_DB.query_one?(request, plid, index, as: String)
end
def select_ids(plid : String, index : VideoIndex, limit = 500) : Array(String)
request = <<-SQL
SELECT id FROM playlist_videos
WHERE plid = $1
ORDER BY array_position($2, index)
LIMIT $3
SQL
return PG_DB.query_all(request, plid, index, limit, as: String)
end
end end

View File

@ -200,8 +200,8 @@ struct InvidiousPlaylist
json.field "videos" do json.field "videos" do
json.array do json.array do
if !offset || offset == 0 if (!offset || offset == 0) && !video_id.nil?
index = PG_DB.query_one?("SELECT index FROM playlist_videos WHERE plid = $1 AND id = $2 LIMIT 1", self.id, video_id, as: Int64) index = Invidious::Database::PlaylistVideos.select_index(self.id, video_id)
offset = self.index.index(index) || 0 offset = self.index.index(index) || 0
end end
@ -225,7 +225,8 @@ struct InvidiousPlaylist
end end
def thumbnail def thumbnail
@thumbnail_id ||= PG_DB.query_one?("SELECT id FROM playlist_videos WHERE plid = $1 ORDER BY array_position($2, index) LIMIT 1", self.id, self.index, as: String) || "-----------" # TODO: Get playlist thumbnail from playlist data rather than first video
@thumbnail_id ||= Invidious::Database::PlaylistVideos.select_one_id(self.id, self.index) || "-----------"
"/vi/#{@thumbnail_id}/mqdefault.jpg" "/vi/#{@thumbnail_id}/mqdefault.jpg"
end end
@ -411,8 +412,7 @@ def get_playlist_videos(db, playlist, offset, locale = nil, video_id = nil)
end end
if playlist.is_a? InvidiousPlaylist if playlist.is_a? InvidiousPlaylist
db.query_all("SELECT * FROM playlist_videos WHERE plid = $1 ORDER BY array_position($2, index) LIMIT 100 OFFSET $3", Invidious::Database::PlaylistVideos.select(playlist.id, playlist.index, offset, limit: 100)
playlist.id, playlist.index, offset, as: PlaylistVideo)
else else
if video_id if video_id
initial_data = YoutubeAPI.next({ initial_data = YoutubeAPI.next({

View File

@ -15,13 +15,14 @@ module Invidious::Routes::Feeds
user = user.as(User) user = user.as(User)
items_created = PG_DB.query_all("SELECT * FROM playlists WHERE author = $1 AND id LIKE 'IV%' ORDER BY created", user.email, as: InvidiousPlaylist) # TODO: make a single DB call and separate the items here?
items_created = Invidious::Database::Playlists.select_like_iv(user.email)
items_created.map! do |item| items_created.map! do |item|
item.author = "" item.author = ""
item item
end end
items_saved = PG_DB.query_all("SELECT * FROM playlists WHERE author = $1 AND id NOT LIKE 'IV%' ORDER BY created", user.email, as: InvidiousPlaylist) items_saved = Invidious::Database::Playlists.select_not_like_iv(user.email)
items_saved.map! do |item| items_saved.map! do |item|
item.author = "" item.author = ""
item item

View File

@ -138,7 +138,7 @@ we're going to need to do it here in order to allow for translations.
</p> </p>
<% if user %> <% if user %>
<% playlists = PG_DB.query_all("SELECT id,title FROM playlists WHERE author = $1 AND id LIKE 'IV%'", user.email, as: {String, String}) %> <% playlists = Invidious::Database::Playlists.select_user_created_playlists(user.email) %>
<% if !playlists.empty? %> <% if !playlists.empty? %>
<form data-onsubmit="return_false" class="pure-form pure-form-stacked" action="/playlist_ajax" method="post"> <form data-onsubmit="return_false" class="pure-form pure-form-stacked" action="/playlist_ajax" method="post">
<div class="pure-control-group"> <div class="pure-control-group">