diff --git a/pyslackersweb/contexts.py b/pyslackersweb/contexts.py index 6bfd6539..79585b46 100644 --- a/pyslackersweb/contexts.py +++ b/pyslackersweb/contexts.py @@ -2,6 +2,7 @@ from aiohttp import ClientSession, web from apscheduler.schedulers.asyncio import AsyncIOScheduler +from slack.io.aiohttp import SlackAPI from . import tasks @@ -33,4 +34,5 @@ async def background_jobs(app: web.Application) -> None: async def client_session(app: web.Application) -> None: async with ClientSession(raise_for_status=True) as session: app["client_session"] = session + app["slack_client"] = SlackAPI(token=app["slack_token"], session=session) yield diff --git a/pyslackersweb/tasks.py b/pyslackersweb/tasks.py index fddd4255..d3f88552 100644 --- a/pyslackersweb/tasks.py +++ b/pyslackersweb/tasks.py @@ -3,6 +3,8 @@ from collections import Counter from typing import List +import slack + from aiohttp import web from .util.log import ContextAwareLoggerAdapter @@ -69,7 +71,7 @@ async def _sync_github() -> None: def sync_slack_users(app: web.Application): - session = app["client_session"] + client = app["slack_client"] async def _sync_slack_users(): logger.debug("Refreshing slack users cache.") @@ -81,30 +83,11 @@ async def _sync_slack_users(): try: counter = Counter() - while True: - params = {} - async with session.get( - "https://slack.com/api/users.list", - headers={"Authorization": f"Bearer {oauth_token}"}, - params=params, - ) as r: - result = await r.json() - - if not result["ok"]: - logger.error("Error fetching users from slack", extra=result) - return - - for user in result["members"]: - if user["deleted"] or user["is_bot"] or not user["tz"]: - continue - - counter[user["tz"]] += 1 - - # next_cursor can be an empty string. We need to check if the value is truthy - if result.get("response_metadata", {}).get("next_cursor"): - params["cursor"] = result["response_metadata"]["next_cursor"] - else: - break + async for user in client.iter(slack.methods.USERS_LIST): + if user["deleted"] or user["is_bot"] or not user["tz"]: + continue + + counter[user["tz"]] += 1 logger.debug( "Found %s users across %s timezones", @@ -124,7 +107,7 @@ async def _sync_slack_users(): def sync_slack_channels(app: web.Application): - session = app["client_session"] + client = app["slack_client"] async def _sync_slack_channel(): logger.debug("Refreshing slack channels cache.") @@ -136,35 +119,16 @@ async def _sync_slack_channel(): try: channels = [] - while True: - params = {} - async with session.get( - "https://slack.com/api/channels.list", - headers={"Authorization": f"Bearer {oauth_token}"}, - params=params, - ) as r: - result = await r.json() - - if not result["ok"]: - logger.error("Error fetching channels from slack", extra=result) - return - - for channel in result["channels"]: - channels.append( - Channel( - id=channel["id"], - name=channel["name"], - topic=channel["topic"]["value"], - purpose=channel["purpose"]["value"], - members=channel["num_members"], - ) - ) - - # next_cursor can be an empty string. We need to check if the value is truthy - if result.get("response_metadata", {}).get("next_cursor"): - params["cursor"] = result["response_metadata"]["next_cursor"] - else: - break + async for channel in client.iter(slack.methods.CHANNELS_LIST): + channels.append( + Channel( + id=channel["id"], + name=channel["name"], + topic=channel["topic"]["value"], + purpose=channel["purpose"]["value"], + members=channel["num_members"], + ) + ) logger.debug("Found %s slack channels", len(channels)) diff --git a/requirements/base.txt b/requirements/base.txt index dbc2a55b..c042ee1c 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -7,3 +7,4 @@ apscheduler==3.6.0 gunicorn==19.9.0 marshmallow==3.0.0rc7 sentry-sdk==0.9.0 +slack-sansio==1.0.0