Move DB queries related to 'users' in a separate module (2/2)

This commit is contained in:
Samantaz Fox 2021-12-03 03:29:52 +01:00
parent 094f835642
commit 7691f53520
No known key found for this signature in database
GPG Key ID: F42821059186176E
10 changed files with 121 additions and 43 deletions

View File

@ -759,18 +759,18 @@ post "/data_control" do |env|
user.subscriptions = get_batch_channels(user.subscriptions, PG_DB, false, false) user.subscriptions = get_batch_channels(user.subscriptions, PG_DB, false, false)
PG_DB.exec("UPDATE users SET feed_needs_update = true, subscriptions = $1 WHERE email = $2", user.subscriptions, user.email) Invidious::Database::Users.update_subscriptions(user)
end end
if body["watch_history"]? if body["watch_history"]?
user.watched += body["watch_history"].as_a.map(&.as_s) user.watched += body["watch_history"].as_a.map(&.as_s)
user.watched.uniq! user.watched.uniq!
PG_DB.exec("UPDATE users SET watched = $1 WHERE email = $2", user.watched, user.email) Invidious::Database::Users.update_watch_history(user)
end end
if body["preferences"]? if body["preferences"]?
user.preferences = Preferences.from_json(body["preferences"].to_json) user.preferences = Preferences.from_json(body["preferences"].to_json)
PG_DB.exec("UPDATE users SET preferences = $1 WHERE email = $2", user.preferences.to_json, user.email) Invidious::Database::Users.update_preferences(user)
end end
if playlists = body["playlists"]?.try &.as_a? if playlists = body["playlists"]?.try &.as_a?
@ -831,7 +831,7 @@ post "/data_control" do |env|
user.subscriptions = get_batch_channels(user.subscriptions, PG_DB, false, false) user.subscriptions = get_batch_channels(user.subscriptions, PG_DB, false, false)
PG_DB.exec("UPDATE users SET feed_needs_update = true, subscriptions = $1 WHERE email = $2", user.subscriptions, user.email) Invidious::Database::Users.update_subscriptions(user)
when "import_freetube" when "import_freetube"
user.subscriptions += body.scan(/"channelId":"(?<channel_id>[a-zA-Z0-9_-]{24})"/).map do |md| user.subscriptions += body.scan(/"channelId":"(?<channel_id>[a-zA-Z0-9_-]{24})"/).map do |md|
md["channel_id"] md["channel_id"]
@ -840,7 +840,7 @@ post "/data_control" do |env|
user.subscriptions = get_batch_channels(user.subscriptions, PG_DB, false, false) user.subscriptions = get_batch_channels(user.subscriptions, PG_DB, false, false)
PG_DB.exec("UPDATE users SET feed_needs_update = true, subscriptions = $1 WHERE email = $2", user.subscriptions, user.email) Invidious::Database::Users.update_subscriptions(user)
when "import_newpipe_subscriptions" when "import_newpipe_subscriptions"
body = JSON.parse(body) body = JSON.parse(body)
user.subscriptions += body["subscriptions"].as_a.compact_map do |channel| user.subscriptions += body["subscriptions"].as_a.compact_map do |channel|
@ -859,7 +859,7 @@ post "/data_control" do |env|
user.subscriptions = get_batch_channels(user.subscriptions, PG_DB, false, false) user.subscriptions = get_batch_channels(user.subscriptions, PG_DB, false, false)
PG_DB.exec("UPDATE users SET feed_needs_update = true, subscriptions = $1 WHERE email = $2", user.subscriptions, user.email) Invidious::Database::Users.update_subscriptions(user)
when "import_newpipe" when "import_newpipe"
Compress::Zip::Reader.open(IO::Memory.new(body)) do |file| Compress::Zip::Reader.open(IO::Memory.new(body)) do |file|
file.each_entry do |entry| file.each_entry do |entry|
@ -871,14 +871,14 @@ post "/data_control" do |env|
user.watched += db.query_all("SELECT url FROM streams", as: String).map(&.lchop("https://www.youtube.com/watch?v=")) user.watched += db.query_all("SELECT url FROM streams", as: String).map(&.lchop("https://www.youtube.com/watch?v="))
user.watched.uniq! user.watched.uniq!
PG_DB.exec("UPDATE users SET watched = $1 WHERE email = $2", user.watched, user.email) Invidious::Database::Users.update_watch_history(user)
user.subscriptions += db.query_all("SELECT url FROM subscriptions", as: String).map(&.lchop("https://www.youtube.com/channel/")) user.subscriptions += db.query_all("SELECT url FROM subscriptions", as: String).map(&.lchop("https://www.youtube.com/channel/"))
user.subscriptions.uniq! user.subscriptions.uniq!
user.subscriptions = get_batch_channels(user.subscriptions, PG_DB, false, false) user.subscriptions = get_batch_channels(user.subscriptions, PG_DB, false, false)
PG_DB.exec("UPDATE users SET feed_needs_update = true, subscriptions = $1 WHERE email = $2", user.subscriptions, user.email) Invidious::Database::Users.update_subscriptions(user)
db.close db.close
tempfile.delete tempfile.delete
@ -962,7 +962,7 @@ post "/change_password" do |env|
end end
new_password = Crypto::Bcrypt::Password.create(new_password, cost: 10) new_password = Crypto::Bcrypt::Password.create(new_password, cost: 10)
PG_DB.exec("UPDATE users SET password = $1 WHERE email = $2", new_password.to_s, user.email) Invidious::Database::Users.update_password(user, new_password.to_s)
env.redirect referer env.redirect referer
end end

