diff --git a/backend/src/ab_sdk/config.py b/backend/src/ab_sdk/config.py index fcac84bc..cd765a87 100644 --- a/backend/src/ab_sdk/config.py +++ b/backend/src/ab_sdk/config.py @@ -5,14 +5,12 @@ from typing import Any from pydantic import Field -def secret_field(default: Any = "", *, description: str = "", **kwargs: Any) -> Any: +def secret_field(default: str = "", *, description: str = "", **kwargs: Any) -> Any: """声明秘密字段(密码、令牌、Cookie 等)。 WebUI 以密码框渲染;读取配置的接口只返回掩码,保存时收到掩码会保留原值。 用法:``cookie: str = secret_field(description="站点 Cookie")``。 - 列表、字典整体声明为秘密时,其中每个值都掩码: - ``urls: list[str] = secret_field(default_factory=list)``。 """ - if "default_factory" not in kwargs: - kwargs["default"] = default - return Field(description=description, json_schema_extra={"secret": True}, **kwargs) + return Field( + default, description=description, json_schema_extra={"secret": True}, **kwargs + ) diff --git a/examples/plugins/apprise-notifier/README.md b/examples/plugins/apprise-notifier/README.md index cf991fad..f1cb25f3 100644 --- a/examples/plugins/apprise-notifier/README.md +++ b/examples/plugins/apprise-notifier/README.md @@ -3,7 +3,7 @@ 通过 [Apprise API](https://github.com/caronc/apprise-api) 服务器把通知转发到 Telegram、Discord、邮件等上百种服务。插件不依赖 `apprise` 库,只调用 HTTP 接口。 - `@provider(points.NOTIFIER, id="apprise")`:返回 `NotifierFactory` -- 填写了 Apprise URL:`POST {server}/notify/`,请求体 `{"urls", "title", "body"}`,多个 URL 以逗号拼接 +- 填写了 Apprise URL:`POST {server}/notify/`,请求体 `{"urls", "title", "body"}`,多个 URL 以逗号拼接成一个字符串 - 否则使用配置键:`POST {server}/notify/{config_key}`,请求体 `{"title", "body"}`,URL 保存在服务器上 - 两者都没填时 `send` 返回 `False`,「测试」提示需要填写其中之一 - HTTP 2xx 即成功 @@ -23,7 +23,7 @@ docker run -d --name apprise -p 8000:8000 caronc/apprise:latest | 字段 | 说明 | | --- | --- | | `server` | Apprise API 服务器地址,默认 `http://apprise:8000` | -| `urls` | 可选:Apprise URL 列表,如 `tgram://bottoken/ChatID`;填写后忽略配置键。URL 含令牌,按秘密字段处理,读取配置时每一项只返回掩码 | +| `urls` | 可选:Apprise URL,如 `tgram://bottoken/ChatID`;多个 URL 用逗号或换行分隔;填写后忽略配置键。URL 含令牌,按秘密字段处理,读取配置时只返回掩码 | | `config_key` | 可选:服务器上保存的配置键 | ## 安装 diff --git a/examples/plugins/apprise-notifier/apprise_notifier/__init__.py b/examples/plugins/apprise-notifier/apprise_notifier/__init__.py index eb0a11f2..8f927b4b 100644 --- a/examples/plugins/apprise-notifier/apprise_notifier/__init__.py +++ b/examples/plugins/apprise-notifier/apprise_notifier/__init__.py @@ -23,10 +23,9 @@ class Options(BaseModel): "http://apprise:8000", title="服务器", description="Apprise API 服务器地址" ) # URL 里带着令牌,按秘密字段掩码 - urls: list[str] = secret_field( - default_factory=list, + urls: str = secret_field( title="Apprise URL", - description="如 tgram://bottoken/ChatID;填写后忽略配置键", + description="多个 Apprise URL 用逗号或换行分隔", ) config_key: str = Field( "", @@ -50,10 +49,12 @@ class Channel: opts = self.options server = opts.server.rstrip("/") payload: dict[str, Any] = {"title": message.title, "body": message.body} - if opts.urls: + urls = [u.strip() for u in opts.urls.replace("\n", ",").split(",")] + urls = [u for u in urls if u] + if urls: url = f"{server}/notify/" # Apprise 接受逗号分隔的 URL 字符串 - payload["urls"] = ", ".join(opts.urls) + payload["urls"] = ",".join(urls) elif opts.config_key: url = f"{server}/notify/{opts.config_key}" else: diff --git a/examples/plugins/apprise-notifier/tests/test_apprise_notifier.py b/examples/plugins/apprise-notifier/tests/test_apprise_notifier.py index a4990246..bfe39d33 100644 --- a/examples/plugins/apprise-notifier/tests/test_apprise_notifier.py +++ b/examples/plugins/apprise-notifier/tests/test_apprise_notifier.py @@ -36,9 +36,9 @@ class TestChannel(NotifierContract): ("options", "path", "payload"), [ ( - {"server": "http://ap:8000/", "urls": ["tgram://t/1", "mailto://u"]}, + {"server": "http://ap:8000/", "urls": "tgram://t/1,\n mailto://u ,"}, "/notify/", - {"title": "标题", "body": "正文", "urls": "tgram://t/1, mailto://u"}, + {"title": "标题", "body": "正文", "urls": "tgram://t/1,mailto://u"}, ), ( {"server": "http://ap:8000", "config_key": "ab"}, @@ -64,7 +64,7 @@ def test_missing_urls_and_config_key_fails_without_request(): def test_urls_is_secret_in_config_schema(): - # URL 里带着令牌:宿主的读配置接口按 schema 中的 secret 标记掩码每一项 + # URL 里带着令牌:宿主的读配置接口按 schema 中的 secret 标记掩码 urls = Options.model_json_schema()["properties"]["urls"] assert urls["secret"] is True - assert urls["type"] == "array" + assert urls["type"] == "string"