from __future__ import annotations from yuxi.channels.adapters.signal.client import RpcClient, RpcError from yuxi.channels.adapters.signal.identity import get_identities async def list_peers( rpc_client: RpcClient, account: str, query: str | None = None, trust_level: str | None = None, ) -> list[dict]: try: identities = await get_identities(rpc_client, account) result = [] for i in identities: entry = { "number": i.get("number", ""), "name": i.get("name", ""), "trust_level": i.get("trustLevel", "UNTRUSTED"), "fingerprint": i.get("fingerprint", ""), } if query and not _match_query(entry, query): continue if trust_level and entry["trust_level"].upper() != trust_level.upper(): continue result.append(entry) return result except RpcError: return [] async def list_groups( rpc_client: RpcClient, account: str, name_filter: str | None = None, ) -> list[dict]: try: result = await rpc_client.call("listGroups", {"account": account}) groups = result.get("groups", []) entries = [] for g in groups: entry = { "group_id": g.get("groupId", g.get("id", "")), "name": g.get("name", ""), "description": g.get("description", ""), "member_count": g.get("memberCount", 0), } if name_filter and name_filter.lower() not in entry["name"].lower(): continue entries.append(entry) return entries except RpcError: return [] def _match_query(entry: dict, query: str) -> bool: q = query.lower() return ( q in entry.get("number", "").lower() or q in entry.get("name", "").lower() or q in entry.get("fingerprint", "").lower() )