Vitor-Avila commented on code in PR #32529:
URL: https://github.com/apache/superset/pull/32529#discussion_r1983814388
##########
superset/utils/slack.py:
##########
@@ -54,60 +56,82 @@ def get_slack_client() -> WebClient:
return client
+@cache_util.memoized_func(
+ key="slack_conversations_list",
+ cache=cache_manager.cache,
+)
+def get_channels(limit: int, extra_params: dict[str, Any]) ->
list[SlackChannelSchema]:
+ """
+ Retrieves a list of all conversations accessible by the bot
+ from the Slack API, and caches results (to avoid rate limits).
+
+ The Slack API does not provide search so to apply a search use
+ get_channels_with_search instead.
+ """
+ client = get_slack_client()
+ channel_schema = SlackChannelSchema()
+ channels: list[SlackChannelSchema] = []
+ cursor = None
+
+ while True:
+ response = client.conversations_list(
+ limit=limit, cursor=cursor, exclude_archived=True, **extra_params
+ )
+ channels.extend(
+ channel_schema.load(channel) for channel in
response.data["channels"]
+ )
+ cursor = response.data.get("response_metadata", {}).get("next_cursor")
+ if not cursor:
+ break
+
+ return channels
+
+
def get_channels_with_search(
search_string: str = "",
limit: int = 999,
types: Optional[list[SlackChannelTypes]] = None,
exact_match: bool = False,
+ force: bool = False,
) -> list[SlackChannelSchema]:
"""
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
"""
-
+ extra_params = {}
+ extra_params["types"] = ",".join(types) if types else None
try:
- client = get_slack_client()
- channel_schema = SlackChannelSchema()
- channels: list[SlackChannelSchema] = []
- cursor = None
- extra_params = {}
- extra_params["types"] = ",".join(types) if types else None
-
- while True:
- response = client.conversations_list(
- limit=limit, cursor=cursor, exclude_archived=True,
**extra_params
- )
- channels.extend(
- channel_schema.load(channel) for channel in
response.data["channels"]
- )
- cursor = response.data.get("response_metadata",
{}).get("next_cursor")
- if not cursor:
- break
-
- # The search string can be multiple channels separated by commas
- if search_string:
- search_array = recipients_string_to_list(search_string)
- channels = [
- channel
- for channel in channels
- if any(
- (
- search.lower() == channel["name"].lower()
- or search.lower() == channel["id"].lower()
- if exact_match
- else (
- search.lower() in channel["name"].lower()
- or search.lower() in channel["id"].lower()
- )
- )
- for search in search_array
- )
- ]
- return channels
+ channels = get_channels(
+ limit=limit,
+ extra_params=extra_params,
+ force=force,
+ cache_timeout=86400,
Review Comment:
Hardcoded here for a day, which I think should be generally good. I could
make this a dynamic config in a follow up if we think it's better.
--
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]