-
Notifications
You must be signed in to change notification settings - Fork 1
fix(slack): route empty-DM thread_ts fetch to conversations.history (#138) #175
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4317,15 +4317,21 @@ async def fetch_messages(self, thread_id: str, options: FetchOptions | None = No | |
| thread_ts = decoded.thread_ts | ||
| direction = getattr(opts, "direction", "backward") or "backward" | ||
| limit = getattr(opts, "limit", 100) if getattr(opts, "limit", 100) is not None else 100 | ||
| cursor = getattr(opts, "cursor", None) | ||
|
|
||
| # Divergence (chat-sdk-python#138): a top-level DM root encodes an empty | ||
| # thread_ts (slack:Dxxx:). conversations.replies(ts="") returns no replies | ||
| # and loses the DM root context, so route empty thread_ts to the channel | ||
| # history path (conversations.history), where the channel *is* the | ||
| # conversation. Upstream's fetchMessages has no empty-thread_ts guard. | ||
| try: | ||
| if not thread_ts: | ||
| if direction == "forward": | ||
| return await self._fetch_channel_messages_forward(channel, limit, cursor) | ||
| return await self._fetch_channel_messages_backward(channel, limit, cursor) | ||
|
Comment on lines
+4330
to
+4331
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.
For an empty-DM thread like Useful? React with 👍 / 👎. |
||
| if direction == "forward": | ||
| return await self._fetch_messages_forward( | ||
| channel, thread_ts, thread_id, limit, getattr(opts, "cursor", None) | ||
| ) | ||
| return await self._fetch_messages_backward( | ||
| channel, thread_ts, thread_id, limit, getattr(opts, "cursor", None) | ||
| ) | ||
| return await self._fetch_messages_forward(channel, thread_ts, thread_id, limit, cursor) | ||
| return await self._fetch_messages_backward(channel, thread_ts, thread_id, limit, cursor) | ||
| except Exception as error: | ||
| self._handle_slack_error(error) | ||
|
|
||
|
|
@@ -4383,9 +4389,16 @@ async def fetch_message(self, thread_id: str, message_id: str) -> Message | None | |
|
|
||
| try: | ||
| client = self._get_client() | ||
| result = await client.conversations_replies( | ||
| channel=channel, ts=thread_ts, oldest=message_id, inclusive=True, limit=1 | ||
| ) | ||
| # Divergence (chat-sdk-python#138): a DM root encodes an empty | ||
| # thread_ts, so conversations.replies(ts="") cannot locate the | ||
| # message. Fetch the single message from conversations.history | ||
| # instead (mirrors the link-preview fetch_message at ~3293). | ||
| if not thread_ts: | ||
| result = await client.conversations_history(channel=channel, latest=message_id, inclusive=True, limit=1) | ||
| else: | ||
| result = await client.conversations_replies( | ||
| channel=channel, ts=thread_ts, oldest=message_id, inclusive=True, limit=1 | ||
| ) | ||
| messages = result.get("messages", []) | ||
| target = next((m for m in messages if m.get("ts") == message_id), None) | ||
| if not target: | ||
|
|
||
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.
When
thread_tsis empty (such as for top-level DM roots),fetch_messagesroutes the request to_fetch_channel_messages_forwardor_fetch_channel_messages_backward. However, these helper methods dynamically construct the messagethread_idusingmsg.get('thread_ts') or msg.get('ts', ''). For top-level DM messages, this falls back to the message's ownts, resulting in a thread ID likeslack:Dxxx:tsinstead of the expectedslack:Dxxx:(with an emptythread_ts). This creates a discrepancy between real-time messages (which correctly useslack:Dxxx:) and fetched history messages.We can resolve this by post-processing the fetched messages to ensure that any top-level DM messages (those without an explicit
thread_tsin their raw payload) are assigned the correctthread_id.