From 1ada71e5ba01c797f4a8aa2993e5e9fbd9ab5b00 Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Thu, 5 Jul 2018 18:43:26 -0500 Subject: [PATCH] Move user state into before_all --- src/invidious.cr | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/invidious.cr b/src/invidious.cr index fe747f79..71051b33 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -221,8 +221,13 @@ before_all do |env| sid = env.request.cookies["SID"].value env.set "sid", sid + subscriptions = PG_DB.query_one?("SELECT subscriptions FROM users WHERE id = $1", sid, as: Array(String)) + subscriptions ||= [] of String + env.set "subscriptions", subscriptions + notifications = PG_DB.query_one?("SELECT cardinality(notifications) FROM users WHERE id = $1", sid, as: Int32) notifications ||= 0 + env.set "notifications", notifications end @@ -236,6 +241,12 @@ get "/" do |env| end get "/watch" do |env| + authorized = env.get? "authorized" + if authorized + subscriptions = env.get("subscriptions").as(Array(String)) + end + subscriptions ||= [] of String + if env.params.query["v"]? id = env.params.query["v"] else @@ -262,14 +273,6 @@ get "/watch" do |env| end listen ||= false - authorized = env.get? "authorized" - if authorized - sid = env.get("sid").as(String) - - subscriptions = PG_DB.query_one?("SELECT subscriptions FROM users WHERE id = $1", sid, as: Array(String)) - end - subscriptions ||= [] of String - client = make_client(YT_URL) begin video = get_video(id, client, PG_DB) @@ -876,7 +879,6 @@ get "/videoplayback*" do |env| if path != "/videoplayback" path = path.lchop("/videoplayback/") path = path.split("/") - # puts path raw_params = {} of String => Array(String) path.each_slice(2) do |pair| @@ -1030,5 +1032,6 @@ end public_folder "assets" add_handler FilteredCompressHandler.new +add_context_storage_type(Array(String)) Kemal.run