Skip to content
This repository has been archived by the owner on Jun 18, 2024. It is now read-only.

Commit

Permalink
Fix for the rate limiting problem
Browse files Browse the repository at this point in the history
This avoids to fetch all the channels for the history and just obtains
the joined ones.

Closes: #440
  • Loading branch information
ltworf committed Nov 10, 2023
1 parent f2aa592 commit ae369f7
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ async def _history(self) -> None:
log(f'Last known timestamp {dt}')

chats: Sequence[IM|Channel] = []
chats += await self.channels() + await self.get_ims() # type: ignore
chats += await self.joined_channels() + await self.get_ims() # type: ignore
for channel in chats:
if isinstance(channel, Channel):
if not channel.is_member:
Expand Down Expand Up @@ -600,6 +600,36 @@ async def get_members(self, channel: str|Channel) -> set[str]:
self._get_members_cache_cursor[id_] = r.get('response_metadata', {}).get('next_cursor')
return self._get_members_cache[id_]

async def joined_channels(self) -> list[Channel]:
"""
Returns the list of the joined slack channels.
Not cached
"""
result: list[Channel] = []
cursor = None
while True:
r = await self.client.api_call(
'users.conversations',
cursor=cursor,
exclude_archived=True,
types='public_channel,private_channel,mpim',
limit=1000, # In vain hope that slack would not ignore this
)
response = self.tload(r, Response)

if response.ok:
conv = self.tload(r, Conversations)
result += conv.channels
# For this API, slack sends an empty string as next cursor, just to show off their programming "skillz"
if not conv.response_metadata or not conv.response_metadata.next_cursor:
break
cursor = conv.response_metadata.next_cursor
else:
raise ResponseException(response.error)
return result


async def channels(self, refresh: bool = False) -> list[Channel]:
"""
Returns the list of slack channels
Expand Down

0 comments on commit ae369f7

Please sign in to comment.