ForcePilot/backend/package/yuxi/channels/adapters/mattermost/reply.py
Kris 1f78c44b03 refactor: 整理并清理项目中的冗余代码与格式问题
这是一个批量整理提交,包含以下主要改动:
1.  删除多处冗余的空行和未使用的导入
2.  修复文件末尾缺少换行符的问题
3.  调整部分模块的导入顺序与代码排版
4.  修复部分配置默认值与策略逻辑
5.  新增多个功能模块与辅助工具
6.  完善异常处理与日志记录
7.  修复速率限制、消息缓存、权限校验等逻辑bug
8.  废弃部分旧有API与配置项并添加警告提示
2026-05-12 14:51:53 +08:00

74 lines
2.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from __future__ import annotations
from dataclasses import dataclass
ReplyToMode = str
REPLY_OFF: ReplyToMode = "off"
REPLY_FIRST: ReplyToMode = "first"
REPLY_ALL: ReplyToMode = "all"
REPLY_BATCHED: ReplyToMode = "batched"
REPLY_MODES = frozenset({REPLY_OFF, REPLY_FIRST, REPLY_ALL, REPLY_BATCHED})
@dataclass
class ReplyConfig:
mode: ReplyToMode = REPLY_OFF
thread_only: bool = False
@classmethod
def from_config(cls, config: dict) -> ReplyConfig:
mode = config.get("reply_to_mode", REPLY_OFF)
if mode not in REPLY_MODES:
mode = REPLY_OFF
thread_only = bool(config.get("reply_thread_only", False))
return cls(mode=mode, thread_only=thread_only)
class ReplyManager:
def __init__(self, config: dict | None = None):
self._config = ReplyConfig.from_config(config or {})
@property
def mode(self) -> ReplyToMode:
return self._config.mode
@property
def thread_only(self) -> bool:
return self._config.thread_only
def reload_config(self, key: str, value) -> None:
from dataclasses import replace
if key == "reply_to_mode" and value in REPLY_MODES:
self._config = replace(self._config, mode=value)
def should_reply(self, chat_type: str, has_thread: bool = False) -> bool:
if chat_type == "direct":
return False
mode = self._config.mode
if mode == REPLY_OFF:
return False
if mode == REPLY_FIRST and chat_type == "direct":
return True
if mode == REPLY_ALL:
return True
if mode == REPLY_BATCHED:
return True
return False
def resolve_reply_target(self, chat_id: str, root_id: str | None) -> str | None:
"""返回应该回复到的消息 ID用作 root_idNone 表示不绑定线程。"""
if self._config.mode == REPLY_OFF:
return None
if self._config.thread_only and root_id:
return root_id
if self._config.mode in (REPLY_ALL, REPLY_BATCHED) and root_id:
return root_id
return None