"""Microsoft Teams 群组管理。 addParticipant / removeParticipant / renameGroup 操作。 """ from __future__ import annotations from typing import Any, TYPE_CHECKING if TYPE_CHECKING: from .graph import GraphClient from yuxi.utils.logging_config import logger async def add_participant( client: GraphClient, chat_id: str, user_id: str, role: str = "member", ) -> dict[str, Any]: path = f"/chats/{chat_id}/members" data = { "@odata.type": "#microsoft.graph.aadUserConversationMember", "roles": [role], "user@odata.bind": f"https://graph.microsoft.com/v1.0/users('{user_id}')", } result = await client._request_with_retry("POST", path, json_data=data) if "error" not in result: logger.info(f"MSTeams participant added: {user_id} to {chat_id}") return result async def remove_participant( client: GraphClient, chat_id: str, membership_id: str, ) -> dict[str, Any]: path = f"/chats/{chat_id}/members/{membership_id}" result = await client._request_with_retry("DELETE", path) if "error" not in result: logger.info(f"MSTeams participant removed: {membership_id} from {chat_id}") return result async def rename_group( client: GraphClient, chat_id: str, new_name: str, ) -> dict[str, Any]: path = f"/chats/{chat_id}" data = {"topic": new_name} result = await client._request_with_retry("PATCH", path, json_data=data) if "error" not in result: logger.info(f"MSTeams group renamed: {chat_id} -> {new_name}") return result async def list_members( client: GraphClient, chat_id: str, ) -> list[dict[str, Any]]: path = f"/chats/{chat_id}/members" result = await client._request_with_retry("GET", path) if "error" in result: return [] return result.get("value", []) async def get_member_info( client: GraphClient, chat_id: str, user_id: str, ) -> dict[str, Any] | None: members = await list_members(client, chat_id) for member in members: if member.get("userId", "") == user_id: return member return None