59 lines
1.6 KiB
Python
59 lines
1.6 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import logging
|
||
|
|
|
||
|
|
logger = logging.getLogger(__name__)
|
||
|
|
|
||
|
|
|
||
|
|
class RocketChatThreadingAdapter:
|
||
|
|
def __init__(self, account: dict):
|
||
|
|
self.reply_to_mode = account.get("reply_to_mode", "off")
|
||
|
|
|
||
|
|
def extract_thread_id(self, raw_msg: dict) -> str | None:
|
||
|
|
tmid = raw_msg.get("tmid", "")
|
||
|
|
return tmid if tmid else None
|
||
|
|
|
||
|
|
def resolve_reply_target(
|
||
|
|
self,
|
||
|
|
msg_id: str,
|
||
|
|
thread_id: str,
|
||
|
|
chat_type: str,
|
||
|
|
) -> tuple[str | None, str | None]:
|
||
|
|
if self.reply_to_mode == "off":
|
||
|
|
return None, None
|
||
|
|
|
||
|
|
if chat_type == "direct":
|
||
|
|
return None, None if self.reply_to_mode != "all" else thread_id
|
||
|
|
|
||
|
|
if self.reply_to_mode == "first":
|
||
|
|
return thread_id or None, thread_id or None
|
||
|
|
|
||
|
|
if self.reply_to_mode == "all":
|
||
|
|
return thread_id or msg_id, thread_id or None
|
||
|
|
|
||
|
|
if self.reply_to_mode == "batched":
|
||
|
|
return thread_id or msg_id, thread_id or None
|
||
|
|
|
||
|
|
return None, None
|
||
|
|
|
||
|
|
def build_thread_context(
|
||
|
|
self,
|
||
|
|
thread_id: str,
|
||
|
|
msg_id: str,
|
||
|
|
room_id: str,
|
||
|
|
chat_type: str,
|
||
|
|
) -> dict:
|
||
|
|
reply_to_id, resolved_thread_id = self.resolve_reply_target(
|
||
|
|
msg_id,
|
||
|
|
thread_id,
|
||
|
|
chat_type,
|
||
|
|
)
|
||
|
|
return {
|
||
|
|
"room_id": room_id,
|
||
|
|
"thread_id": thread_id,
|
||
|
|
"msg_id": msg_id,
|
||
|
|
"reply_to_id": reply_to_id,
|
||
|
|
"resolved_thread_id": resolved_thread_id,
|
||
|
|
"reply_to_mode": self.reply_to_mode,
|
||
|
|
}
|