Skip to content

Commit

Permalink
slack-sansio
Browse files Browse the repository at this point in the history
  • Loading branch information
ovv committed Jun 20, 2019
1 parent ccad1c6 commit 2e3d281
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 55 deletions.
2 changes: 2 additions & 0 deletions pyslackersweb/contexts.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from aiohttp import ClientSession, web
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from slack.io.aiohttp import SlackAPI

from . import tasks

Expand Down Expand Up @@ -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
74 changes: 19 additions & 55 deletions pyslackersweb/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from collections import Counter
from typing import List

import slack

from aiohttp import web

from .util.log import ContextAwareLoggerAdapter
Expand Down Expand Up @@ -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.")
Expand All @@ -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",
Expand All @@ -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.")
Expand All @@ -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))

Expand Down
1 change: 1 addition & 0 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 2e3d281

Please sign in to comment.