Skip to content

fix(import): normalize JSON-stringified like/ups/downs arrays on Twikoo import#956

Merged
imaegoo merged 1 commit into
twikoojs:mainfrom
wyf027:fix/import-like-field-json-string
May 28, 2026
Merged

fix(import): normalize JSON-stringified like/ups/downs arrays on Twikoo import#956
imaegoo merged 1 commit into
twikoojs:mainfrom
wyf027:fix/import-like-field-json-string

Conversation

@wyf027

@wyf027 wyf027 commented May 25, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Normalize like, ups, and downs during Twikoo JSON import when those fields were exported as JSON-stringified strings (e.g. "[]") instead of real arrays
  • Avoid mutating the source comment object while normalizing _id, pid, and rid

Problem

Fixes #845. Importing Twikoo JSON with like: "[]" stored the value as a string, and subsequent export/import cycles double-escaped it to "\"[]\"".

Test plan

  • Manual logic review for like: "[]", ups: "[]", and already-array values
  • Import a Twikoo JSON file containing like: "[]" and verify DB stores []
  • Export and re-import to confirm no double-escaping

Summary by Sourcery

在进行 JSON 导入时对 Twikoo 评论数据进行规范化处理,以支持旧版 ID 格式和类数组字段,同时不修改原始记录。

Bug Fixes:

  • 确保从 Twikoo JSON 导入的 likeupsdowns 字段在被以 JSON 字符串形式导出时,依然会被存储为数组。

Enhancements:

  • 抽取可复用的 Twikoo 评论规范化助手,用于清理旧格式的 _idpidrid 值,同时不对源对象进行修改。
Original summary in English

Summary by Sourcery

Normalize Twikoo comment data during JSON import to handle legacy ID formats and array-like fields without mutating the original records.

Bug Fixes:

  • Ensure like, ups, and downs fields imported from Twikoo JSON are stored as arrays even when exported as JSON-stringified strings.

Enhancements:

  • Extract a reusable Twikoo comment normalization helper to clean up legacy _id, pid, and rid values without mutating the source objects.

…oo import

When importing Twikoo JSON exports where array fields such as `like` were
stored as stringified JSON (e.g. `"[]"`), the values were persisted as
strings and became double-escaped on subsequent export/import cycles.

Parse string values that look like JSON arrays during Twikoo import so
`like`, `ups`, and `downs` are stored as real arrays.

Fixes #845

Co-authored-by: Cursor <cursoragent@cursor.com>
@sourcery-ai

sourcery-ai Bot commented May 25, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

重构 Twikoo JSON 导入时的评论规范化逻辑,以避免修改源评论对象本身,并为 JSON 字符串化的数组字段(like/ups/downs 等)提供更健壮的处理,使其以真正的数组而非字符串形式存储。

Twikoo 评论导入规范化流程图

flowchart TD
  A[Raw comment from Twikoo JSON] --> B[normalizeTwikooComment]
  B --> C{_id has $oid?}
  C -- yes --> C1[Set parsed._id = comment._id.$oid]
  C -- no --> C2[Keep existing _id]

  B --> D{pid === null?}
  D -- yes --> D1[delete parsed.pid]
  D -- no --> D2[Keep pid]

  B --> E{rid === null?}
  E -- yes --> E1[delete parsed.rid]
  E -- no --> E2[Keep rid]

  subgraph ArrayFieldNormalization
    F[ARRAY_FIELDS like ups downs]
    F --> G[parseJsonArrayField]
    G --> H{value is array?}
    H -- yes --> I[Return value]
    H -- no --> J{value is string starting with '&#91;'?}
    J -- no --> K[Return original value]
    J -- yes --> L[JSON.parse]
    L --> M{parsed is array?}
    M -- yes --> N[Return parsed]
    M -- no --> K
  end

  B --> F
  N --> O[Normalized comment object]
  I --> O
  K --> O

  O --> P[Push into comments array for import]
Loading

File-Level Changes

Change Details Files
引入可复用的 Twikoo 评论与数组字段规范化辅助函数,并在导入时使用它们,修复 JSON 字符串化的 like/ups/downs 字段问题,同时避免直接修改原始评论对象。
  • 添加 ARRAY_FIELDS 常量,用于集中管理需要规范化的、类似数组的 Twikoo 评论字段列表。
  • 实现 parseJsonArrayField,在值看起来像 JSON 数组时,将 JSON 字符串化的数组字符串安全地转换为真实数组;对非数组或无效值保持不变。
  • 实现 normalizeTwikooComment,克隆原始评论,从嵌套的 $oid 中规范化 _id,移除为 nullpid/rid,并通过 parseJsonArrayField 规范化 like/ups/downs。
  • 更新主要导入循环,使用 normalizeTwikooComment,而不是在推入评论集合前就地修改评论对象。
