nomadro/apps/api/services/ebook_svc/services/join_group_config.py
eric 534400f554 Merge stash WIP onto BMI A/B: join-group, live config, channel tracking.
Keep Douyin homeVariant BMI router intact while restoring admin join-group controls, TLS-safe config fetch, and ntfy channel labels.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-03 15:42:09 +08:00

108 lines
3.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""加群引导远程开关 — 只改 flags,不碰 modules / platforms.tt / 订单数据。"""
from __future__ import annotations
from datetime import datetime, timezone
from typing import Any
from ebook_svc.services.tt_platform_config import resolve_config_file
# 加群相关 flag 白名单(其它 flag 只读透传,不在此写入)
JOIN_FLAG_KEYS = (
"joinGroupWechat",
"joinGroupDouyin",
"socialFollowAuto",
)
DEFAULT_JOIN_FLAGS = {
"joinGroupWechat": True,
"joinGroupDouyin": False,
"socialFollowAuto": False,
}
def _read_raw() -> dict[str, Any]:
path = resolve_config_file()
if not path.is_file():
return {}
try:
import json
raw = json.loads(path.read_text(encoding="utf-8"))
return raw if isinstance(raw, dict) else {}
except Exception:
return {}
def read_join_group_config() -> dict[str, Any]:
raw = _read_raw()
flags = dict(raw.get("flags") or {})
out_flags = {k: bool(flags.get(k, DEFAULT_JOIN_FLAGS[k])) for k in JOIN_FLAG_KEYS}
copy = raw.get("copy") or {}
return {
"flags": out_flags,
"copy": {
"joinGroupDouyinHint": str(
copy.get("joinGroupDouyinHint")
or "打开抖音搜群「数字游民」或扫码进群"
),
"joinGroupDouyinQr": str(copy.get("joinGroupDouyinQr") or ""),
"wechatOaName": str(copy.get("wechatOaName") or "异度世界"),
},
"updatedAt": raw.get("updatedAt"),
"version": raw.get("version"),
"configPath": str(resolve_config_file()),
}
def write_join_group_config(
*,
flags_patch: dict[str, Any] | None = None,
copy_patch: dict[str, Any] | None = None,
) -> dict[str, Any]:
"""增量写 flags / 文案;保留 modules、platforms、其它 flags。"""
import json
path = resolve_config_file()
path.parent.mkdir(parents=True, exist_ok=True)
existing = _read_raw()
out = dict(existing) if existing else {"version": 2, "modules": {}, "flags": {}, "copy": {}}
flags = dict(out.get("flags") or {})
# 补齐默认,避免旧文件缺键
for k, v in DEFAULT_JOIN_FLAGS.items():
if k not in flags:
flags[k] = v
if flags_patch:
for k, v in flags_patch.items():
if k not in JOIN_FLAG_KEYS:
continue
if isinstance(v, bool):
flags[k] = v
out["flags"] = flags
copy = dict(out.get("copy") or {})
if copy_patch:
for k in ("joinGroupDouyinHint", "joinGroupDouyinQr", "wechatOaName"):
if k in copy_patch and copy_patch[k] is not None:
copy[k] = str(copy_patch[k])
out["copy"] = copy
out["updatedAt"] = datetime.now(timezone.utc).isoformat()
try:
out["version"] = int(out.get("version") or 1) + 1
except Exception:
out["version"] = 2
# 备份上一份
if path.is_file():
bak = path.with_suffix(path.suffix + f".bak-{out['updatedAt'].replace(':', '-')}")
try:
bak.write_text(path.read_text(encoding="utf-8"), encoding="utf-8")
except Exception:
pass
path.write_text(json.dumps(out, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
return read_join_group_config()