diff --git a/assets/css/darktheme.css b/assets/css/darktheme.css index bd043d9e..8ecf2ea3 100644 --- a/assets/css/darktheme.css +++ b/assets/css/darktheme.css @@ -21,6 +21,10 @@ body { color: #f0f0f0; } -.pure-form > fieldset > input { +.pure-form > fieldset > input, .pure-control-group > input { + color: #101010; +} + +.pure-form > fieldset > select, .pure-control-group > select { color: #101010; } diff --git a/config/sql/users.sql b/config/sql/users.sql index 0142ab40..5340d52a 100644 --- a/config/sql/users.sql +++ b/config/sql/users.sql @@ -9,6 +9,7 @@ CREATE TABLE public.users notifications text[] COLLATE pg_catalog."default", subscriptions text[] COLLATE pg_catalog."default", email text COLLATE pg_catalog."default" NOT NULL, + preferences text COLLAGE pg_catalog."default", CONSTRAINT users_email_key UNIQUE (email), CONSTRAINT users_id_key UNIQUE (id) ) diff --git a/src/invidious.cr b/src/invidious.cr index ded7e6b9..7254d9f5 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -214,24 +214,19 @@ spawn do end before_all do |env| - if env.request.cookies.has_key?("SID") - env.set "authorized", true + if env.request.cookies.has_key? "SID" + headers = HTTP::Headers.new + headers["Cookie"] = env.request.headers["Cookie"] 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 + begin + client = make_client(YT_URL) + user = get_user(sid, client, headers, PG_DB, false) - notifications = PG_DB.query_one?("SELECT cardinality(notifications) FROM users WHERE id = $1", sid, as: Int32) - notifications ||= 0 - - env.set "notifications", notifications - end - - if env.request.cookies.has_key?("darktheme") && env.request.cookies["darktheme"].value == "true" - env.set "darktheme", true + env.set "user", user + rescue ex + end end end @@ -240,9 +235,11 @@ get "/" do |env| end get "/watch" do |env| - authorized = env.get? "authorized" - if authorized - subscriptions = env.get("subscriptions").as(Array(String)) + user = env.get? "user" + if user + user = user.as(User) + preferences = user.preferences + subscriptions = user.subscriptions.as(Array(String)) end subscriptions ||= [] of String @@ -679,11 +676,77 @@ get "/signout" do |env| env.redirect referer end +get "/preferences" do |env| + user = env.get? "user" + + referer = env.request.headers["referer"]? + referer ||= "/preferences" + + if referer.size > 64 + puts "nope" + referer = "/preferences" + end + + if user + user = user.as(User) + templated "preferences" + else + env.redirect referer + end +end + +post "/preferences" do |env| + user = env.get? "user" + + referer = env.params.query["referer"]? + referer ||= "/preferences" + + if user + user = user.as(User) + + video_loop = env.params.body["video_loop"]?.try &.as(String) + video_loop ||= "off" + video_loop = video_loop == "on" + + autoplay = env.params.body["autoplay"]?.try &.as(String) + autoplay ||= "off" + autoplay = autoplay == "on" + + speed = env.params.body["speed"]?.try &.as(String).to_f + speed ||= 1.0 + + quality = env.params.body["quality"]?.try &.as(String) + quality ||= "hd720" + + volume = env.params.body["volume"]?.try &.as(String).to_i + volume ||= 100 + + dark_mode = env.params.body["dark_mode"]?.try &.as(String) + dark_mode ||= "off" + dark_mode = dark_mode == "on" + + preferences = { + "video_loop" => video_loop, + "autoplay" => autoplay, + "speed" => speed, + "quality" => quality, + "volume" => volume, + "dark_mode" => dark_mode, + }.to_json + + PG_DB.exec("UPDATE users SET preferences = $1 WHERE email = $2", preferences, user.email) + end + + env.redirect referer +end + # Get subscriptions for authorized user get "/feed/subscriptions" do |env| - authorized = env.get? "authorized" + user = env.get? "user" + + if user + user = user.as(User) - if authorized max_results = env.params.query["maxResults"]?.try &.to_i || 40 page = env.params.query["page"]?.try &.to_i @@ -697,14 +760,6 @@ get "/feed/subscriptions" do |env| offset = (page - 1) * max_results end - headers = HTTP::Headers.new - headers["Cookie"] = env.request.headers["Cookie"] - - sid = env.get("sid").as(String) - - client = make_client(YT_URL) - user = get_user(sid, client, headers, PG_DB) - args = arg_array(user.subscriptions, 3) videos = PG_DB.query_all("SELECT * FROM channel_videos WHERE ucid IN (#{args}) \ ORDER BY published DESC LIMIT $1 OFFSET $2", [limit, offset] + user.subscriptions, as: ChannelVideo) @@ -718,7 +773,7 @@ get "/feed/subscriptions" do |env| videos = videos[0..max_results] end - PG_DB.exec("UPDATE users SET notifications = $1 WHERE id = $2", [] of String, sid) + PG_DB.exec("UPDATE users SET notifications = $1 WHERE id = $2", [] of String, user.id) env.set "notifications", 0 templated "subscriptions" @@ -735,12 +790,14 @@ end # /modify_notifications?receive_all_updates=false&receive_no_updates=false # will "unding" all subscriptions. get "/modify_notifications" do |env| - authorized = env.get? "authorized" + user = env.get? "user" referer = env.request.headers["referer"]? referer ||= "/" - if authorized + if user + user = user.as(User) + channel_req = {} of String => String channel_req["receive_all_updates"] = env.params.query["receive_all_updates"]? || "true" @@ -779,12 +836,14 @@ get "/modify_notifications" do |env| end get "/subscription_manager" do |env| - authorized = env.get? "authorized" - if !authorized + user = env.get? "user" + + if !user next env.redirect "/" end - subscriptions = env.get?("subscriptions").as(Array(String)) + user = user.as(User) + subscriptions = user.subscriptions subscriptions ||= [] of String client = make_client(YT_URL) @@ -797,11 +856,13 @@ get "/subscription_manager" do |env| end get "/subscription_ajax" do |env| - authorized = env.get? "authorized" + user = env.get? "user" referer = env.request.headers["referer"]? referer ||= "/" - if authorized + if user + user = user.as(User) + if env.params.query["action_create_subscription_to_channel"]? action = "action_create_subscription_to_channel" elsif env.params.query["action_remove_subscriptions"]? @@ -836,7 +897,7 @@ get "/subscription_ajax" do |env| # Update user if client.post(post_url, headers, post_req).status_code == 200 - sid = env.get("sid").as(String) + sid = user.id case action when .starts_with? "action_create" @@ -856,11 +917,10 @@ get "/user/:user" do |env| end get "/channel/:ucid" do |env| - 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)) + user = env.get? "user" + if user + user = user.as(User) + subscriptions = user.subscriptions end subscriptions ||= [] of String @@ -898,20 +958,6 @@ get "/channel/:ucid" do |env| templated "channel" end -get "/modify_theme" do |env| - referer = env.request.headers["referer"]? - referer ||= "/" - - if env.params.query["dark"]? - env.response.cookies["darktheme"] = "true" - elsif env.params.query["light"]? - env.request.cookies["darktheme"].expires = Time.new(1990, 1, 1) - env.request.cookies.add_response_headers(env.response.headers) - end - - env.redirect referer -end - get "/redirect" do |env| if env.params.query["q"]? env.redirect env.params.query["q"] @@ -1167,6 +1213,6 @@ end public_folder "assets" add_handler FilteredCompressHandler.new -add_context_storage_type(Array(String)) +add_context_storage_type(User) Kemal.run diff --git a/src/invidious/helpers.cr b/src/invidious/helpers.cr index 3129471b..81c025a3 100644 --- a/src/invidious/helpers.cr +++ b/src/invidious/helpers.cr @@ -17,6 +17,15 @@ macro rendered(filename) render "src/invidious/views/#{{{filename}}}.ecr" end +DEFAULT_USER_PREFERENCES = Preferences.from_json({ + "video_loop" => false, + "autoplay" => false, + "speed" => 1.0, + "quality" => "hd720", + "volume" => 100, + "dark_mode" => false, +}.to_json) + class Config YAML.mapping({ crawl_threads: Int32, @@ -86,12 +95,6 @@ class Video end class InvidiousChannel - module XMLConverter - def self.from_rs(rs) - XML.parse_html(rs.read(String)) - end - end - add_mapping({ id: String, author: String, @@ -111,12 +114,34 @@ class ChannelVideo end class User + module PreferencesConverter + def self.from_rs(rs) + Preferences.from_json(rs.read(String)) + end + end + add_mapping({ id: String, updated: Time, notifications: Array(String), subscriptions: Array(String), email: String, + preferences: { + type: Preferences, + default: DEFAULT_USER_PREFERENCES, + converter: PreferencesConverter, + }, + }) +end + +class Preferences + JSON.mapping({ + video_loop: Bool, + autoplay: Bool, + speed: Float32, + quality: String, + volume: Int32, + dark_mode: Bool, }) end @@ -674,11 +699,11 @@ def fetch_channel(ucid, client, db, pull_all_videos = true) return channel end -def get_user(sid, client, headers, db) +def get_user(sid, client, headers, db, refresh = true) if db.query_one?("SELECT EXISTS (SELECT true FROM users WHERE id = $1)", sid, as: Bool) user = db.query_one("SELECT * FROM users WHERE id = $1", sid, as: User) - if Time.now - user.updated > 1.minute + if refresh && Time.now - user.updated > 1.minute user = fetch_user(sid, client, headers, db) user_array = user.to_a args = arg_array(user_array) @@ -723,7 +748,7 @@ def fetch_user(sid, client, headers, db) email = "" end - user = User.new(sid, Time.now, [] of String, channels, email) + user = User.new(sid, Time.now, [] of String, channels, email, DEFAULT_USER_PREFERENCES) return user end diff --git a/src/invidious/views/channel.ecr b/src/invidious/views/channel.ecr index e67dbfda..084ad10d 100644 --- a/src/invidious/views/channel.ecr +++ b/src/invidious/views/channel.ecr @@ -3,7 +3,7 @@ <% end %>
diff --git a/src/invidious/views/layout.ecr b/src/invidious/views/layout.ecr
index 11ea7b48..89764f18 100644
--- a/src/invidious/views/layout.ecr
+++ b/src/invidious/views/layout.ecr
@@ -8,7 +8,7 @@
- <% if env.get? "darktheme" %>
+ <% if env.get?("user") && env.get("user").as(User).preferences.dark_mode %>
<% else %>
@@ -21,46 +21,46 @@