feat(settings): 保存超级编导账号/密码后立刻重试自动追加
填好凭据还要等两个小时才生效没道理。保存 superdir_username/password/token 时,把抖音作品任务的自动追加调度钟挪到现在,下一个 tick 就用新凭据续期。
This commit is contained in:
+26
-1
@@ -27,9 +27,13 @@ no gain.
|
|||||||
from typing import Any, Dict
|
from typing import Any, Dict
|
||||||
|
|
||||||
from fastapi import APIRouter, HTTPException, Query
|
from fastapi import APIRouter, HTTPException, Query
|
||||||
|
from sqlalchemy import update
|
||||||
|
|
||||||
from ..monitor import app_settings
|
from tools.time_util import get_current_timestamp
|
||||||
|
|
||||||
|
from ..monitor import adapters, app_settings
|
||||||
from ..monitor.db import get_session
|
from ..monitor.db import get_session
|
||||||
|
from ..monitor.models import MODE_NOTE, MonitorTask
|
||||||
from ..monitor.platforms import PLATFORM_XHS
|
from ..monitor.platforms import PLATFORM_XHS
|
||||||
from ..schemas.settings import SettingsUpdatePayload
|
from ..schemas.settings import SettingsUpdatePayload
|
||||||
|
|
||||||
@@ -67,4 +71,25 @@ async def write_settings(
|
|||||||
except app_settings.SettingValidationError as exc:
|
except app_settings.SettingValidationError as exc:
|
||||||
raise HTTPException(status_code=400, detail=str(exc))
|
raise HTTPException(status_code=400, detail=str(exc))
|
||||||
|
|
||||||
|
# 刚填好超级编导的账号密码(或换了新 token),不该让用户再等两小时才看到它生效:
|
||||||
|
# 把自动追加的调度钟挪到「现在」,下一个 tick 就用新凭据续期/查询。
|
||||||
|
if platform == adapters.PLATFORM_DY and any(
|
||||||
|
key.rsplit(".", 1)[-1]
|
||||||
|
in ("superdir_username", "superdir_password", "superdir_token")
|
||||||
|
for key in changed
|
||||||
|
):
|
||||||
|
async with get_session() as session:
|
||||||
|
await session.execute(
|
||||||
|
update(MonitorTask)
|
||||||
|
.where(
|
||||||
|
MonitorTask.platform == adapters.PLATFORM_DY,
|
||||||
|
MonitorTask.mode == MODE_NOTE,
|
||||||
|
MonitorTask.superdir_auto_enabled.is_(True),
|
||||||
|
)
|
||||||
|
.values(
|
||||||
|
superdir_auto_next_at=get_current_timestamp(),
|
||||||
|
superdir_auto_last_error="",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
return {"message": f"已保存 {len(changed)} 项设置", "changed": changed}
|
return {"message": f"已保存 {len(changed)} 项设置", "changed": changed}
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ from api.main import app
|
|||||||
from api.monitor import app_settings, db as monitor_db
|
from api.monitor import app_settings, db as monitor_db
|
||||||
from api.monitor import scheduler as scheduler_module
|
from api.monitor import scheduler as scheduler_module
|
||||||
from api.monitor.scheduler import MonitorScheduler
|
from api.monitor.scheduler import MonitorScheduler
|
||||||
|
from api.monitor.models import MonitorTask
|
||||||
from api.monitor.settings import get_setting
|
from api.monitor.settings import get_setting
|
||||||
|
|
||||||
SECRET_VALUE = "web_session=SUPERSECRET; a1=abc"
|
SECRET_VALUE = "web_session=SUPERSECRET; a1=abc"
|
||||||
@@ -75,6 +76,43 @@ class TestReadSettings:
|
|||||||
|
|
||||||
|
|
||||||
class TestUpdateSettings:
|
class TestUpdateSettings:
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_saving_superdir_credentials_retries_auto_append_now(self, client):
|
||||||
|
"""填好账号密码之后不该再等一个间隔才生效 —— 下一 tick 就试着续期。"""
|
||||||
|
async with monitor_db.get_session() as session:
|
||||||
|
task = MonitorTask(
|
||||||
|
name="自动追加",
|
||||||
|
platform="dy",
|
||||||
|
mode="note",
|
||||||
|
enabled=True,
|
||||||
|
interval_minutes=360,
|
||||||
|
max_notes_count=20,
|
||||||
|
enable_comments=False,
|
||||||
|
max_comments_count=50,
|
||||||
|
run_timeout_seconds=3600,
|
||||||
|
superdir_auto_enabled=True,
|
||||||
|
superdir_auto_interval_minutes=120,
|
||||||
|
superdir_auto_next_at=9_999_999_999_999,
|
||||||
|
superdir_auto_last_error="token 失效",
|
||||||
|
created_at=0,
|
||||||
|
updated_at=0,
|
||||||
|
)
|
||||||
|
session.add(task)
|
||||||
|
await session.flush()
|
||||||
|
task_id = task.id
|
||||||
|
|
||||||
|
await client.put(
|
||||||
|
"/api/settings",
|
||||||
|
params={"platform": "dy"},
|
||||||
|
json={"platform.dy.superdir_username": "15655035256"},
|
||||||
|
)
|
||||||
|
|
||||||
|
async with monitor_db.get_session() as session:
|
||||||
|
task = await session.get(MonitorTask, task_id)
|
||||||
|
|
||||||
|
assert task.superdir_auto_next_at < 9_999_999_999_999
|
||||||
|
assert task.superdir_auto_last_error == ""
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_partial_update_leaves_other_keys_alone(self, client):
|
async def test_partial_update_leaves_other_keys_alone(self, client):
|
||||||
await client.put(
|
await client.put(
|
||||||
|
|||||||
Reference in New Issue
Block a user