Files
readest/apps/readest.koplugin/syncannotations.lua
T
Huang Xin ed8956e9ed feat(koplugin): Readest Library view in KOReader (#4056)
* feat(koplugin/library): data layer + busted harness + design doc

- LibraryStore: per-user SQLite index merging cloud + local books by
  partial-md5 hash. listBooks/listBookshelfBooks/listBookshelfGroups,
  upsertBook with cloud_present/local_present OR-merge + _force/clear
  sentinels, parseSyncRow, getChangedBooks for tombstone push.
- EXTS table mirroring web's document.ts.
- busted harness with KOReader stubs (G_reader_settings, DataStorage,
  lua-ljsqlite3 against :memory:); spec_helper, exts_spec, smoke_spec,
  librarystore_spec covering schema, sort, group nesting, dedupe.
- Library design doc + spec README.
- pnpm test:lua wired through root + app package.json; lint-koplugin
  recurses into library/ + spec/.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* feat(koplugin/library): cloud sync — push, pull, upload, delete, downloads

- Spore methods: pullBooks (incremental /sync), getDownloadUrl,
  getUploadUrl, listFiles, deleteFile.
- syncbooks.lua: pushBook + pushChangedBooks (advances watermark to
  max(updated_at, deleted_at)), syncBooks(opts, mode) for push/pull/both,
  downloadBook + downloadCover (sync socket.http with file sink; cover
  download via fork+poll with single-slot queue + visible-page filter +
  coalesced refresh), uploadBook (presigned-PUT flow + best-effort
  cover), deleteCloudFiles (list-then-delete-each, mirrors
  cloudService.deleteBook).
- SyncAuth.withFreshToken wrapper resolves the ensureClient race; 401/403
  unified across syncconfig + syncannotations.
- Cloud + local book covers shared by partial-md5 hash; cover.png cached
  at <settings>/readest_covers/<hash>.png with sentinel for known 404s.
- syncbooks_spec covers row-to-wire conversion + file_key shape.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* feat(koplugin/library): local discovery — sidecar scanner + cover provider

- localscanner.lightScan iterates ReadHistory entries, reads
  partial_md5_checksum from .sdr sidecars, and upserts local rows.
  Slow filesystem walks deferred to fullSidecarWalk (24h-gated).
- coverprovider wraps BookInfoManager:getBookInfo for local books with
  graceful FakeCover fallback when coverbrowser is absent.
- localscanner_spec + coverprovider_spec.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* feat(koplugin/library): UI — widget, item, view menu, FileManager hooks

- librarywidget: full-screen Menu mixed in with CoverMenu + Mosaic/List
  per zen_ui's group_view pattern. Title-bar tap → view menu, search via
  left icon, drill-in/back for grouping. Async cloud sync deferred via
  scheduleIn so the menu paints before HTTP fires.
- libraryitem: BIM patch for cloud-only (readest-cloud://) and group
  (readest-group://) URIs; group-cover composer (2x2 mosaic for grid,
  same in list) with cache key derived from the actual first-N hashes
  for content-based invalidation; ListMenuItem update + paintTo patches
  for wider list-mode cover strip and cloud-up/down icon overlay.
- libraryviewmenu: ButtonDialog with View/Group by/Sort by/Actions.
  Default Group by = Groups (parity with web), values authors/groups.
- librarypaint: partial-page e-ink repaint shim adapted from zen_ui.
- main.lua: Library menu entry, dispatcher actions (Open Library / Push
  / Pull as general; progress + annotations as reader-only),
  "Add to Readest" button in FileManager's long-press file dialog
  (dedupe by partial_md5; bumps updated_at when present, inserts a
  fresh local-only row otherwise; un-tombstones via _clear_fields).
  Push books on Library open when auto_sync is on, pull-only otherwise.
- Long-press action sheet with Readest BookDetailView parity:
  Remove from Cloud & Device / Cloud Only / Device Only,
  Upload to Cloud, Download Book / Cover / All.
- Cloud-down + cloud-up SVG icons (LiaCloudDownloadAltSolid /
  LiaCloudUploadAltSolid) painted in the right-side wpageinfo slot.
- i18n catalog updated for new strings.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* refactor(koplugin/library): split libraryitem into focused modules

libraryitem.lua had grown to 1018 lines mixing five unrelated
concerns. Split along the natural seams:

  cloud_covers — readest-cloud:// URI scheme, on-disk <hash>.png
                 cache, single-slot async download queue, visible-page
                 filter
  group_covers — readest-group:// URI scheme, 2x2 mosaic composer with
                 content-derived cache key (first-N hashes), cell
                 layout table
  cloud_icons  — bundled cloud-up/cloud-down SVG loader, IconWidget
                 cache, paint-overlay positioning
  list_strip   — list-mode group row builder (4-cover wider strip
                 replacing ListMenu's square cover slot)
  bim_patch    — BookInfoManager:getBookInfo router (cloud / group /
                 local) + ListMenuItem update + paintTo patches; owns
                 the _library_local_paths set and orig BIM reference

libraryitem.lua is now 141 lines: just the entry-table constructors
(entry_from_row, entry_from_group, entry_back) plus thin install /
set_visible_hashes delegates. Each new module is 88-216 lines.

No behavior change — same 113 specs pass.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* chore(koplugin): co-locate dev tooling, ship-zip exclusions, CI job split

- apps/readest.koplugin/scripts/build-koplugin.mjs: local sideloading
  build with the same exclusions the release workflow uses.
- Move lint-koplugin + test-koplugin from apps/readest-app/scripts/ to
  apps/readest.koplugin/scripts/. All koplugin dev tooling now lives
  with the koplugin and is excluded from the published release zip.
- Rename to .mjs so Node treats them as ESM without the reparse warning
  (the i18n CommonJS scripts stay .js).
- Release workflow: zip -r exclusions for scripts/, docs/, spec/,
  .busted so dev artifacts don't ship to end users.
- PR workflow: split build_web_app into build_web_app + test_web_app
  for parallelism. The test job installs luarocks + busted +
  lsqlite3complete and runs pnpm test:lua. test-koplugin.mjs now
  hard-fails (instead of soft-skipping) when CI=true and a tool is
  missing — a broken CI toolchain previously exited 0 silently.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-04 23:12:34 +02:00

376 lines
14 KiB
Lua

local Event = require("ui/event")
local InfoMessage = require("ui/widget/infomessage")
local NetworkMgr = require("ui/network/manager")
local UIManager = require("ui/uimanager")
local logger = require("logger")
local sha2 = require("ffi/sha2")
local T = require("ffi/util").template
local _ = require("i18n")
local SyncAnnotations = {}
-- KOReader color name → Readest color value
local KO_TO_READEST_COLOR = {
yellow = "yellow",
red = "red",
green = "green",
blue = "blue",
purple = "violet",
orange = "#ff8800",
cyan = "#00bcd4",
olive = "#808000",
gray = "#9e9e9e",
}
-- Readest color value → KOReader color name
local READEST_TO_KO_COLOR = {
yellow = "yellow",
red = "red",
green = "green",
blue = "blue",
violet = "purple",
["#ff8800"] = "orange",
["#00bcd4"] = "cyan",
["#808000"] = "olive",
["#9e9e9e"] = "gray",
}
function SyncAnnotations:parseDatetimeToMs(dt)
if not dt then return os.time() * 1000 end
local y, m, d, h, min, s = dt:match("(%d+)-(%d+)-(%d+) (%d+):(%d+):(%d+)")
if y then
return os.time({
year = tonumber(y), month = tonumber(m), day = tonumber(d),
hour = tonumber(h), min = tonumber(min), sec = tonumber(s),
}) * 1000
end
return os.time() * 1000
end
function SyncAnnotations:parseISODatetime(dt)
if not dt then return os.time() end
local y, m, d, h, min, s = dt:match("(%d+)-(%d+)-(%d+)T(%d+):(%d+):(%d+)")
if y then
return os.time({
year = tonumber(y), month = tonumber(m), day = tonumber(d),
hour = tonumber(h), min = tonumber(min), sec = tonumber(s),
})
end
return os.time()
end
function SyncAnnotations:generateNoteId(book_hash, note_type, pos0, pos1)
local raw = "ko:" .. book_hash .. ":" .. note_type .. ":" .. (pos0 or "") .. ":" .. (pos1 or "")
return sha2.md5(raw):sub(1, 7)
end
function SyncAnnotations:getAnnotations(ui, settings, book_hash, meta_hash, full_sync)
local annotations = ui.annotation and ui.annotation.annotations
if not annotations then return {} end
local last_sync = full_sync and 0 or (settings.last_notes_sync_at or 0)
local notes = {}
for _, item in ipairs(annotations) do
local updated_at = self:parseDatetimeToMs(item.datetime_updated or item.datetime)
if updated_at <= last_sync then
goto skip
end
local pos0 = item.pos0
local pos1 = item.pos1
if type(pos0) == "table" then pos0 = nil end
if type(pos1) == "table" then pos1 = nil end
if item.drawer and pos0 then
-- Annotation (highlight/underline/strikeout): has drawer and pos0/pos1
local style = "highlight"
if item.drawer == "underscore" then
style = "underline"
elseif item.drawer == "strikeout" then
style = "squiggly"
end
local id = item.id or self:generateNoteId(book_hash, "annotation", tostring(pos0), pos1 and tostring(pos1))
table.insert(notes, {
bookHash = book_hash,
metaHash = meta_hash,
id = id,
type = "annotation",
xpointer0 = tostring(pos0),
xpointer1 = pos1 and tostring(pos1) or nil,
text = item.text or "",
note = item.note or "",
style = style,
color = KO_TO_READEST_COLOR[item.color or "yellow"],
page = item.pageno,
createdAt = self:parseDatetimeToMs(item.datetime),
updatedAt = updated_at,
})
elseif not item.drawer and type(item.page) == "string" then
-- Bookmark: no drawer, position in page field (xpointer string)
local page_xp = item.page
local id = item.id or self:generateNoteId(book_hash, "bookmark", page_xp)
table.insert(notes, {
bookHash = book_hash,
metaHash = meta_hash,
id = id,
type = "bookmark",
xpointer0 = page_xp,
text = item.text or "",
note = item.note or "",
page = item.pageno,
createdAt = self:parseDatetimeToMs(item.datetime),
updatedAt = updated_at,
})
end
::skip::
end
return notes
end
function SyncAnnotations:push(ui, settings, client, interactive, full_sync)
local book_hash = ui.doc_settings:readSetting("partial_md5_checksum")
local meta_hash = ui.doc_settings:readSetting("readest_sync") or {}
meta_hash = meta_hash.meta_hash_v1
if not book_hash or not meta_hash then return end
local annotations = self:getAnnotations(ui, settings, book_hash, meta_hash, full_sync)
if #annotations == 0 then
if interactive then
UIManager:show(InfoMessage:new{
text = _("No annotations to push"),
timeout = 2,
})
end
return
end
if interactive then
UIManager:show(InfoMessage:new{
text = _("Pushing annotations..."),
timeout = 1,
})
end
local payload = {
books = {},
notes = annotations,
configs = {},
}
logger.dbg("ReadestSync: Pushing annotations, payload:", payload)
client:pushChanges(
payload,
function(success, _response)
if interactive then
if success then
UIManager:show(InfoMessage:new{
text = T(_("%1 annotations pushed successfully"), #annotations),
timeout = 2,
})
else
UIManager:show(InfoMessage:new{
text = _("Failed to push annotations"),
timeout = 2,
})
end
end
if success then
settings.last_notes_sync_at = os.time() * 1000
G_reader_settings:saveSetting("readest_sync", settings)
if ui.doc_settings then
local doc_readest_sync = ui.doc_settings:readSetting("readest_sync") or {}
doc_readest_sync.last_synced_at_notes = os.time()
ui.doc_settings:saveSetting("readest_sync", doc_readest_sync)
end
end
end
)
end
function SyncAnnotations:pull(ui, settings, client, book_hash, meta_hash, dialog, interactive, full_sync)
if ui.document.info.has_pages then
if interactive then
UIManager:show(InfoMessage:new{
text = _("Annotation sync is not supported for PDF documents"),
timeout = 3,
})
end
return
end
if interactive then
UIManager:show(InfoMessage:new{
text = full_sync and _("Full sync: pulling all annotations...") or _("Pulling annotations..."),
timeout = 1,
})
end
client:pullChanges(
{
since = full_sync and 0 or (settings.last_notes_sync_at or 0),
type = "notes",
book = book_hash,
meta_hash = meta_hash,
},
function(success, response, status)
if not success then
-- Treat HTTP 401/403 as auth failure regardless of body shape
-- so a future server tweak to the error string doesn't
-- silently turn relogin into "Failed to pull annotations"
-- noise (codex round 1 finding 15).
local is_auth_fail = status == 401 or status == 403
or (response and response.error == "Not authenticated")
if interactive then
UIManager:show(InfoMessage:new{
text = is_auth_fail
and _("Authentication failed, please login again")
or _("Failed to pull annotations"),
timeout = 2,
})
end
return
end
local data = response.notes
if not data or #data == 0 then
if interactive then
UIManager:show(InfoMessage:new{
text = _("No new annotations found"),
timeout = 2,
})
end
return
end
logger.dbg("ReadestSync: Pulled annotations from sync:", #data)
local annotation_mgr = ui.annotation
if not annotation_mgr then return end
-- Build dedup sets: by ID, by pos0|pos1 for annotations, by page xpointer for bookmarks
local existing_ids = {}
local existing_annotations = {}
local existing_bookmarks = {}
for _, item in ipairs(annotation_mgr.annotations) do
-- Use stored id if available
if item.id then
existing_ids[item.id] = true
end
if item.drawer then
local pos0 = item.pos0
local pos1 = item.pos1
if type(pos0) == "table" then pos0 = nil end
if type(pos1) == "table" then pos1 = nil end
local key = tostring(pos0) .. "|" .. tostring(pos1 or "")
existing_annotations[key] = true
-- Also generate ID for annotations without id
if not item.id and pos0 then
local id = self:generateNoteId(book_hash, "annotation", tostring(pos0), pos1 and tostring(pos1))
existing_ids[id] = true
end
elseif type(item.page) == "string" then
existing_bookmarks[item.page] = true
if not item.id then
local id = self:generateNoteId(book_hash, "bookmark", item.page)
existing_ids[id] = true
end
end
end
local added = 0
for _, note in ipairs(data) do
if note.deleted_at then
goto continue
end
local xp0 = note.xpointer0
if not xp0 then goto continue end
-- Deduplicate by server-provided ID
if note.id and existing_ids[note.id] then goto continue end
local note_type = note.type
local item
local created = self:parseISODatetime(note.created_at)
local updated = self:parseISODatetime(note.updated_at) or created
local datetime_str = os.date("%Y-%m-%d %H:%M:%S", created)
local datetime_updated_str = os.date("%Y-%m-%d %H:%M:%S", updated)
-- Resolve KOReader page number from xpointer
local pageno = ui.document:getPageFromXPointer(xp0) or note.page
if note_type == "bookmark" then
if existing_bookmarks[xp0] then goto continue end
item = {
id = note.id,
page = xp0,
text = note.text or "",
note = note.note or "",
pageno = pageno,
datetime = datetime_str,
datetime_updated = datetime_updated_str,
}
existing_bookmarks[xp0] = true
else
local xp1 = note.xpointer1
local key = xp0 .. "|" .. (xp1 or "")
if existing_annotations[key] then goto continue end
local drawer = "lighten"
if note.style == "underline" then
drawer = "underscore"
elseif note.style == "squiggly" then
drawer = "strikeout"
end
item = {
id = note.id,
pos0 = xp0,
pos1 = xp1 or xp0,
page = xp0,
text = note.text or "",
note = note.note or "",
drawer = drawer,
color = READEST_TO_KO_COLOR[note.color] or "yellow",
pageno = pageno,
datetime = datetime_str,
datetime_updated = datetime_updated_str,
}
existing_annotations[key] = true
end
local index = annotation_mgr:addItem(item)
ui:handleEvent(Event:new("AnnotationsModified", { item, index_modified = index }))
logger.dbg("ReadestSync: Added annotation from sync:", item)
added = added + 1
::continue::
end
settings.last_notes_sync_at = os.time() * 1000
G_reader_settings:saveSetting("readest_sync", settings)
if ui.doc_settings then
local doc_readest_sync = ui.doc_settings:readSetting("readest_sync") or {}
doc_readest_sync.last_synced_at_notes = os.time()
ui.doc_settings:saveSetting("readest_sync", doc_readest_sync)
end
if interactive then
UIManager:show(InfoMessage:new{
text = T(_("%1 annotations pulled"), added),
timeout = 2,
})
end
if added > 0 then
UIManager:setDirty(dialog, "ui")
end
end
)
end
return SyncAnnotations