73 lines
2.2 KiB
Python
73 lines
2.2 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import logging
|
||
|
|
|
||
|
|
logger = logging.getLogger(__name__)
|
||
|
|
|
||
|
|
SESSION_KEY_SEPARATOR = ":"
|
||
|
|
|
||
|
|
|
||
|
|
def build_dm_key_per_user(user_id: str) -> str:
|
||
|
|
return f"agent{SESSION_KEY_SEPARATOR}main{SESSION_KEY_SEPARATOR}direct{SESSION_KEY_SEPARATOR}{user_id}"
|
||
|
|
|
||
|
|
|
||
|
|
def build_dm_key_per_room(user_id: str, room_id: str) -> str:
|
||
|
|
return (
|
||
|
|
f"agent{SESSION_KEY_SEPARATOR}main{SESSION_KEY_SEPARATOR}matrix"
|
||
|
|
f"{SESSION_KEY_SEPARATOR}direct{SESSION_KEY_SEPARATOR}{user_id}"
|
||
|
|
f"{SESSION_KEY_SEPARATOR}{room_id}"
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
def build_dm_key_main() -> str:
|
||
|
|
return f"agent{SESSION_KEY_SEPARATOR}main{SESSION_KEY_SEPARATOR}main"
|
||
|
|
|
||
|
|
|
||
|
|
def build_group_key(room_id: str) -> str:
|
||
|
|
return (
|
||
|
|
f"agent{SESSION_KEY_SEPARATOR}main{SESSION_KEY_SEPARATOR}matrix"
|
||
|
|
f"{SESSION_KEY_SEPARATOR}group{SESSION_KEY_SEPARATOR}{room_id}"
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
def build_thread_key(room_id: str, thread_root_id: str) -> str:
|
||
|
|
return (
|
||
|
|
f"agent{SESSION_KEY_SEPARATOR}main{SESSION_KEY_SEPARATOR}matrix"
|
||
|
|
f"{SESSION_KEY_SEPARATOR}group{SESSION_KEY_SEPARATOR}{room_id}"
|
||
|
|
f"{SESSION_KEY_SEPARATOR}thread{SESSION_KEY_SEPARATOR}{thread_root_id}"
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
def extract_thread_id(msg: object) -> str | None:
|
||
|
|
if hasattr(msg, "thread_parent_id") and msg.thread_parent_id:
|
||
|
|
return msg.thread_parent_id
|
||
|
|
if hasattr(msg, "message_thread_id") and msg.message_thread_id:
|
||
|
|
return msg.message_thread_id
|
||
|
|
return None
|
||
|
|
|
||
|
|
|
||
|
|
def resolve_reply_transport(msg: object, thread_id: str | None):
|
||
|
|
from yuxi.channel.protocols import ReplyTransport
|
||
|
|
|
||
|
|
transport = ReplyTransport()
|
||
|
|
if thread_id and hasattr(msg, "group") and msg.group:
|
||
|
|
room_id = msg.group.id if hasattr(msg.group, "id") else ""
|
||
|
|
transport.thread_id = thread_id
|
||
|
|
transport.target_id = room_id or transport.target_id
|
||
|
|
return transport
|
||
|
|
|
||
|
|
|
||
|
|
def get_thread_replies_mode(account: dict) -> str:
|
||
|
|
if isinstance(account, dict):
|
||
|
|
return account.get("thread_replies", "inbound")
|
||
|
|
return getattr(account, "thread_replies", "inbound")
|
||
|
|
|
||
|
|
|
||
|
|
def should_thread_reply(msg: object, account: dict) -> bool:
|
||
|
|
mode = get_thread_replies_mode(account)
|
||
|
|
if mode == "always":
|
||
|
|
return True
|
||
|
|
if mode == "inbound":
|
||
|
|
return extract_thread_id(msg) is not None
|
||
|
|
return False
|