From 352ee1bd99fa06960a4fcc2094c4b7185b345f66 Mon Sep 17 00:00:00 2001 From: Garfield Date: Tue, 1 Dec 2026 12:47:12 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20(notes):=20=E6=83=B3=E6=B3=95=E6=8C=89?= =?UTF-8?q?=E7=A7=81=E5=AF=86=E9=98=85=E8=AF=BB=E8=AE=BE=E7=BD=AE=E5=8F=91?= =?UTF-8?q?=E8=A1=A8=E5=B9=B6=E5=9B=9E=E8=AF=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新想法跟这本书的私密阅读走,修改已有想法沿用原来的可见范围。 回读对不上就当这轮失败,不把 HTTP 成功当成已经私密。 --- moonsource-weread.koplugin/README.md | 2 +- .../moonsource_weread/chapter.lua | 5 +- .../moonsource_weread/context.lua | 20 +++ .../moonsource_weread/l10n/en.lua | 2 + .../moonsource_weread/l10n/zh_TW.lua | 2 + .../moonsource_weread/mapper.lua | 13 ++ .../moonsource_weread/notes.lua | 58 ++++++-- .../moonsource_weread/notes_push.lua | 134 ++++++++++++------ tests/weread/notes_push_spec.lua | 125 +++++++++++++++- tests/weread/sync_spec.lua | 43 ++++++ tests/weread/wechat_spec.lua | 12 ++ 11 files changed, 359 insertions(+), 57 deletions(-) diff --git a/moonsource-weread.koplugin/README.md b/moonsource-weread.koplugin/README.md index e7e4dc6..35fec8b 100644 --- a/moonsource-weread.koplugin/README.md +++ b/moonsource-weread.koplugin/README.md @@ -11,7 +11,7 @@ | 阅读进度同步 | 支持 | 开书比较云端位置;本地先落盘,阅读中合并推送,离线保留待同步数据 | | 阅读时长同步 | 支持 | 上传本地采集时长及补报数据 | | 维持官网「在读」状态 | 已实现,未实测 | 零时长 Web 心跳,不重复增加阅读时长 | -| 划线与想法同步 | 支持 | 走属主书籍的注解接口 | +| 划线与想法同步 | 支持 | 新想法按这本书的私密阅读设置带 isPrivate;修改已有想法沿用原来的可见范围,并回读确认 | | 整书书评与独立章评 | 不支持 | 未接入可验证接口,不能把其他书籍的想法当作属主书评 | | 段评与回复 | 支持 | 按章节及文本范围定位,使用公共评论标记和列表 | | 离线阅读 | 支持 | 已下载内容和本地记录可用;联网后再同步 | diff --git a/moonsource-weread.koplugin/moonsource_weread/chapter.lua b/moonsource-weread.koplugin/moonsource_weread/chapter.lua index 92a85c3..d89812c 100644 --- a/moonsource-weread.koplugin/moonsource_weread/chapter.lua +++ b/moonsource-weread.koplugin/moonsource_weread/chapter.lua @@ -22,7 +22,6 @@ local Toc = require("moonsource_weread.toc") local Client = require("moonsource_weread.client") local Mem = require("utils.mem") local Context = require("moonsource_weread.context") -local Mapper = require("moonsource_weread.mapper") local _ = require("l10n").get("moonsource_weread") local Chapter = {} @@ -74,12 +73,12 @@ local function downloadPackageAsync(bookId, chapters, book_type, pick, cb, opts) fail(err) return end - local version = Mapper.bookVersion(wire) + Context.rememberBook(bookId, wire) + local version = Context.bookVersion(bookId) if not version then fail(_("缺少书籍版本")) return end - Context.rememberBookVersion(bookId, version) download(attempt, version) end) if not fired then call = next_call end diff --git a/moonsource-weread.koplugin/moonsource_weread/context.lua b/moonsource-weread.koplugin/moonsource_weread/context.lua index 5bbb6db..ed56fc5 100644 --- a/moonsource-weread.koplugin/moonsource_weread/context.lua +++ b/moonsource-weread.koplugin/moonsource_weread/context.lua @@ -7,6 +7,7 @@ App 进度与时长走 App ``POST /book/read``;Web 在读状态用独立的网 --]] local Protocol = require("moonsource_weread.protocol") +local Mapper = require("moonsource_weread.mapper") local Context = {} @@ -24,6 +25,8 @@ local readers = {} local sessions = {} ---@type table local book_version_by_id = {} +---@type table +local book_secret_by_id = {} ---@class WechatReportSession ---@field psvts string @@ -88,17 +91,34 @@ function Context.rememberBookVersion(book_id, version) end end +--- 从书籍详情记下版本号和私密阅读。认不出的字段不覆盖已有值。 +---@param book_id string +---@param wire table|nil +function Context.rememberBook(book_id, wire) + Context.rememberBookVersion(book_id, Mapper.bookVersion(wire)) + local secret = Mapper.bookSecret(wire) + if secret ~= nil then book_secret_by_id[book_id] = secret end +end + ---@param book_id string ---@return number|nil function Context.bookVersion(book_id) return book_version_by_id[book_id] end +--- 这本书是否私密阅读。没读过详情是 nil,不是公开。 +---@param book_id string +---@return integer|nil +function Context.bookSecret(book_id) + return book_secret_by_id[book_id] +end + --- 清空进程内缓存的 reader 状态与书籍版本号(换账号或登出后必须调)。 function Context.clear() readers = {} sessions = {} book_version_by_id = {} + book_secret_by_id = {} end return Context diff --git a/moonsource-weread.koplugin/moonsource_weread/l10n/en.lua b/moonsource-weread.koplugin/moonsource_weread/l10n/en.lua index ac8dea7..e64c636 100644 --- a/moonsource-weread.koplugin/moonsource_weread/l10n/en.lua +++ b/moonsource-weread.koplugin/moonsource_weread/l10n/en.lua @@ -24,6 +24,8 @@ return { ["试读结束"] = "Trial ended", ["想法上传失败"] = "Failed to upload note", ["想法上传成功但缺少 reviewId"] = "Note upload succeeded without a reviewId", + ["无法确认想法是否私密"] = "Cannot confirm whether the note is private", + ["想法的可见范围没有按设置保存"] = "The note visibility was not saved as requested", ["按章书籍请打开章节后同步划线"] = "Open a chapter first to sync highlights", ["无法定位划线范围"] = "Cannot locate highlight range", ["无法开始登录"] = "Cannot start login", diff --git a/moonsource-weread.koplugin/moonsource_weread/l10n/zh_TW.lua b/moonsource-weread.koplugin/moonsource_weread/l10n/zh_TW.lua index 0f4de8a..9f3b9bd 100644 --- a/moonsource-weread.koplugin/moonsource_weread/l10n/zh_TW.lua +++ b/moonsource-weread.koplugin/moonsource_weread/l10n/zh_TW.lua @@ -24,6 +24,8 @@ return { ["试读结束"] = "試讀結束", ["想法上传失败"] = "想法上傳失敗", ["想法上传成功但缺少 reviewId"] = "想法上傳成功但缺少 reviewId", + ["无法确认想法是否私密"] = "無法確認想法是否私密", + ["想法的可见范围没有按设置保存"] = "想法的可見範圍沒有按設定保存", ["按章书籍请打开章节后同步划线"] = "分章書籍請開啟章節後再同步劃線", ["无法定位划线范围"] = "無法定位劃線範圍", ["无法开始登录"] = "無法開始登入", diff --git a/moonsource-weread.koplugin/moonsource_weread/mapper.lua b/moonsource-weread.koplugin/moonsource_weread/mapper.lua index 5e581c1..b1e2ddb 100644 --- a/moonsource-weread.koplugin/moonsource_weread/mapper.lua +++ b/moonsource-weread.koplugin/moonsource_weread/mapper.lua @@ -126,6 +126,19 @@ function Mapper.bookVersion(wire) return tonumber(book.version or book.bookVersion or wire.version or wire.bookVersion) end +--- 书籍详情 wire → 私密阅读。``secret==1`` 为 1,明确不是私密阅读为 0,认不出书就 nil。 +---@param wire table|nil +---@return integer|nil +function Mapper.bookSecret(wire) + if type(wire) ~= "table" then return nil end + local book = type(wire.book) == "table" and wire.book or wire + if book.secret == nil and book.bookId == nil and book.version == nil and book.bookVersion == nil then + return nil + end + if book.secret == 1 or book.secret == true then return 1 end + return 0 +end + --- 微信专辑 → Book(及封面 URL)。 ---@param album table ---@return Book|nil, string|nil diff --git a/moonsource-weread.koplugin/moonsource_weread/notes.lua b/moonsource-weread.koplugin/moonsource_weread/notes.lua index fe44ac2..0595e4b 100644 --- a/moonsource-weread.koplugin/moonsource_weread/notes.lua +++ b/moonsource-weread.koplugin/moonsource_weread/notes.lua @@ -33,7 +33,7 @@ local LOCAL_COLOR = { } local WR_FIELDS = { - "wr_range", "wr_bookmark_id", "wr_review_id", "wr_review_only", + "wr_range", "wr_bookmark_id", "wr_review_id", "wr_review_only", "wr_private", "wr_delete_review", "wr_update_review", "wr_update_bookmark", } @@ -47,6 +47,7 @@ local WR_FIELDS = { ---@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_deleted boolean|nil ---@field chapter_idx number|nil ---@field datetime string|nil @@ -76,6 +77,31 @@ local function unwrapReview(entry) 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 + +--- 发表/修改要带的 ``isPrivate``。已有想法没记过可见范围时返回 nil,请求里就不带这个字段。 +---@param annotation table +---@param book_secret integer|boolean|nil 书籍 ``secret==1`` 表示私密阅读 +---@return integer|nil +function Notes.reviewIsPrivate(annotation, book_secret) + local known = annotation.wr_private + if known == 1 or known == true then return 1 end + if known == 0 or known == false then return 0 end + if annotation.wr_review_id ~= nil then return nil end + if book_secret == 1 or book_secret == true then return 1 end + return 0 +end + ---@param items table[]|nil ---@param total_pages integer|nil ---@return table[] @@ -452,6 +478,10 @@ function Notes.reconcileReviews(annotations, wire) 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 confirmed[item] = true end end @@ -517,6 +547,8 @@ function Notes.toAnnotations(wire, chapter_uid, reviews, source_id, stable_id) 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 out[#out + 1] = ann end end @@ -538,6 +570,8 @@ function Notes.toAnnotations(wire, chapter_uid, reviews, source_id, stable_id) 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 ann.wr_review_only = true out[#out + 1] = ann end @@ -561,7 +595,7 @@ function Notes.keepLocalReviews(annotations, local_items) 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.note, ann.wr_review_id, ann.wr_private = old.note, old.wr_review_id, old.wr_private elseif old.wr_review_only then annotations[#annotations + 1] = old end @@ -672,13 +706,15 @@ function Notes.toBookmarkUpdateBody(annotation) } end ---- KOReader 注解 → ``/web/review/add`` 请求体。 +--- KOReader 注解 → ``/review/add`` 请求体。 +--- 新想法:私密阅读的书 ``isPrivate=1``,否则 ``0``。已有想法沿用 ``wr_private``。 ---@param book_id string ---@param chapter_uid string|number ---@param annotation table +---@param book_secret integer|boolean|nil ---@return table|nil body ---@return string|nil err -function Notes.toReviewBody(book_id, chapter_uid, annotation) +function Notes.toReviewBody(book_id, chapter_uid, annotation, book_secret) local content = annotation.note if type(content) ~= "string" or content == "" then return nil, "empty note" @@ -691,28 +727,32 @@ function Notes.toReviewBody(book_id, chapter_uid, annotation) if type(abstract) ~= "string" or abstract == "" then return nil, "missing highlight text" end - return { + local body = { bookId = tostring(book_id), content = content, type = 1, range = range, abstract = abstract, chapterUid = tonumber(chapter_uid) or chapter_uid, - }, nil + } + local private = Notes.reviewIsPrivate(annotation, book_secret) + if private ~= nil then body.isPrivate = private end + return body, nil end ---- KOReader 注解 → ``/web/review/edit`` 请求体。 +--- KOReader 注解 → ``/review/useredit`` 请求体。 ---@param book_id string ---@param chapter_uid string|number ---@param annotation table +---@param book_secret integer|boolean|nil ---@return table|nil body ---@return string|nil err -function Notes.toReviewEditBody(book_id, chapter_uid, annotation) +function Notes.toReviewEditBody(book_id, chapter_uid, annotation, book_secret) 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) + local body, err = Notes.toReviewBody(book_id, chapter_uid, annotation, book_secret) if not body then return nil, err end body.reviewId = tostring(review_id) return body, nil diff --git a/moonsource-weread.koplugin/moonsource_weread/notes_push.lua b/moonsource-weread.koplugin/moonsource_weread/notes_push.lua index a86882b..a37482c 100644 --- a/moonsource-weread.koplugin/moonsource_weread/notes_push.lua +++ b/moonsource-weread.koplugin/moonsource_weread/notes_push.lua @@ -13,7 +13,6 @@ local Annotations = require("book.annotations") local Toc = require("moonsource_weread.toc") local Position = require("moonsource_weread.progress") local Context = require("moonsource_weread.context") -local Mapper = require("moonsource_weread.mapper") local _ = require("l10n").get("moonsource_weread") ---@class WechatNotesPush @@ -263,12 +262,12 @@ function Push:withBookVersion(candidates) self:call(function(done) return self.client:bookInfoAsync(self.book_id, done) end, nil, function(wire) - version = Mapper.bookVersion(wire) + Context.rememberBook(self.book_id, wire) + version = Context.bookVersion(self.book_id) if not version then self:finish(nil, _("缺少书籍版本")) return end - Context.rememberBookVersion(self.book_id, version) self:bookmarks(version, candidates) end) end @@ -329,56 +328,107 @@ function Push:updates() end, function() self:reviews() end) end ---- 串行推想法:已有 reviewId 走 edit,否则 add。 +--- 发表或修改之后读回 ``review.isPrivate``。对不上就停,不把 HTTP 成功当成私密已生效。 +---@param item table +---@param wanted integer +---@param next fun() +function Push:confirmPrivate(item, wanted, next) + item.wr_private = wanted + item.wr_update_review = true + self:call(function(done) + return self.client:reviewSingleAsync(item.wr_review_id, 0, 1, done) + end, _("无法确认想法是否私密"), function(wire) + local got = Notes.privateOf(wire) + if got == nil then + self:finish(nil, _("无法确认想法是否私密")) + return + end + item.wr_private = got + if got ~= wanted then + item.wr_update_review = nil + self:finish(nil, _("想法的可见范围没有按设置保存")) + return + end + item.wr_update_review = nil + next() + end) +end + +--- 串行推想法:已有 reviewId 走 edit,否则 add。新想法先读这本书是不是私密阅读。 function Push:reviews() - local deferred = false - self:each(Notes.notePushCandidates(self.annotations), function(item, next) - if not item.wr_review_id and self.reviews_unknown then - deferred = true - next() - return - end - if not item.wr_review_id and not self.confirmed[item] and not item.wr_review_only then - self:finish(nil, _("远端划线未确认,不能上传想法")) - return - end - if item.wr_review_id then - local body, body_err = Notes.toReviewEditBody(self.book_id, self.chapter_uid, item) + local function submit() + local deferred = false + local secret = Context.bookSecret(self.book_id) + self:each(Notes.notePushCandidates(self.annotations), function(item, next) + if not item.wr_review_id and self.reviews_unknown then + deferred = true + next() + return + end + if not item.wr_review_id and not self.confirmed[item] and not item.wr_review_only then + self:finish(nil, _("远端划线未确认,不能上传想法")) + return + end + local body, body_err + local send + if item.wr_review_id then + body, body_err = Notes.toReviewEditBody(self.book_id, self.chapter_uid, item, secret) + send = function(done) return self.client:editReviewAsync(body, done) end + else + body, body_err = Notes.toReviewBody(self.book_id, self.chapter_uid, item, secret) + send = function(done) return self.client:addReviewAsync(body, done) end + end if not body then self:finish(nil, body_err) return end - self:call(function(done) - return self.client:editReviewAsync(body, done) - end, _("想法上传失败"), function() - item.wr_update_review = nil - next() + self:call(send, _("想法上传失败"), function(wire) + if not item.wr_review_id then + local review_id = wire.reviewId or (type(wire.data) == "table" and wire.data.reviewId) + if not review_id then + self:finish(nil, _("想法上传成功但缺少 reviewId")) + return + end + item.wr_review_id = review_id + end + if body.isPrivate == nil then + item.wr_update_review = nil + next() + return + end + self:confirmPrivate(item, body.isPrivate, next) end) - return - end - local body, body_err = Notes.toReviewBody(self.book_id, self.chapter_uid, item) - if not body then - self:finish(nil, body_err) - return + end, function() + if deferred then + self:finish(nil, _("想法列表暂不可用,稍后重试")) + else + self:finish(true) + end + end) + end + local needs_secret = false + if not self.reviews_unknown then + for _idx, item in ipairs(Notes.notePushCandidates(self.annotations)) do + if not item.wr_review_id and item.wr_private ~= 0 and item.wr_private ~= 1 then + needs_secret = true + break + end end + end + if needs_secret and Context.bookSecret(self.book_id) == nil then self:call(function(done) - return self.client:addReviewAsync(body, done) - end, _("想法上传失败"), function(wire) - local review_id = wire.reviewId or (type(wire.data) == "table" and wire.data.reviewId) - if not review_id then - self:finish(nil, _("想法上传成功但缺少 reviewId")) + return self.client:bookInfoAsync(self.book_id, done) + end, _("书籍详情为空"), function(wire) + Context.rememberBook(self.book_id, wire) + if Context.bookSecret(self.book_id) == nil then + self:finish(nil, _("书籍详情为空")) return end - item.wr_review_id = review_id - next() + submit() end) - end, function() - if deferred then - self:finish(nil, _("想法列表暂不可用,稍后重试")) - else - self:finish(true) - end - end) + return + end + submit() end --- 推送一章注解。 diff --git a/tests/weread/notes_push_spec.lua b/tests/weread/notes_push_spec.lua index 741b42a..e938349 100644 --- a/tests/weread/notes_push_spec.lua +++ b/tests/weread/notes_push_spec.lua @@ -13,12 +13,13 @@ local NotesPush = require("moonsource_weread.notes_push") local function fakeClient(replies) local calls = {} ---@type { calls: string[], [string]: any } - local client = { calls = calls } + local client = { calls = calls, sent = {} } local function method(name) return function(_self, ...) local args = { ... } local cb = args[#args] calls[#calls + 1] = name + client.sent[name] = args local reply = replies[name] require("ui/uimanager"):nextTick(function() if type(reply) == "function" then reply = reply() end @@ -29,7 +30,7 @@ local function fakeClient(replies) end for _idx, name in ipairs({ "removeBookmarkAsync", "deleteReviewAsync", "bookmarkListAsync", "myReviewsAsync", "addBookmarkAsync", "updateBookmarkAsync", "editReviewAsync", - "addReviewAsync", "bookInfoAsync" }) do + "addReviewAsync", "bookInfoAsync", "reviewSingleAsync" }) do client[name] = method(name) end return client @@ -166,3 +167,123 @@ do -- 想法清单拿不到:新想法不推(认不回上轮已成功的会 Assert.is_nil(item.wr_review_id) for _, name in ipairs(client.calls) do Assert.is_true(name ~= "addReviewAsync", "不得重复新增想法") end end + +do -- 私密阅读的新想法带 isPrivate=1,回读确认后才算成功 + require("moonsource_weread.context").clear() + local Toc = require("moonsource_weread.toc") + local Position = require("moonsource_weread.progress") + local old_read, old_load = Toc.read, Position.loadAsync + Toc.read = function() return { { uid = "c1" } } end + Position.loadAsync = function(_identity, _chapter, cb) + cb("

原文

", "html") + return nil + end + local item = { + drawer = "lighten", text = "原文", note = "只给自己看", + wr_range = "1-2", wr_review_only = true, + pos0 = "/html/body/p/text().0", pos1 = "/html/body/p/text().2", + } + local client = fakeClient({ + bookmarkListAsync = { updated = {} }, + myReviewsAsync = { reviews = {}, hasMore = 0, totalCount = 0 }, + bookInfoAsync = { book = { bookId = "b1", version = 1, secret = 1 } }, + addReviewAsync = { reviewId = "rv-priv" }, + reviewSingleAsync = { review = { reviewId = "rv-priv", isPrivate = 1 } }, + }) + local ok, err + NotesPush.run(client, identity, "c1", + "

原文

", { item }, function(o, e) ok, err = o, e end) + Stubs.flush() + Toc.read, Position.loadAsync = old_read, old_load + Assert.is_true(ok, tostring(err)) + Assert.eq(client.sent.addReviewAsync[1].isPrivate, 1) + Assert.eq(item.wr_review_id, "rv-priv") + Assert.eq(item.wr_private, 1) + Assert.is_nil(item.wr_update_review) +end + +do -- 回读不是私密:留下 reviewId,整轮失败,不再当成成功 + require("moonsource_weread.context").clear() + local Toc = require("moonsource_weread.toc") + local Position = require("moonsource_weread.progress") + local old_read, old_load = Toc.read, Position.loadAsync + Toc.read = function() return { { uid = "c1" } } end + Position.loadAsync = function(_identity, _chapter, cb) + cb("

原文

", "html") + return nil + end + local item = { + drawer = "lighten", text = "原文", note = "只给自己看", + wr_range = "1-2", wr_review_only = true, + pos0 = "/html/body/p/text().0", pos1 = "/html/body/p/text().2", + } + local client = fakeClient({ + bookmarkListAsync = { updated = {} }, + myReviewsAsync = { reviews = {}, hasMore = 0, totalCount = 0 }, + bookInfoAsync = { book = { bookId = "b1", version = 1, secret = 1 } }, + addReviewAsync = { reviewId = "rv-pub" }, + reviewSingleAsync = { review = { isPrivate = 0 } }, + }) + local ok, err + NotesPush.run(client, identity, "c1", + "

原文

", { item }, function(o, e) ok, err = o, e end) + Stubs.flush() + Toc.read, Position.loadAsync = old_read, old_load + Assert.is_nil(ok) + Assert.eq(err, "想法的可见范围没有按设置保存") + Assert.eq(item.wr_review_id, "rv-pub") + Assert.eq(item.wr_private, 0) + Assert.is_nil(item.wr_update_review) +end + +do -- 改已有想法:沿用拉下来的 isPrivate,不按书籍公开设置改掉 + require("moonsource_weread.context").clear() + local item = { + drawer = "lighten", text = "原文", note = "改过", + wr_range = "1-2", wr_bookmark_id = "bm1", wr_review_id = "rv1", + wr_private = 1, wr_update_review = true, + } + local client = fakeClient({ + bookmarkListAsync = { updated = { { + chapterUid = "c1", range = "1-2", bookmarkId = "bm1", markText = "原文", type = 1, + } } }, + myReviewsAsync = { reviews = { { review = { + reviewId = "rv1", isPrivate = 0, range = "1-2", abstract = "原文", content = "改过", + } } }, hasMore = 0, totalCount = 1 }, + editReviewAsync = { succ = 1 }, + reviewSingleAsync = { review = { isPrivate = 1 } }, + }) + local ok, err + NotesPush.run(client, identity, "c1", nil, { item }, function(o, e) ok, err = o, e end) + Stubs.flush() + Assert.is_true(ok, tostring(err)) + Assert.eq(client.sent.editReviewAsync[1].isPrivate, 1) + for _, name in ipairs(client.calls) do + Assert.is_true(name ~= "bookInfoAsync", "已有可见范围不必再读私密阅读设置") + end + Assert.is_nil(item.wr_update_review) +end + +do -- 没记过可见范围的旧想法:修改不带 isPrivate,也不回读 + require("moonsource_weread.context").clear() + local item = { + drawer = "lighten", text = "原文", note = "改过", + wr_range = "1-2", wr_bookmark_id = "bm1", wr_review_id = "rv1", + wr_update_review = true, + } + local client = fakeClient({ + bookmarkListAsync = { updated = { { + chapterUid = "c1", range = "1-2", bookmarkId = "bm1", markText = "原文", type = 1, + } } }, + myReviewsAsync = { reviews = { { review = { reviewId = "rv1" } } }, hasMore = 0, totalCount = 1 }, + editReviewAsync = { succ = 1 }, + }) + local ok, err + NotesPush.run(client, identity, "c1", nil, { item }, function(o, e) ok, err = o, e end) + Stubs.flush() + Assert.is_true(ok, tostring(err)) + Assert.is_nil(client.sent.editReviewAsync[1].isPrivate) + for _, name in ipairs(client.calls) do + Assert.is_true(name ~= "reviewSingleAsync") + end +end diff --git a/tests/weread/sync_spec.lua b/tests/weread/sync_spec.lua index dabd2ac..94e76f3 100644 --- a/tests/weread/sync_spec.lua +++ b/tests/weread/sync_spec.lua @@ -357,6 +357,49 @@ do Assert.eq(review_body.abstract, "原文") Assert.eq(review_body.range, "0-5") Assert.eq(review_body.chapterUid, 9) + Assert.eq(review_body.isPrivate, 0, "非私密阅读的新想法明确公开") + local secret_body = Notes.toReviewBody("1", 9, { + note = "想法", text = "原文", wr_range = "0-5", + }, 1) + Assert.eq(secret_body.isPrivate, 1) + local kept = Notes.toReviewBody("1", 9, { + note = "想法", text = "原文", wr_range = "0-5", wr_private = 1, + }, 0) + Assert.eq(kept.isPrivate, 1, "条目自己的可见范围优先于书籍设置") + local edit_unknown = Notes.toReviewEditBody("1", 9, { + note = "想法", text = "原文", wr_range = "0-5", wr_review_id = "rv1", + }, 1) + Assert.is_nil(edit_unknown.isPrivate, "没记过可见范围的旧想法不能在修改时改成公开或私密") + local edit_private = Notes.toReviewEditBody("1", 9, { + note = "想法", text = "原文", wr_range = "0-5", wr_review_id = "rv1", wr_private = 1, + }, 0) + Assert.eq(edit_private.isPrivate, 1) + Assert.eq(edit_private.reviewId, "rv1") + local pulled_private = Notes.toAnnotations({ updated = {} }, "9", { + reviews = { { review = { + reviewId = "rv-p", chapterUid = "9", range = "0-1", + abstract = "原文", content = "私密", isPrivate = 1, + } } }, + }) + Assert.eq(pulled_private[1].wr_private, 1) + local reconciled = { note = "想法", text = "原文", wr_range = "0-5", wr_review_id = "rv1" } + Notes.reconcileReviews({ reconciled }, { + reviews = { { review = { reviewId = "rv1", isPrivate = 1, range = "0-5", abstract = "原文", content = "想法" } } }, + }) + Assert.eq(reconciled.wr_private, 1) + local pending_private = { + note = "想法", text = "原文", wr_range = "0-5", wr_review_id = "rv1", wr_private = 0, + } + Notes.reconcileReviews({ pending_private }, { + reviews = { { review = { reviewId = "rv1", isPrivate = 1 } } }, + }) + Assert.eq(pending_private.wr_private, 0, "本地已记下的可见范围不被列表覆盖") + Assert.eq(Mapper.bookSecret({ book = { bookId = "1", secret = 1 } }), 1) + Assert.eq(Mapper.bookSecret({ book = { bookId = "1", version = 3 } }), 0) + Assert.is_nil(Mapper.bookSecret({})) + Assert.eq(Notes.privateOf({ review = { isPrivate = 1 } }), 1) + Assert.eq(Notes.privateOf({ review = { isPrivate = 0 } }), 0) + Assert.is_nil(Notes.privateOf({ review = {} })) local pending = { text = "原文", note = "想法", wr_range = "0-5", wr_review_only = true, } diff --git a/tests/weread/wechat_spec.lua b/tests/weread/wechat_spec.lua index 08d20bd..7162367 100644 --- a/tests/weread/wechat_spec.lua +++ b/tests/weread/wechat_spec.lua @@ -76,6 +76,9 @@ stub("book.books_db", function() local m = remembered_stats_books.marked_deleted return m and not remembered_stats_books.removed and { m } or {} end, + hasUnsynced = function() + return remembered_stats_books.marked_deleted ~= nil and not remembered_stats_books.removed + end, markSynced = function() return true end, setRead = function(source_id, stable_id, is_read) remembered_stats_books.set_read = { @@ -95,6 +98,7 @@ stub("book.progress_db", function() get = function() return existing_progress end, upsertRemote = function() return true end, unsynced = function() return {} end, + hasUnsynced = function() return false end, } end) stub("book.chapters_db", function() @@ -1076,6 +1080,14 @@ do cb({ succ = 1 }) return { cancel = function() end } end + fake_client.bookInfoAsync = function(_, _, cb) + cb({ book = { bookId = "bpush", version = 7 } }) + return { cancel = function() end } + end + fake_client.reviewSingleAsync = function(_, _, _, _, cb) + cb({ review = { isPrivate = 0 } }) + return { cancel = function() end } + end fake_client.deleteReviewAsync = function(_, review_id, cb) calls[#calls + 1] = { api = "deleteReview", review_id = review_id } cb({ succ = 1 })