From 25efa2ce760ac4f2da61ee55c84845c9daedb444 Mon Sep 17 00:00:00 2001 From: Greg V Date: Wed, 9 Oct 2024 21:17:33 -0700 Subject: [PATCH] [Praises] Enrich backend call to add slack details on people --- api/messages/messages_service.py | 32 ++++++++++++++++++--- common/utils/slack.py | 48 ++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 4 deletions(-) diff --git a/api/messages/messages_service.py b/api/messages/messages_service.py index 4099644..fc0f075 100644 --- a/api/messages/messages_service.py +++ b/api/messages/messages_service.py @@ -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 @@ -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" ) @@ -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) @@ -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) diff --git a/common/utils/slack.py b/common/utils/slack.py index dc85a4e..783e058 100644 --- a/common/utils/slack.py +++ b/common/utils/slack.py @@ -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") @@ -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 \ No newline at end of file