src/server/function/twikoo/utils/import.js

Assessment against linked issues

Issue Objective Addressed Explanation
#845 在导入 Twikoo JSON 时,确保 like 字段(例如 "[]")在数据库中以正确的数组形式([])存储,而不是带引号的字符串。
#845 确保在导入 Twikoo JSON 之后,再次导出时生成的 like 字段格式与原始输入保持一致(不出现 ""[]"" 这样的二次转义),即导出为一致的、未字符串化的数组。

Possibly linked issues

  • #bug: 在前端控制面板中 import 的时候,like 字段会上传多余的双引号:本 PR 在导入时增加了对 like/ups/downs 数组的规范化处理,修复了该 bug 中描述的多余双引号 "[]" 问题。

Tips and commands

Interacting with Sourcery

  • 触发新的代码审查: 在 pull request 中评论 @sourcery-ai review
  • 继续讨论: 直接回复 Sourcery 的审查评论。
  • 从审查评论生成 GitHub issue: 在某条审查评论下回复,请求 Sourcery 从该评论创建 issue。你也可以直接回复 @sourcery-ai issue 来从该评论创建 issue。
  • 生成 pull request 标题: 在 pull request 标题中的任意位置写上 @sourcery-ai,即可随时生成一个标题。你也可以在 pull request 中评论 @sourcery-ai title 来(重新)生成标题。
  • 生成 pull request 总结: 在 pull request 正文任意位置写上 @sourcery-ai summary,即可在那个位置生成 PR 总结。你也可以在 pull request 中评论 @sourcery-ai summary 来(重新)生成总结。
  • 生成 Reviewer’s Guide: 在 pull request 中评论 @sourcery-ai guide,即可随时(重新)生成 Reviewer’s Guide。
  • 一次性解决所有 Sourcery 评论: 在 pull request 中评论 @sourcery-ai resolve,即可将所有 Sourcery 评论标记为已解决。如果你已经处理完所有评论且不想再看到它们,这会很有用。
  • 一次性撤销所有 Sourcery 审查: 在 pull request 中评论 @sourcery-ai dismiss,即可撤销所有现有的 Sourcery 审查。特别适合你想从头开始新的审查时使用——记得重新评论 @sourcery-ai review 来触发新审查!

Customizing Your Experience

访问你的 dashboard 以:

  • 启用或禁用诸如 Sourcery 自动生成的 pull request 总结、Reviewer’s Guide 等审查功能。
  • 更改审查语言。
  • 添加、移除或编辑自定义审查说明。
  • 调整其他审查设置。

Getting Help

Original review guide in English

Reviewer's Guide

Refactors Twikoo JSON import comment normalization to avoid mutating source comment objects and adds robust handling for JSON-stringified array fields (like/ups/downs) so they are stored as proper arrays instead of strings.

Flow diagram for Twikoo comment normalization on import

flowchart TD
  A[Raw comment from Twikoo JSON] --> B[normalizeTwikooComment]
  B --> C{_id has $oid?}
  C -- yes --> C1[Set parsed._id = comment._id.$oid]
  C -- no --> C2[Keep existing _id]

  B --> D{pid === null?}
  D -- yes --> D1[delete parsed.pid]
  D -- no --> D2[Keep pid]

  B --> E{rid === null?}
  E -- yes --> E1[delete parsed.rid]
  E -- no --> E2[Keep rid]

  subgraph ArrayFieldNormalization
    F[ARRAY_FIELDS like ups downs]
    F --> G[parseJsonArrayField]
    G --> H{value is array?}
    H -- yes --> I[Return value]
    H -- no --> J{value is string starting with '&#91;'?}
    J -- no --> K[Return original value]
    J -- yes --> L[JSON.parse]
    L --> M{parsed is array?}
    M -- yes --> N[Return parsed]
    M -- no --> K
  end

  B --> F
  N --> O[Normalized comment object]
  I --> O
  K --> O

  O --> P[Push into comments array for import]
Loading

