Optionally include reading progress in response

This commit is contained in:
Alex Ling
2022-03-22 12:58:37 +00:00
parent 61e85dd49f
commit 21d8d0e8a7
3 changed files with 57 additions and 24 deletions
+17 -8
View File
@@ -139,21 +139,30 @@ class Library
titles.flat_map &.deep_entries
end
def build_json(*, slim = false, depth = -1, sort_context = nil)
def build_json(*, slim = false, depth = -1, sort_context = nil, percentage = false)
_titles = if sort_context
sorted_titles sort_context[:username],
sort_context[:opt]
else
self.titles
end
JSON.build do |json|
json.object do
json.field "dir", @dir
json.field "titles" do
json.array do
_titles = if sort_context
sorted_titles sort_context[:username],
sort_context[:opt]
else
self.titles
end
_titles.each do |title|
json.raw title.build_json(slim: slim, depth: depth,
sort_context: sort_context)
sort_context: sort_context, percentage: percentage)
end
end
end
if percentage && sort_context
json.field "title_percentages" do
json.array do
_titles.each do |title|
json.number title.load_percentage sort_context[:username]
end
end
end
end
+32 -14
View File
@@ -202,7 +202,21 @@ class Title
alias SortContext = NamedTuple(username: String, opt: SortOptions)
def build_json(*, slim = false, depth = -1,
sort_context : SortContext? = nil)
sort_context : SortContext? = nil,
percentage = false)
_titles = if sort_context
sorted_titles sort_context[:username],
sort_context[:opt]
else
self.titles
end
_entries = if sort_context
sorted_entries sort_context[:username],
sort_context[:opt]
else
@entries
end
JSON.build do |json|
json.object do
{% for str in ["dir", "title", "id"] %}
@@ -218,32 +232,36 @@ class Title
unless depth == 0
json.field "titles" do
json.array do
_titles = if sort_context
sorted_titles sort_context[:username],
sort_context[:opt]
else
self.titles
end
_titles.each do |title|
json.raw title.build_json(slim: slim,
depth: depth > 0 ? depth - 1 : depth,
sort_context: sort_context)
sort_context: sort_context, percentage: percentage)
end
end
end
json.field "entries" do
json.array do
_entries = if sort_context
sorted_entries sort_context[:username],
sort_context[:opt]
else
@entries
end
_entries.each do |entry|
json.raw entry.build_json(slim: slim)
end
end
end
if percentage && sort_context
json.field "title_percentages" do
json.array do
_titles.each do |t|
json.number t.load_percentage sort_context[:username]
end
end
end
json.field "entry_percentages" do
json.array do
load_percentage_for_all_entries(sort_context[:username], sort_context[:opt]).each do |percentage|
json.number percentage.nan? ? 0 : percentage
end
end
end
end
end
json.field "parents" do
json.array do