Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions elementary/messages/messaging_integrations/slack_web.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import time
from typing import Any, Dict, Iterator, Optional

from pydantic import BaseModel
Expand Down Expand Up @@ -128,22 +129,32 @@ def _handle_send_err(self, err: SlackApiError, channel_name: str):
@sleep_and_retry
@limits(calls=20, period=ONE_MINUTE)
def _iter_channels(
self, cursor: Optional[str] = None, only_public: bool = False
self,
cursor: Optional[str] = None,
only_public: bool = False,
timeout: float = 300.0,
) -> Iterator[dict]:
if timeout <= 0:
raise MessagingIntegrationError("Channel iteration timed out")

call_start = time.time()
response = self.client.conversations_list(
cursor=cursor,
types="public_channel" if only_public else "public_channel,private_channel",
exclude_archived=True,
limit=1000,
)
call_duration = time.time() - call_start

channels = response["channels"]
yield from channels
response_metadata = response.get("response_metadata") or {}
next_cursor = response_metadata.get("next_cursor")
if next_cursor:
if not isinstance(next_cursor, str):
raise ValueError("Next cursor is not a string")
yield from self._iter_channels(next_cursor, only_public)
timeout_left = timeout - call_duration
yield from self._iter_channels(next_cursor, only_public, timeout_left)

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