896 lines
35 KiB
Lua
896 lines
35 KiB
Lua
--[[--
|
|
微信读书划线/想法 ↔ KOReader 注解与 HTML 注入。
|
|
|
|
@module koplugin.book.moonsource_weread.notes
|
|
--]]
|
|
|
|
local Text = require("utils.text")
|
|
local Annotations = require("book.annotations")
|
|
local Toc = require("moonsource_weread.toc")
|
|
local Normalize = require("book.note.normalize")
|
|
|
|
local Notes = {}
|
|
|
|
local COLOR_STYLE = {
|
|
blue = 1,
|
|
green = 2,
|
|
yellow = 3,
|
|
purple = 4,
|
|
orange = 5,
|
|
lightgray = 5,
|
|
gray = 5,
|
|
cyan = 1,
|
|
red = 5,
|
|
pink = 4,
|
|
}
|
|
|
|
local LOCAL_COLOR = {
|
|
[1] = "blue",
|
|
[2] = "green",
|
|
[3] = "yellow",
|
|
[4] = "purple",
|
|
[5] = "orange",
|
|
}
|
|
|
|
local WR_FIELDS = {
|
|
"wr_range", "wr_bookmark_id", "wr_review_id", "wr_review_only", "wr_private", "wr_friendship",
|
|
"wr_delete_review", "wr_update_review", "wr_update_bookmark",
|
|
}
|
|
|
|
--- 阅读器注解条目(KOReader annotations + 微信扩展字段)。
|
|
---@class WechatAnnotation
|
|
---@field pos0 string|nil
|
|
---@field pos1 string|nil
|
|
---@field page string|number|nil
|
|
---@field pageno number|nil
|
|
---@field text string|nil
|
|
---@field wr_bookmark_id string|number|nil
|
|
---@field wr_range string|nil
|
|
---@field wr_review_id string|number|nil
|
|
---@field wr_private integer|nil 0 公开,1 仅自己可见
|
|
---@field wr_friendship integer|nil 1 仅互相关注可见
|
|
---@field wr_deleted boolean|nil
|
|
---@field chapter_idx number|nil
|
|
---@field datetime string|nil
|
|
---@field [any] any
|
|
|
|
--- 删除墓碑:只留远端 id,等上行删掉远端后出列。
|
|
---@param item table 被删的注解
|
|
---@param bookmark_id string|number|nil
|
|
---@param review_id string|number|nil
|
|
---@return table
|
|
local function tombstone(item, bookmark_id, review_id)
|
|
return {
|
|
datetime = item.datetime or os.date("%Y-%m-%d %H:%M:%S"),
|
|
chapter_idx = item.chapter_idx,
|
|
wr_bookmark_id = bookmark_id,
|
|
wr_review_id = review_id,
|
|
wr_range = item.wr_range,
|
|
wr_deleted = true,
|
|
}
|
|
end
|
|
|
|
--- 想法列表条目有的包一层 ``review``,有的直接是想法本身。
|
|
---@param entry any
|
|
---@return table|nil
|
|
local function unwrapReview(entry)
|
|
local review = type(entry) == "table" and (entry.review or entry) or nil
|
|
return type(review) == "table" and review or nil
|
|
end
|
|
|
|
--- 想法上的 ``isPrivate``:1 仅自己可见,0 公开。没有这个字段就返回 nil,不能当成公开。
|
|
---@param review table|nil
|
|
---@return integer|nil
|
|
function Notes.privateOf(review)
|
|
if type(review) ~= "table" then return nil end
|
|
local body = type(review.review) == "table" and review.review or review
|
|
local flag = body.isPrivate
|
|
if flag == 1 or flag == true then return 1 end
|
|
if flag == 0 or flag == false then return 0 end
|
|
return nil
|
|
end
|
|
|
|
--- 想法上的 ``friendship``:1 仅互相关注可见。没有这个字段就返回 nil。
|
|
---@param review table|nil
|
|
---@return integer|nil
|
|
function Notes.friendshipOf(review)
|
|
if type(review) ~= "table" then return nil end
|
|
local body = type(review.review) == "table" and review.review or review
|
|
local flag = body.friendship
|
|
if flag == 1 or flag == true then return 1 end
|
|
if flag == 0 or flag == false then return 0 end
|
|
return nil
|
|
end
|
|
|
|
--- 发表/修改要带的可见范围。已有想法没记过时两个都是 nil,请求里就不带。
|
|
--- choice 为设置项:public / follow。没设过或 private 都是私有。
|
|
---@param annotation table
|
|
---@param book_secret integer|boolean|nil 保留参数,新想法不再看私密阅读
|
|
---@param choice string|nil
|
|
---@return integer|nil is_private
|
|
---@return integer|nil friendship
|
|
function Notes.reviewFlags(annotation, book_secret, choice)
|
|
local function known(value)
|
|
if value == 1 or value == true then return 1 end
|
|
if value == 0 or value == false then return 0 end
|
|
return nil
|
|
end
|
|
local private, friendship = known(annotation.wr_private), known(annotation.wr_friendship)
|
|
if private ~= nil or friendship ~= nil then return private, friendship end
|
|
if annotation.wr_review_id ~= nil then return nil, nil end
|
|
if choice == "public" then return 0, 0 end
|
|
if choice == "follow" then return 0, 1 end
|
|
return 1, 0
|
|
end
|
|
|
|
---@param items table[]|nil
|
|
---@param total_pages integer|nil
|
|
---@return table[]
|
|
function Notes.cleanAnnotations(items, total_pages)
|
|
local out = {}
|
|
for _idx, item in ipairs(items or {}) do
|
|
if type(item) == "table" and item.wr_deleted
|
|
and (item.wr_bookmark_id or item.wr_review_id) then
|
|
out[#out + 1] = tombstone(item, item.wr_bookmark_id, item.wr_review_id)
|
|
elseif type(item) == "table" then
|
|
local placeholder = not item.page and not item.pageref and item.wr_range
|
|
local source = item
|
|
if placeholder then
|
|
source = {}
|
|
for k, v in pairs(item) do source[k] = v end
|
|
source.page = placeholder
|
|
end
|
|
local clean = Normalize.clean({ source }, total_pages)[1]
|
|
if clean then
|
|
if placeholder then clean.page = nil end
|
|
for _idx, field in ipairs(WR_FIELDS) do clean[field] = item[field] end
|
|
out[#out + 1] = clean
|
|
end
|
|
end
|
|
end
|
|
return (Normalize.unique(out))
|
|
end
|
|
|
|
---@param previous table[]|nil
|
|
---@param current table[]
|
|
---@return table[]
|
|
function Notes.prepareLocalAnnotations(previous, current)
|
|
local by_bookmark, by_review = {}, {}
|
|
for _idx, item in ipairs(current) do
|
|
if item.wr_bookmark_id then by_bookmark[tostring(item.wr_bookmark_id)] = item end
|
|
if item.wr_review_id then by_review[tostring(item.wr_review_id)] = item end
|
|
end
|
|
for _idx, old in ipairs(previous or {}) do
|
|
if type(old) == "table" and old.wr_deleted then
|
|
local keep_bookmark = old.wr_bookmark_id
|
|
and not by_bookmark[tostring(old.wr_bookmark_id)]
|
|
local keep_review = old.wr_review_id
|
|
and not by_review[tostring(old.wr_review_id)]
|
|
if keep_bookmark or keep_review then
|
|
current[#current + 1] = tombstone(old,
|
|
keep_bookmark and old.wr_bookmark_id or nil,
|
|
keep_review and old.wr_review_id or nil)
|
|
end
|
|
elseif type(old) == "table" then
|
|
local bookmark_id, review_id = old.wr_bookmark_id, old.wr_review_id
|
|
local current_item = bookmark_id
|
|
and by_bookmark[tostring(bookmark_id)] or nil
|
|
current_item = current_item or (review_id
|
|
and by_review[tostring(review_id)] or nil)
|
|
if current_item and (old.color ~= current_item.color
|
|
or old.drawer ~= current_item.drawer) then
|
|
current_item.wr_update_bookmark = true
|
|
end
|
|
if review_id and current_item and old.note and old.note ~= ""
|
|
and current_item.note and current_item.note ~= ""
|
|
and old.note ~= current_item.note then
|
|
current_item.wr_update_review = true
|
|
end
|
|
if review_id and current_item and old.note and old.note ~= ""
|
|
and (not current_item.note or current_item.note == "") then
|
|
current_item.wr_review_id = review_id
|
|
current_item.wr_delete_review = true
|
|
else
|
|
local delete_bookmark = bookmark_id
|
|
and not by_bookmark[tostring(bookmark_id)]
|
|
local delete_review = review_id
|
|
and not by_review[tostring(review_id)]
|
|
if delete_bookmark or delete_review then
|
|
current[#current + 1] = tombstone(old,
|
|
delete_bookmark and bookmark_id or nil,
|
|
delete_review and review_id or nil)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
return current
|
|
end
|
|
|
|
--- 是否为个人划线(bookmarklist ``updated`` 条目;排除 type=0 书签位)。
|
|
---@param row table
|
|
---@return boolean
|
|
function Notes.isHighlightRow(row)
|
|
if type(row) ~= "table" then
|
|
return false
|
|
end
|
|
local kind = tonumber(row.type)
|
|
if kind == 0 then
|
|
return false
|
|
end
|
|
return tostring(row.range or "") ~= ""
|
|
end
|
|
|
|
--- 微信划线 → 可在阅读器内显示的原生注解(按段落偏移直算 xpointer)。
|
|
---@param document table|nil
|
|
---@param annotations table[]
|
|
---@param html_path string|nil 章节 HTML 路径
|
|
---@param current table[]|nil 阅读器已有注解;同一远端 id 优先复用已验证的 xpointer
|
|
---@return table[]
|
|
function Notes.localizeAnnotations(document, annotations, html_path, current)
|
|
if type(annotations) ~= "table" then
|
|
return {}
|
|
end
|
|
---@param item table
|
|
---@return boolean
|
|
local function needsLocate(item)
|
|
if item.pos0 then return false end
|
|
local text = item.text
|
|
local range = item.wr_range
|
|
return (type(text) == "string" and text ~= "")
|
|
or (type(range) == "string" and range ~= "")
|
|
end
|
|
local unresolved = {}
|
|
for _idx, item in ipairs(annotations) do
|
|
if type(item) == "table" and needsLocate(item) then
|
|
unresolved[#unresolved + 1] = item
|
|
end
|
|
end
|
|
-- 没有待定位的划线就不必读整章 HTML;开书若再 apply 一次,已定位条目会走这条快路径。
|
|
if #unresolved == 0 then
|
|
return annotations
|
|
end
|
|
local html
|
|
if type(html_path) == "string" and html_path ~= "" then
|
|
local f = io.open(html_path, "rb")
|
|
if f then
|
|
html = f:read("*a")
|
|
f:close()
|
|
end
|
|
end
|
|
if not html then
|
|
return annotations
|
|
end
|
|
-- rune 流只随 HTML 变,整章建一次给所有划线复用。
|
|
local flow = Annotations.flow(html)
|
|
---@type table<string, WechatAnnotation>
|
|
local positioned = {}
|
|
for _idx, item in ipairs(current or {}) do
|
|
if type(item) == "table" and item.wr_bookmark_id and item.pos0 and item.pos1 then
|
|
positioned[tostring(item.wr_bookmark_id)] = item
|
|
end
|
|
end
|
|
local located = Annotations.locateBatch(flow, unresolved)
|
|
local out = {}
|
|
for _idx, item in ipairs(annotations) do
|
|
local ann = item
|
|
if type(item) == "table" and not item.pos0 then
|
|
---@type WechatAnnotation|nil
|
|
local saved = item.wr_bookmark_id and positioned[tostring(item.wr_bookmark_id)] or nil
|
|
local location = located[item]
|
|
if saved or location then
|
|
ann = {}
|
|
for k, v in pairs(item) do ann[k] = v end
|
|
-- 当前章节文本能唯一确定时以重算结果为准,避免错位坐标;
|
|
-- 只有仍然歧义时才保留同 bookmarkId 的既有原生位置。
|
|
if location then
|
|
ann.pos0, ann.pos1, ann.page = location.pos0, location.pos1, location.pos0
|
|
ann.pageno = nil
|
|
elseif saved then
|
|
ann.pos0, ann.pos1 = saved.pos0, saved.pos1
|
|
ann.page, ann.pageno = saved.page or saved.pos0, saved.pageno
|
|
end
|
|
if not ann.pageno and document and type(document.getPageFromXPointer) == "function" then
|
|
local ok, pageno = pcall(document.getPageFromXPointer, document, ann.pos0)
|
|
if ok and pageno then
|
|
ann.pageno = pageno
|
|
end
|
|
end
|
|
end
|
|
end
|
|
if type(ann) == "table" then
|
|
out[#out + 1] = ann
|
|
end
|
|
end
|
|
return out
|
|
end
|
|
|
|
---@param remote table[]
|
|
---@param local_items table[]
|
|
---@param paging boolean|nil
|
|
---@param authoritative boolean|nil
|
|
---@return table[] merged
|
|
---@return integer dropped 权威快照里远端已不存在的本地条目数
|
|
---@return integer deduped 因 id / range / 坐标 / 内容键重复被折叠的条数
|
|
function Notes.mergeAnnotations(remote, local_items, paging, authoritative)
|
|
local function rangeKey(item)
|
|
if type(item.text) ~= "string" or item.text == ""
|
|
or not item.wr_range or item.wr_range == "" then return nil end
|
|
return table.concat({ item.text, tostring(item.wr_range) }, "\31")
|
|
end
|
|
local function noteKey(item)
|
|
if type(item.text) ~= "string" or item.text == ""
|
|
or type(item.note) ~= "string" or item.note == "" then
|
|
return nil
|
|
end
|
|
return table.concat({ item.text, item.note }, "\31")
|
|
end
|
|
-- 本地新划线上传后确认没回来(超时、断网)就没有 id / range,下次拉取时只能靠坐标认出它就是远端那条。
|
|
local function posKey(item)
|
|
if type(item.pos0) ~= "string" or type(item.pos1) ~= "string" then return nil end
|
|
return table.concat({ item.pos0, item.pos1 }, "\31")
|
|
end
|
|
local function samePosition(item, other)
|
|
if not other then return false end
|
|
local pos, other_pos = posKey(item), posKey(other)
|
|
if pos or other_pos then return pos ~= nil and pos == other_pos end
|
|
return item.page == other.page
|
|
end
|
|
---@param item table
|
|
---@param seen table<string, table>
|
|
local function mark(item, seen)
|
|
if item.wr_bookmark_id then seen["bookmark:" .. tostring(item.wr_bookmark_id)] = item end
|
|
if item.wr_review_id then seen["review:" .. tostring(item.wr_review_id)] = item end
|
|
local range_key, note_key, pos_key = rangeKey(item), noteKey(item), posKey(item)
|
|
if range_key then seen["range:" .. range_key] = item end
|
|
if note_key then seen["note:" .. note_key] = item end
|
|
if pos_key then seen["pos:" .. pos_key] = item end
|
|
-- 没有 id / 原文的位置记录(纯书签或误写入的进度残片)只能靠坐标内容去重;
|
|
-- 漏掉这一层时,历史快照里的几千份拷贝会在每次开书合并时再翻倍。
|
|
local content_key = Normalize.contentKey(item)
|
|
if content_key then seen["content:" .. content_key] = item end
|
|
end
|
|
---@param item table
|
|
---@param seen table<string, table>
|
|
---@return boolean
|
|
local function isDup(item, seen)
|
|
local range_key, note_key, pos_key = rangeKey(item), noteKey(item), posKey(item)
|
|
local content_key = Normalize.contentKey(item)
|
|
return (item.wr_bookmark_id and seen["bookmark:" .. tostring(item.wr_bookmark_id)] ~= nil)
|
|
or (item.wr_review_id and seen["review:" .. tostring(item.wr_review_id)] ~= nil)
|
|
or (range_key and samePosition(item, seen["range:" .. range_key]))
|
|
or (note_key and samePosition(item, seen["note:" .. note_key]))
|
|
or (not item.wr_bookmark_id and pos_key and seen["pos:" .. pos_key] ~= nil)
|
|
or (content_key and seen["content:" .. content_key] ~= nil)
|
|
or false
|
|
end
|
|
authoritative = authoritative == true
|
|
local merged, seen, dropped, deduped = {}, {}, 0, 0
|
|
for _idx, item in ipairs(remote or {}) do
|
|
if type(item) == "table"
|
|
and Normalize.renderable(item, paging) then
|
|
-- 同文同 range 的多条远端划线是同一处(重复上传留下的),手机端也只显示一条。
|
|
if isDup(item, seen) then
|
|
deduped = deduped + 1
|
|
else
|
|
mark(item, seen)
|
|
merged[#merged + 1] = item
|
|
end
|
|
end
|
|
end
|
|
for _idx, item in ipairs(local_items or {}) do
|
|
if type(item) == "table" then
|
|
local duplicate = isDup(item, seen)
|
|
local deleted_remote = authoritative and not duplicate
|
|
and (item.wr_bookmark_id or item.wr_review_id)
|
|
if deleted_remote then
|
|
dropped = dropped + 1
|
|
elseif duplicate then
|
|
deduped = deduped + 1
|
|
elseif Normalize.renderable(item, paging) then
|
|
mark(item, seen)
|
|
merged[#merged + 1] = item
|
|
end
|
|
end
|
|
end
|
|
return merged, dropped, deduped
|
|
end
|
|
|
|
--- bookmarklist 的 markText 可能是 base64。
|
|
---@return string
|
|
function Notes.decodeMarkText(value)
|
|
local raw = tostring(value or "")
|
|
if raw == "" then
|
|
return ""
|
|
end
|
|
if raw:match("^[%w%+/]+=*$") and #raw % 4 == 0 then
|
|
local decoded = Text.base64Decode(raw)
|
|
if type(decoded) == "string" and decoded ~= "" and Text.isValidUtf8(decoded) then
|
|
return decoded
|
|
end
|
|
end
|
|
return raw
|
|
end
|
|
|
|
---@param color string|nil
|
|
---@return integer
|
|
function Notes.colorStyle(color)
|
|
return COLOR_STYLE[color] or 5
|
|
end
|
|
|
|
---@return string|nil
|
|
function Notes.localColor(style)
|
|
return LOCAL_COLOR[tonumber(style)]
|
|
end
|
|
|
|
--- ``/review/list/mine`` wire → 按 ``range`` 索引的划线想法。
|
|
---
|
|
--- 想法条目形如 ``{ review = { range, content, reviewId } }``;无 ``range`` 的是
|
|
--- 整本书评或章节点评,不挂到划线上。
|
|
---@param wire table|nil
|
|
---@return table<string, table>
|
|
function Notes.reviewsByRange(wire)
|
|
local out = {}
|
|
if type(wire) ~= "table" then
|
|
return out
|
|
end
|
|
for _idx, entry in ipairs(wire.reviews or {}) do
|
|
local review = unwrapReview(entry)
|
|
if review then
|
|
local range = tostring(review.range or "")
|
|
local content = review.content
|
|
if range ~= "" and type(content) == "string" and content ~= "" then
|
|
out[range] = review
|
|
end
|
|
end
|
|
end
|
|
return out
|
|
end
|
|
|
|
---@param wire table|nil
|
|
---@return table<string, boolean>
|
|
function Notes.reviewIds(wire)
|
|
local out = {}
|
|
for _idx, entry in ipairs(type(wire) == "table" and wire.reviews or {}) do
|
|
local review = unwrapReview(entry)
|
|
local id = review and (review.reviewId or entry.reviewId) or nil
|
|
if id then out[tostring(id)] = true end
|
|
end
|
|
return out
|
|
end
|
|
|
|
--- 用完整远端想法快照回填 reviewId;新增请求已成功但本地未确认时可避免重复提交。
|
|
---@param annotations table[]
|
|
---@param wire table|nil
|
|
---@return table<table, boolean>
|
|
function Notes.reconcileReviews(annotations, wire)
|
|
local by_id, by_key, by_semantic = {}, {}, {}
|
|
for _idx, entry in ipairs(type(wire) == "table" and wire.reviews or {}) do
|
|
local review = unwrapReview(entry)
|
|
if review then
|
|
local id = review.reviewId or entry.reviewId
|
|
if id then by_id[tostring(id)] = review end
|
|
local range = tostring(review.range or "")
|
|
local content = review.content
|
|
local abstract = review.abstract or review.contextAbstract
|
|
if range ~= "" and type(content) == "string" and type(abstract) == "string" then
|
|
local key = table.concat({ range, abstract, content }, "\31")
|
|
if by_key[key] == nil then by_key[key] = review else by_key[key] = false end
|
|
local semantic = table.concat({ abstract, content }, "\31")
|
|
if by_semantic[semantic] == nil then
|
|
by_semantic[semantic] = review
|
|
else
|
|
by_semantic[semantic] = false
|
|
end
|
|
end
|
|
end
|
|
end
|
|
local confirmed = {}
|
|
for _idx, item in ipairs(annotations or {}) do
|
|
if type(item) == "table" and not item.wr_deleted
|
|
and type(item.note) == "string" and item.note ~= "" then
|
|
local review = item.wr_review_id and by_id[tostring(item.wr_review_id)] or nil
|
|
if not review and not item.wr_review_id and item.wr_range and item.text then
|
|
review = by_key[table.concat({
|
|
tostring(item.wr_range), item.text, item.note,
|
|
}, "\31")]
|
|
end
|
|
if not review and not item.wr_review_id and item.text then
|
|
review = by_semantic[table.concat({ item.text, item.note }, "\31")]
|
|
end
|
|
if review then
|
|
item.wr_review_id = review.reviewId
|
|
if item.wr_private ~= 0 and item.wr_private ~= 1 then
|
|
local private = Notes.privateOf(review)
|
|
if private ~= nil then item.wr_private = private end
|
|
end
|
|
if item.wr_friendship ~= 0 and item.wr_friendship ~= 1 then
|
|
local friendship = Notes.friendshipOf(review)
|
|
if friendship ~= nil then item.wr_friendship = friendship end
|
|
end
|
|
confirmed[item] = true
|
|
end
|
|
end
|
|
end
|
|
return confirmed
|
|
end
|
|
|
|
---@param wire table|nil
|
|
---@param chapter_uid string|number
|
|
---@return table<string, boolean>
|
|
function Notes.bookmarkIds(wire, chapter_uid)
|
|
local out = {}
|
|
for _idx, row in ipairs(type(wire) == "table" and wire.updated or {}) do
|
|
if type(row) == "table"
|
|
and tostring(row.chapterUid or row.chapter_uid or "") == tostring(chapter_uid)
|
|
and Notes.isHighlightRow(row) then
|
|
local id = row.bookmarkId or row.id
|
|
if id then out[tostring(id)] = true end
|
|
end
|
|
end
|
|
return out
|
|
end
|
|
|
|
--- bookmarklist wire → 通用 KOReader 注解(``chapter_idx`` 用于按章分片落库)。
|
|
---@param wire table
|
|
---@param chapter_uid string|nil 非 nil 时只返回该章
|
|
---@param reviews table|nil ``/review/list/mine`` wire
|
|
---@param source_id string|nil
|
|
---@param stable_id string|nil
|
|
---@return table[]
|
|
function Notes.toAnnotations(wire, chapter_uid, reviews, source_id, stable_id)
|
|
local items = (type(wire) == "table" and wire.updated) or {}
|
|
--- 划线行与想法条目共用的注解骨架。
|
|
--- bookmarklist 的 chapterIdx 是微信原始序号;本地目录过滤过封面和空章,
|
|
--- 只能按 uid 映射,映射不到就留空,不能把原始序号当本地章节号。
|
|
--- page/pos0/pos1 留空:开章后由 localizeAnnotations 按 wr_range 补
|
|
--- xpointer。数字占位会让 KOReader 误判整份注解为 mupdf 格式。
|
|
local function skeleton(row, uid)
|
|
local idx = source_id and stable_id and Toc.index(source_id, stable_id, uid) or nil
|
|
local ts = tonumber(row.createTime or row.updateTime) or os.time()
|
|
return {
|
|
datetime = os.date("%Y-%m-%d %H:%M:%S", ts),
|
|
datetime_updated = os.date("%Y-%m-%d %H:%M:%S", ts),
|
|
drawer = "lighten",
|
|
color = Notes.localColor(row.colorStyle),
|
|
chapter = row.chapterTitle or "",
|
|
chapter_idx = idx,
|
|
}
|
|
end
|
|
local review_by_range = Notes.reviewsByRange(reviews)
|
|
local matched_reviews = {}
|
|
local out = {}
|
|
for _idx, row in ipairs(items) do
|
|
if type(row) == "table" and Notes.isHighlightRow(row) then
|
|
local uid = tostring(row.chapterUid or row.chapter_uid or "")
|
|
if not chapter_uid or uid == tostring(chapter_uid) then
|
|
local range = tostring(row.range or "")
|
|
local review = review_by_range[range]
|
|
if review then matched_reviews[review] = true end
|
|
local ann = skeleton(row, uid)
|
|
ann.text = Notes.decodeMarkText(row.markText or row.bookmarkText or row.text or "")
|
|
ann.note = review and review.content or nil
|
|
ann.wr_range = range ~= "" and range or nil
|
|
ann.wr_bookmark_id = row.bookmarkId or row.id
|
|
ann.wr_review_id = review and review.reviewId or nil
|
|
local private = review and Notes.privateOf(review) or nil
|
|
if private ~= nil then ann.wr_private = private end
|
|
local friendship = review and Notes.friendshipOf(review) or nil
|
|
if friendship ~= nil then ann.wr_friendship = friendship end
|
|
out[#out + 1] = ann
|
|
end
|
|
end
|
|
end
|
|
-- 有想法不等于 bookmarklist 必有同 range 划线。微信允许直接发表选中文本想法;
|
|
-- 这种条目也必须映射为 KOReader 原生高亮+笔记,不能静默丢掉。
|
|
for _idx, entry in ipairs(type(reviews) == "table" and reviews.reviews or {}) do
|
|
local review = unwrapReview(entry)
|
|
if review and not matched_reviews[review] then
|
|
local uid = tostring(review.chapterUid or review.chapter_uid or "")
|
|
local range = tostring(review.range or "")
|
|
local text = review.abstract or review.contextAbstract
|
|
local content = review.content
|
|
if range ~= "" and type(text) == "string" and text ~= ""
|
|
and type(content) == "string" and content ~= ""
|
|
and (not chapter_uid or uid == tostring(chapter_uid)) then
|
|
local ann = skeleton(review, uid)
|
|
ann.text = text
|
|
ann.note = content
|
|
ann.wr_range = range
|
|
ann.wr_review_id = review.reviewId or entry.reviewId
|
|
local private = Notes.privateOf(review)
|
|
if private ~= nil then ann.wr_private = private end
|
|
local friendship = Notes.friendshipOf(review)
|
|
if friendship ~= nil then ann.wr_friendship = friendship end
|
|
ann.wr_review_only = true
|
|
out[#out + 1] = ann
|
|
end
|
|
end
|
|
end
|
|
return out
|
|
end
|
|
|
|
--- 想法列表没拿到时,拉回来的划线都不带想法:把本地快照里已有的想法接回去,
|
|
--- 否则远端快照覆盖本地、想法丢失,下一轮上行还会把它当成「用户删了想法」去删云端。
|
|
---@param annotations table[] toAnnotations 的结果(就地改)
|
|
---@param local_items table[] 本地快照里的条目
|
|
---@return table[]
|
|
function Notes.keepLocalReviews(annotations, local_items)
|
|
local by_bookmark, known = {}, {}
|
|
for _idx, ann in ipairs(annotations) do
|
|
if ann.wr_bookmark_id then by_bookmark[tostring(ann.wr_bookmark_id)] = ann end
|
|
if ann.wr_review_id then known[tostring(ann.wr_review_id)] = true end
|
|
end
|
|
for _idx, old in ipairs(local_items) do
|
|
if type(old) == "table" and old.wr_review_id and not known[tostring(old.wr_review_id)] then
|
|
local ann = old.wr_bookmark_id and by_bookmark[tostring(old.wr_bookmark_id)] or nil
|
|
if ann then
|
|
ann.note, ann.wr_review_id = old.note, old.wr_review_id
|
|
ann.wr_private, ann.wr_friendship = old.wr_private, old.wr_friendship
|
|
elseif old.wr_review_only then
|
|
annotations[#annotations + 1] = old
|
|
end
|
|
end
|
|
end
|
|
return annotations
|
|
end
|
|
|
|
---@param item table
|
|
---@return boolean
|
|
local function hasNote(item)
|
|
return type(item.note) == "string" and item.note ~= ""
|
|
end
|
|
|
|
--- 上行前整理:KOReader 原生 note 对应微信想法,标 wr_review_only,不先制造一条远端 bookmark。
|
|
--- 返回有没有要推的(删除、改样式、改想法、新划线、新想法)。
|
|
---@param annotations table[] 就地改写
|
|
---@return boolean
|
|
function Notes.preparePush(annotations)
|
|
local has_work = false
|
|
for _idx, item in ipairs(annotations) do
|
|
local note = hasNote(item)
|
|
if note and not item.wr_bookmark_id and not item.wr_review_id then
|
|
item.wr_review_only = true
|
|
end
|
|
if item.wr_deleted or item.wr_delete_review or item.wr_update_bookmark or item.wr_update_review
|
|
or (item.drawer and not item.wr_bookmark_id and not note)
|
|
or (note and not item.wr_review_id) then
|
|
has_work = true
|
|
end
|
|
end
|
|
return has_work
|
|
end
|
|
|
|
--- 需要取章节坐标的条目:待传新划线,加上还没确认远端划线的待传想法。
|
|
---@param annotations table[]
|
|
---@param confirmed table<table, boolean> 本轮已在远端确认的条目
|
|
---@return table[]
|
|
function Notes.rangeItems(annotations, confirmed)
|
|
local out = Notes.pushCandidates(annotations)
|
|
for _idx, item in ipairs(annotations) do
|
|
if hasNote(item) and not item.wr_review_id
|
|
and (item.wr_review_only or (item.wr_bookmark_id and not confirmed[item])) then
|
|
out[#out + 1] = item
|
|
end
|
|
end
|
|
return out
|
|
end
|
|
|
|
--- 想法列表只取一页;只有确认是全量时,“不在列表里”才等于“云端已删”。
|
|
---@param reviews table|nil
|
|
---@return boolean
|
|
function Notes.reviewsComplete(reviews)
|
|
if type(reviews) ~= "table" then return false end
|
|
local total_count = tonumber(reviews.totalCount)
|
|
return tonumber(reviews.hasMore or 0) ~= 1
|
|
and (not total_count or total_count <= #(reviews.reviews or {}))
|
|
end
|
|
|
|
--- 待上传的划线想法(划线已登记远端 id)。
|
|
---@param annotations table[]
|
|
---@return table[]
|
|
function Notes.notePushCandidates(annotations)
|
|
local out = {}
|
|
for _idx, item in ipairs(annotations) do
|
|
if hasNote(item)
|
|
and ((item.wr_review_id and item.wr_update_review)
|
|
or (not item.wr_review_id
|
|
and (item.wr_bookmark_id or item.wr_review_only)
|
|
and type(item.wr_range) == "string" and item.wr_range ~= "")) then
|
|
out[#out + 1] = item
|
|
end
|
|
end
|
|
return out
|
|
end
|
|
|
|
---@param annotations table[]
|
|
---@return table[]
|
|
function Notes.deleteCandidates(annotations)
|
|
local out = {}
|
|
for _idx, item in ipairs(annotations) do
|
|
if item.wr_deleted or item.wr_delete_review then
|
|
out[#out + 1] = item
|
|
end
|
|
end
|
|
return out
|
|
end
|
|
|
|
---@param annotations table[]
|
|
---@return table[]
|
|
function Notes.bookmarkUpdateCandidates(annotations)
|
|
local out = {}
|
|
for _idx, item in ipairs(annotations) do
|
|
if not item.wr_deleted and item.wr_bookmark_id and item.wr_update_bookmark then
|
|
out[#out + 1] = item
|
|
end
|
|
end
|
|
return out
|
|
end
|
|
|
|
---@param annotation table
|
|
---@return table
|
|
function Notes.toBookmarkUpdateBody(annotation)
|
|
return {
|
|
bookmarkId = tostring(annotation.wr_bookmark_id),
|
|
style = 1,
|
|
colorStyle = Notes.colorStyle(annotation.color),
|
|
}
|
|
end
|
|
|
|
--- KOReader 注解 → ``/review/add`` 请求体。
|
|
--- 新想法按设置项;没设过时私有。已有想法沿用记下来的范围。
|
|
---@param book_id string
|
|
---@param chapter_uid string|number
|
|
---@param annotation table
|
|
---@param book_secret integer|boolean|nil
|
|
---@param choice string|nil public / private / follow
|
|
---@return table|nil body
|
|
---@return string|nil err
|
|
function Notes.toReviewBody(book_id, chapter_uid, annotation, book_secret, choice)
|
|
local content = annotation.note
|
|
if type(content) ~= "string" or content == "" then
|
|
return nil, "empty note"
|
|
end
|
|
local range = annotation.wr_range
|
|
if type(range) ~= "string" or range == "" then
|
|
return nil, "missing range"
|
|
end
|
|
local abstract = annotation.text
|
|
if type(abstract) ~= "string" or abstract == "" then
|
|
return nil, "missing highlight text"
|
|
end
|
|
local body = {
|
|
bookId = tostring(book_id),
|
|
content = content,
|
|
type = 1,
|
|
range = range,
|
|
abstract = abstract,
|
|
chapterUid = tonumber(chapter_uid) or chapter_uid,
|
|
}
|
|
local private, friendship = Notes.reviewFlags(annotation, book_secret, choice)
|
|
if private ~= nil then body.isPrivate = private end
|
|
if friendship ~= nil then body.friendship = friendship end
|
|
return body, nil
|
|
end
|
|
|
|
--- KOReader 注解 → ``/review/useredit`` 请求体。
|
|
---@param book_id string
|
|
---@param chapter_uid string|number
|
|
---@param annotation table
|
|
---@param book_secret integer|boolean|nil
|
|
---@param choice string|nil
|
|
---@return table|nil body
|
|
---@return string|nil err
|
|
function Notes.toReviewEditBody(book_id, chapter_uid, annotation, book_secret, choice)
|
|
local review_id = annotation.wr_review_id
|
|
if type(review_id) ~= "string" and type(review_id) ~= "number" then
|
|
return nil, "missing review id"
|
|
end
|
|
local body, err = Notes.toReviewBody(book_id, chapter_uid, annotation, book_secret, choice)
|
|
if not body then return nil, err end
|
|
body.reviewId = tostring(review_id)
|
|
return body, nil
|
|
end
|
|
|
|
--- 待上传的本地划线(跳过已登记远端 id)。
|
|
---@param annotations table[]
|
|
---@return table[]
|
|
function Notes.pushCandidates(annotations)
|
|
local out = {}
|
|
for _idx, item in ipairs(annotations) do
|
|
if item.drawer and type(item.text) == "string" and item.text ~= "" and not hasNote(item)
|
|
and not item.wr_bookmark_id and not item.wr_review_id then
|
|
out[#out + 1] = item
|
|
end
|
|
end
|
|
return out
|
|
end
|
|
|
|
--- 用 bookmarklist 的 canonical 字段回填本地注解。
|
|
--- 已有 id 优先;无 id 时只接受 ``text + 当前 wire range`` 精确匹配,避免同文句串线。
|
|
--- 按 range 找远端划线 id:只有想法的条目没记过 bookmarkId,删除时靠它连带删掉划线。
|
|
---@param wire table|nil
|
|
---@param chapter_uid string|number
|
|
---@param range string
|
|
---@return string|number|nil
|
|
function Notes.bookmarkIdByRange(wire, chapter_uid, range)
|
|
for _idx, row in ipairs(type(wire) == "table" and wire.updated or {}) do
|
|
if type(row) == "table"
|
|
and tostring(row.chapterUid or row.chapter_uid or "") == tostring(chapter_uid)
|
|
and tostring(row.range or "") == range and Notes.isHighlightRow(row) then
|
|
return row.bookmarkId or row.id
|
|
end
|
|
end
|
|
end
|
|
|
|
---@param annotations table[]
|
|
---@param wire table|nil
|
|
---@param chapter_uid string|number
|
|
---@return table<table, boolean> confirmed
|
|
function Notes.reconcileBookmarks(annotations, wire, chapter_uid)
|
|
local by_id, by_key = {}, {}
|
|
for _idx, row in ipairs(type(wire) == "table" and wire.updated or {}) do
|
|
local uid = tostring(row.chapterUid or row.chapter_uid or "")
|
|
if uid == tostring(chapter_uid) and Notes.isHighlightRow(row) then
|
|
local id = row.bookmarkId or row.id
|
|
local range = tostring(row.range or "")
|
|
local text = Notes.decodeMarkText(row.markText or row.bookmarkText or row.text or "")
|
|
if id then by_id[tostring(id)] = row end
|
|
if text ~= "" and range ~= "" then
|
|
local key = text .. "\31" .. range
|
|
if by_key[key] == nil then
|
|
by_key[key] = row
|
|
else
|
|
by_key[key] = false
|
|
end
|
|
end
|
|
end
|
|
end
|
|
local confirmed = {}
|
|
for _idx, item in ipairs(annotations or {}) do
|
|
if type(item) == "table" then
|
|
local row = item.wr_bookmark_id and by_id[tostring(item.wr_bookmark_id)] or nil
|
|
if not row and not item.wr_bookmark_id and item.text and item.wr_range then
|
|
row = by_key[item.text .. "\31" .. tostring(item.wr_range)]
|
|
end
|
|
if row then
|
|
item.wr_bookmark_id = row.bookmarkId or row.id
|
|
item.wr_range = tostring(row.range)
|
|
confirmed[item] = true
|
|
end
|
|
end
|
|
end
|
|
return confirmed
|
|
end
|
|
|
|
--- KOReader 注解 → addBookmark 请求体。
|
|
---@param book_id string
|
|
---@param chapter_uid string|number
|
|
---@param chapter_idx integer
|
|
---@param book_version number
|
|
---@param annotation table
|
|
---@return table|nil body
|
|
---@return string|nil err
|
|
function Notes.toBookmarkBody(book_id, chapter_uid, chapter_idx, book_version, annotation)
|
|
local text = annotation.text
|
|
if type(text) ~= "string" or text == "" then
|
|
return nil, "empty highlight"
|
|
end
|
|
local range = annotation.wr_range
|
|
if type(range) ~= "string" or range == "" then
|
|
return nil, "无法定位划线范围"
|
|
end
|
|
return {
|
|
bookId = tostring(book_id),
|
|
chapterUid = tonumber(chapter_uid) or chapter_uid,
|
|
chapterIdx = chapter_idx,
|
|
bookVersion = book_version,
|
|
type = 1,
|
|
style = 1,
|
|
colorStyle = Notes.colorStyle(annotation.color),
|
|
range = range,
|
|
markText = text,
|
|
}, nil
|
|
end
|
|
|
|
return Notes
|