From 7b3eeb8fe618317a03b321d7ce7240bab10f79dc Mon Sep 17 00:00:00 2001 From: Garfield Date: Thu, 26 Nov 2026 16:55:31 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20(reviews):=20=E8=AF=84=E8=AE=BA?= =?UTF-8?q?=E9=85=8D=E5=9B=BE=E5=B8=A6=E4=B8=8A=E5=BE=AE=E4=BF=A1=E9=98=85?= =?UTF-8?q?=E8=AF=BB=E6=9D=A5=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 空地址丢掉。没有图时不给出图片列表。 关联 #148 --- .../moonsource_weread/reviews.lua | 26 +++++++++++++++++++ tests/weread/reviews_spec.lua | 21 +++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/moonsource-weread.koplugin/moonsource_weread/reviews.lua b/moonsource-weread.koplugin/moonsource_weread/reviews.lua index 8dd1c2f..d780546 100644 --- a/moonsource-weread.koplugin/moonsource_weread/reviews.lua +++ b/moonsource-weread.koplugin/moonsource_weread/reviews.lua @@ -87,6 +87,30 @@ function Reviews.locate(hotspots, html) return out end +local PICTURE_HEADERS = { Referer = "https://weread.qq.com/" } + +--- 评论配图。没有图时 nil,空数组不当成有图。 +---@param review table|nil +---@return { url: string, width: integer|nil, height: integer|nil, headers: table }[]|nil +local function imagesOf(review) + local pics = type(review) == "table" and review.pictures or nil + if type(pics) ~= "table" then return nil end + local images = {} + for _idx, pic in ipairs(pics) do + local url = type(pic) == "table" and pic.url or nil + if type(url) == "string" and url ~= "" then + images[#images + 1] = { + url = url, + width = tonumber(pic.width), + height = tonumber(pic.height), + headers = PICTURE_HEADERS, + } + end + end + if #images == 0 then return nil end + return images +end + --- 单条 pageReviews → 展示项。 ---@param entry table ---@return WechatReview @@ -96,6 +120,7 @@ function Reviews.item(entry) id = entry.reviewId, author = type(review.author) == "table" and review.author.name or "", content = review.content, + images = imagesOf(review) or imagesOf(entry), likes = tonumber(entry.likesCount) or 0, replies = tonumber(entry.commentsCount) or tonumber(review.commentsCount) or 0, time = tonumber(review.createTime) or 0, @@ -114,6 +139,7 @@ function Reviews.commentItem(entry) id = tostring(entry.commentId or comment.commentId or ""), author = type(author) == "table" and author.name or "", content = tostring(comment.content or entry.content or ""), + images = imagesOf(comment) or imagesOf(entry), likes = tonumber(entry.likesCount or comment.likesCount) or 0, replies = 0, reply_to = type(reply_user) == "table" and reply_user.name or nil, diff --git a/tests/weread/reviews_spec.lua b/tests/weread/reviews_spec.lua index 667ebda..a8772e6 100644 --- a/tests/weread/reviews_spec.lua +++ b/tests/weread/reviews_spec.lua @@ -79,6 +79,27 @@ do review = { content = "点评", createTime = 1, author = { name = "甲" } }, }) Assert.eq(with_replies.replies, 4) + Assert.is_nil(with_replies.images) + local pictured = Reviews.item({ + reviewId = "pic", likesCount = 2, + review = { + content = "带图", createTime = 3, author = { name = "甲" }, + pictures = { + { url = "https://weread-picture.example/a.png", width = 800, height = 1067 }, + { url = "", width = 1, height = 1 }, + }, + }, + }) + Assert.len(pictured.images, 1) + Assert.eq(pictured.images[1].url, "https://weread-picture.example/a.png") + Assert.eq(pictured.images[1].width, 800) + Assert.eq(pictured.images[1].height, 1067) + Assert.eq(pictured.images[1].headers.Referer, "https://weread.qq.com/") + local reply = Reviews.commentItem({ + commentId = "c", content = "回复", createTime = 4, author = { name = "乙" }, + pictures = { { url = "https://weread-picture.example/b.png", width = 40, height = 40 } }, + }) + Assert.eq(reply.images[1].url, "https://weread-picture.example/b.png") end do -- 想法下的评论页:楼中楼只记被回复者;空列表但声称有条数是失败;游标不前进就停