Groups now tells user if they are in group
This commit is contained in:
parent
1fc97b6256
commit
595348fbd6
1 changed files with 28 additions and 4 deletions
|
|
@ -49,10 +49,22 @@ def get_keycloak_client() -> KeycloakAdmin:
|
|||
verify=True,
|
||||
)
|
||||
|
||||
def get_requestable_groups() -> tuple[bool, str]:
|
||||
"""Fetches available Keycloak groups, filtering out the 'admin' group."""
|
||||
def get_requestable_groups(username: str) -> tuple[bool, str]:
|
||||
"""Fetches available Keycloak groups, filtering out the 'admin' group, and checks user membership."""
|
||||
try:
|
||||
kc = get_keycloak_client()
|
||||
|
||||
# 1. Fetch User UUID to get their current groups
|
||||
users = kc.get_users(query={"username": username, "exact": True})
|
||||
if not users:
|
||||
# If the user doesn't exist in Keycloak, we just show groups normally
|
||||
user_group_names = set()
|
||||
else:
|
||||
user_id = users[0]["id"]
|
||||
user_groups = kc.get_user_groups(user_id=user_id)
|
||||
user_group_names = {g["name"] for g in user_groups}
|
||||
|
||||
# 2. Fetch all groups
|
||||
groups = kc.get_groups()
|
||||
|
||||
# Filter out 'admin' (case-insensitive)
|
||||
|
|
@ -61,7 +73,15 @@ def get_requestable_groups() -> tuple[bool, str]:
|
|||
if not valid_groups:
|
||||
return True, "No groups are currently available to request."
|
||||
|
||||
group_list = "\n".join([f"• `{name}`" for name in valid_groups])
|
||||
# 3. Format the output list with membership status
|
||||
group_lines = []
|
||||
for name in valid_groups:
|
||||
if name in user_group_names:
|
||||
group_lines.append(f"• `{name}` *(Already a member)*")
|
||||
else:
|
||||
group_lines.append(f"• `{name}`")
|
||||
|
||||
group_list = "\n".join(group_lines)
|
||||
return True, f"**Available Groups:**\n{group_list}"
|
||||
|
||||
except KeycloakError as e:
|
||||
|
|
@ -126,9 +146,13 @@ async def on_message(client: AsyncClient, room: MatrixRoom, event: RoomMessageTe
|
|||
|
||||
body = event.body.strip()
|
||||
|
||||
# Extract username early so both commands can utilize it
|
||||
matrix_user = event.sender
|
||||
kc_username = matrix_user.split(":")[0].lstrip("@")
|
||||
|
||||
# --- COMMAND: !groups ---
|
||||
if body.startswith("!groups"):
|
||||
success, msg = get_requestable_groups()
|
||||
success, msg = get_requestable_groups(kc_username)
|
||||
await send_message_safe(
|
||||
client,
|
||||
room_id=room.room_id,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue