ForcePilot/backend/package/yuxi/channel/extensions/tlon/cite_parser.py

62 lines
1.6 KiB
Python
Raw Normal View History

from dataclasses import dataclass
@dataclass
class Cite:
type: str
ship: str | None = None
group_flag: str | None = None
channel_nest: str | None = None
msg_id: str | None = None
def parse_cite(raw: str) -> Cite | None:
if not raw:
return None
if raw.startswith("~"):
parts = raw.split("/")
if len(parts) >= 2:
second = parts[1]
if second.isdigit():
return Cite(type="message", ship=parts[0], msg_id=raw)
return Cite(type="dm", ship=parts[0])
return Cite(type="ship", ship=raw)
if raw.startswith("chat/"):
parts = raw.split("/")
if len(parts) >= 3:
return Cite(
type="channel",
channel_nest=raw,
ship=parts[1],
)
if "/" in raw and raw.split("/")[0].startswith("~"):
parts = raw.split("/")
ship = parts[0]
if len(parts) >= 2:
return Cite(type="message", ship=ship, msg_id=raw)
return None
def resolve_cite_text(cite: Cite) -> str:
if cite.type == "ship":
return f"@{cite.ship}"
elif cite.type == "dm":
return f"@{cite.ship}"
elif cite.type == "channel":
return f"#{cite.channel_nest}"
elif cite.type == "message":
return f"{cite.ship}"
return ""
async def resolve_all_cites(raw_cites: list[str]) -> list[dict]:
results = []
for raw in raw_cites:
cite = parse_cite(raw)
if cite:
results.append({"raw": raw, "cite": cite, "text": resolve_cite_text(cite)})
return results