fix(import): normalize JSON-stringified like/ups/downs arrays on Twikoo import#956
Conversation
…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>
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 '['?}
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]
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your Experience访问你的 dashboard 以:
Getting HelpOriginal review guide in EnglishReviewer's GuideRefactors 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 importflowchart 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 '['?}
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]
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
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>帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据你的反馈改进后续的代码审查。
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>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) |
There was a problem hiding this comment.
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} 解析成功`)
Summary
like,ups, anddownsduring Twikoo JSON import when those fields were exported as JSON-stringified strings (e.g."[]") instead of real arrays_id,pid, andridProblem
Fixes #845. Importing Twikoo JSON with
like: "[]"stored the value as a string, and subsequent export/import cycles double-escaped it to"\"[]\"".Test plan
like: "[]",ups: "[]", and already-array valueslike: "[]"and verify DB stores[]Summary by Sourcery
在进行 JSON 导入时对 Twikoo 评论数据进行规范化处理,以支持旧版 ID 格式和类数组字段,同时不修改原始记录。
Bug Fixes:
like、ups和downs字段在被以 JSON 字符串形式导出时,依然会被存储为数组。Enhancements:
_id、pid和rid值,同时不对源对象进行修改。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:
like,ups, anddownsfields imported from Twikoo JSON are stored as arrays even when exported as JSON-stringified strings.Enhancements:
_id,pid, andridvalues without mutating the source objects.