diff --git a/public/js/title.js b/public/js/title.js index 1b423d1..18a39ed 100644 --- a/public/js/title.js +++ b/public/js/title.js @@ -38,8 +38,12 @@ function showModal(encodedPath, pages, percentage, encodedeTitle, encodedEntryTi styleModal(); } -function updateProgress(titleID, entryID, page) { - $.post('/api/progress/' + titleID + '/' + entryID + '/' + page, function(data) { +const updateProgress = (tid, eid, page) => { + let url = `/api/progress/${tid}/${page}` + const query = $.param({entry: eid}); + if (eid) + url += `?${query}`; + $.post(url, (data) => { if (data.success) { location.reload(); } @@ -48,7 +52,7 @@ function updateProgress(titleID, entryID, page) { alert('danger', error); } }); -} +}; const renameSubmit = (name, eid) => { const upload = $('.upload-field'); diff --git a/src/library.cr b/src/library.cr index d490a3d..058b04a 100644 --- a/src/library.cr +++ b/src/library.cr @@ -258,6 +258,26 @@ class Title end end + # Set the reading progress of all entries and nested libraries to 100% + def read_all(username) + @entries.each do |e| + save_progress username, e.title, e.pages + end + titles.each do |t| + t.read_all username + end + end + + # Set the reading progress of all entries and nested libraries to 0% + def unread_all(username) + @entries.each do |e| + save_progress username, e.title, 0 + end + titles.each do |t| + t.unread_all username + end + end + # For backward backward compatibility with v0.1.0, we save entry titles # instead of IDs in info.json def save_progress(username, entry, page) diff --git a/src/routes/api.cr b/src/routes/api.cr index 3e75b41..e888239 100644 --- a/src/routes/api.cr +++ b/src/routes/api.cr @@ -69,16 +69,23 @@ class APIRouter < Router end end - post "/api/progress/:title/:entry/:page" do |env| + post "/api/progress/:title/:page" do |env| begin username = get_username env title = (@context.library.get_title env.params.url["title"]) .not_nil! - entry = (title.get_entry env.params.url["entry"]).not_nil! page = env.params.url["page"].to_i + entry_id = env.params.query["entry"]? - raise "incorrect page value" if page < 0 || page > entry.pages - title.save_progress username, entry.title, page + if !entry_id.nil? + entry = title.get_entry(entry_id).not_nil! + raise "incorrect page value" if page < 0 || page > entry.pages + title.save_progress username, entry.title, page + elsif page == 0 + title.unread_all username + else + title.read_all username + end rescue e @context.error e send_json env, {