Update handling for preferences

This commit is contained in:
Omar Roth 2019-03-28 13:43:40 -05:00
parent eee973fe86
commit 388e58bf1e
4 changed files with 63 additions and 90 deletions

View File

@ -160,7 +160,12 @@ proxies = PROXY_LIST
before_all do |env| before_all do |env|
env.response.headers["X-XSS-Protection"] = "1; mode=block;" env.response.headers["X-XSS-Protection"] = "1; mode=block;"
env.response.headers["X-Content-Type-Options"] = "nosniff" env.response.headers["X-Content-Type-Options"] = "nosniff"
preferences = DEFAULT_USER_PREFERENCES.dup
begin
preferences = Preferences.from_json(env.request.cookies["PREFS"]?.try &.value || "{}")
rescue
preferences = Preferences.from_json("{}")
end
if env.request.cookies.has_key? "SID" if env.request.cookies.has_key? "SID"
headers = HTTP::Headers.new headers = HTTP::Headers.new
@ -201,10 +206,6 @@ before_all do |env|
end end
end end
if env.request.cookies.has_key? "PREFS"
preferences = Preferences.from_json(env.request.cookies["PREFS"].value)
end
dark_mode = env.params.query["dark_mode"]? || preferences.dark_mode.to_s dark_mode = env.params.query["dark_mode"]? || preferences.dark_mode.to_s
dark_mode = dark_mode == "true" dark_mode = dark_mode == "true"
@ -1082,12 +1083,6 @@ post "/login" do |env|
next templated "error" next templated "error"
end end
user = PG_DB.query_one?("SELECT * FROM users WHERE LOWER(email) = LOWER($1) AND password IS NOT NULL", email, as: User)
if user
error_message = translate(locale, "Please sign in")
next templated "error"
end
sid = Base64.urlsafe_encode(Random::Secure.random_bytes(32)) sid = Base64.urlsafe_encode(Random::Secure.random_bytes(32))
user, sid = create_user(sid, email, password) user, sid = create_user(sid, email, password)
user_array = user.to_a user_array = user.to_a

View File

@ -9,6 +9,17 @@ macro add_mapping(mapping)
DB.mapping({{mapping}}) DB.mapping({{mapping}})
end end
macro json_mapping(mapping)
def initialize({{*mapping.keys.map { |id| "@#{id}".id }}})
end
def to_a
return [{{*mapping.keys.map { |id| "@#{id}".id }}}]
end
JSON.mapping({{mapping}})
end
macro templated(filename, template = "template") macro templated(filename, template = "template")
render "src/invidious/views/#{{{filename}}}.ecr", "src/invidious/views/#{{{template}}}.ecr" render "src/invidious/views/#{{{filename}}}.ecr", "src/invidious/views/#{{{template}}}.ecr"
end end

View File

