"""Microsoft Teams 频道路由配置。 resolveMSTeamsRouteConfig,id + name + slug 候选键匹配, wildcard fallback 支持。 """ from __future__ import annotations from typing import Any def resolve_route_config( config: dict[str, Any], team_id: str = "", team_name: str = "", channel_id: str = "", channel_name: str = "", ) -> dict[str, Any]: routes = config.get("routes", []) or [] if not routes: return config for route in routes: if _match_route(route, team_id, team_name, channel_id, channel_name): merged = {**config, **route.get("config", {})} return merged wildcard = _find_wildcard_route(routes) if wildcard: return {**config, **wildcard.get("config", {})} return config def _match_route( route: dict[str, Any], team_id: str, team_name: str, channel_id: str, channel_name: str, ) -> bool: match_team = route.get("match_team", {}) or {} match_channel = route.get("match_channel", {}) or {} team_match = _match_candidate( match_team, {"id": team_id, "name": team_name}, ) if match_team and not team_match: return False channel_match = _match_candidate( match_channel, {"id": channel_id, "name": channel_name}, ) if match_channel and not channel_match: return False return True def _match_candidate( matcher: dict[str, str], candidate: dict[str, str], ) -> bool: if not matcher: return True if matcher.get("id") and matcher["id"] != candidate.get("id"): return False if matcher.get("name") and matcher["name"].lower() != (candidate.get("name", "")).lower(): return False slug = matcher.get("slug", "") if slug: name_slug = (candidate.get("name", "")).lower().replace(" ", "-") if slug != name_slug: return False return True def _find_wildcard_route( routes: list[dict[str, Any]], ) -> dict[str, Any] | None: for route in routes: if route.get("match_team", {}).get("id") == "*": return route return None