56 lines
1.5 KiB
Python
56 lines
1.5 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import logging
|
||
|
|
|
||
|
|
logger = logging.getLogger(__name__)
|
||
|
|
|
||
|
|
|
||
|
|
class MattermostThreadingAdapter:
|
||
|
|
def __init__(self, account: dict):
|
||
|
|
self.reply_to_mode = account.get("reply_to_mode", "off")
|
||
|
|
|
||
|
|
def extract_thread_id(self, root_id: str) -> str | None:
|
||
|
|
if root_id:
|
||
|
|
return root_id
|
||
|
|
return None
|
||
|
|
|
||
|
|
def resolve_reply_target(
|
||
|
|
self,
|
||
|
|
post_id: str,
|
||
|
|
root_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 root_id
|
||
|
|
|
||
|
|
if self.reply_to_mode == "first":
|
||
|
|
return root_id or None, root_id or None
|
||
|
|
|
||
|
|
if self.reply_to_mode == "all":
|
||
|
|
return root_id or post_id, root_id or None
|
||
|
|
|
||
|
|
if self.reply_to_mode == "batched":
|
||
|
|
return root_id or post_id, root_id or None
|
||
|
|
|
||
|
|
return None, None
|
||
|
|
|
||
|
|
def build_thread_context(
|
||
|
|
self,
|
||
|
|
root_id: str,
|
||
|
|
post_id: str,
|
||
|
|
channel_id: str,
|
||
|
|
chat_type: str,
|
||
|
|
) -> dict:
|
||
|
|
reply_to_id, thread_id = self.resolve_reply_target(post_id, root_id, chat_type)
|
||
|
|
return {
|
||
|
|
"channel_id": channel_id,
|
||
|
|
"root_id": root_id,
|
||
|
|
"post_id": post_id,
|
||
|
|
"reply_to_id": reply_to_id,
|
||
|
|
"thread_id": thread_id,
|
||
|
|
"reply_to_mode": self.reply_to_mode,
|
||
|
|
}
|