Skip to content

Commit

Permalink
[Praises] Enrich backend call to add slack details on people
Browse files Browse the repository at this point in the history
  • Loading branch information
gregv committed Oct 10, 2024
1 parent aa74882 commit 25efa2c
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 4 deletions.
32 changes: 28 additions & 4 deletions api/messages/messages_service.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from common.utils import safe_get_env_var
from common.utils.slack import send_slack_audit, create_slack_channel, send_slack, invite_user_to_channel
from common.utils.slack import send_slack_audit, create_slack_channel, send_slack, invite_user_to_channel, get_user_info
from common.utils.firebase import get_hackathon_by_event_id, upsert_news, upsert_praise, get_github_contributions_for_user,get_volunteer_from_db_by_event, get_user_by_user_id, get_recent_praises, get_praises_by_user_id
from common.utils.openai_api import generate_and_save_image_to_cdn
from common.utils.github import create_github_repo, get_all_repos, validate_github_username
Expand Down Expand Up @@ -1120,7 +1120,7 @@ def single_add_volunteer(event_id, json, volunteer_type, propel_id):
doc = db.collection('volunteers').add(json)

get_volunteer_by_event.cache_clear()

return Message(
"Added Hackathon Volunteer"
)
Expand Down Expand Up @@ -1289,11 +1289,24 @@ def save_praise(json):


@cached(cache=TTLCache(maxsize=100, ttl=600))
def get_all_praises():

def get_all_praises():
# Get the praises about user with user_id
results = get_recent_praises()

# Get unique list of praise_sender and praise_receiver
slack_ids = set()
for r in results:
slack_ids.add(r["praise_receiver"])
slack_ids.add(r["praise_sender"])

logger.info(f"SlackIDS: {slack_ids}")
slack_user_info = get_user_info(slack_ids)
logger.info(f"Slack User Info; {slack_user_info}")

for r in results:
r['praise_receiver_details'] = slack_user_info[r['praise_receiver']]
r['praise_sender_details'] = slack_user_info[r['praise_sender']]

logger.info(f"Here are the 20 most recently written praises: {results}")
return Message(results)

Expand All @@ -1303,6 +1316,17 @@ def get_praises_about_user(user_id):
# Get the praises about user with user_id
results = get_praises_by_user_id(user_id)

slack_ids = set()
for r in results:
slack_ids.add(r["praise_receiver"])
slack_ids.add(r["praise_sender"])
logger.info(f"Slack IDs: {slack_ids}")
slack_user_info = get_user_info(slack_ids)
logger.info(f"Slack User Info: {slack_user_info}")
for r in results:
r['praise_receiver_details'] = slack_user_info[r['praise_receiver']]
r['praise_sender_details'] = slack_user_info[r['praise_sender']]

logger.info(f"Here are all praises related to {user_id}: {results}")
return Message(results)

Expand Down
48 changes: 48 additions & 0 deletions common/utils/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
from slack_sdk.errors import SlackApiError
from dotenv import load_dotenv
from requests.exceptions import ConnectionError
from cachetools import TTLCache, cached
from ratelimit import limits, sleep_and_retry

load_dotenv()

SLACK_URL = safe_get_env_var("SLACK_WEBHOOK")
Expand Down Expand Up @@ -188,3 +191,48 @@ def send_slack(message="", channel="", icon_emoji=None, username="Hackathon Bot"
logger.error(e.response["error"])
assert e.response["error"]




# Assuming 50 calls per minute for Slack API
CALLS = 50
RATE_LIMIT = 60

@sleep_and_retry
@limits(calls=CALLS, period=RATE_LIMIT)
def rate_limited_get_user_info(user_id):
client = get_client()
try:
result = client.users_info(user=user_id)
return result["user"]
except SlackApiError as e:
logger.error(f"Error fetching info for user {user_id}: {e}")
return None

def get_user_info(user_ids):
"""
Fetch user information for a list of Slack user IDs.
:param user_ids: List of Slack user IDs (e.g., ["U049S78NLCA", "U049S78NLCB"])
:return: Dictionary of user information, keyed by user ID
"""
client = get_client()

# Fetch user info for all unique slack_ids
users_info = {}
for user_id in user_ids:
user_info = rate_limited_get_user_info(user_id)
if user_info:
users_info[user_id] = {
"id": user_info["id"],
"name": user_info["name"],
"real_name": user_info.get("real_name", ""),
"display_name": user_info["profile"].get("display_name", ""),
"email": user_info["profile"].get("email", ""),
"is_admin": user_info.get("is_admin", False),
"is_owner": user_info.get("is_owner", False),
"is_bot": user_info.get("is_bot", False),
"updated": user_info.get("updated", 0)
}

return users_info

0 comments on commit 25efa2c

Please sign in to comment.