From d0b30ad977123a0afc83628782b339cfea28005b Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Sat, 15 Dec 2018 12:05:52 -0600 Subject: [PATCH] Don't pass HTTP client in args --- src/invidious.cr | 34 +++++++++++----------------------- src/invidious/channels.cr | 4 +++- src/invidious/users.cr | 11 ++++++----- 3 files changed, 20 insertions(+), 29 deletions(-) diff --git a/src/invidious.cr b/src/invidious.cr index 8c57e419..e8d07fd4 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -152,8 +152,7 @@ before_all do |env| end else begin - client = make_client(YT_URL) - user = get_user(sid, client, headers, PG_DB, false) + user = get_user(sid, headers, PG_DB, false) challenge, token = create_response(user.email, "sign_out", HMAC_KEY, PG_DB, 1.week) env.set "challenge", challenge @@ -825,8 +824,7 @@ post "/login" do |env| sid = login.cookies["SID"].value - client = make_client(YT_URL) - user = get_user(sid, client, headers, PG_DB) + user = get_user(sid, headers, PG_DB) # We are now logged in @@ -1286,8 +1284,7 @@ get "/subscription_manager" do |env| headers = HTTP::Headers.new headers["Cookie"] = env.request.headers["Cookie"] - client = make_client(YT_URL) - user = get_user(user.id[0], client, headers, PG_DB) + user = get_user(user.id[0], headers, PG_DB) end action_takeout = env.params.query["action_takeout"]?.try &.to_i? @@ -1297,12 +1294,10 @@ get "/subscription_manager" do |env| format = env.params.query["format"]? format ||= "rss" - client = make_client(YT_URL) - subscriptions = [] of InvidiousChannel user.subscriptions.each do |ucid| begin - subscriptions << get_channel(ucid, client, PG_DB, false) + subscriptions << get_channel(ucid, PG_DB, false) rescue ex next end @@ -1391,8 +1386,7 @@ post "/data_control" do |env| user.subscriptions.select! do |ucid| begin - client = make_client(YT_URL) - get_channel(ucid, client, PG_DB, false, false) + get_channel(ucid, PG_DB, false, false) true rescue ex false @@ -1421,8 +1415,7 @@ post "/data_control" do |env| user.subscriptions.select! do |ucid| begin - client = make_client(YT_URL) - get_channel(ucid, client, PG_DB, false, false) + get_channel(ucid, PG_DB, false, false) true rescue ex false @@ -1438,8 +1431,7 @@ post "/data_control" do |env| user.subscriptions.select! do |ucid| begin - client = make_client(YT_URL) - get_channel(ucid, client, PG_DB, false, false) + get_channel(ucid, PG_DB, false, false) true rescue ex false @@ -1456,8 +1448,7 @@ post "/data_control" do |env| user.subscriptions.each do |ucid| begin - client = make_client(YT_URL) - get_channel(ucid, client, PG_DB, false, false) + get_channel(ucid, PG_DB, false, false) rescue ex next end @@ -1482,8 +1473,7 @@ post "/data_control" do |env| user.subscriptions.select! do |ucid| begin - client = make_client(YT_URL) - get_channel(ucid, client, PG_DB, false, false) + get_channel(ucid, PG_DB, false, false) true rescue ex false @@ -1567,8 +1557,7 @@ get "/subscription_ajax" do |env| if !user.subscriptions.includes? channel_id PG_DB.exec("UPDATE users SET subscriptions = array_append(subscriptions,$1) WHERE email = $2", channel_id, email) - client = make_client(YT_URL) - get_channel(channel_id, client, PG_DB, false, false) + get_channel(channel_id, PG_DB, false, false) end when .starts_with? "action_remove" PG_DB.exec("UPDATE users SET subscriptions = array_remove(subscriptions,$1) WHERE email = $2", channel_id, email) @@ -1708,8 +1697,7 @@ get "/feed/subscriptions" do |env| headers["Cookie"] = env.request.headers["Cookie"] if !user.password - client = make_client(YT_URL) - user = get_user(user.id[0], client, headers, PG_DB) + user = get_user(user.id[0], headers, PG_DB) end max_results = preferences.max_results diff --git a/src/invidious/channels.cr b/src/invidious/channels.cr index d5003509..cfe389f1 100644 --- a/src/invidious/channels.cr +++ b/src/invidious/channels.cr @@ -21,7 +21,9 @@ class ChannelVideo }) end -def get_channel(id, client, db, refresh = true, pull_all_videos = true) +def get_channel(id, db, refresh = true, pull_all_videos = true) + client = make_client(YT_URL) + if db.query_one?("SELECT EXISTS (SELECT true FROM channels WHERE id = $1)", id, as: Bool) channel = db.query_one("SELECT * FROM channels WHERE id = $1", id, as: InvidiousChannel) diff --git a/src/invidious/users.cr b/src/invidious/users.cr index 7caefb7c..ccd36db5 100644 --- a/src/invidious/users.cr +++ b/src/invidious/users.cr @@ -116,12 +116,12 @@ class Preferences }) end -def get_user(sid, client, headers, db, refresh = true) +def get_user(sid, headers, db, refresh = true) if db.query_one?("SELECT EXISTS (SELECT true FROM users WHERE $1 = ANY(id))", sid, as: Bool) user = db.query_one("SELECT * FROM users WHERE $1 = ANY(id)", sid, as: User) if refresh && Time.now - user.updated > 1.minute - user = fetch_user(sid, client, headers, db) + user = fetch_user(sid, headers, db) user_array = user.to_a user_array[5] = user_array[5].to_json @@ -140,7 +140,7 @@ def get_user(sid, client, headers, db, refresh = true) end end else - user = fetch_user(sid, client, headers, db) + user = fetch_user(sid, headers, db) user_array = user.to_a user_array[5] = user_array[5].to_json @@ -162,7 +162,8 @@ def get_user(sid, client, headers, db, refresh = true) return user end -def fetch_user(sid, client, headers, db) +def fetch_user(sid, headers, db) + client = make_client(YT_URL) feed = client.get("/subscription_manager?disable_polymer=1", headers) feed = XML.parse_html(feed.body) @@ -172,7 +173,7 @@ def fetch_user(sid, client, headers, db) channel_id = channel["href"].lstrip("/channel/") begin - channel = get_channel(channel_id, client, db, false, false) + channel = get_channel(channel_id, db, false, false) channels << channel.id rescue ex next