Set max preference size

This commit is contained in:
Omar Roth 2019-06-08 16:04:55 -05:00
parent b43e9ed7e7
commit d1635cf24e
No known key found for this signature in database
GPG Key ID: B8254FB7EC3D37F2
7 changed files with 82 additions and 62 deletions

View File

@ -52,7 +52,7 @@ TEST_IDS = {"AgbeGFYluEA", "BaW_jenozKc", "a9LDPn-MO4I", "ddFvjfvPnqk"
CURRENT_BRANCH = {{ "#{`git branch | sed -n '/\* /s///p'`.strip}" }} CURRENT_BRANCH = {{ "#{`git branch | sed -n '/\* /s///p'`.strip}" }}
CURRENT_COMMIT = {{ "#{`git rev-list HEAD --max-count=1 --abbrev-commit`.strip}" }} CURRENT_COMMIT = {{ "#{`git rev-list HEAD --max-count=1 --abbrev-commit`.strip}" }}
CURRENT_VERSION = {{ "#{`git describe --tags --abbrev=0`.strip}" }} CURRENT_VERSION = {{ "#{`git describe --tags --abbrev=0`.strip}" }}
MAX_ITEMS_PER_PAGE = 1000 MAX_ITEMS_PER_PAGE = 1500
# This is used to determine the `?v=` on the end of file URLs (for cache busting). We # This is used to determine the `?v=` on the end of file URLs (for cache busting). We
# only need to expire modified assets, so we can use this to find the last commit that changes # only need to expire modified assets, so we can use this to find the last commit that changes
@ -1346,7 +1346,7 @@ post "/preferences" do |env|
local ||= "off" local ||= "off"
local = local == "on" local = local == "on"
speed = env.params.body["speed"]?.try &.as(String).to_f? speed = env.params.body["speed"]?.try &.as(String).to_f32?
speed ||= CONFIG.default_user_preferences.speed speed ||= CONFIG.default_user_preferences.speed
quality = env.params.body["quality"]?.try &.as(String) quality = env.params.body["quality"]?.try &.as(String)
@ -1402,31 +1402,31 @@ post "/preferences" do |env|
notifications_only ||= "off" notifications_only ||= "off"
notifications_only = notifications_only == "on" notifications_only = notifications_only == "on"
preferences = { preferences = Preferences.from_json({
"video_loop" => video_loop, annotations: annotations,
"annotations" => annotations, annotations_subscribed: annotations_subscribed,
"annotations_subscribed" => annotations_subscribed, autoplay: autoplay,
"autoplay" => autoplay, captions: captions,
"continue" => continue, comments: comments,
"continue_autoplay" => continue_autoplay, continue: continue,
"listen" => listen, continue_autoplay: continue_autoplay,
"local" => local, dark_mode: dark_mode,
"speed" => speed, latest_only: latest_only,
"quality" => quality, listen: listen,
"volume" => volume, local: local,
"comments" => comments, locale: locale,
"captions" => captions, max_results: max_results,
"related_videos" => related_videos, notifications_only: notifications_only,
"redirect_feed" => redirect_feed, quality: quality,
"locale" => locale, redirect_feed: redirect_feed,
"dark_mode" => dark_mode, related_videos: related_videos,
"thin_mode" => thin_mode, sort: sort,
"max_results" => max_results, speed: speed,
"sort" => sort, thin_mode: thin_mode,
"latest_only" => latest_only, unseen_only: unseen_only,
"unseen_only" => unseen_only, video_loop: video_loop,
"notifications_only" => notifications_only, volume: volume,
}.to_json }.to_json).to_json
if user = env.get? "user" if user = env.get? "user"
user = user.as(User) user = user.as(User)
@ -2388,9 +2388,9 @@ get "/feed/subscriptions" do |env|
user, sid = get_user(sid, headers, PG_DB) user, sid = get_user(sid, headers, PG_DB)
end end
max_results = user.preferences.max_results max_results = env.params.query["max_results"]?.try &.to_i?.try &.clamp(0, MAX_ITEMS_PER_PAGE)
max_results ||= env.params.query["max_results"]?.try &.to_i? max_results ||= user.preferences.max_results
max_results ||= 40 max_results ||= CONFIG.default_user_preferences.max_results
page = env.params.query["page"]?.try &.to_i? page = env.params.query["page"]?.try &.to_i?
page ||= 1 page ||= 1
@ -2424,12 +2424,14 @@ get "/feed/history" do |env|
user = user.as(User) user = user.as(User)
limit = user.preferences.max_results.clamp(0, MAX_ITEMS_PER_PAGE) max_results = env.params.query["max_results"]?.try &.to_i?.try &.clamp(0, MAX_ITEMS_PER_PAGE)
if user.watched[(page - 1) * limit]? max_results ||= user.preferences.max_results
watched = user.watched.reverse[(page - 1) * limit, limit] max_results ||= CONFIG.default_user_preferences.max_results
else
watched = [] of String if user.watched[(page - 1) * max_results]?
watched = user.watched.reverse[(page - 1) * max_results, max_results]
end end
watched ||= [] of String
templated "history" templated "history"
end end
@ -2525,9 +2527,9 @@ get "/feed/private" do |env|
next next
end end
max_results = user.preferences.max_results max_results = env.params.query["max_results"]?.try &.to_i?.try &.clamp(0, MAX_ITEMS_PER_PAGE)
max_results = env.params.query["max_results"]?.try &.to_i? max_results ||= user.preferences.max_results
max_results ||= 40 max_results ||= CONFIG.default_user_preferences.max_results
page = env.params.query["page"]?.try &.to_i? page = env.params.query["page"]?.try &.to_i?
page ||= 1 page ||= 1
@ -4030,9 +4032,9 @@ get "/api/v1/auth/feed" do |env|
user = env.get("user").as(User) user = env.get("user").as(User)
locale = LOCALES[env.get("preferences").as(Preferences).locale]? locale = LOCALES[env.get("preferences").as(Preferences).locale]?
max_results = user.preferences.max_results max_results = env.params.query["max_results"]?.try &.to_i?
max_results ||= env.params.query["max_results"]?.try &.to_i? max_results ||= user.preferences.max_results
max_results ||= 40 max_results ||= CONFIG.default_user_preferences.max_results
page = env.params.query["page"]?.try &.to_i? page = env.params.query["page"]?.try &.to_i?
page ||= 1 page ||= 1

View File

@ -41,10 +41,10 @@ struct Preferences
begin begin
result = [] of String result = [] of String
value.read_array do value.read_array do
result << HTML.escape(value.read_string) result << HTML.escape(value.read_string[0, 100])
end end
rescue ex rescue ex
result = [HTML.escape(value.read_string), ""] result = [HTML.escape(value.read_string[0, 100]), ""]
end end
result result
@ -70,11 +70,11 @@ struct Preferences
node.raise "Expected scalar, not #{item.class}" node.raise "Expected scalar, not #{item.class}"
end end
result << HTML.escape(item.value) result << HTML.escape(item.value[0, 100])
end end
rescue ex rescue ex
if node.is_a?(YAML::Nodes::Scalar) if node.is_a?(YAML::Nodes::Scalar)
result = [HTML.escape(node.value), ""] result = [HTML.escape(node.value[0, 100]), ""]
else else
result = ["", ""] result = ["", ""]
end end
@ -84,13 +84,13 @@ struct Preferences
end end
end end
module EscapeString module ProcessString
def self.to_json(value : String, json : JSON::Builder) def self.to_json(value : String, json : JSON::Builder)
json.string value json.string value
end end
def self.from_json(value : JSON::PullParser) : String def self.from_json(value : JSON::PullParser) : String
HTML.escape(value.read_string) HTML.escape(value.read_string[0, 100])
end end
def self.to_yaml(value : String, yaml : YAML::Nodes::Builder) def self.to_yaml(value : String, yaml : YAML::Nodes::Builder)
@ -98,7 +98,25 @@ struct Preferences
end end
def self.from_yaml(ctx : YAML::ParseContext, node : YAML::Nodes::Node) : String def self.from_yaml(ctx : YAML::ParseContext, node : YAML::Nodes::Node) : String
HTML.escape(node.value) HTML.escape(node.value[0, 100])
end
end
module ClampInt
def self.to_json(value : Int32, json : JSON::Builder)
json.number value
end
def self.from_json(value : JSON::PullParser) : Int32
value.read_int.clamp(0, MAX_ITEMS_PER_PAGE).to_i32
end
def self.to_yaml(value : Int32, yaml : YAML::Nodes::Builder)
yaml.scalar value
end
def self.from_yaml(ctx : YAML::ParseContext, node : YAML::Nodes::Node) : Int32
node.value.clamp(0, MAX_ITEMS_PER_PAGE)
end end
end end
@ -114,13 +132,13 @@ struct Preferences
latest_only: {type: Bool, default: CONFIG.default_user_preferences.latest_only}, latest_only: {type: Bool, default: CONFIG.default_user_preferences.latest_only},
listen: {type: Bool, default: CONFIG.default_user_preferences.listen}, listen: {type: Bool, default: CONFIG.default_user_preferences.listen},
local: {type: Bool, default: CONFIG.default_user_preferences.local}, local: {type: Bool, default: CONFIG.default_user_preferences.local},
locale: {type: String, default: CONFIG.default_user_preferences.locale, converter: EscapeString}, locale: {type: String, default: CONFIG.default_user_preferences.locale, converter: ProcessString},
max_results: {type: Int32, default: CONFIG.default_user_preferences.max_results}, max_results: {type: Int32, default: CONFIG.default_user_preferences.max_results, converter: ClampInt},
notifications_only: {type: Bool, default: CONFIG.default_user_preferences.notifications_only}, notifications_only: {type: Bool, default: CONFIG.default_user_preferences.notifications_only},
quality: {type: String, default: CONFIG.default_user_preferences.quality, converter: EscapeString}, quality: {type: String, default: CONFIG.default_user_preferences.quality, converter: ProcessString},
redirect_feed: {type: Bool, default: CONFIG.default_user_preferences.redirect_feed}, redirect_feed: {type: Bool, default: CONFIG.default_user_preferences.redirect_feed},
related_videos: {type: Bool, default: CONFIG.default_user_preferences.related_videos}, related_videos: {type: Bool, default: CONFIG.default_user_preferences.related_videos},
sort: {type: String, default: CONFIG.default_user_preferences.sort, converter: EscapeString}, sort: {type: String, default: CONFIG.default_user_preferences.sort, converter: ProcessString},
speed: {type: Float32, default: CONFIG.default_user_preferences.speed}, speed: {type: Float32, default: CONFIG.default_user_preferences.speed},
thin_mode: {type: Bool, default: CONFIG.default_user_preferences.thin_mode}, thin_mode: {type: Bool, default: CONFIG.default_user_preferences.thin_mode},
unseen_only: {type: Bool, default: CONFIG.default_user_preferences.unseen_only}, unseen_only: {type: Bool, default: CONFIG.default_user_preferences.unseen_only},

View File

@ -67,7 +67,7 @@
<div class="pure-g h-box"> <div class="pure-g h-box">
<div class="pure-u-1 pure-u-lg-1-5"> <div class="pure-u-1 pure-u-lg-1-5">
<% if page >= 2 %> <% if page > 1 %>
<a href="/channel/<%= ucid %>?page=<%= page - 1 %><% if sort_by != "newest" %>&sort_by=<%= sort_by %><% end %>"> <a href="/channel/<%= ucid %>?page=<%= page - 1 %><% if sort_by != "newest" %>&sort_by=<%= sort_by %><% end %>">
<%= translate(locale, "Previous page") %> <%= translate(locale, "Previous page") %>
</a> </a>

View File

@ -56,16 +56,16 @@ var watched_data = {
<div class="pure-g h-box"> <div class="pure-g h-box">
<div class="pure-u-1 pure-u-lg-1-5"> <div class="pure-u-1 pure-u-lg-1-5">
<% if page >= 2 %> <% if page > 1 %>
<a href="/feed/history?page=<%= page - 1 %>"> <a href="/feed/history?page=<%= page - 1 %><% if env.params.query["max_results"]? %>&max_results=<%= max_results %><% end %>">
<%= translate(locale, "Previous page") %> <%= translate(locale, "Previous page") %>
</a> </a>
<% end %> <% end %>
</div> </div>
<div class="pure-u-1 pure-u-lg-3-5"></div> <div class="pure-u-1 pure-u-lg-3-5"></div>
<div class="pure-u-1 pure-u-lg-1-5" style="text-align:right"> <div class="pure-u-1 pure-u-lg-1-5" style="text-align:right">
<% if watched.size >= limit %> <% if watched.size >= max_results %>
<a href="/feed/history?page=<%= page + 1 %>"> <a href="/feed/history?page=<%= page + 1 %><% if env.params.query["max_results"]? %>&max_results=<%= max_results %><% end %>">
<%= translate(locale, "Next page") %> <%= translate(locale, "Next page") %>
</a> </a>
<% end %> <% end %>

View File

@ -46,7 +46,7 @@
<div class="pure-g h-box"> <div class="pure-g h-box">
<div class="pure-u-1 pure-u-lg-1-5"> <div class="pure-u-1 pure-u-lg-1-5">
<% if page >= 2 %> <% if page > 1 %>
<a href="/playlist?list=<%= playlist.id %>&page=<%= page - 1 %>"> <a href="/playlist?list=<%= playlist.id %>&page=<%= page - 1 %>">
<%= translate(locale, "Previous page") %> <%= translate(locale, "Previous page") %>
</a> </a>

View File

@ -12,7 +12,7 @@
<div class="pure-g h-box"> <div class="pure-g h-box">
<div class="pure-u-1 pure-u-lg-1-5"> <div class="pure-u-1 pure-u-lg-1-5">
<% if page >= 2 %> <% if page > 1 %>
<a href="/search?q=<%= HTML.escape(query.not_nil!) %>&page=<%= page - 1 %>"> <a href="/search?q=<%= HTML.escape(query.not_nil!) %>&page=<%= page - 1 %>">
<%= translate(locale, "Previous page") %> <%= translate(locale, "Previous page") %>
</a> </a>

View File

@ -62,8 +62,8 @@ var watched_data = {
<div class="pure-g h-box"> <div class="pure-g h-box">
<div class="pure-u-1 pure-u-lg-1-5"> <div class="pure-u-1 pure-u-lg-1-5">
<% if page >= 2 %> <% if page > 1 %>
<a href="/feed/subscriptions?max_results=<%= max_results %>&page=<%= page - 1 %>"> <a href="/feed/subscriptions?page=<%= page - 1 %><% if env.params.query["max_results"]? %>&max_results=<%= max_results %><% end %>">
<%= translate(locale, "Previous page") %> <%= translate(locale, "Previous page") %>
</a> </a>
<% end %> <% end %>
@ -71,7 +71,7 @@ var watched_data = {
<div class="pure-u-1 pure-u-lg-3-5"></div> <div class="pure-u-1 pure-u-lg-3-5"></div>
<div class="pure-u-1 pure-u-lg-1-5" style="text-align:right"> <div class="pure-u-1 pure-u-lg-1-5" style="text-align:right">
<% if (videos.size + notifications.size) == max_results %> <% if (videos.size + notifications.size) == max_results %>
<a href="/feed/subscriptions?max_results=<%= max_results %>&page=<%= page + 1 %>"> <a href="/feed/subscriptions?page=<%= page + 1 %><% if env.params.query["max_results"]? %>&max_results=<%= max_results %><% end %>">
<%= translate(locale, "Next page") %> <%= translate(locale, "Next page") %>
</a> </a>
<% end %> <% end %>