From 08bb9cb7da8a5b79f28c319c87e68707742d77c8 Mon Sep 17 00:00:00 2001 From: Codex Date: Mon, 20 Jul 2026 22:29:17 +0800 Subject: [PATCH] feat: add cross-platform target abstraction --- access_policy.py | 10 +++++-- notification_service.py | 11 +++++--- platform_compat.py | 52 +++++++++++++++++++++++++++++++++++ tests/test_platform_compat.py | 37 +++++++++++++++++++++++++ 4 files changed, 104 insertions(+), 6 deletions(-) create mode 100644 platform_compat.py create mode 100644 tests/test_platform_compat.py diff --git a/access_policy.py b/access_policy.py index 49631ef..4c335a6 100644 --- a/access_policy.py +++ b/access_policy.py @@ -77,13 +77,19 @@ class AccessPolicy: def is_allowed(self, event: Any) -> bool: """Return whether an event may operate MoviePilot.""" identity = identity_from_event(event) - if not identity.platform_name or identity.platform_name not in self._allowed_platforms: + if ( + not identity.platform_name + or identity.platform_name not in self._allowed_platforms + ): return False if not identity.user_id: return False if identity.is_private: return identity.user_id in self._private_user_ids - return identity.group_id in self._group_permissions and identity.user_id in self._group_permissions[identity.group_id] + return ( + identity.group_id in self._group_permissions + and identity.user_id in self._group_permissions[identity.group_id] + ) def is_allowed_identity(self, identity: PlatformIdentity) -> bool: """Return whether a normalized identity may operate MoviePilot.""" diff --git a/notification_service.py b/notification_service.py index 6271069..8b8da62 100644 --- a/notification_service.py +++ b/notification_service.py @@ -11,6 +11,7 @@ from pathlib import Path from typing import Any from moviepilot_client import MoviePilotClient +from platform_compat import target_from_config NotificationSender = Callable[[str, str], Awaitable[None]] @@ -134,10 +135,12 @@ def fixed_notification_targets(config: Mapping[str, Any]) -> list[str]: targets: list[str] = [] group_id = str(config.get("push_group_id", "")).strip() private_user_id = str(config.get("push_private_user_id", "")).strip() - if platform_id and group_id: - targets.append(f"{platform_id}:GroupMessage:{group_id}") - if platform_id and private_user_id: - targets.append(f"{platform_id}:FriendMessage:{private_user_id}") + group_target = target_from_config(platform_id, group_id=group_id) + private_target = target_from_config(platform_id, user_id=private_user_id) + if group_target: + targets.append(group_target.unified_msg_origin) + if private_target: + targets.append(private_target.unified_msg_origin) return targets diff --git a/platform_compat.py b/platform_compat.py new file mode 100644 index 0000000..be9c8d7 --- /dev/null +++ b/platform_compat.py @@ -0,0 +1,52 @@ +"""Platform-neutral identities and fixed notification targets.""" + +from __future__ import annotations + +from dataclasses import dataclass + +from access_policy import PlatformIdentity + + +@dataclass(frozen=True, slots=True) +class PlatformTarget: + """A target address that AstrBot can use for proactive messages.""" + + platform_id: str + message_type: str + session_id: str + + @property + def unified_msg_origin(self) -> str: + return f"{self.platform_id}:{self.message_type}:{self.session_id}" + + +def target_from_config( + platform_id: str, + *, + group_id: str = "", + user_id: str = "", +) -> PlatformTarget | None: + """Build a fixed group or private target from configured identifiers.""" + normalized_platform = str(platform_id or "").strip() + normalized_group = str(group_id or "").strip() + normalized_user = str(user_id or "").strip() + if not normalized_platform: + return None + if normalized_group: + return PlatformTarget(normalized_platform, "GroupMessage", normalized_group) + if normalized_user: + return PlatformTarget(normalized_platform, "FriendMessage", normalized_user) + return None + + +def parse_unified_msg_origin(value: str) -> PlatformTarget | None: + """Parse AstrBot's platform/type/session address.""" + parts = str(value or "").split(":", 2) + if len(parts) != 3 or not all(parts): + return None + return PlatformTarget(parts[0], parts[1], parts[2]) + + +def is_supported_platform(identity: PlatformIdentity) -> bool: + """Return whether the normalized identity is a supported target platform.""" + return identity.platform_name in {"aiocqhttp", "telegram"} diff --git a/tests/test_platform_compat.py b/tests/test_platform_compat.py new file mode 100644 index 0000000..e320fbe --- /dev/null +++ b/tests/test_platform_compat.py @@ -0,0 +1,37 @@ +from access_policy import PlatformIdentity +from platform_compat import ( + is_supported_platform, + parse_unified_msg_origin, + target_from_config, +) + + +def test_targets_support_qq_and_telegram_without_platform_specific_logic() -> None: + qq_group = target_from_config("aiocqhttp", group_id="200") + telegram_user = target_from_config("telegram", user_id="chat-100") + + assert qq_group is not None + assert qq_group.unified_msg_origin == "aiocqhttp:GroupMessage:200" + assert telegram_user is not None + assert telegram_user.unified_msg_origin == "telegram:FriendMessage:chat-100" + + +def test_target_parser_and_invalid_inputs() -> None: + parsed = parse_unified_msg_origin("aiocqhttp:GroupMessage:200") + + assert parsed is not None + assert parsed.session_id == "200" + assert target_from_config("", group_id="200") is None + assert parse_unified_msg_origin("invalid") is None + + +def test_supported_platforms() -> None: + assert is_supported_platform( + PlatformIdentity("aiocqhttp", "bot", "100", "", "origin") + ) + assert is_supported_platform( + PlatformIdentity("telegram", "bot", "100", "", "origin") + ) + assert not is_supported_platform( + PlatformIdentity("discord", "bot", "100", "", "origin") + )