View File

@ -238,8 +238,7 @@ def fetch_channel(ucid, db, pull_all_videos = true, locale = nil)
if was_insert if was_insert
LOGGER.trace("fetch_channel: #{ucid} : video #{video_id} : Inserted, updating subscriptions") LOGGER.trace("fetch_channel: #{ucid} : video #{video_id} : Inserted, updating subscriptions")
db.exec("UPDATE users SET notifications = array_append(notifications, $1), \ Invidious::Database::Users.add_notification(video)
feed_needs_update = true WHERE $2 = ANY(subscriptions)", video.id, video.ucid)
else else
LOGGER.trace("fetch_channel: #{ucid} : video #{video_id} : Updated") LOGGER.trace("fetch_channel: #{ucid} : video #{video_id} : Updated")
end end
@ -275,9 +274,7 @@ def fetch_channel(ucid, db, pull_all_videos = true, locale = nil)
# so since they don't provide a published date here we can safely ignore them. # so since they don't provide a published date here we can safely ignore them.
if Time.utc - video.published > 1.minute if Time.utc - video.published > 1.minute
was_insert = Invidious::Database::ChannelVideos.insert(video) was_insert = Invidious::Database::ChannelVideos.insert(video)
Invidious::Database::Users.add_notification(video) if was_insert
db.exec("UPDATE users SET notifications = array_append(notifications, $1), \
feed_needs_update = true WHERE $2 = ANY(subscriptions)", video.id, video.ucid) if was_insert
end end
end end

View File

@ -39,6 +39,16 @@ module Invidious::Database::Users
# Update (history) # Update (history)
# ------------------- # -------------------
def update_watch_history(user : User)
request = <<-SQL
UPDATE users
SET watched = $1
WHERE email = $2
SQL
PG_DB.exec(request, user.watched, user.email)
end
def mark_watched(user : User, vid : String) def mark_watched(user : User, vid : String)
request = <<-SQL request = <<-SQL
UPDATE users UPDATE users
@ -73,6 +83,16 @@ module Invidious::Database::Users
# Update (channels) # Update (channels)
# ------------------- # -------------------
def update_subscriptions(user : User)
request = <<-SQL
UPDATE users
SET feed_needs_update = true, subscriptions = $1
WHERE email = $2
SQL
PG_DB.exec(request, user.subscriptions, user.email)
end
def subscribe_channel(user : User, ucid : String) def subscribe_channel(user : User, ucid : String)
request = <<-SQL request = <<-SQL
UPDATE users UPDATE users
@ -95,6 +115,65 @@ module Invidious::Database::Users
PG_DB.exec(request, ucid, user.email) PG_DB.exec(request, ucid, user.email)
end end
# -------------------
# Update (notifs)
# -------------------
def add_notification(video : ChannelVideo)
request = <<-SQL
UPDATE users
SET notifications = array_append(notifications, $1),
feed_needs_update = true
WHERE $2 = ANY(subscriptions)
SQL
PG_DB.exec(request, video.id, video.ucid)
end
def remove_notification(user : User, vid : String)
request = <<-SQL
UPDATE users
SET notifications = array_remove(notifications, $1)
WHERE email = $2
SQL
PG_DB.exec(request, vid, user.email)
end
def clear_notifications(user : User)
request = <<-SQL
UPDATE users
SET notifications = $1, updated = $2
WHERE email = $3
SQL
PG_DB.exec(request, [] of String, Time.utc, user)
end
# -------------------
# Update (misc)
# -------------------
def update_preferences(user : User)
request = <<-SQL
UPDATE users
SET preferences = $1
WHERE email = $2
SQL
PG_DB.exec(request, user.preferences.to_json, user.email)
end
def update_password(user : User, pass : String)
request = <<-SQL
UPDATE users
SET password = $1
WHERE email = $2
SQL
PG_DB.exec(request, user.email, pass)
end
# ------------------- # -------------------
# Select # Select
# ------------------- # -------------------
@ -126,4 +205,14 @@ module Invidious::Database::Users
return PG_DB.query_one?(request, token, as: User) return PG_DB.query_one?(request, token, as: User)
end end
def select_notifications(user : User) : Array(String)
request = <<-SQL
SELECT notifications
FROM users
WHERE email = $1
SQL
return PG_DB.query_one(request, user.email, as: Array(String))
end
end end

View File

@ -22,12 +22,11 @@ module Invidious::Routes::API::V1::Authenticated
user = env.get("user").as(User) user = env.get("user").as(User)
begin begin
preferences = Preferences.from_json(env.request.body || "{}") user.preferences = Preferences.from_json(env.request.body || "{}")
rescue rescue
preferences = user.preferences
end end
PG_DB.exec("UPDATE users SET preferences = $1 WHERE email = $2", preferences.to_json, user.email) Invidious::Database::Users.update_preferences(user)
env.response.status_code = 204 env.response.status_code = 204
end end

View File

@ -137,7 +137,7 @@ module Invidious::Routes::Embed
# end # end
if notifications && notifications.includes? id if notifications && notifications.includes? id
PG_DB.exec("UPDATE users SET notifications = array_remove(notifications, $1) WHERE email = $2", id, user.as(User).email) Invidious::Database::Users.remove_notification(user.as(User), id)
env.get("user").as(User).notifications.delete(id) env.get("user").as(User).notifications.delete(id)
notifications.delete(id) notifications.delete(id)
end end

View File

@ -99,8 +99,7 @@ module Invidious::Routes::Feeds
# we know a user has looked at their feed e.g. in the past 10 minutes, # we know a user has looked at their feed e.g. in the past 10 minutes,
# they've already seen a video posted 20 minutes ago, and don't need # they've already seen a video posted 20 minutes ago, and don't need
# to be notified. # to be notified.
PG_DB.exec("UPDATE users SET notifications = $1, updated = $2 WHERE email = $3", [] of String, Time.utc, Invidious::Database::Users.clear_notifications(user)
user.email)
user.notifications = [] of String user.notifications = [] of String
env.set "user", user env.set "user", user
@ -417,9 +416,7 @@ module Invidious::Routes::Feeds
}) })
was_insert = Invidious::Database::ChannelVideos.insert(video, with_premiere_timestamp: true) was_insert = Invidious::Database::ChannelVideos.insert(video, with_premiere_timestamp: true)
Invidious::Database::Users.add_notification(video) if was_insert
PG_DB.exec("UPDATE users SET notifications = array_append(notifications, $1),
feed_needs_update = true WHERE $2 = ANY(subscriptions)", video.id, video.ucid) if was_insert
end end
end end

View File

@ -303,8 +303,8 @@ module Invidious::Routes::Login
end end
if env.request.cookies["PREFS"]? if env.request.cookies["PREFS"]?
preferences = env.get("preferences").as(Preferences) user.preferences = env.get("preferences").as(Preferences)
PG_DB.exec("UPDATE users SET preferences = $1 WHERE email = $2", preferences.to_json, user.email) Invidious::Database::Users.update_preferences(user)
cookie = env.request.cookies["PREFS"] cookie = env.request.cookies["PREFS"]
cookie.expires = Time.utc(1990, 1, 1) cookie.expires = Time.utc(1990, 1, 1)
@ -470,8 +470,8 @@ module Invidious::Routes::Login
end end
if env.request.cookies["PREFS"]? if env.request.cookies["PREFS"]?
preferences = env.get("preferences").as(Preferences) user.preferences = env.get("preferences").as(Preferences)
PG_DB.exec("UPDATE users SET preferences = $1 WHERE email = $2", preferences.to_json, user.email) Invidious::Database::Users.update_preferences(user)
cookie = env.request.cookies["PREFS"] cookie = env.request.cookies["PREFS"]
cookie.expires = Time.utc(1990, 1, 1) cookie.expires = Time.utc(1990, 1, 1)

View File

@ -170,11 +170,12 @@ module Invidious::Routes::PreferencesRoute
vr_mode: vr_mode, vr_mode: vr_mode,
show_nick: show_nick, show_nick: show_nick,
save_player_pos: save_player_pos, save_player_pos: save_player_pos,
}.to_json).to_json }.to_json)
if user = env.get? "user" if user = env.get? "user"
user = user.as(User) user = user.as(User)
PG_DB.exec("UPDATE users SET preferences = $1 WHERE email = $2", preferences, user.email) user.preferences = preferences
Invidious::Database::Users.update_preferences(user)
if CONFIG.admins.includes? user.email if CONFIG.admins.includes? user.email
CONFIG.default_user_preferences.default_home = env.params.body["admin_default_home"]?.try &.as(String) || CONFIG.default_user_preferences.default_home CONFIG.default_user_preferences.default_home = env.params.body["admin_default_home"]?.try &.as(String) || CONFIG.default_user_preferences.default_home
@ -220,10 +221,10 @@ module Invidious::Routes::PreferencesRoute
end end
if CONFIG.domain if CONFIG.domain
env.response.cookies["PREFS"] = HTTP::Cookie.new(name: "PREFS", domain: "#{CONFIG.domain}", value: URI.encode_www_form(preferences), expires: Time.utc + 2.years, env.response.cookies["PREFS"] = HTTP::Cookie.new(name: "PREFS", domain: "#{CONFIG.domain}", value: URI.encode_www_form(preferences.to_json), expires: Time.utc + 2.years,
secure: secure, http_only: true) secure: secure, http_only: true)
else else
env.response.cookies["PREFS"] = HTTP::Cookie.new(name: "PREFS", value: URI.encode_www_form(preferences), expires: Time.utc + 2.years, env.response.cookies["PREFS"] = HTTP::Cookie.new(name: "PREFS", value: URI.encode_www_form(preferences.to_json), expires: Time.utc + 2.years,
secure: secure, http_only: true) secure: secure, http_only: true)
end end
end end
@ -241,18 +242,15 @@ module Invidious::Routes::PreferencesRoute
if user = env.get? "user" if user = env.get? "user"
user = user.as(User) user = user.as(User)
preferences = user.preferences
case preferences.dark_mode case user.preferences.dark_mode
when "dark" when "dark"
preferences.dark_mode = "light" user.preferences.dark_mode = "light"
else else
preferences.dark_mode = "dark" user.preferences.dark_mode = "dark"
end end
preferences = preferences.to_json Invidious::Database::Users.update_preferences(user)
PG_DB.exec("UPDATE users SET preferences = $1 WHERE email = $2", preferences, user.email)
else else
preferences = env.get("preferences").as(Preferences) preferences = env.get("preferences").as(Preferences)

