Skip to content

Commit 5d82de8

Browse files
committed
Enhance SlackWebMessagingIntegration to include a timeout feature for channel iteration, raising an error if the timeout is exceeded.
1 parent 4288f49 commit 5d82de8

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

elementary/messages/messaging_integrations/slack_web.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import time
23
from typing import Any, Dict, Iterator, Optional
34

45
from pydantic import BaseModel
@@ -128,22 +129,32 @@ def _handle_send_err(self, err: SlackApiError, channel_name: str):
128129
@sleep_and_retry
129130
@limits(calls=20, period=ONE_MINUTE)
130131
def _iter_channels(
131-
self, cursor: Optional[str] = None, only_public: bool = False
132+
self,
133+
cursor: Optional[str] = None,
134+
only_public: bool = False,
135+
timeout: float = 300.0,
132136
) -> Iterator[dict]:
137+
if timeout <= 0:
138+
raise MessagingIntegrationError("Channel iteration timed out")
139+
140+
call_start = time.time()
133141
response = self.client.conversations_list(
134142
cursor=cursor,
135143
types="public_channel" if only_public else "public_channel,private_channel",
136144
exclude_archived=True,
137145
limit=1000,
138146
)
147+
call_duration = time.time() - call_start
148+
139149
channels = response["channels"]
140150
yield from channels
141151
response_metadata = response.get("response_metadata") or {}
142152
next_cursor = response_metadata.get("next_cursor")
143153
if next_cursor:
144154
if not isinstance(next_cursor, str):
145155
raise ValueError("Next cursor is not a string")
146-
yield from self._iter_channels(next_cursor, only_public)
156+
timeout_left = timeout - call_duration
157+
yield from self._iter_channels(next_cursor, only_public, timeout_left)
147158

148159
def _get_channel_id(self, channel_name: str, only_public: bool = False) -> str:
149160
for channel in self._iter_channels(only_public=only_public):

0 commit comments

Comments
 (0)