mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-09-05 15:38:19 +08:00
refactor: unify service configuration boundary
This commit is contained in:
@@ -12,6 +12,7 @@ from pathlib import Path
|
||||
from typing import Any, Dict, Optional
|
||||
from uuid import uuid4
|
||||
|
||||
from app.application.notification import get_notification_configs
|
||||
from app.runtime.settings import RuntimeSettingsCompat
|
||||
|
||||
settings = RuntimeSettingsCompat()
|
||||
@@ -801,9 +802,7 @@ class AgentCapabilityManager:
|
||||
if not source:
|
||||
return False
|
||||
|
||||
from app.runtime.extensions.service_config import ServiceConfigHelper
|
||||
|
||||
for config in ServiceConfigHelper.get_notification_configs():
|
||||
for config in get_notification_configs(include_disabled=True):
|
||||
if config.name != source:
|
||||
continue
|
||||
return (config.config or {}).get("WECHAT_MODE", "app") != "bot"
|
||||
|
||||
@@ -23,7 +23,7 @@ from app.runtime.settings import RuntimeSettingsCompat
|
||||
|
||||
settings = RuntimeSettingsCompat()
|
||||
from app.application.messaging.agent import matches_channel_admin
|
||||
from app.runtime.extensions.service_config import ServiceConfigHelper
|
||||
from app.application.notification import get_notification_configs
|
||||
from app.runtime.log import logger
|
||||
from app.schemas.message import Message
|
||||
from app.schemas.types import NotificationChannel, MessageType
|
||||
@@ -719,7 +719,7 @@ class MoviePilotTool(BaseTool, metaclass=ABCMeta):
|
||||
return False
|
||||
|
||||
try:
|
||||
configs = ServiceConfigHelper.get_notification_configs()
|
||||
configs = get_notification_configs(include_disabled=True)
|
||||
for config in configs:
|
||||
if config.name == self._source and config.config:
|
||||
return matches_channel_admin(
|
||||
|
||||
@@ -9,7 +9,7 @@ from pydantic import BaseModel, Field
|
||||
from app.agent.tools.base import MoviePilotTool
|
||||
from app.agent.tools.tags import ToolTag
|
||||
from app.chain.mediaserver import MediaServerChain
|
||||
from app.runtime.extensions.service_config import ServiceConfigHelper
|
||||
from app.application.mediaserver import get_mediaserver_configs
|
||||
from app.runtime.log import logger
|
||||
|
||||
PAGE_SIZE = 20
|
||||
@@ -61,8 +61,7 @@ class QueryLibraryLatestTool(MoviePilotTool):
|
||||
@staticmethod
|
||||
def _get_enabled_servers() -> list[str]:
|
||||
"""同步读取启用的媒体服务器列表。"""
|
||||
mediaservers = ServiceConfigHelper.get_mediaserver_configs()
|
||||
return [ms.name for ms in mediaservers if ms.enabled]
|
||||
return [config.name for config in get_mediaserver_configs()]
|
||||
|
||||
@staticmethod
|
||||
def _load_latest_items(
|
||||
|
||||
@@ -28,7 +28,7 @@ from app.application.configuration import (
|
||||
from app.api.dependencies.agent import get_message_query_service
|
||||
from app.api.dependencies.auth import get_current_active_superuser
|
||||
from app.application.messaging.message import MessageQueryService
|
||||
from app.runtime.extensions.service_config import ServiceConfigHelper
|
||||
from app.application.notification import get_notification_configs
|
||||
from app.runtime.log import logger
|
||||
from app.adapters.external.wechat_crypt import WXBizMsgCrypt
|
||||
from app.schemas.types import NotificationChannel, SystemConfigKey
|
||||
@@ -261,7 +261,7 @@ def wechat_verify(
|
||||
微信验证响应
|
||||
"""
|
||||
# 获取服务配置
|
||||
client_configs = ServiceConfigHelper.get_notification_configs()
|
||||
client_configs = get_notification_configs(include_disabled=True)
|
||||
if not client_configs:
|
||||
return "未找到对应的消息配置"
|
||||
client_config = next(
|
||||
|
||||
@@ -304,3 +304,12 @@ class MediaServerHelper(ServiceBaseHelper[MediaServerConf]):
|
||||
"""判断给定服务或服务名称是否属于指定媒体服务器类型。"""
|
||||
service = service or self.get_service(name=name)
|
||||
return bool(service and service.type == service_type)
|
||||
|
||||
|
||||
def get_mediaserver_configs(
|
||||
include_disabled: bool = False,
|
||||
) -> list[MediaServerConf]:
|
||||
"""返回媒体服务器配置列表,并按调用方需要决定是否包含禁用项。"""
|
||||
return list(
|
||||
MediaServerHelper().get_configs(include_disabled=include_disabled).values()
|
||||
)
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
from typing import Optional
|
||||
|
||||
from app.application.service import ServiceBaseHelper
|
||||
from app.schemas.system import NotificationConf
|
||||
from app.application.service import ServiceBaseHelper, get_service_configs
|
||||
from app.schemas.system import NotificationConf, NotificationSwitchConf
|
||||
from app.schemas.system import ServiceInfo
|
||||
from app.schemas.types import ModuleType, SystemConfigKey
|
||||
from app.schemas.types import MessageType, ModuleType, SystemConfigKey
|
||||
|
||||
|
||||
class NotificationHelper(ServiceBaseHelper[NotificationConf]):
|
||||
@@ -33,3 +33,23 @@ class NotificationHelper(ServiceBaseHelper[NotificationConf]):
|
||||
"""
|
||||
service = service or self.get_service(name=name)
|
||||
return bool(service and service.type == service_type)
|
||||
|
||||
|
||||
def get_notification_configs(
|
||||
include_disabled: bool = False,
|
||||
) -> list[NotificationConf]:
|
||||
"""返回通知配置列表,并按调用方需要决定是否包含禁用项。"""
|
||||
return list(
|
||||
NotificationHelper().get_configs(include_disabled=include_disabled).values()
|
||||
)
|
||||
|
||||
|
||||
def get_notification_switch(mtype: MessageType) -> Optional[str]:
|
||||
"""返回指定通知场景的目标范围。"""
|
||||
for switch in get_service_configs(
|
||||
SystemConfigKey.NotificationSwitchs,
|
||||
NotificationSwitchConf,
|
||||
):
|
||||
if switch.type == mtype.value:
|
||||
return switch.action
|
||||
return None
|
||||
|
||||
@@ -41,6 +41,14 @@ def configure_service_directory(
|
||||
_module_loader = modules
|
||||
|
||||
|
||||
def get_service_configs(
|
||||
config_key: SystemConfigKey,
|
||||
conf_type: Type[TConf],
|
||||
) -> list[TConf]:
|
||||
"""通过组合根登记的读取器返回已校验服务配置。"""
|
||||
return _config_loader(config_key, conf_type)
|
||||
|
||||
|
||||
class ServiceBaseHelper(Generic[TConf]):
|
||||
"""通过应用端口查询服务配置和对应运行实例。"""
|
||||
|
||||
@@ -57,7 +65,7 @@ class ServiceBaseHelper(Generic[TConf]):
|
||||
|
||||
def get_configs(self, include_disabled: bool = False) -> Dict[str, TConf]:
|
||||
"""返回按名称索引的有效服务配置。"""
|
||||
configs = _config_loader(self.config_key, self.conf_type)
|
||||
configs = get_service_configs(self.config_key, self.conf_type)
|
||||
return {
|
||||
config.name: config
|
||||
for config in configs
|
||||
|
||||
@@ -13,7 +13,7 @@ from app.domain.context import Context, MediaInfo, MusicInfo, TorrentInfo
|
||||
from app.domain.meta.metabase import MetaBase
|
||||
from app.foundation.identity import normalize_internal_user_id
|
||||
from app.application.messaging.message import MessageTemplateHelper
|
||||
from app.runtime.extensions.service_config import ServiceConfigHelper
|
||||
from app.application.notification import get_notification_switch
|
||||
from app.runtime.log import logger
|
||||
from app.schemas.message import MessageResponse
|
||||
from app.schemas.message import Message
|
||||
@@ -159,7 +159,7 @@ class NotificationMixin:
|
||||
# 发送消息按设置隔离
|
||||
if not dispatch_message.userid and dispatch_message.mtype:
|
||||
# 消息隔离设置
|
||||
notify_action = ServiceConfigHelper.get_notification_switch(
|
||||
notify_action = get_notification_switch(
|
||||
dispatch_message.mtype
|
||||
)
|
||||
if notify_action:
|
||||
@@ -277,7 +277,7 @@ class NotificationMixin:
|
||||
# 发送消息按设置隔离
|
||||
if not dispatch_message.userid and dispatch_message.mtype:
|
||||
# 消息隔离设置
|
||||
notify_action = ServiceConfigHelper.get_notification_switch(
|
||||
notify_action = get_notification_switch(
|
||||
dispatch_message.mtype
|
||||
)
|
||||
if notify_action:
|
||||
|
||||
@@ -5,7 +5,7 @@ from typing import Callable, Dict, List, Union, Optional, Generator, Any, Tuple
|
||||
from app.chain import ChainBase
|
||||
from app.runtime.config import global_vars
|
||||
from app.application.chain.data import get_chain_media_server_port
|
||||
from app.runtime.extensions.service_config import ServiceConfigHelper
|
||||
from app.application.mediaserver import get_mediaserver_configs
|
||||
from app.runtime.log import logger
|
||||
from app.schemas.mediaserver import MediaServerLibrary
|
||||
from app.schemas.mediaserver import MediaServerItem
|
||||
@@ -458,7 +458,7 @@ class MediaServerChain(ChainBase):
|
||||
:param server: 指定媒体服务器名称,为空时同步全部已启用服务器
|
||||
"""
|
||||
# 设置的媒体服务器
|
||||
mediaservers = ServiceConfigHelper.get_mediaserver_configs()
|
||||
mediaservers = get_mediaserver_configs(include_disabled=True)
|
||||
if not mediaservers:
|
||||
if progress_callback:
|
||||
progress_callback(value=100, text="未配置媒体服务器,跳过同步")
|
||||
|
||||
+2
-2
@@ -38,10 +38,10 @@ from app.application.configuration import (
|
||||
get_scheduler_runtime_config,
|
||||
)
|
||||
from app.application.image import WallpaperHelper
|
||||
from app.application.mediaserver import get_mediaserver_configs
|
||||
from app.application.messaging.message import MessageHelper
|
||||
from app.runtime.progress import AsyncProgressHelper, ProgressHelper
|
||||
from app.adapters.external.server import MoviePilotServerHelper
|
||||
from app.runtime.extensions.service_config import ServiceConfigHelper
|
||||
from app.application.site.sites import SitesHelper # pylint: disable=import-error,no-name-in-module
|
||||
from app.runtime.log import logger
|
||||
from app.schemas.message import Message
|
||||
@@ -603,7 +603,7 @@ class Scheduler(ConfigReloadMixin, metaclass=SingletonClass):
|
||||
|
||||
# 按媒体服务器分别注册自动同步任务
|
||||
mediaserver_schedules = self._build_mediaserver_sync_schedules(
|
||||
mediaservers=ServiceConfigHelper.get_mediaserver_configs(),
|
||||
mediaservers=get_mediaserver_configs(include_disabled=True),
|
||||
default_interval=config.mediaserver_sync_interval,
|
||||
)
|
||||
for mediaserver_schedule in mediaserver_schedules:
|
||||
|
||||
Reference in New Issue
Block a user