🔒 (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

@@ -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