mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-09-29 00:32:06 +08:00
♻️ refactor(plugin_switch): 重构功能开关与帮助系统并优化 UI 报表渲染 (#2111)
* ♻️ refactor(plugin_switch): 重构功能开关与帮助系统并优化 UI 报表渲染 - 引入策略模式 (Strategy Pattern) 统一插件与被动任务的开关逻辑 - 新增超级用户强制管控功能,支持系统级禁用且群管无法自行开启 - 新增全服群组活跃状态报表与功能全局覆盖率可视化统计图表 - 优化指令解析系统,支持多功能批量开关及更智能的快捷词映射 - 重构帮助系统,支持在帮助页区分展示管理员与超级用户指令 - 细化插件列表状态显示,区分群控、系统管控与全局禁用状态 - UI 组件 UserInfoBlock 新增 extra 扩展插槽支持 - 更新 resources.spec 资源版本要求至 1.1.1 * 🚨 auto fix by pre-commit hooks --------- Co-authored-by: webjoin111 <455457521@qq.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
co-authored by
webjoin111
pre-commit-ci[bot]
parent
ce94f63d9a
commit
0f91ce03e4
@@ -0,0 +1,187 @@
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Any, cast
|
||||
|
||||
from zhenxun.models.group_console import GroupConsole
|
||||
from zhenxun.models.plugin_info import PluginInfo
|
||||
from zhenxun.models.task_info import TaskInfo
|
||||
from zhenxun.services.cache import CacheRoot
|
||||
from zhenxun.services.cache.runtime_cache import (
|
||||
PluginInfoMemoryCache,
|
||||
TaskInfoMemoryCache,
|
||||
)
|
||||
from zhenxun.utils.enum import BlockType, CacheType, PluginType
|
||||
|
||||
|
||||
class SwitchStrategy(ABC):
|
||||
"""插件与被动技能切换策略基类"""
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def entity_type_name(self) -> str:
|
||||
pass
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def norm_field(self) -> str:
|
||||
"""普通的群组禁用字段名"""
|
||||
pass
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def su_field(self) -> str:
|
||||
"""超级用户群组禁用字段名"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
async def get_entity(self, name: str) -> Any:
|
||||
"""通过名称获取实体信息"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
async def check_block_status(self, group_id: str, module: str) -> tuple[bool, bool]:
|
||||
"""检查目标群组的禁用状态,返回 (is_su_blocked, is_norm_blocked)"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
async def get_all_modules(self) -> list[str]:
|
||||
"""获取所有模块的名称列表"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
async def set_default_status(self, entity: Any, status: bool) -> None:
|
||||
"""设置单个实体的进群默认状态"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
async def set_global_status(
|
||||
self, entity: Any, status: bool, block_type: BlockType | None = None
|
||||
) -> None:
|
||||
"""设置单个实体的全局状态"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
async def set_all_default_status(self, status: bool) -> None:
|
||||
"""设置所有实体的进群默认状态"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
async def set_all_global_status(self, status: bool) -> None:
|
||||
"""设置所有实体的全局状态"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
async def refresh_cache(self) -> None:
|
||||
"""刷新相关的内存缓存"""
|
||||
pass
|
||||
|
||||
|
||||
class PluginStrategy(SwitchStrategy):
|
||||
@property
|
||||
def entity_type_name(self) -> str:
|
||||
return "功能"
|
||||
|
||||
@property
|
||||
def norm_field(self) -> str:
|
||||
return "block_plugin"
|
||||
|
||||
@property
|
||||
def su_field(self) -> str:
|
||||
return "superuser_block_plugin"
|
||||
|
||||
async def get_entity(self, name: str) -> Any:
|
||||
if name.isdigit():
|
||||
return await PluginInfo.get_or_none(id=int(name))
|
||||
return await PluginInfo.get_or_none(
|
||||
name=name, load_status=True, plugin_type__not=PluginType.PARENT
|
||||
)
|
||||
|
||||
async def check_block_status(self, group_id: str, module: str) -> tuple[bool, bool]:
|
||||
is_su_blocked = await GroupConsole.is_superuser_block_plugin(group_id, module)
|
||||
is_norm_blocked = await GroupConsole.is_normal_block_plugin(group_id, module)
|
||||
return is_su_blocked, is_norm_blocked
|
||||
|
||||
async def get_all_modules(self) -> list[str]:
|
||||
return cast(
|
||||
list[str],
|
||||
await PluginInfo.filter(plugin_type=PluginType.NORMAL).values_list(
|
||||
"module", flat=True
|
||||
),
|
||||
)
|
||||
|
||||
async def set_default_status(self, entity: PluginInfo, status: bool) -> None:
|
||||
entity.default_status = status
|
||||
await entity.save(update_fields=["default_status"])
|
||||
|
||||
async def set_global_status(
|
||||
self, entity: PluginInfo, status: bool, block_type: BlockType | None = None
|
||||
) -> None:
|
||||
entity.block_type = block_type
|
||||
entity.status = not bool(block_type)
|
||||
await entity.save(update_fields=["status", "block_type"])
|
||||
|
||||
async def set_all_default_status(self, status: bool) -> None:
|
||||
await PluginInfo.filter(plugin_type=PluginType.NORMAL).update(
|
||||
default_status=status
|
||||
)
|
||||
await self.refresh_cache()
|
||||
|
||||
async def set_all_global_status(self, status: bool) -> None:
|
||||
await PluginInfo.filter(plugin_type=PluginType.NORMAL).update(
|
||||
status=status, block_type=None if status else BlockType.ALL
|
||||
)
|
||||
await self.refresh_cache()
|
||||
|
||||
async def refresh_cache(self) -> None:
|
||||
await CacheRoot.invalidate_cache(CacheType.PLUGINS)
|
||||
await PluginInfoMemoryCache.refresh()
|
||||
|
||||
|
||||
class TaskStrategy(SwitchStrategy):
|
||||
@property
|
||||
def entity_type_name(self) -> str:
|
||||
return "被动"
|
||||
|
||||
@property
|
||||
def norm_field(self) -> str:
|
||||
return "block_task"
|
||||
|
||||
@property
|
||||
def su_field(self) -> str:
|
||||
return "superuser_block_task"
|
||||
|
||||
async def get_entity(self, name: str) -> Any:
|
||||
return await TaskInfo.get_or_none(name=name)
|
||||
|
||||
async def check_block_status(self, group_id: str, module: str) -> tuple[bool, bool]:
|
||||
is_su_blocked = await GroupConsole.is_superuser_block_task(group_id, module)
|
||||
is_norm_blocked = await GroupConsole.is_block_task(group_id, module)
|
||||
return is_su_blocked, is_norm_blocked
|
||||
|
||||
async def get_all_modules(self) -> list[str]:
|
||||
return cast(list[str], await TaskInfo.all().values_list("module", flat=True))
|
||||
|
||||
async def set_default_status(self, entity: TaskInfo, status: bool) -> None:
|
||||
entity.default_status = status
|
||||
await entity.save(update_fields=["default_status"])
|
||||
|
||||
async def set_global_status(
|
||||
self, entity: TaskInfo, status: bool, block_type: BlockType | None = None
|
||||
) -> None:
|
||||
entity.status = status
|
||||
await entity.save(update_fields=["status"])
|
||||
|
||||
async def set_all_default_status(self, status: bool) -> None:
|
||||
await TaskInfo.all().update(default_status=status)
|
||||
await self.refresh_cache()
|
||||
|
||||
async def set_all_global_status(self, status: bool) -> None:
|
||||
await TaskInfo.all().update(status=status)
|
||||
await self.refresh_cache()
|
||||
|
||||
async def refresh_cache(self) -> None:
|
||||
await TaskInfoMemoryCache.refresh()
|
||||
|
||||
|
||||
def get_strategy(is_task: bool) -> SwitchStrategy:
|
||||
"""工厂方法:获取对应的处理策略"""
|
||||
return TaskStrategy() if is_task else PluginStrategy()
|
||||
Reference in New Issue
Block a user