feat: add cross-platform target abstraction
这个提交存在于:
父节点
39e09a2da1
当前提交
08bb9cb7da
共有 4 个文件被更改,包括 104 次插入 和 6 次删除
|
|
@ -77,13 +77,19 @@ class AccessPolicy:
|
||||||
def is_allowed(self, event: Any) -> bool:
|
def is_allowed(self, event: Any) -> bool:
|
||||||
"""Return whether an event may operate MoviePilot."""
|
"""Return whether an event may operate MoviePilot."""
|
||||||
identity = identity_from_event(event)
|
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
|
return False
|
||||||
if not identity.user_id:
|
if not identity.user_id:
|
||||||
return False
|
return False
|
||||||
if identity.is_private:
|
if identity.is_private:
|
||||||
return identity.user_id in self._private_user_ids
|
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:
|
def is_allowed_identity(self, identity: PlatformIdentity) -> bool:
|
||||||
"""Return whether a normalized identity may operate MoviePilot."""
|
"""Return whether a normalized identity may operate MoviePilot."""
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ from pathlib import Path
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from moviepilot_client import MoviePilotClient
|
from moviepilot_client import MoviePilotClient
|
||||||
|
from platform_compat import target_from_config
|
||||||
|
|
||||||
|
|
||||||
NotificationSender = Callable[[str, str], Awaitable[None]]
|
NotificationSender = Callable[[str, str], Awaitable[None]]
|
||||||
|
|
@ -134,10 +135,12 @@ def fixed_notification_targets(config: Mapping[str, Any]) -> list[str]:
|
||||||
targets: list[str] = []
|
targets: list[str] = []
|
||||||
group_id = str(config.get("push_group_id", "")).strip()
|
group_id = str(config.get("push_group_id", "")).strip()
|
||||||
private_user_id = str(config.get("push_private_user_id", "")).strip()
|
private_user_id = str(config.get("push_private_user_id", "")).strip()
|
||||||
if platform_id and group_id:
|
group_target = target_from_config(platform_id, group_id=group_id)
|
||||||
targets.append(f"{platform_id}:GroupMessage:{group_id}")
|
private_target = target_from_config(platform_id, user_id=private_user_id)
|
||||||
if platform_id and private_user_id:
|
if group_target:
|
||||||
targets.append(f"{platform_id}:FriendMessage:{private_user_id}")
|
targets.append(group_target.unified_msg_origin)
|
||||||
|
if private_target:
|
||||||
|
targets.append(private_target.unified_msg_origin)
|
||||||
return targets
|
return targets
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
52
platform_compat.py
普通文件
52
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"}
|
||||||
37
tests/test_platform_compat.py
普通文件
37
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")
|
||||||
|
)
|
||||||
正在加载…
添加表格
在新工单中引用