Skip to content

Conversation

PaloMiku
Copy link
Contributor

@PaloMiku PaloMiku commented Oct 17, 2025

Motivation / 动机

修复对最新 Astrbot 版本的 Misskey 适配器引用用户贴文回复问题问题

Screenshots or Test Results / 运行截图或测试结果

问题截图

image

修复后截图

image

Compatibility & Breaking Changes / 兼容性与破坏性变更

  • 这是一个破坏性变更 (Breaking Change)。/ This is a breaking change.
  • 这不是一个破坏性变更。/ This is NOT a breaking change.

Checklist / 检查清单

  • 😊 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。/ If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
  • 👀 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”。/ My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
  • 🤓 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到了 requirements.txtpyproject.toml 文件相应位置。/ I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in requirements.txt and pyproject.toml.
  • 😮 我的更改没有引入恶意代码。/ My changes do not introduce malicious code.

Sourcery 总结

优化 Misskey 适配器,通过改进日志记录、修复 @mention 行为、更准确地管理用户缓存,并添加 reply_id 支持以正确链接回复到原始帖子。

新功能:

  • 在创建笔记时添加对 reply_id 的支持,以便将回复链接到原始帖子

错误修复:

  • 修复不正确的 @mention 生成,通过使用有效的用户名并避免提及无效 ID
  • 解决 Misskey 通知和聊天消息中缺失的链接用户提及和回复处理

增强功能:

  • 改进通知、聊天事件和未处理事件中的调试日志记录,以包含事件类型和 ID
  • 解析 session_id 以提取用户 ID,以便更准确地查找用户缓存
  • 在缓存中存储原始消息 ID,用于回复处理
  • 移除 Misskey API 层中冗余的调试输出
Original summary in English

Summary by Sourcery

Optimize the Misskey adapter by refining logging, fixing @mention behavior, managing user cache more accurately, and adding reply_id support to properly link replies to original posts.

New Features:

  • Add support for reply_id when creating notes to link replies to original posts

Bug Fixes:

  • Fix incorrect @mention generation by using valid usernames and avoid invalid ID mentions
  • Resolve missing linked user mention and reply handling in Misskey notifications and chat messages

Enhancements:

  • Improve debug logging in notifications, chat events, and unhandled events to include event types and IDs
  • Parse session_id to extract user IDs for more accurate user cache lookups
  • Store original message IDs in cache for use in reply handling
  • Remove redundant debug output in Misskey API layer

@auto-assign auto-assign bot requested review from Larch-C and advent259141 October 17, 2025 14:58
@PaloMiku PaloMiku changed the title fix(紧急): 优化 Misskey 适配器的通知和聊天消息处理,改进 @用户提及逻辑 fix(紧急): 修复 Misskey 适配器的通知和聊天消息处理,改进 @用户提及逻辑 Oct 17, 2025
@PaloMiku PaloMiku changed the title fix(紧急): 修复 Misskey 适配器的通知和聊天消息处理,改进 @用户提及逻辑 fix: 修复 Misskey 适配器的通知和聊天消息处理,改进 @用户提及逻辑 Oct 17, 2025
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

你好 - 我已经审阅了你的更改 - 这里有一些反馈:

  • session_id 解析逻辑提取到一个共享的辅助函数中,以避免在 send_by_session_upload_comp 中重复分割/验证代码。
  • 考虑在使用后清除或使缓存的 reply_to_note_id 失效,以防止在后续消息中意外回复过时的笔记。
给AI代理的提示
请处理此代码审查中的评论:

## 总体评论
-`session_id` 解析逻辑提取到一个共享的辅助函数中,以避免在 `send_by_session``_upload_comp` 中重复分割/验证代码。
- 考虑在使用后清除或使缓存的 `reply_to_note_id` 失效,以防止在后续消息中意外回复过时的笔记。

## 独立评论

### 评论 1
<location> `astrbot/core/platform/sources/misskey/misskey_utils.py:89-92` </location>
<code_context>
-            return f"@{component.qq}"
+            # 优先使用name字段(用户名),如果没有则使用qq字段
+            # 这样可以避免在Misskey中生成 @<user_id> 这样的无效提及
+            if hasattr(component, "name") and component.name:
+                return f"@{component.name}"
+            else:
+                return f"@{component.qq}"
</code_context>

<issue_to_address>
**建议:** 考虑在使用 'name' 字段进行提及之前对其进行规范化或验证。

带有空格或特殊字符的名称可能导致无效提及。请确保 'name' 已正确规范化或验证以兼容 Misskey。

```suggestion
            def normalize_misskey_name(name):
                # Misskey usernames must be alphanumeric or underscores, no spaces or special chars
                import re
                # Remove leading/trailing whitespace, replace spaces with underscores, remove invalid chars
                name = name.strip().replace(" ", "_")
                name = re.sub(r"[^\\w_]", "", name)
                return name

            if hasattr(component, "name") and component.name:
                normalized_name = normalize_misskey_name(component.name)
                if normalized_name:  # Only use if valid after normalization
                    return f"@{normalized_name}"
                else:
                    return f"@{component.qq}"
            else:
                return f"@{component.qq}"
```
</issue_to_address>

Sourcery 对开源项目免费 - 如果你喜欢我们的评论,请考虑分享它们 ✨
帮助我更有用!请点击每个评论上的 👍 或 👎,我将利用这些反馈来改进你的评论。
Original comment in English

Hey there - I've reviewed your changes - here's some feedback:

  • Extract the session_id parsing logic into a shared helper function to avoid duplicating split/validation code in both send_by_session and _upload_comp.
  • Consider clearing or invalidating the cached reply_to_note_id after use to prevent accidentally replying to a stale note on subsequent messages.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Extract the session_id parsing logic into a shared helper function to avoid duplicating split/validation code in both send_by_session and _upload_comp.
- Consider clearing or invalidating the cached reply_to_note_id after use to prevent accidentally replying to a stale note on subsequent messages.

## Individual Comments

### Comment 1
<location> `astrbot/core/platform/sources/misskey/misskey_utils.py:89-92` </location>
<code_context>
-            return f"@{component.qq}"
+            # 优先使用name字段(用户名),如果没有则使用qq字段
+            # 这样可以避免在Misskey中生成 @<user_id> 这样的无效提及
+            if hasattr(component, "name") and component.name:
+                return f"@{component.name}"
+            else:
+                return f"@{component.qq}"
</code_context>

<issue_to_address>
**suggestion:** Consider normalizing or validating the 'name' field before using it for mentions.

Names with spaces or special characters may lead to invalid mentions. Please ensure the 'name' is properly normalized or validated for Misskey compatibility.

```suggestion
            def normalize_misskey_name(name):
                # Misskey usernames must be alphanumeric or underscores, no spaces or special chars
                import re
                # Remove leading/trailing whitespace, replace spaces with underscores, remove invalid chars
                name = name.strip().replace(" ", "_")
                name = re.sub(r"[^\w_]", "", name)
                return name

            if hasattr(component, "name") and component.name:
                normalized_name = normalize_misskey_name(component.name)
                if normalized_name:  # Only use if valid after normalization
                    return f"@{normalized_name}"
                else:
                    return f"@{component.qq}"
            else:
                return f"@{component.qq}"
```
</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.

@Soulter Soulter merged commit 8b0d4d4 into AstrBotDevs:master Oct 19, 2025
5 checks 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.

2 participants