@ -6,7 +6,7 @@ class User
begin begin
Preferences.from_json(rs.read(String)) Preferences.from_json(rs.read(String))
rescue ex rescue ex
DEFAULT_USER_PREFERENCES Preferences.from_json("{}")
end end
end end
end end
@ -18,7 +18,6 @@ class User
email: String, email: String,
preferences: { preferences: {
type: Preferences, type: Preferences,
default: DEFAULT_USER_PREFERENCES,
converter: PreferencesConverter, converter: PreferencesConverter,
}, },
password: String?, password: String?,
@ -27,30 +26,30 @@ class User
}) })
end end
DEFAULT_USER_PREFERENCES = Preferences.from_json({ DEFAULT_USER_PREFERENCES = Preferences.new(
"video_loop" => false, video_loop: false,
"autoplay" => false, autoplay: false,
"continue" => false, continue: false,
"local" => false, local: false,
"listen" => false, listen: false,
"speed" => 1.0, speed: 1.0_f32,
"quality" => "hd720", quality: "hd720",
"volume" => 100, volume: 100,
"comments" => ["youtube", ""], comments: ["youtube", ""],
"captions" => ["", "", ""], captions: ["", "", ""],
"related_videos" => true, related_videos: true,
"redirect_feed" => false, redirect_feed: false,
"locale" => "en-US", locale: "en-US",
"dark_mode" => false, dark_mode: false,
"thin_mode" => false, thin_mode: false,
"max_results" => 40, max_results: 40,
"sort" => "published", sort: "published",
"latest_only" => false, latest_only: false,
"unseen_only" => false, unseen_only: false,
"notifications_only" => false, notifications_only: false,
}.to_json) )
class Preferences struct Preferences
module StringToArray module StringToArray
def self.to_json(value : Array(String), json : JSON::Builder) def self.to_json(value : Array(String), json : JSON::Builder)
json.array do json.array do
@ -74,58 +73,27 @@ class Preferences
end end
end end
JSON.mapping({ json_mapping({
video_loop: Bool, video_loop: {type: Bool, default: DEFAULT_USER_PREFERENCES.video_loop},
autoplay: Bool, autoplay: {type: Bool, default: DEFAULT_USER_PREFERENCES.autoplay},
continue: { continue: {type: Bool, default: DEFAULT_USER_PREFERENCES.continue},
type: Bool, local: {type: Bool, default: DEFAULT_USER_PREFERENCES.local},
default: DEFAULT_USER_PREFERENCES.continue, listen: {type: Bool, default: DEFAULT_USER_PREFERENCES.listen},
}, speed: {type: Float32, default: DEFAULT_USER_PREFERENCES.speed},
local: { quality: {type: String, default: DEFAULT_USER_PREFERENCES.quality},
type: Bool, volume: {type: Int32, default: DEFAULT_USER_PREFERENCES.volume},
default: DEFAULT_USER_PREFERENCES.local, comments: {type: Array(String), default: DEFAULT_USER_PREFERENCES.comments, converter: StringToArray},
}, captions: {type: Array(String), default: DEFAULT_USER_PREFERENCES.captions, converter: StringToArray},
listen: { redirect_feed: {type: Bool, default: DEFAULT_USER_PREFERENCES.redirect_feed},
type: Bool, related_videos: {type: Bool, default: DEFAULT_USER_PREFERENCES.related_videos},
default: DEFAULT_USER_PREFERENCES.listen, dark_mode: {type: Bool, default: DEFAULT_USER_PREFERENCES.dark_mode},
}, thin_mode: {type: Bool, default: DEFAULT_USER_PREFERENCES.thin_mode},
speed: Float32, max_results: {type: Int32, default: DEFAULT_USER_PREFERENCES.max_results},
quality: String, sort: {type: String, default: DEFAULT_USER_PREFERENCES.sort},
volume: Int32, latest_only: {type: Bool, default: DEFAULT_USER_PREFERENCES.latest_only},
comments: { unseen_only: {type: Bool, default: DEFAULT_USER_PREFERENCES.unseen_only},
type: Array(String), notifications_only: {type: Bool, default: DEFAULT_USER_PREFERENCES.notifications_only},
default: DEFAULT_USER_PREFERENCES.comments, locale: {type: String, default: DEFAULT_USER_PREFERENCES.locale},
converter: StringToArray,
},
captions: {
type: Array(String),
default: DEFAULT_USER_PREFERENCES.captions,
},
redirect_feed: {
type: Bool,
default: DEFAULT_USER_PREFERENCES.redirect_feed,
},
related_videos: {
type: Bool,
default: DEFAULT_USER_PREFERENCES.related_videos,
},
dark_mode: Bool,
thin_mode: {
type: Bool,
default: DEFAULT_USER_PREFERENCES.thin_mode,
},
max_results: Int32,
sort: String,
latest_only: Bool,
unseen_only: Bool,
notifications_only: {
type: Bool,
default: DEFAULT_USER_PREFERENCES.notifications_only,
},
locale: {
type: String,
default: DEFAULT_USER_PREFERENCES.locale,
},
}) })
end end

View File

@ -46,8 +46,7 @@
<% if env.get? "user" %> <% if env.get? "user" %>
<div class="pure-u-1-4"> <div class="pure-u-1-4">
<a href="/toggle_theme?referer=<%= env.get?("current_page") %>" class="pure-menu-heading"> <a href="/toggle_theme?referer=<%= env.get?("current_page") %>" class="pure-menu-heading">
<% preferences = env.get("user").as(User).preferences %> <% if env.get("preferences").as(Preferences).dark_mode %>
<% if preferences.dark_mode %>
<i class="icon ion-ios-sunny"></i> <i class="icon ion-ios-sunny"></i>
<% else %> <% else %>
<i class="icon ion-ios-moon"></i> <i class="icon ion-ios-moon"></i>