Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
8e70431
Tests: a mock BookOrbit server and a probe script
claude Nov 22, 2026
6583840
BookOrbit as the book server: sign in, browse, covers, download
claude Nov 22, 2026
390ae89
Setup guide: first ask how GZreader will be used
claude Nov 22, 2026
119ede4
BookOrbit: the other server's books stay in the Downloads tab (wordin…
claude Nov 22, 2026
49fd9f9
Merge feat/bookorbit-books (the Downloads tab wording) into the setup…
claude Nov 22, 2026
3362582
Tests: run the mock BookOrbit test from the plugin suite, not run.sh
claude Nov 22, 2026
3d34aff
Merge feat/bookorbit-mock; the BookOrbit tests move away from the end…
claude Nov 22, 2026
6c4dde6
Merge branch 'feat/bookorbit-books' into feat/first-run-choice
claude Nov 22, 2026
2c2d792
BookOrbit probe: say why there was no answer; use the system's certif…
claude Nov 22, 2026
169ca09
Mock BookOrbit: a book's main file has role "primary", as a real serv…
claude Nov 22, 2026
5fe86a9
Merge branch 'feat/bookorbit-mock' into feat/bookorbit-books
claude Nov 22, 2026
9b68acc
BookOrbit: download a book's main file (role "primary")
claude Nov 22, 2026
0bfbf64
Merge branch 'feat/bookorbit-books' into feat/first-run-choice
claude Nov 22, 2026
6fab094
BookOrbit: reading progress, and books on the Kindle matched by finge…
claude Nov 22, 2026
0b98d12
BookOrbit: highlights and their notes, both ways
claude Nov 22, 2026
46cc700
BookOrbit: page bookmarks, both ways
claude Nov 22, 2026
3dac597
BookOrbit: audiobooks through its main API, with an API key
claude Nov 22, 2026
f3f8da7
BookOrbit: reading time and ratings
claude Nov 22, 2026
51f73ea
BookOrbit: add books to collections and take them off (API key)
claude Nov 22, 2026
9ddfee0
BookOrbit: request a book from the Catalog (API key)
claude Nov 22, 2026
a831e29
BookOrbit: sign in with only an API key
claude Nov 22, 2026
9ee32ca
BookOrbit probe: can the sync page reach the server, and what the key…
claude Nov 22, 2026
39be500
Remove a compiled Python file added by mistake
claude Nov 22, 2026
5f70c06
Reading stats from BookOrbit too, and a stats page behind the widget
claude Nov 22, 2026
36e7273
BookOrbit: no Grimmory work in the background
claude Nov 22, 2026
41f9857
Wiki: the reverse-proxy rule that lets the sync page reach BookOrbit
claude Nov 22, 2026
8972911
BookOrbit: say what the API key needs; check the sign-in's delivery s…
claude Nov 23, 2026
3e6931a
Setup page: a Books tab that fits the chosen book server
claude Nov 23, 2026
b73d717
BookOrbit: match the Kindle's books right after sign-in and on Wi-Fi
claude Nov 23, 2026
f113c06
BookOrbit: the Catalog says Collections, SmartScope and BookOrbit
claude Nov 23, 2026
230798c
Zip entries get their own inflate; the shared gunzip is left as on main
claude Nov 23, 2026
9f0d22f
Merge branch 'feat/bookorbit-key-signin' into feat/bookorbit-cors-check
claude Nov 23, 2026
5554eb1
Merge branch 'feat/bookorbit-cors-check' into feat/server-stats-page
claude Nov 23, 2026
479da1f
Merge branch 'feat/server-stats-page' into fix/bookorbit-no-grimmory-…
claude Nov 23, 2026
03b33ad
Merge branch 'fix/bookorbit-no-grimmory-work' into docs/bookorbit-syn…
claude Nov 23, 2026
01794ae
Merge branch 'docs/bookorbit-sync-proxy' into fix/bookorbit-key-permi…
claude Nov 23, 2026
7f9a528
Merge branch 'fix/bookorbit-key-permissions' into fix/setup-page-book…
claude Nov 23, 2026
3264c60
Merge branch 'fix/setup-page-book-server' into fix/bookorbit-match-at…
claude Nov 23, 2026
efca3c4
Merge branch 'fix/bookorbit-match-at-signin' into fix/bookorbit-catal…
claude Nov 23, 2026
9569fc7
Leave a comment as on main (it sat beside a line another open PR chan…
claude Nov 23, 2026
f7e5d38
Merge branch 'feat/server-stats-page' into fix/bookorbit-no-grimmory-…
claude Nov 23, 2026
20012f7
Merge branch 'fix/bookorbit-no-grimmory-work' into docs/bookorbit-syn…
claude Nov 23, 2026
0be435f
Merge branch 'docs/bookorbit-sync-proxy' into fix/bookorbit-key-permi…
claude Nov 23, 2026
bb9c202
Merge branch 'fix/bookorbit-key-permissions' into fix/setup-page-book…
claude Nov 23, 2026
15ad25a
Merge branch 'fix/setup-page-book-server' into fix/bookorbit-match-at…
claude Nov 23, 2026
7001b60
Merge branch 'fix/bookorbit-match-at-signin' into fix/bookorbit-catal…
claude Nov 23, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugins/gzreader.koplugin/gzreader_annotations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function Anno:new(sync)
end

function Anno:enabled()
return self.plugin:syncEnabled() and self.plugin.store.annotation_sync_disabled ~= true
return self.plugin:syncEnabled() and self.plugin:serverCan("annotations") and self.plugin.store.annotation_sync_disabled ~= true
and self.is_epub and self.file ~= nil
end

Expand Down
3 changes: 3 additions & 0 deletions plugins/gzreader.koplugin/gzreader_async.lua
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,9 @@ end
--- A valid login for a process forked outside this queue (downloads, the
--- library index): refreshed in the background if needed, then cb(ok, err).
function Async.ensureToken(api, cb)
-- (BookOrbit has no login to refresh: its sign-in goes with every request. Without this,
-- every download and index run started a background process that did nothing.)
if api.koreader_positions then return cb(true) end
local store = api.store or {}
if store.access_token and os.time() < (store.access_expires_at or 0) - 120 then return cb(true) end
Async.run(api, function() return true end, function(ok, err) cb(ok == true, err) end, "refresh login")
Expand Down
344 changes: 344 additions & 0 deletions plugins/gzreader.koplugin/gzreader_bo_annotations.lua

Large diffs are not rendered by default.

236 changes: 236 additions & 0 deletions plugins/gzreader.koplugin/gzreader_bo_bookmarks.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
--[[--
Two-way page-bookmark sync with BookOrbit (the dog-ears), for books whose
place is an XPointer (EPUB and the other reflowable formats), when the server
says it does bookmarks (its `bookmarkSync` capability).

BookOrbit's exchange (POST /koreader/plugin/bookmarks/exchange), like the one
for highlights (gzreader_bo_annotations.lua): the Kindle sends the key of every
bookmark in the book (md5 of "<datetime>|<place>") and the ones BookOrbit hasn't
had from it; BookOrbit answers with bookmarks to add (made elsewhere) and to
remove. A bookmark BookOrbit gives has no date: the Kindle dates it and says
in the acknowledgement which key it now has here. Bookmarks aren't edited.

The same interface as gzreader_bookmarks.lua (new, reconcile,
onAnnotationsModified): main picks this one when the book server is BookOrbit.

State in the download entry:
entry.bo_marks = { [key] = true } what BookOrbit has had from, or given to, this copy
(nil: never exchanged: sent as sidecarState "new", so nothing is deleted)

Written from BookOrbit's routes and field names; none of its plugin's code is here.
]]

local UIManager = require("ui/uimanager")
local Log = require("gzreader_log")

local BM = { DEBOUNCE = 3, MAX_CHANGES = 50, MAX_KEYS = 2000 }
BM.__index = BM

function BM:new(sync)
return setmetatable({
sync = sync, ui = sync.ui, api = sync.api, plugin = sync.plugin, entry = sync.entry,
book_id = sync.book_id, file = sync.ui.document and sync.ui.document.file,
rolling = sync.ui.document ~= nil and sync:isRolling(),
}, self)
end

function BM:enabled()
return self.plugin:syncEnabled() and self.plugin:serverCan("bookmarks") and self.plugin.store.bookmark_sync_disabled ~= true
and self.rolling and self.file ~= nil and type(self.entry.hash) == "string" and not self.off
and (type(self.api.can) ~= "function" or self.api:can("bookmarkSync"))
end

local function md5(s) return require("ffi/sha2").md5(s) end
local function text(v) return type(v) == "string" and v ~= "" and v or nil end

local function isPageBookmark(item)
return type(item) == "table" and not item.drawer and not item.pos0 and type(item.page) == "string"
end
BM.isPageBookmark = isPageBookmark

function BM.key(item) return md5(tostring(item.datetime) .. "|" .. tostring(item.page)) end

function BM.toRemote(item)
return { datetime = item.datetime, datetimeUpdated = text(item.datetime_updated), pos = item.page,
pageno = type(item.pageno) == "number" and item.pageno >= 0 and math.floor(item.pageno) or nil,
chapter = text(item.chapter) and item.chapter:sub(1, 500) or nil,
note = text(item.text) and item.text:sub(1, 500) or nil }
end

function BM:live()
local ui = self.ui
if ui.document and ui.document.file == self.file and ui.annotation then return ui.annotation.annotations or {}, true end
local ok, list = pcall(function() return require("docsettings"):open(self.file):readSetting("annotations") end)
return ok and type(list) == "table" and list or {}, false
end

--- What to send: every bookmark's key, and the ones not in `known`. A bookmark with no
--- date (one an older GZreader put there) is dated now, once (dated = true then).
function BM.outgoing(list, known, now)
local keys, changes, dated = {}, {}, false
known = type(known) == "table" and known or {}
for _i, item in ipairs(list) do
if isPageBookmark(item) then
if not text(item.datetime) then item.datetime = now or os.date("%Y-%m-%d %H:%M:%S"); dated = true end
local k = BM.key(item)
if #keys < BM.MAX_KEYS then keys[#keys + 1] = { k = k, dt = item.datetime } end
if not known[k] and #changes < BM.MAX_CHANGES then changes[#changes + 1] = BM.toRemote(item) end
end
end
return keys, changes, #keys < BM.MAX_KEYS, dated
end

--- Put BookOrbit's answer into the list (in place). Returns changed, applied, deleted.
function BM.merge(list, res, valid, chapterOf, now)
local by_key, by_pos = {}, {}
for i, item in ipairs(list) do
if isPageBookmark(item) then
if text(item.datetime) then by_key[BM.key(item)] = i end
by_pos[item.page] = i
end
end
local applied, deleted, changed, remove = {}, {}, false, {}
for _i, e in ipairs(res.delete or {}) do
local i = type(e.key) == "string" and by_key[e.key]
if i then remove[i] = true end
deleted[#deleted + 1] = { serverId = e.serverId, status = "applied" }
end
if next(remove) then
for i = #list, 1, -1 do
if remove[i] then by_pos[list[i].page] = nil; table.remove(list, i) end
end
changed = true
by_pos = {}
for i, item in ipairs(list) do if isPageBookmark(item) then by_pos[item.page] = i end end
end
for _i, e in ipairs(res.add or {}) do
local pos = text(e.pos)
if pos and pos:sub(1, 1) == "/" and (not valid or valid(pos)) then
local item = by_pos[pos] and list[by_pos[pos]]
if not item then -- (a page that's already dog-eared here keeps its own bookmark)
item = { datetime = now or os.date("%Y-%m-%d %H:%M:%S"), page = pos, text = text(e.title),
pageno = type(e.pageno) == "number" and e.pageno or nil, chapter = chapterOf and text(chapterOf(pos)) or nil }
list[#list + 1] = item
by_pos[pos] = #list
changed = true
elseif not text(item.datetime) then
item.datetime = now or os.date("%Y-%m-%d %H:%M:%S")
changed = true
end
applied[#applied + 1] = { serverId = e.serverId, status = "applied", key = BM.key(item), datetime = item.datetime, pos = pos }
else
applied[#applied + 1] = { serverId = e.serverId, status = "failed" }
end
end
return changed, applied, deleted
end

function BM:apply(res, sent_keys)
local list, open = self:live()
local doc = open and self.ui.document
local valid = doc and doc.isXPointerInDocument and function(xp)
local ok, yes = pcall(doc.isXPointerInDocument, doc, xp)
return ok and yes
end or nil
local chapterOf = open and self.ui.toc and function(xp)
local ok, title = pcall(self.ui.toc.getTocTitleByPage, self.ui.toc, xp)
return ok and title or nil
end or nil
local changed, applied, deleted = BM.merge(list, res, valid, chapterOf)
local saved = true
if changed or self.dated then
self.applying = true
if open and self.ui.annotation.sortItems then pcall(self.ui.annotation.sortItems, self.ui.annotation, list) end
local copy = {}
for i, item in ipairs(list) do copy[i] = item end
saved = require("gzreader_annotations").saveLocal(self, copy)
if open then
list = self.ui.annotation.annotations
if self.ui.bookmark and self.ui.bookmark.setDogearVisibility then
pcall(function() self.ui.bookmark:setDogearVisibility(self.ui.bookmark:getCurrentPageNumber()) end)
end
end
self.applying = false
self.dated = nil
end
local known = {}
local ok_keys = {}
for _i, k in ipairs(sent_keys or {}) do ok_keys[k] = true end
for _i, e in ipairs(applied) do if e.key then ok_keys[e.key] = true end end
local old = type(self.entry.bo_marks) == "table" and self.entry.bo_marks or {}
for _i, item in ipairs(list) do
if isPageBookmark(item) and text(item.datetime) then
local k = BM.key(item)
if ok_keys[k] or old[k] then known[k] = true end
end
end
self.entry.bo_marks = known
self.entry.bm_dirty = (not saved or res.more) and true or nil
self.plugin:save()
Log.info("BookOrbit bookmarks", self.book_id, "sent", #(sent_keys or {}), "added here", #(res.add or {}), "removed here", #(res.delete or {}))
if saved and (#applied > 0 or #deleted > 0) then
local hash = self.entry.hash
require("gzreader_async").run(self.api, function(api) return api:ackExchange("bookmarks", hash, applied, deleted) end, function(ok, err)
if not ok then Log.info("BookOrbit bookmarks: the acknowledgement didn't arrive (asked again next time):", tostring(err)) end
end, "bookmarks ack")
end
end

--- One pass (in the background): called when the book opens, on Sync now, and after a change.
function BM:reconcile(on_done)
if not self:enabled() then return on_done and on_done(false) end
if self.busy then self.again = true; return end
if not require("gzreader_net").connected() then return on_done and on_done(false) end
self.busy, self.again = true, false
local list = self:live()
local keys, changes, complete, dated = BM.outgoing(list, self.entry.bo_marks)
self.dated = dated or self.dated
local sent = {}
for _i, c in ipairs(changes) do sent[#sent + 1] = md5(c.datetime .. "|" .. c.pos) end
local Json = require("gzreader_json")
local book = { hash = self.entry.hash, keys = Json.list(keys), keysComplete = complete,
sidecarState = type(self.entry.bo_marks) == "table" and "known" or "new", changes = Json.list(changes) }
require("gzreader_async").run(self.api, function(api) return api:exchange("bookmarks", book) end, function(res, err)
self.busy = false
local ok = false
if type(res) ~= "table" then
if err == "bookorbit_unmatched" or err == "bookorbit_incompatible" then
Log.info("BookOrbit bookmarks: not synced for this book:", err)
self.off = true
else
Log.warn("BookOrbit bookmark sync failed:", tostring(err))
self.entry.bm_dirty = true
self.plugin:save()
end
else
res.add = type(res.toApply) == "table" and res.toApply.add or {}
res.delete = type(res.toApply) == "table" and res.toApply.delete or {}
local done, crash = pcall(self.apply, self, res, sent)
if not done then Log.warn("BookOrbit bookmark merge failed:", tostring(crash)) else ok = true end
end
if self.again then
self.again = false
UIManager:scheduleIn(1, function() self:reconcile(on_done) end)
return
end
if on_done then on_done(ok) end
end, "bookmarks", { nice = true })
end

--- KOReader event: a bookmark was added or removed.
function BM:onAnnotationsModified(items)
if self.applying or type(items) ~= "table" then return end
local item = items[1]
if type(item) ~= "table" or item.drawer or item.pos0 then return end -- (a highlight: its own sync)
if not self:enabled() then return end
self.entry.bm_dirty = true
if self.pending then UIManager:unschedule(self.pending) end
if not require("gzreader_net").connected() then return end -- sent at the next open
self.pending = function()
self.pending = nil
self:reconcile()
end
UIManager:scheduleIn(BM.DEBOUNCE, self.pending)
end

return BM
Loading