File-Level Changes

Change Details Files
Introduce reusable normalization helpers for Twikoo comments and array fields, and use them during import to fix JSON-stringified like/ups/downs issues without mutating the original comment objects.
  • Add ARRAY_FIELDS constant to centralize the list of array-like Twikoo comment fields that require normalization.
  • Implement parseJsonArrayField to safely convert JSON-stringified array strings into real arrays when they look like JSON arrays, leaving non-array or invalid values unchanged.
  • Implement normalizeTwikooComment to clone the original comment, normalize _id from nested $oid, remove null pid/rid, and normalize like/ups/downs via parseJsonArrayField.
  • Update the main import loop to use normalizeTwikooComment instead of mutating comment inline before pushing it into the comments collection.
src/server/function/twikoo/utils/import.js

Assessment against linked issues

Issue Objective Addressed Explanation
#845 On Twikoo JSON import, ensure the like field (e.g. "[]") is stored as a proper array ([]) in the database instead of a quoted string.
#845 Ensure that after importing Twikoo JSON, subsequent exports produce the same like field format as the original input (no double-escaping such as ""[]""), i.e., export consistent, non-stringified arrays.

Possibly linked issues

  • #bug: 在前端控制面板中 import 的时候,like 字段会上传多余的双引号: PR adds import normalization for like/ups/downs arrays, fixing the double-quoted "[]" issue described in the bug.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - 我发现了 1 个问题。

给 AI 智能体的提示
请根据本次代码审查的评论进行修改:

## 单独评论

### 评论 1
<location path="src/server/function/twikoo/utils/import.js" line_range="278" />
<code_context>
-        if (comment.rid === null) {
-          delete comment.rid
-        }
+        const parsed = normalizeTwikooComment(comment)
         comments.push(parsed)
         log(`${comment._id} 解析成功`)
</code_context>
<issue_to_address>
**suggestion:** 建议记录归一化后的 `_id`,以避免在 `_id` 是 ObjectId 包装类型时记录为对象形式。

当前成功日志仍在使用 `comment._id````js
log(`${comment._id} 解析成功`)
```

对于旧格式 `{ $oid: '...' }` 的值,这可能会记录为一个对象,而不是归一化后的字符串。请改为记录 `parsed._id`,这样日志信息才能准确反映归一化后的标识符:

```js
log(`${parsed._id} 解析成功`)
```
</issue_to_address>

Sourcery 对开源项目是免费的——如果你觉得我们的评审有帮助,欢迎分享 ✨
帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据你的反馈改进后续的代码审查。
Original comment in English

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="src/server/function/twikoo/utils/import.js" line_range="278" />
<code_context>
-        if (comment.rid === null) {
-          delete comment.rid
-        }
+        const parsed = normalizeTwikooComment(comment)
         comments.push(parsed)
         log(`${comment._id} 解析成功`)
</code_context>
<issue_to_address>
**suggestion:** Consider logging the normalized `_id` to avoid logging an object `_id` when it’s an ObjectId wrapper.

The success log is still using `comment._id`:

```js
log(`${comment._id} 解析成功`)
```

For legacy `{ $oid: '...' }` values this may log an object rather than the normalized string. Please log `parsed._id` instead so the message reflects the normalized identifier:

```js
log(`${parsed._id} 解析成功`)
```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

if (comment.rid === null) {
delete comment.rid
}
const parsed = normalizeTwikooComment(comment)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: 建议记录归一化后的 _id,以避免在 _id 是 ObjectId 包装类型时记录为对象形式。

当前成功日志仍在使用 comment._id

log(`${comment._id} 解析成功`)

对于旧格式 { $oid: '...' } 的值,这可能会记录为一个对象,而不是归一化后的字符串。请改为记录 parsed._id,这样日志信息才能准确反映归一化后的标识符:

log(`${parsed._id} 解析成功`)
Original comment in English

suggestion: Consider logging the normalized _id to avoid logging an object _id when it’s an ObjectId wrapper.

The success log is still using comment._id:

log(`${comment._id} 解析成功`)

For legacy { $oid: '...' } values this may log an object rather than the normalized string. Please log parsed._id instead so the message reflects the normalized identifier:

log(`${parsed._id} 解析成功`)

@imaegoo imaegoo merged commit 4661d49 into twikoojs:main May 28, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: 在前端控制面板中 import 的时候,like 字段会上传多余的双引号

2 participants