ForcePilot/backend/package/yuxi/channels/adapters/urbit/targets.py

44 lines
1.4 KiB
Python
Raw Normal View History

from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
pass
def parse_tlon_target(target: str) -> dict[str, str]:
target = target.strip().lstrip("~")
if target.startswith("tlon:dm/"):
return {"chat_type": "direct", "peer_id": target[len("tlon:dm/") :]}
if target.startswith("dm/"):
return {"chat_type": "direct", "peer_id": target[3:]}
if target.startswith("group:chat/"):
parts = target[len("group:chat/") :].split("/")
if len(parts) >= 2:
return {"chat_type": "group", "group_name": "/".join(parts), "channel_type": "chat"}
return {"chat_type": "group", "group_name": target[len("group:chat/") :]}
if target.startswith("group:"):
return {"chat_type": "group", "group_name": target[6:]}
nest_match = target.split("/")
if len(nest_match) >= 2 and nest_match[0] in ("chat", "diary", "heap"):
return {
"chat_type": "group",
"channel_type": nest_match[0],
"group_name": "/".join(nest_match[1:]),
}
return {"chat_type": "direct", "peer_id": target}
def format_target_hint(chat_type: str, channel_id: str, host_ship: str = "") -> str:
if chat_type == "direct":
return f"dm/~{channel_id.lstrip('~')}"
host = host_ship.lstrip("~")
return f"~{host}/{channel_id} | chat/~{host}/{channel_id} | group:~{host}/{channel_id}"