-
Notifications
You must be signed in to change notification settings - Fork 52.3k
fix(feishu): include attachments from replied messages #54570
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3130,7 +3130,14 @@ async def _process_inbound_message( | |
| or getattr(message, "root_id", None) | ||
| or None | ||
| ) | ||
| reply_to_text = await self._fetch_message_text(reply_to_message_id) if reply_to_message_id else None | ||
| reply_to_text = None | ||
| reply_media_urls: List[str] = [] | ||
| reply_media_types: List[str] = [] | ||
| if reply_to_message_id: | ||
| reply_to_text, reply_media_urls, reply_media_types = await self._fetch_message_context(reply_to_message_id) | ||
| if reply_media_urls: | ||
| media_urls.extend(reply_media_urls) | ||
| media_types.extend(reply_media_types) | ||
|
|
||
| sender_primary = ( | ||
| getattr(sender_id, "open_id", None) | ||
|
|
@@ -4023,38 +4030,49 @@ async def _fetch_bot_names(self, bot_ids: List[str]) -> Optional[Dict[str, str]] | |
| logger.debug("[Feishu] Failed to fetch bot names for %s", bot_ids, exc_info=True) | ||
| return None | ||
|
|
||
| async def _fetch_message_text(self, message_id: str) -> Optional[str]: | ||
| async def _fetch_message_context(self, message_id: str) -> tuple[Optional[str], List[str], List[str]]: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a bounded parent-context cache here. Repeat replies to the same parent re-run the download at line 4057, and the cache helpers create UUID-named files for each download, so repeated replies duplicate cached media until cleanup. |
||
| if not self._client or not message_id: | ||
| return None | ||
| if message_id in self._message_text_cache: | ||
| self._message_text_cache.move_to_end(message_id) | ||
| return self._message_text_cache[message_id] | ||
| return None, [], [] | ||
| try: | ||
| request = self._build_get_message_request(message_id) | ||
| response = await asyncio.to_thread(self._client.im.v1.message.get, request) | ||
| if not response or getattr(response, "success", lambda: False)() is False: | ||
| code = getattr(response, "code", "unknown") | ||
| msg = getattr(response, "msg", "message lookup failed") | ||
| logger.warning("[Feishu] Failed to fetch parent message %s: [%s] %s", message_id, code, msg) | ||
| return None | ||
| return None, [], [] | ||
| items = getattr(getattr(response, "data", None), "items", None) or [] | ||
| parent = items[0] if items else None | ||
| body = getattr(parent, "body", None) | ||
| msg_type = getattr(parent, "msg_type", "") or "" | ||
| raw_content = getattr(body, "content", "") or "" | ||
| msg_type = getattr(parent, "msg_type", "") or getattr(parent, "message_type", "") or "" | ||
| raw_content = getattr(body, "content", "") or getattr(parent, "content", "") or "" | ||
| parent_mentions = getattr(parent, "mentions", None) if parent else None | ||
| text = self._extract_text_from_raw_content( | ||
| msg_type=msg_type, | ||
| normalized = normalize_feishu_message( | ||
| message_type=msg_type, | ||
| raw_content=raw_content, | ||
| mentions=parent_mentions, | ||
| bot=self._bot_identity(), | ||
| ) | ||
| text = self._extract_text_from_normalized(normalized) | ||
| media_urls, media_types = await self._download_feishu_message_resources( | ||
| message_id=message_id, | ||
| normalized=normalized, | ||
| ) | ||
| self._message_text_cache[message_id] = text | ||
| while len(self._message_text_cache) > _FEISHU_MESSAGE_TEXT_CACHE_SIZE: | ||
| self._message_text_cache.popitem(last=False) | ||
| return text | ||
| return text, media_urls, media_types | ||
| except Exception: | ||
| logger.warning("[Feishu] Failed to fetch parent message %s", message_id, exc_info=True) | ||
| return None, [], [] | ||
|
|
||
| async def _fetch_message_text(self, message_id: str) -> Optional[str]: | ||
| if not message_id: | ||
| return None | ||
| if message_id in self._message_text_cache: | ||
| return self._message_text_cache[message_id] | ||
| text, _, _ = await self._fetch_message_context(message_id) | ||
| return text | ||
|
|
||
| def _extract_text_from_raw_content( | ||
| self, | ||
|
|
@@ -4069,6 +4087,10 @@ def _extract_text_from_raw_content( | |
| mentions=mentions, | ||
| bot=self._bot_identity(), | ||
| ) | ||
| return self._extract_text_from_normalized(normalized) | ||
|
|
||
| @staticmethod | ||
| def _extract_text_from_normalized(normalized: FeishuNormalizedMessage) -> Optional[str]: | ||
| if normalized.text_content: | ||
| return normalized.text_content | ||
| placeholder = normalized.metadata.get("placeholder_text") if isinstance(normalized.metadata, dict) else None | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This lookup occurs after the empty-text guard at lines 3116-3119. A user who quote-replies to an attachment with only an @bot mention is stripped to empty and returned before this runs; move hydration before that guard (or make the guard parent-media-aware) and add that regression case.