🔒 (notes): 新想法默认私有,设置里可改可见范围

公开和仅关注只对新想法生效。已有想法沿用原来记下的范围。
This commit is contained in:
Garfield committed 2026-12-01 15:37:10 +08:00
1 parent 352ee1bd99
commit 338e2d2d3c
10 files changed
+207 -61

No files matched your search

@@ -24,6 +24,11 @@ return {
["试读结束"] = "Trial ended",
["想法上传失败"] = "Failed to upload note",
["想法上传成功但缺少 reviewId"] = "Note upload succeeded without a reviewId",
["想法默认可见"] = "Default note visibility",
["公开"] = "Public",
["仅关注"] = "Mutuals only",
["私有"] = "Private",
["点按切换。仅关注是互相关注可见,只影响新想法"] = "Tap to change. Mutuals only means visible to people you follow each other. New notes only",
["无法确认想法是否私密"] = "Cannot confirm whether the note is private",
["想法的可见范围没有按设置保存"] = "The note visibility was not saved as requested",
["按章书籍请打开章节后同步划线"] = "Open a chapter first to sync highlights",
@@ -24,6 +24,11 @@ return {
["试读结束"] = "試讀結束",
["想法上传失败"] = "想法上傳失敗",
["想法上传成功但缺少 reviewId"] = "想法上傳成功但缺少 reviewId",
["想法默认可见"] = "想法預設可見",
["公开"] = "公開",
["仅关注"] = "僅關注",
["私有"] = "私有",
["点按切换。仅关注是互相关注可见,只影响新想法"] = "點按切換。僅關注是互相關注可見,只影響新想法",
["无法确认想法是否私密"] = "無法確認想法是否私密",
["想法的可见范围没有按设置保存"] = "想法的可見範圍沒有按設定保存",
["按章书籍请打开章节后同步划线"] = "分章書籍請開啟章節後再同步劃線",
@@ -33,7 +33,7 @@ local LOCAL_COLOR = {
}
local WR_FIELDS = {
"wr_range", "wr_bookmark_id", "wr_review_id", "wr_review_only", "wr_private",
"wr_range", "wr_bookmark_id", "wr_review_id", "wr_review_only", "wr_private", "wr_friendship",
"wr_delete_review", "wr_update_review", "wr_update_bookmark",
}
@@ -48,6 +48,7 @@ local WR_FIELDS = {
---@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
@@ -89,17 +90,37 @@ function Notes.privateOf(review)
return nil
end
--- 发表/修改要带的 ``isPrivate``。已有想法没记过可见范围时返回 nil,请求里就不带这个字段。
---@param annotation table
---@param book_secret integer|boolean|nil 书籍 ``secret==1`` 表示私密阅读
--- 想法上的 ``friendship``:1 仅互相关注可见。没有这个字段就返回 nil。
---@param review table|nil
---@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
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
@@ -482,6 +503,10 @@ function Notes.reconcileReviews(annotations, wire)
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
@@ -549,6 +574,8 @@ function Notes.toAnnotations(wire, chapter_uid, reviews, source_id, stable_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
@@ -572,6 +599,8 @@ function Notes.toAnnotations(wire, chapter_uid, reviews, source_id, stable_id)
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
@@ -595,7 +624,8 @@ 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, ann.wr_private = old.note, old.wr_review_id, old.wr_private
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
@@ -707,14 +737,15 @@ function Notes.toBookmarkUpdateBody(annotation)
end
--- 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
---@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)
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"
@@ -735,8 +766,9 @@ function Notes.toReviewBody(book_id, chapter_uid, annotation, book_secret)
abstract = abstract,
chapterUid = tonumber(chapter_uid) or chapter_uid,
}
local private = Notes.reviewIsPrivate(annotation, book_secret)
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
@@ -745,14 +777,15 @@ end
---@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)
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)
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
@@ -328,23 +328,30 @@ function Push:updates()
end, function() self:reviews() end)
end
--- 发表或修改之后读回 ``review.isPrivate``。对不上就停,不把 HTTP 成功当成私密已生效。
--- 发表或修改之后读回可见范围。对不上就停,不把 HTTP 成功当成已经按设置生效。
---@param item table
---@param wanted integer
---@param wanted_private integer|nil
---@param wanted_friendship integer|nil
---@param next fun()
function Push:confirmPrivate(item, wanted, next)
item.wr_private = wanted
function Push:confirmPrivate(item, wanted_private, wanted_friendship, next)
if wanted_private ~= nil then item.wr_private = wanted_private end
if wanted_friendship ~= nil then item.wr_friendship = wanted_friendship end
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
local got_friendship = Notes.friendshipOf(wire)
if wanted_private ~= nil and got == nil then
self:finish(nil, _("无法确认想法是否私密"))
return
end
item.wr_private = got
if got ~= wanted then
if wanted_friendship ~= nil and got_friendship == nil then got_friendship = 0 end
local mismatch = (wanted_private ~= nil and got ~= wanted_private)
or (wanted_friendship ~= nil and got_friendship ~= wanted_friendship)
if got ~= nil then item.wr_private = got end
if Notes.friendshipOf(wire) ~= nil then item.wr_friendship = Notes.friendshipOf(wire) end
if mismatch then
item.wr_update_review = nil
self:finish(nil, _("想法的可见范围没有按设置保存"))
return
@@ -354,11 +361,20 @@ function Push:confirmPrivate(item, wanted, next)
end)
end
--- 串行推想法:已有 reviewId 走 edit,否则 add。新想法先读这本书是不是私密阅读。
--- 设置里的想法默认范围。没设过或值不对就是私有。
---@return string
function Push:noteVisibility()
local choice = require("utils.settings").getSource("weread").note_visibility
if choice == "public" or choice == "follow" then return choice end
return "private"
end
--- 串行推想法:已有 reviewId 走 edit,否则 add。
function Push:reviews()
local function submit()
local deferred = false
local secret = Context.bookSecret(self.book_id)
local choice = self:noteVisibility()
self:each(Notes.notePushCandidates(self.annotations), function(item, next)
if not item.wr_review_id and self.reviews_unknown then
deferred = true
@@ -372,10 +388,10 @@ function Push:reviews()
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)
body, body_err = Notes.toReviewEditBody(self.book_id, self.chapter_uid, item, secret, choice)
send = function(done) return self.client:editReviewAsync(body, done) end
else
body, body_err = Notes.toReviewBody(self.book_id, self.chapter_uid, item, secret)
body, body_err = Notes.toReviewBody(self.book_id, self.chapter_uid, item, secret, choice)
send = function(done) return self.client:addReviewAsync(body, done) end
end
if not body then
@@ -391,12 +407,12 @@ function Push:reviews()
end
item.wr_review_id = review_id
end
if body.isPrivate == nil then
if body.isPrivate == nil and body.friendship == nil then
item.wr_update_review = nil
next()
return
end
self:confirmPrivate(item, body.isPrivate, next)
self:confirmPrivate(item, body.isPrivate, body.friendship, next)
end)
end, function()
if deferred then
@@ -406,28 +422,6 @@ function Push:reviews()
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: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
submit()
end)
return
end
submit()
end
@@ -159,6 +159,31 @@ function Setting.rows(plugin)
toggle("merge_local_stats", false, "history", _("合并本地阅读时长"),
_("关闭时以云端为准;打开后每天取云端与本地中较大的,补上离线阅读。只影响月读里的统计"))
toggle("show_paragraph_reviews", false, "forum", _("显示段评"), _("阅读时给有他人段评的句子画线,点按查看;需联网"))
local visibility = {
{ value = "private", label = _("私有") },
{ value = "public", label = _("公开") },
{ value = "follow", label = _("仅关注") },
}
rows[#rows + 1] = function(iw)
local cfg = Settings.getSource(SOURCE_ID)
local current = cfg.note_visibility
if current ~= "public" and current ~= "follow" then current = "private" end
local index = 1
for i, item in ipairs(visibility) do
if item.value == current then index = i end
end
return Row.widget({ width = iw, chevron = false, kind = "action", icon = "lock",
title = _("想法默认可见"),
subtitle = _("点按切换。仅关注是互相关注可见,只影响新想法"),
status = visibility[index].label,
status_on = current ~= "public",
callback = function()
local next_item = visibility[index % #visibility + 1]
cfg.note_visibility = next_item.value
Settings.saveSource(SOURCE_ID, cfg)
if plugin and plugin.desktop then plugin.desktop:updateView() end
end })
end
rows[#rows + 1] = require("source.category_setting").row(SOURCE_ID, plugin)
if not Auth.hasSession() then return rows end
rows[#rows + 1] = function(iw)