from __future__ import annotations import logging logger = logging.getLogger(__name__) class RocketChatSessionManager: def build_session_key( self, account_id: str, chat_type: str, room_id: str, thread_id: str | None = None, ) -> str: parts = [account_id, chat_type, room_id] if thread_id: parts.append(f"thread:{thread_id}") return ":".join(parts) def build_dm_session_key( self, account_id: str, user_id: str, ) -> str: return f"{account_id}:direct:{user_id}" def build_group_session_key( self, account_id: str, room_id: str, ) -> str: return f"{account_id}:group:{room_id}" def build_channel_session_key( self, account_id: str, room_id: str, ) -> str: return f"{account_id}:channel:{room_id}" def build_thread_session_key( self, account_id: str, room_id: str, thread_root_id: str, ) -> str: return f"{account_id}:thread:{room_id}:{thread_root_id}" def extract_from_session_key(self, session_key: str) -> dict: parts = session_key.split(":") if len(parts) < 3: return {"account_id": "", "chat_type": "", "room_id": ""} result = { "account_id": parts[0], "chat_type": parts[1], "room_id": parts[2], "thread_id": None, } if len(parts) >= 5 and parts[3] == "thread": result["thread_id"] = parts[4] return result def resolve_parent_session_key( self, session_key: str, chat_type: str, ) -> str | None: if chat_type == "direct": return None parts = session_key.split(":") if len(parts) >= 5: non_thread = ":".join(parts[:3]) return non_thread return None