gabotorresruiz commented on code in PR #35832:
URL: https://github.com/apache/superset/pull/35832#discussion_r2485207244


##########
superset/utils/slack.py:
##########
@@ -117,22 +116,151 @@ def get_channels() -> list[SlackChannelSchema]:
         raise
 
 
+def _fetch_channels_without_search(
+    client: WebClient,
+    channel_schema: SlackChannelSchema,
+    types_param: str,
+    cursor: Optional[str],
+    limit: int,
+) -> dict[str, Any]:
+    """Fetch channels without search filtering, paginating for large limits."""
+    channels: list[SlackChannelSchema] = []
+    slack_cursor = cursor
+    page_size = min(limit, 200)
+
+    while True:
+        response = client.conversations_list(
+            limit=page_size,
+            cursor=slack_cursor,
+            exclude_archived=True,
+            types=types_param,
+        )
+
+        page_channels = [
+            channel_schema.load(channel) for channel in 
response.data["channels"]
+        ]
+        channels.extend(page_channels)
+
+        slack_cursor = response.data.get("response_metadata", 
{}).get("next_cursor")
+
+        if not slack_cursor or len(page_channels) < page_size or len(channels) 
>= limit:
+            break
+
+    return {
+        "result": channels[:limit],
+        "next_cursor": slack_cursor,
+        "has_more": bool(slack_cursor),
+    }
+
+
+def _fetch_channels_with_search(
+    client: WebClient,
+    channel_schema: SlackChannelSchema,
+    types_param: str,
+    search_string: str,
+    exact_match: bool,
+    cursor: Optional[str],
+    limit: int,
+) -> dict[str, Any]:
+    """Fetch channels with search filtering, streaming through pages."""
+    matches: list[SlackChannelSchema] = []
+    slack_cursor = cursor
+    max_pages_to_fetch = 50
+    pages_fetched = 0
+    search_string_lower = search_string.lower()
+
+    while len(matches) < limit and pages_fetched < max_pages_to_fetch:
+        response = client.conversations_list(
+            limit=200,
+            cursor=slack_cursor,
+            exclude_archived=True,
+            types=types_param,
+        )
+
+        for channel_data in response.data["channels"]:
+            channel_name_lower = channel_data["name"].lower()
+            channel_id_lower = channel_data["id"].lower()
+
+            is_match = False
+            if exact_match:
+                is_match = (
+                    search_string_lower == channel_name_lower
+                    or search_string_lower == channel_id_lower
+                )
+            else:
+                is_match = (
+                    search_string_lower in channel_name_lower
+                    or search_string_lower in channel_id_lower
+                )
+
+            if is_match:
+                channel = channel_schema.load(channel_data)
+                matches.append(channel)
+
+            if len(matches) >= limit:
+                break
+
+        slack_cursor = response.data.get("response_metadata", 
{}).get("next_cursor")
+        if not slack_cursor:
+            break
+
+        pages_fetched += 1
+
+    has_more = bool(slack_cursor) and len(matches) >= limit
+
+    return {
+        "result": matches[:limit],
+        "next_cursor": slack_cursor if has_more else None,
+        "has_more": has_more,
+    }
+
+
 def get_channels_with_search(
     search_string: str = "",
     types: Optional[list[SlackChannelTypes]] = None,
     exact_match: bool = False,
-    force: bool = False,
-) -> list[SlackChannelSchema]:
+    cursor: Optional[str] = None,
+    limit: int = 100,
+) -> dict[str, Any]:
     """
-    The slack api is paginated but does not include search, so we need to fetch
-    all channels and filter them ourselves
-    This will search by slack name or id
+    Fetches Slack channels with pagination and search support.
+
+    Returns a dict with:
+    - result: list of channels
+    - next_cursor: cursor for next page (None if no more pages)
+    - has_more: boolean indicating if more pages exist
+
+    The Slack API is paginated but does not include search.
+    We handle two cases:
+    1. WITHOUT search: Fetch single page from Slack API
+    2. WITH search: Stream through Slack API pages until we find enough matches
+       (stops early to prevent timeouts on large workspaces)
     """
     try:
-        channels = get_channels(

Review Comment:
   Good catch! You're right, I can remove `get_channels` function just making a 
small change to the `cache_channels` task. Once I do that, we can get rid of 
`get_channels` function



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to