Skip to content

Sync category records during emoji category updates#7

Merged
dingyi222666 merged 2 commits intoChatLunaLab:mainfrom
Juvwxyz:main
Mar 10, 2026
Merged

Sync category records during emoji category updates#7
dingyi222666 merged 2 commits intoChatLunaLab:mainfrom
Juvwxyz:main

Conversation

@Juvwxyz
Copy link
Copy Markdown
Contributor

@Juvwxyz Juvwxyz commented Mar 4, 2026

Fix #5 WebUI修改分类联动数据库的问题

Summary by CodeRabbit

  • Bug Fixes
    • Trimmed whitespace from category names to prevent duplicates and normalize inputs
    • Prevented creation of empty categories
    • Ensured category existence during imports and updates (creates missing categories when needed)
    • Recalculated and persisted emoji counts after category changes or imports
    • Avoided redundant updates when no effective category change occurs

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 4, 2026

📝 Walkthrough

Walkthrough

Normalizes category names by trimming inputs across service methods, prevents creation of empty categories, reuses or auto-creates categories as needed, and ensures emoji count recalculations and database updates when categories change or are imported.

Changes

Cohort / File(s) Summary
Category / Emoji service
src/service.ts
Trim category names on input; reject empty names; reuse existing category when possible; create missing categories when counting/importing if count > 0; avoid redundant updates; persist changes and trigger recalculation of emoji counts after category moves or imports.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐇 I nudge the names, a gentle trim,

No empty bins or duplicates grim.
Counts refreshed, the ledger sings—
Hooray for tidy category things! 🥕

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: syncing category records during emoji category updates to fix the database inconsistency issue.
Linked Issues check ✅ Passed The PR implements all coding requirements from issue #5: trimming category names, updating emoji counts when categories change, creating new category entries, and syncing database records.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing category synchronization issues in the linked issue; no out-of-scope modifications detected.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

此拉取请求旨在解决 WebUI 中表情符号分类更新与数据库不同步的问题。通过增强分类添加、表情符号分类更新和分类表情符号计数更新的逻辑,确保了数据的一致性,并改进了对分类名称的验证和处理。

Highlights

  • 增强了 addCategory 方法: 增加了对空分类名称的验证,修剪了输入,并添加了逻辑以更新现有分类的描述或在提供重复名称时返回现有分类。
  • 改进了 updateEmojiCategory 方法: 增加了对新分类名称的修剪,处理了旧分类和新分类相同的情况,并确保对旧分类和新分类都调用 updateCategoryEmojiCount 以保持准确的计数。
  • 优化了 updateCategoryEmojiCount 方法: 增加了对分类名称的修剪,处理了空分类名称,并添加了逻辑以在表情符号被分配到不存在的分类且其计数大于零时自动创建新分类。

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • src/service.ts
    • addCategory 方法增加了输入验证和现有分类更新逻辑。
    • updateEmojiCategory 方法现在处理分类名称修剪、重复分类检查,并触发旧分类和新分类的计数更新。
    • updateCategoryEmojiCount 方法现在修剪分类名称,并能在需要时自动创建新分类。
Activity
  • 目前没有人工活动。
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

这次的 Pull Request 修复了 WebUI 修改表情分类时数据库同步的问题,做得很好。主要的改动包括:

  1. addCategory 方法现在能处理已存在分类的情况,并可以更新其描述。
  2. updateEmojiCategory 方法在更新表情分类后,会正确地同步更新旧分类和新分类的表情数量。
  3. updateCategoryEmojiCount 方法现在能够自动创建尚不存在的分类。

