mirror of
https://gitea.invidious.io/iv-org/invidious-copy-2022-08-14.git
synced 2024-08-15 00:53:20 +00:00
Move DB queries related to playlists in a separate module (1/3)
This commit is contained in:
parent
998edba6f0
commit
3deafe9f8d
5 changed files with 108 additions and 31 deletions
|
@ -816,11 +816,8 @@ post "/data_control" do |env|
|
||||||
index: Random::Secure.rand(0_i64..Int64::MAX),
|
index: Random::Secure.rand(0_i64..Int64::MAX),
|
||||||
})
|
})
|
||||||
|
|
||||||
video_array = playlist_video.to_a
|
Invidious::Database::PlaylistVideos.insert(playlist_video)
|
||||||
args = arg_array(video_array)
|
Invidious::Database::Playlists.update_video_added(playlist.id, playlist_video.index)
|
||||||
|
|
||||||
PG_DB.exec("INSERT INTO playlist_videos VALUES (#{args})", args: video_array)
|
|
||||||
PG_DB.exec("UPDATE playlists SET index = array_append(index, $1), video_count = cardinality(index) + 1, updated = $2 WHERE id = $3", playlist_video.index, Time.utc, playlist.id)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
94
src/invidious/database/playlists.cr
Normal file
94
src/invidious/database/playlists.cr
Normal file
|
@ -0,0 +1,94 @@
|
||||||
|
require "./base.cr"
|
||||||
|
|
||||||
|
#
|
||||||
|
# This module contains functions related to the "playlists" table.
|
||||||
|
#
|
||||||
|
module Invidious::Database::Playlists
|
||||||
|
extend self
|
||||||
|
|
||||||
|
# -------------------
|
||||||
|
# Insert / delete
|
||||||
|
# -------------------
|
||||||
|
|
||||||
|
def insert(playlist : InvidiousPlaylist)
|
||||||
|
playlist_array = playlist.to_a
|
||||||
|
|
||||||
|
request = <<-SQL
|
||||||
|
INSERT INTO playlists
|
||||||
|
VALUES (#{arg_array(playlist_array)})
|
||||||
|
SQL
|
||||||
|
|
||||||
|
PG_DB.exec(request, args: playlist_array)
|
||||||
|
end
|
||||||
|
|
||||||
|
# this function is a bit special: it will also remove all videos
|
||||||
|
# related to the given playlist ID in the "playlist_videos" table,
|
||||||
|
# in addition to deleting said ID from "playlists".
|
||||||
|
def delete(id : String)
|
||||||
|
request = <<-SQL
|
||||||
|
DELETE FROM playlist_videos * WHERE plid = $1;
|
||||||
|
DELETE FROM playlists * WHERE id = $1
|
||||||
|
SQL
|
||||||
|
|
||||||
|
PG_DB.exec(request, id)
|
||||||
|
end
|
||||||
|
|
||||||
|
# -------------------
|
||||||
|
# Update
|
||||||
|
# -------------------
|
||||||
|
|
||||||
|
def update_video_added(id : String, index : String | Int64)
|
||||||
|
request = <<-SQL
|
||||||
|
UPDATE playlists
|
||||||
|
SET index = array_append(index, $1),
|
||||||
|
video_count = cardinality(index) + 1,
|
||||||
|
updated = $2
|
||||||
|
WHERE id = $3
|
||||||
|
SQL
|
||||||
|
|
||||||
|
PG_DB.exec(request, index, Time.utc, id)
|
||||||
|
end
|
||||||
|
|
||||||
|
def update_video_removed(id : String, index : String | Int64)
|
||||||
|
request = <<-SQL
|
||||||
|
UPDATE playlists
|
||||||
|
SET index = array_remove(index, $1),
|
||||||
|
video_count = cardinality(index) - 1,
|
||||||
|
updated = $2
|
||||||
|
WHERE id = $3
|
||||||
|
SQL
|
||||||
|
|
||||||
|
PG_DB.exec(request, index, Time.utc, id)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
#
|
||||||
|
# This module contains functions related to the "playlist_videos" table.
|
||||||
|
#
|
||||||
|
module Invidious::Database::PlaylistVideos
|
||||||
|
extend self
|
||||||
|
|
||||||
|
# -------------------
|
||||||
|
# Insert / Delete
|
||||||
|
# -------------------
|
||||||
|
|
||||||
|
def insert(video : PlaylistVideo)
|
||||||
|
video_array = video.to_a
|
||||||
|
|
||||||
|
request = <<-SQL
|
||||||
|
INSERT INTO playlist_videos
|
||||||
|
VALUES (#{arg_array(video_array)})
|
||||||
|
SQL
|
||||||
|
|
||||||
|
PG_DB.exec(request, args: video_array)
|
||||||
|
end
|
||||||
|
|
||||||
|
def delete(index)
|
||||||
|
request = <<-SQL
|
||||||
|
DELETE FROM playlist_videos *
|
||||||
|
WHERE index = $1
|
||||||
|
SQL
|
||||||
|
|
||||||
|
PG_DB.exec(request, index)
|
||||||
|
end
|
||||||
|
end
|
|
@ -261,10 +261,7 @@ def create_playlist(db, title, privacy, user)
|
||||||
index: [] of Int64,
|
index: [] of Int64,
|
||||||
})
|
})
|
||||||
|
|
||||||
playlist_array = playlist.to_a
|
Invidious::Database::Playlists.insert(playlist)
|
||||||
args = arg_array(playlist_array)
|
|
||||||
|
|
||||||
db.exec("INSERT INTO playlists VALUES (#{args})", args: playlist_array)
|
|
||||||
|
|
||||||
return playlist
|
return playlist
|
||||||
end
|
end
|
||||||
|
@ -282,10 +279,7 @@ def subscribe_playlist(db, user, playlist)
|
||||||
index: [] of Int64,
|
index: [] of Int64,
|
||||||
})
|
})
|
||||||
|
|
||||||
playlist_array = playlist.to_a
|
Invidious::Database::Playlists.insert(playlist)
|
||||||
args = arg_array(playlist_array)
|
|
||||||
|
|
||||||
db.exec("INSERT INTO playlists VALUES (#{args})", args: playlist_array)
|
|
||||||
|
|
||||||
return playlist
|
return playlist
|
||||||
end
|
end
|
||||||
|
|
|
@ -216,8 +216,7 @@ module Invidious::Routes::API::V1::Authenticated
|
||||||
return error_json(403, "Invalid user")
|
return error_json(403, "Invalid user")
|
||||||
end
|
end
|
||||||
|
|
||||||
PG_DB.exec("DELETE FROM playlist_videos * WHERE plid = $1", plid)
|
Invidious::Database::Playlists.delete(plid)
|
||||||
PG_DB.exec("DELETE FROM playlists * WHERE id = $1", plid)
|
|
||||||
|
|
||||||
env.response.status_code = 204
|
env.response.status_code = 204
|
||||||
end
|
end
|
||||||
|
@ -266,11 +265,8 @@ module Invidious::Routes::API::V1::Authenticated
|
||||||
index: Random::Secure.rand(0_i64..Int64::MAX),
|
index: Random::Secure.rand(0_i64..Int64::MAX),
|
||||||
})
|
})
|
||||||
|
|
||||||
video_array = playlist_video.to_a
|
Invidious::Database::PlaylistVideos.insert(playlist_video)
|
||||||
args = arg_array(video_array)
|
Invidious::Database::Playlists.update_video_added(plid, playlist_video.index)
|
||||||
|
|
||||||
PG_DB.exec("INSERT INTO playlist_videos VALUES (#{args})", args: video_array)
|
|
||||||
PG_DB.exec("UPDATE playlists SET index = array_append(index, $1), video_count = cardinality(index) + 1, updated = $2 WHERE id = $3", playlist_video.index, Time.utc, plid)
|
|
||||||
|
|
||||||
env.response.headers["Location"] = "#{HOST_URL}/api/v1/auth/playlists/#{plid}/videos/#{playlist_video.index.to_u64.to_s(16).upcase}"
|
env.response.headers["Location"] = "#{HOST_URL}/api/v1/auth/playlists/#{plid}/videos/#{playlist_video.index.to_u64.to_s(16).upcase}"
|
||||||
env.response.status_code = 201
|
env.response.status_code = 201
|
||||||
|
@ -302,8 +298,8 @@ module Invidious::Routes::API::V1::Authenticated
|
||||||
return error_json(404, "Playlist does not contain index")
|
return error_json(404, "Playlist does not contain index")
|
||||||
end
|
end
|
||||||
|
|
||||||
PG_DB.exec("DELETE FROM playlist_videos * WHERE index = $1", index)
|
Invidious::Database::PlaylistVideos.delete(index)
|
||||||
PG_DB.exec("UPDATE playlists SET index = array_remove(index, $1), video_count = cardinality(index) - 1, updated = $2 WHERE id = $3", index, Time.utc, plid)
|
Invidious::Database::Playlists.update_video_removed(plid, index)
|
||||||
|
|
||||||
env.response.status_code = 204
|
env.response.status_code = 204
|
||||||
end
|
end
|
||||||
|
|
|
@ -122,8 +122,7 @@ module Invidious::Routes::Playlists
|
||||||
return env.redirect referer
|
return env.redirect referer
|
||||||
end
|
end
|
||||||
|
|
||||||
PG_DB.exec("DELETE FROM playlist_videos * WHERE plid = $1", plid)
|
Invidious::Database::Playlists.delete(plid)
|
||||||
PG_DB.exec("DELETE FROM playlists * WHERE id = $1", plid)
|
|
||||||
|
|
||||||
env.redirect "/feed/playlists"
|
env.redirect "/feed/playlists"
|
||||||
end
|
end
|
||||||
|
@ -363,15 +362,12 @@ module Invidious::Routes::Playlists
|
||||||
index: Random::Secure.rand(0_i64..Int64::MAX),
|
index: Random::Secure.rand(0_i64..Int64::MAX),
|
||||||
})
|
})
|
||||||
|
|
||||||
video_array = playlist_video.to_a
|
Invidious::Database::PlaylistVideos.insert(playlist_video)
|
||||||
args = arg_array(video_array)
|
Invidious::Database::Playlists.update_video_added(playlist_id, playlist_video.index)
|
||||||
|
|
||||||
PG_DB.exec("INSERT INTO playlist_videos VALUES (#{args})", args: video_array)
|
|
||||||
PG_DB.exec("UPDATE playlists SET index = array_append(index, $1), video_count = cardinality(index) + 1, updated = $2 WHERE id = $3", playlist_video.index, Time.utc, playlist_id)
|
|
||||||
when "action_remove_video"
|
when "action_remove_video"
|
||||||
index = env.params.query["set_video_id"]
|
index = env.params.query["set_video_id"]
|
||||||
PG_DB.exec("DELETE FROM playlist_videos * WHERE index = $1", index)
|
Invidious::Database::PlaylistVideos.delete(index)
|
||||||
PG_DB.exec("UPDATE playlists SET index = array_remove(index, $1), video_count = cardinality(index) - 1, updated = $2 WHERE id = $3", index, Time.utc, playlist_id)
|
Invidious::Database::Playlists.update_video_removed(playlist_id, index)
|
||||||
when "action_move_video_before"
|
when "action_move_video_before"
|
||||||
# TODO: Playlist stub
|
# TODO: Playlist stub
|
||||||
else
|
else
|
||||||
|
|
Loading…
Reference in a new issue