ubottu-web/ubottu/launchpad/utils.py

82 lines
2.8 KiB
Python
Raw Permalink Normal View History

import redis
import json
2024-04-05 04:53:31 +00:00
import traceback
2024-09-20 12:23:39 +00:00
from .launchpad_singleton import get_launchpad
# Connect to Redis
cache = redis.Redis(host='localhost', port=6379, db=0)
def fetch_matrix_accounts(profile_id):
try:
2024-09-20 12:23:39 +00:00
# Try to fetch from cache first
cached_result = cache.get(f"matrix_{profile_id}")
if cached_result:
return json.loads(cached_result)
2024-09-20 12:23:39 +00:00
# Fetch Launchpad API data
launchpad = get_launchpad()
person = launchpad.people[profile_id]
2024-09-20 12:23:39 +00:00
# Fetch social accounts by platform (Matrix)
matrix_accounts = person.getSocialAccountsByPlatform(platform='Matrix platform')
# Extract the Matrix IDs
matrix_ids = []
2024-09-20 12:23:39 +00:00
for account in matrix_accounts:
username = account['identity']['username']
homeserver = account['identity']['homeserver']
matrix_id = f"@{username}:{homeserver}"
matrix_ids.append(matrix_id)
2024-09-20 12:23:39 +00:00
# Cache the result with expiration time of 30 minutes (1800 seconds)
cache.setex(f"matrix_{profile_id}", 1800, json.dumps(matrix_ids))
2024-09-20 12:23:39 +00:00
2024-07-18 14:39:49 +00:00
return matrix_ids
except KeyError as e:
2024-09-20 12:23:39 +00:00
print(f"Profile with name {profile_id} was not found. Error: {e}")
print(traceback.format_exc())
return False
except Exception as e:
print(f"An error occurred: {e}")
print(traceback.format_exc())
return False
2024-04-05 03:49:37 +00:00
2024-04-05 20:04:37 +00:00
def fetch_group_members(group_name, recurse=False):
try:
2024-09-20 12:23:39 +00:00
# Try to fetch from cache first
cached_result = cache.get(f"group_members_{group_name}")
if cached_result:
return json.loads(cached_result)
2024-09-20 12:23:39 +00:00
# Fetch the group from the Launchpad API
launchpad = get_launchpad()
group = launchpad.people[group_name]
2024-04-05 03:49:37 +00:00
2024-04-05 04:53:31 +00:00
group_members = set()
2024-04-05 03:49:37 +00:00
for person in group.members:
2024-04-05 20:04:37 +00:00
if not person.is_team:
group_members.add(person.name)
2024-09-20 12:23:39 +00:00
# Optional recursion to get sub-teams' members
elif recurse:
sub_group_members = fetch_group_members(person.name, recurse=True)
group_members.update(sub_group_members['group_members'])
2024-04-05 20:04:37 +00:00
2024-09-20 12:23:39 +00:00
# Generate MXIDs for individual members
mxids = [f"@{member}:ubuntu.com" for member in group_members]
2024-04-05 04:53:31 +00:00
result = {'group_members': tuple(group_members), 'group_name': group_name, 'mxids': mxids}
# Cache the result with expiration time of 30 minutes (1800 seconds)
cache.setex(f"group_members_{group_name}", 1800, json.dumps(result))
return result
except KeyError as e:
print(f"Group with name {group_name} was not found. Error: {e}")
2024-04-05 04:53:31 +00:00
print(traceback.format_exc())
return False
except Exception as e:
print(f"An error occurred: {e}")
2024-04-05 04:53:31 +00:00
print(traceback.format_exc())
return False