这些改动增强了系统的健壮性和数据一致性。我发现一处可以优化的地方,即在 updateCategoryEmojiCount 中创建新分类时可以避免一次额外的数据库写入,具体建议见评论。

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/service.ts`:
- Around line 1422-1427: The count is wrong when stored emoji.category contains
surrounding whitespace: use a trimmed comparison when computing counts by
normalizing stored categories before matching (e.g., in the filter over
this._emojiStorage, compare emoji.category.trim() to normalizedCategoryName) and
apply the same normalization to the other block referenced (lines 1429–1435)
that updates/creates categories so all category comparisons and updates use
trimmed values. Ensure you only adjust comparisons/updates (not DB schema) and
reference normalizedCategoryName and this._emojiStorage / emoji.category when
making the change.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 267b2880-a331-4eca-b424-a5bb1da2a4c5

📥 Commits

Reviewing files that changed from the base of the PR and between 08b87b9 and b9739d3.

📒 Files selected for processing (1)
  • src/service.ts

Comment thread src/service.ts
Ensure stored emoji.category and category.name values are trimmed when comparing
against normalizedCategoryName to prevent whitespace mismatches that cause
incorrect emoji counts and failed category lookups.
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/service.ts`:
- Around line 1118-1119: The early-exit comparison uses the raw emoji.category
(const oldCategory = emoji.category) which may include leading/trailing
whitespace and falsely fail equality against nextCategory; change the comparison
to use a trimmed oldCategory (e.g., use emoji.category?.trim() or set const
oldCategory = (emoji.category || '').trim()) or trim both sides before comparing
so the early return if (oldCategory === nextCategory) correctly avoids redundant
DB updates and count recalculations in the function handling the emoji update.
- Line 874: getCategoryByName currently compares stored category names directly
(cat.name === name) which fails if stored names have extra whitespace; modify
getCategoryByName to compare trimmed values (e.g., compare cat.name?.trim() ===
name?.trim()) so lookups match regardless of leading/trailing spaces, mirroring
the trimmed comparison used in updateCategoryEmojiCount and ensuring
null/undefined safety.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 30519568-01c9-4a16-99ff-67264cf786f2

📥 Commits

Reviewing files that changed from the base of the PR and between b9739d3 and 8b3578b.

📒 Files selected for processing (1)
  • src/service.ts

Comment thread src/service.ts
throw new Error('分类名称不能为空')
}

const existingCategory = await this.getCategoryByName(categoryName)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

getCategoryByName does not trim stored category names, causing lookup inconsistency.

The getCategoryByName method (line 945-949) compares cat.name === name without trimming stored values. If any stored category name contains leading/trailing whitespace, lookups will fail even when categoryName is correctly trimmed here. This is inconsistent with the trimmed comparison now used in updateCategoryEmojiCount (line 1430).

Consider updating getCategoryByName to use trimmed comparison:

Proposed fix
 async getCategoryByName(name: string): Promise<Category | null> {
+    const normalizedName = name.trim()
     return (
-        Object.values(this._categories).find((cat) => cat.name === name) ||
+        Object.values(this._categories).find((cat) => cat.name.trim() === normalizedName) ||
         null
     )
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/service.ts` at line 874, getCategoryByName currently compares stored
category names directly (cat.name === name) which fails if stored names have
extra whitespace; modify getCategoryByName to compare trimmed values (e.g.,
compare cat.name?.trim() === name?.trim()) so lookups match regardless of
leading/trailing spaces, mirroring the trimmed comparison used in
updateCategoryEmojiCount and ensuring null/undefined safety.

Comment thread src/service.ts
Comment on lines +1118 to +1119
const oldCategory = emoji.category
if (oldCategory === nextCategory) return true
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Early-exit comparison uses untrimmed oldCategory, may cause redundant DB writes.

If an emoji's stored category contains trailing/leading whitespace (e.g., "Memes "), and the user submits the same category trimmed ("Memes"), the comparison oldCategory === nextCategory will be false, triggering an unnecessary update and count recalculations.

Consider trimming oldCategory for the comparison:

Proposed fix
     const oldCategory = emoji.category
-    if (oldCategory === nextCategory) return true
+    if (oldCategory.trim() === nextCategory) return true
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const oldCategory = emoji.category
if (oldCategory === nextCategory) return true
const oldCategory = emoji.category
if (oldCategory.trim() === nextCategory) return true
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/service.ts` around lines 1118 - 1119, The early-exit comparison uses the
raw emoji.category (const oldCategory = emoji.category) which may include
leading/trailing whitespace and falsely fail equality against nextCategory;
change the comparison to use a trimmed oldCategory (e.g., use
emoji.category?.trim() or set const oldCategory = (emoji.category || '').trim())
or trim both sides before comparing so the early return if (oldCategory ===
nextCategory) correctly avoids redundant DB updates and count recalculations in
the function handling the emoji update.

@dingyi222666
Copy link
Copy Markdown
Member

忘记看了,今晚看一下

@dingyi222666
Copy link
Copy Markdown
Member

又忘记了,明日...

@dingyi222666 dingyi222666 merged commit 8b3578b into ChatLunaLab:main Mar 10, 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.

WebUI修改分类联动数据库的问题

2 participants