Add option to change max_results

This commit is contained in:
Omar Roth 2018-07-16 19:31:49 -05:00
parent cae8e1b883
commit a6292d956d
3 changed files with 33 additions and 20 deletions

View File

@ -683,7 +683,6 @@ get "/preferences" do |env|
referer ||= "/preferences" referer ||= "/preferences"
if referer.size > 64 if referer.size > 64
puts "nope"
referer = "/preferences" referer = "/preferences"
end end
@ -725,13 +724,17 @@ post "/preferences" do |env|
dark_mode ||= "off" dark_mode ||= "off"
dark_mode = dark_mode == "on" dark_mode = dark_mode == "on"
max_results = env.params.body["max_results"]?.try &.as(String).to_i
max_results ||= 40
preferences = { preferences = {
"video_loop" => video_loop, "video_loop" => video_loop,
"autoplay" => autoplay, "autoplay" => autoplay,
"speed" => speed, "speed" => speed,
"quality" => quality, "quality" => quality,
"volume" => volume, "volume" => volume,
"dark_mode" => dark_mode, "dark_mode" => dark_mode,
"max_results" => max_results,
}.to_json }.to_json
PG_DB.exec("UPDATE users SET preferences = $1 WHERE email = $2", preferences, user.email) PG_DB.exec("UPDATE users SET preferences = $1 WHERE email = $2", preferences, user.email)
@ -747,7 +750,9 @@ get "/feed/subscriptions" do |env|
if user if user
user = user.as(User) user = user.as(User)
max_results = env.params.query["maxResults"]?.try &.to_i || 40 max_results = user.preferences.max_results
max_results ||= env.params.query["maxResults"]?.try &.to_i
max_results ||= 40
page = env.params.query["page"]?.try &.to_i page = env.params.query["page"]?.try &.to_i
page ||= 1 page ||= 1

View File

@ -18,12 +18,13 @@ macro rendered(filename)
end end
DEFAULT_USER_PREFERENCES = Preferences.from_json({ DEFAULT_USER_PREFERENCES = Preferences.from_json({
"video_loop" => false, "video_loop" => false,
"autoplay" => false, "autoplay" => false,
"speed" => 1.0, "speed" => 1.0,
"quality" => "hd720", "quality" => "hd720",
"volume" => 100, "volume" => 100,
"dark_mode" => false, "dark_mode" => false,
"max_results" => 40,
}.to_json) }.to_json)
class Config class Config
@ -136,12 +137,13 @@ end
class Preferences class Preferences
JSON.mapping({ JSON.mapping({
video_loop: Bool, video_loop: Bool,
autoplay: Bool, autoplay: Bool,
speed: Float32, speed: Float32,
quality: String, quality: String,
volume: Int32, volume: Int32,
dark_mode: Bool, dark_mode: Bool,
max_results: Int32,
}) })
end end

View File

@ -52,6 +52,12 @@ function update_value(element) {
<input name="dark_mode" id="dark_mode" type="checkbox" <% if user.preferences.dark_mode %>checked<% end %>> <input name="dark_mode" id="dark_mode" type="checkbox" <% if user.preferences.dark_mode %>checked<% end %>>
</div> </div>
<legend>Subscription preferences</legend>
<div class="pure-control-group">
<label for="max_results">Number of videos shown in feed: </label>
<input name="max_results" id="max_results" type="number" value="<%= user.preferences.max_results %>">
</div>
<div class="pure-controls"> <div class="pure-controls">
<button type="submit" class="pure-button pure-button-primary">Save preferences</button> <button type="submit" class="pure-button pure-button-primary">Save preferences</button>
</div> </div>