Files
readest/apps/readest.koplugin/library/libraryitem.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

142 lines
5.6 KiB
Lua

-- libraryitem.lua
-- Maps LibraryStore rows (and group descriptors) into Menu entry
-- tables that MosaicMenuItem / ListMenuItem can render. The heavy
-- lifting is in:
--
-- library.cloud_covers — single-book cloud cover lifecycle
-- library.group_covers — folder-preview composite mosaics
-- library.cloud_icons — cloud-up/down overlay icons
-- library.list_strip — list-mode group row widget
-- library.bim_patch — BookInfoManager + ListMenuItem patches
--
-- This module is a thin glue layer that produces entries shaped the
-- way the rendering pipeline expects, then delegates lifecycle hooks
-- (install, set_visible_hashes) to the right submodule.
local cloud_covers = require("library.cloud_covers")
local group_covers = require("library.group_covers")
local bim_patch = require("library.bim_patch")
local M = {}
-- Sentinel fields on entry tables. Tap dispatch in librarywidget reads
-- them; the bim_patch list-row patches read them via M.<flag>.
M.CLOUD_ONLY_FLAG = bim_patch.CLOUD_ONLY_FLAG
M.LOCAL_ONLY_FLAG = bim_patch.LOCAL_ONLY_FLAG
-- ---------------------------------------------------------------------------
-- Lifecycle delegates
-- ---------------------------------------------------------------------------
-- Called once when the Library widget opens. Wires the patched BIM
-- with the user's current sync_auth/path/settings.
function M.install(opts)
bim_patch.install(opts)
end
-- Limit cloud-cover downloads to entries on the current Menu page.
function M.set_visible_hashes(menu)
cloud_covers.set_visible_hashes(menu, M.CLOUD_ONLY_FLAG)
end
-- ---------------------------------------------------------------------------
-- Entry constructors
-- ---------------------------------------------------------------------------
-- Group folder. Two render modes:
-- * opts.with_cover ~= false → entry.file = readest-group:// URI so
-- MosaicMenuItem treats it as a file and routes the paint through
-- the patched BIM, which serves a 2x2 cover mosaic.
-- * with_cover = false → entry has no `file` field, so the menu
-- widget renders the default folder treatment (rounded frame +
-- count badge).
function M.entry_from_group(group, opts)
opts = opts or {}
local entry = {
text = group.display_name or group.name,
mandatory = tostring(group.count or 0),
_readest_group = group,
}
-- Stash group_by on the descriptor so list_strip can resolve
-- children without re-parsing the URI.
if opts.group_by then group._group_by = opts.group_by end
if opts.group_by and opts.with_cover ~= false then
local shape = opts.shape or "grid"
local uri = group_covers.build_uri(opts.group_by, group.name, shape)
entry.file = uri
entry.is_file = true
-- Stash by URI so the BIM patch can return a proper title for
-- FakeCover when the composite isn't cached yet on first paint.
cloud_covers.set_meta(uri, {
title = group.display_name or group.name,
})
end
return entry
end
-- "Up one level" entry. _readest_back_to is the parent path to
-- navigate to (nil = back to root); the boolean flag distinguishes a
-- root-back entry from a regular row that just happens to lack a
-- back path.
function M.entry_back(parent_path, label)
return {
text = label,
mandatory = "",
_readest_is_back = true,
_readest_back_to = parent_path,
}
end
-- Convert a LibraryStore row into a Menu item_table entry. The Menu
-- item layer expects entry.file, entry.text, entry.is_file etc.
-- _readest_row is preserved so the tap handler in librarywidget can
-- dispatch on cloud_present / local_present without re-querying.
function M.entry_from_row(row, _opts)
if not row then return nil end
local entry = {
text = row.title,
author = row.author,
series = row.series,
series_index = row.series_index,
cover_path = row.cover_path,
is_file = true,
mandatory = "",
}
local EXTS = require("library.exts")
local ext = (EXTS[row.format] or "epub")
if row.local_present == 1 and row.file_path then
entry.file = row.file_path
-- BIM patch tags this path with _no_provider so ListMenuItem
-- renders mandatory verbatim (= the format) without the
-- trailing "<filetype> " padding the standard format-string
-- adds when mandatory is short. Keeps right-side text
-- right-aligned with cloud rows that already use _no_provider.
entry.mandatory = ext
bim_patch.register_local_path(row.file_path)
-- Mark "local but not in cloud" so the paintTo overlay paints
-- the cloud-upload icon (mirroring Readest's BookItem rule:
-- !uploadedAt → cloud-up).
if (row.cloud_present or 0) == 0 then
entry[M.LOCAL_ONLY_FLAG] = true
end
else
-- Encode the real extension into the URI so listmenu's
-- splitFileNameType returns a non-nil filetype for the right
-- column. The patched BIM strips it back off.
entry.file = cloud_covers.URI_PREFIX .. row.hash .. "." .. ext
entry[M.CLOUD_ONLY_FLAG] = true
-- Same _no_provider treatment as the local branch.
entry.mandatory = ext
-- Stash title/author by hash so the patched BIM (keyed by URI
-- /path, not by row) can return them for FakeCover.
cloud_covers.set_meta(row.hash, {
title = row.title,
author = row.author,
})
end
entry._readest_row = row
return entry
end
return M