View File

@ -80,7 +80,7 @@ module Invidious::Routes::Watch
end end
if notifications && notifications.includes? id if notifications && notifications.includes? id
PG_DB.exec("UPDATE users SET notifications = array_remove(notifications, $1) WHERE email = $2", id, user.as(User).email) Invidious::Database::Users.remove_notification(user.as(User), id)
env.get("user").as(User).notifications.delete(id) env.get("user").as(User).notifications.delete(id)
notifications.delete(id) notifications.delete(id)
end end

View File

@ -224,8 +224,7 @@ def get_subscription_feed(db, user, max_results = 40, page = 1)
limit = max_results.clamp(0, MAX_ITEMS_PER_PAGE) limit = max_results.clamp(0, MAX_ITEMS_PER_PAGE)
offset = (page - 1) * limit offset = (page - 1) * limit
notifications = db.query_one("SELECT notifications FROM users WHERE email = $1", user.email, notifications = Invidious::Database::Users.select_notifications(user)
as: Array(String))
view_name = "subscriptions_#{sha256(user.email)}" view_name = "subscriptions_#{sha256(user.email)}"
if user.preferences.notifications_only && !notifications.empty? if user.preferences.notifications_only && !notifications.empty?
@ -296,8 +295,7 @@ def get_subscription_feed(db, user, max_results = 40, page = 1)
else nil # Ignore else nil # Ignore
end end
notifications = PG_DB.query_one("SELECT notifications FROM users WHERE email = $1", user.email, as: Array(String)) notifications = Invidious::Database::Users.select_notifications(user)
notifications = videos.select { |v| notifications.includes? v.id } notifications = videos.select { |v| notifications.includes? v.id }
videos = videos - notifications videos = videos - notifications
end end