feat(webui): improve setup flows across chat channels (NAN-112) (#5356)
* feat(webui): improve channel setup flows * fix(webui): refine channel setup interactions (NAN-112) * fix(webui): keep email setup sections channel-owned * fix(webui): remove concrete channel details from shared components * refactor(webui): remove unused channel setup resources * fix(webui): finish channel-owned setup boundaries * fix(webui): update channel switches optimistically * test(channels): remove obsolete absence assertions * fix(webui): flatten channel instance settings disclosure * fix(webui): complete channel setup and connection flows * fix(whatsapp): wait for session database release * fix(webui): preserve scrolling channel controls * fix(webui): stage channel connection checks * fix(channels): align verification actions * fix(email): expose connection checks * fix(webui): align channel field actions * fix(email): verify account credentials * fix(wecom): bypass system proxy for websocket * fix(channels): stabilize async connection cleanup * fix(webui): autosave channel credentials * fix(email): make provider presets non-destructive * test(weixin): isolate persisted QR credentials
This commit is contained in:
1 parent
3cc3012b3a
commit
e8a29a0087
291 files changed
+12194
-3979
No files matched your search
@@ -0,0 +1,338 @@
|
||||
"""WhatsApp-owned interactive QR connection flow."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import os
|
||||
import secrets
|
||||
import time
|
||||
from contextlib import suppress
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from typing import TYPE_CHECKING, Any, cast
|
||||
|
||||
from nanobot.channels.connect import ChannelConnectError, QueryParams, query_first
|
||||
from nanobot.config.loader import load_config
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from nanobot.channels.whatsapp.runtime import WhatsAppChannel
|
||||
|
||||
|
||||
_NATIVE_STOP_TIMEOUT_SECONDS = 10.0
|
||||
_DATABASE_REPLACE_TIMEOUT_SECONDS = 10.0
|
||||
_FINALIZE_POLL_WAIT_SECONDS = 0.05
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class WhatsAppConnectSession:
|
||||
id: str
|
||||
channel: WhatsAppChannel
|
||||
client: Any
|
||||
result: asyncio.Future[None]
|
||||
connect_task: asyncio.Task[asyncio.Task[Any] | None]
|
||||
target_path: Path
|
||||
pending_path: Path
|
||||
created_wall: float
|
||||
deadline: float
|
||||
qr_url: str = ""
|
||||
finalize_task: asyncio.Task[None] | None = None
|
||||
|
||||
|
||||
class WhatsAppConnectStore:
|
||||
"""In-memory WhatsApp linked-device sessions for the WebUI."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self._sessions: dict[str, WhatsAppConnectSession] = {}
|
||||
|
||||
async def handle(self, action: str, query: QueryParams) -> dict[str, Any]:
|
||||
if action == "start":
|
||||
force = (query_first(query, "force") or "").strip().lower() in {
|
||||
"1",
|
||||
"true",
|
||||
"yes",
|
||||
}
|
||||
return await self.start(force=force)
|
||||
|
||||
session_id = (query_first(query, "session_id") or "").strip()
|
||||
if not session_id:
|
||||
raise ChannelConnectError("missing WhatsApp connect session")
|
||||
if action == "poll":
|
||||
return await self.poll(session_id)
|
||||
if action == "cancel":
|
||||
return await self.cancel(session_id)
|
||||
raise ChannelConnectError(f"unsupported WhatsApp connect action: {action}", status=404)
|
||||
|
||||
async def start(self, *, force: bool = False) -> dict[str, Any]:
|
||||
await self._cleanup()
|
||||
channel, target_path = self._build_channel()
|
||||
if not force and self._local_state_present(target_path):
|
||||
return {
|
||||
"session_id": "",
|
||||
"status": "succeeded",
|
||||
"message": "WhatsApp is already connected.",
|
||||
"interval_ms": 2000,
|
||||
}
|
||||
|
||||
session_id = secrets.token_urlsafe(18)
|
||||
pending_path = target_path.with_name(f".{target_path.name}.{session_id}.connect")
|
||||
pending_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
self._remove_database(pending_path)
|
||||
channel.config.database_path = str(pending_path)
|
||||
now_wall = time.time()
|
||||
|
||||
async def capture_qr(qr_data: bytes) -> None:
|
||||
session = self._sessions.get(session_id)
|
||||
if session is not None:
|
||||
session.qr_url = qr_data.decode("utf-8")
|
||||
|
||||
client, result = channel.connect_open_client(capture_qr)
|
||||
connect_task = asyncio.create_task(
|
||||
self._connect(channel, client, result),
|
||||
name=f"whatsapp-connect-{session_id}",
|
||||
)
|
||||
session = WhatsAppConnectSession(
|
||||
id=session_id,
|
||||
channel=channel,
|
||||
client=client,
|
||||
result=result,
|
||||
connect_task=connect_task,
|
||||
target_path=target_path,
|
||||
pending_path=pending_path,
|
||||
created_wall=now_wall,
|
||||
deadline=time.monotonic() + 600,
|
||||
)
|
||||
self._sessions[session_id] = session
|
||||
|
||||
qr_wait = asyncio.create_task(self._wait_for_qr(session))
|
||||
try:
|
||||
await asyncio.wait(
|
||||
{qr_wait, connect_task},
|
||||
timeout=3,
|
||||
return_when=asyncio.FIRST_COMPLETED,
|
||||
)
|
||||
finally:
|
||||
if not qr_wait.done():
|
||||
qr_wait.cancel()
|
||||
return self._pending_payload(session)
|
||||
|
||||
async def poll(self, session_id: str) -> dict[str, Any]:
|
||||
await self._cleanup()
|
||||
session = self._sessions.get(session_id)
|
||||
if session is None:
|
||||
return {
|
||||
"session_id": session_id,
|
||||
"status": "expired",
|
||||
"message": "This WhatsApp login has expired. Start again.",
|
||||
}
|
||||
if not session.result.done():
|
||||
return self._pending_payload(session)
|
||||
|
||||
finalize_task = session.finalize_task
|
||||
if finalize_task is None:
|
||||
finalize_task = asyncio.create_task(
|
||||
self._finalize_session(session),
|
||||
name=f"whatsapp-connect-finalize-{session.id}",
|
||||
)
|
||||
session.finalize_task = finalize_task
|
||||
if not finalize_task.done():
|
||||
# Finish fast clients in this request without letting native shutdown
|
||||
# block the polling response path.
|
||||
await asyncio.wait({finalize_task}, timeout=_FINALIZE_POLL_WAIT_SECONDS)
|
||||
if not finalize_task.done():
|
||||
payload = self._pending_payload(session)
|
||||
payload["qr_url"] = ""
|
||||
payload["message"] = "Finishing the WhatsApp connection."
|
||||
return payload
|
||||
|
||||
self._sessions.pop(session_id, None)
|
||||
try:
|
||||
finalize_task.result()
|
||||
except Exception as exc:
|
||||
return {
|
||||
"session_id": session_id,
|
||||
"status": "failed",
|
||||
"message": f"WhatsApp QR login failed: {exc}",
|
||||
}
|
||||
return {
|
||||
"session_id": session_id,
|
||||
"status": "succeeded",
|
||||
"message": "WhatsApp is connected.",
|
||||
}
|
||||
|
||||
async def _finalize_session(self, session: WhatsAppConnectSession) -> None:
|
||||
try:
|
||||
session.result.result()
|
||||
await self._close_session(session)
|
||||
await self._commit_database(session.pending_path, session.target_path)
|
||||
except BaseException:
|
||||
await self._close_session(session)
|
||||
self._remove_database(session.pending_path)
|
||||
raise
|
||||
|
||||
async def cancel(self, session_id: str) -> dict[str, Any]:
|
||||
session = self._sessions.pop(session_id, None)
|
||||
if session is not None:
|
||||
await self._discard_session(session)
|
||||
return {
|
||||
"session_id": session_id,
|
||||
"status": "cancelled",
|
||||
"message": "WhatsApp login cancelled.",
|
||||
}
|
||||
|
||||
async def _cleanup(self) -> None:
|
||||
expired = [
|
||||
session_id
|
||||
for session_id, session in self._sessions.items()
|
||||
if time.monotonic() >= session.deadline
|
||||
]
|
||||
for session_id in expired:
|
||||
session = self._sessions.pop(session_id)
|
||||
await self._discard_session(session)
|
||||
|
||||
async def close(self) -> None:
|
||||
"""Stop every login client before the gateway event loop closes."""
|
||||
sessions = tuple(self._sessions.values())
|
||||
self._sessions.clear()
|
||||
for session in sessions:
|
||||
await self._discard_session(session)
|
||||
|
||||
async def _discard_session(self, session: WhatsAppConnectSession) -> None:
|
||||
finalize_task = session.finalize_task
|
||||
if (
|
||||
finalize_task is not None
|
||||
and finalize_task is not asyncio.current_task()
|
||||
):
|
||||
if not finalize_task.done():
|
||||
finalize_task.cancel()
|
||||
await asyncio.gather(finalize_task, return_exceptions=True)
|
||||
else:
|
||||
await self._close_session(session)
|
||||
self._remove_database(session.pending_path)
|
||||
|
||||
@staticmethod
|
||||
async def _connect(
|
||||
channel: WhatsAppChannel,
|
||||
client: Any,
|
||||
result: asyncio.Future[None],
|
||||
) -> asyncio.Task[Any] | None:
|
||||
try:
|
||||
return await channel.connect_start_client(client, result)
|
||||
except asyncio.CancelledError:
|
||||
raise
|
||||
except Exception as exc:
|
||||
if not result.done():
|
||||
result.set_exception(exc)
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
async def _wait_for_qr(session: WhatsAppConnectSession) -> None:
|
||||
while not session.qr_url and not session.result.done():
|
||||
await asyncio.sleep(0.05)
|
||||
|
||||
@staticmethod
|
||||
async def _close_session(session: WhatsAppConnectSession) -> None:
|
||||
if not session.result.done():
|
||||
session.result.cancel()
|
||||
if not session.connect_task.done():
|
||||
session.connect_task.cancel()
|
||||
with suppress(Exception, asyncio.CancelledError):
|
||||
await session.connect_task
|
||||
native_task: asyncio.Task[Any] | None = None
|
||||
if session.connect_task.done() and not session.connect_task.cancelled():
|
||||
with suppress(Exception, asyncio.CancelledError):
|
||||
native_task = session.connect_task.result()
|
||||
client_native_task = getattr(session.client, "connect_task", None)
|
||||
if native_task is None and isinstance(client_native_task, asyncio.Task):
|
||||
native_task = cast(asyncio.Task[Any], client_native_task)
|
||||
with suppress(Exception):
|
||||
await asyncio.wait_for(session.client.stop(), timeout=5)
|
||||
if native_task is not None:
|
||||
if not native_task.done():
|
||||
try:
|
||||
# Stop only signals neonize's Go worker. Wait for that worker
|
||||
# to return so Windows releases its SQLite file handle before
|
||||
# the temporary login database is promoted.
|
||||
await asyncio.wait_for(
|
||||
asyncio.shield(native_task),
|
||||
timeout=_NATIVE_STOP_TIMEOUT_SECONDS,
|
||||
)
|
||||
except TimeoutError:
|
||||
native_task.cancel()
|
||||
await asyncio.gather(native_task, return_exceptions=True)
|
||||
|
||||
@staticmethod
|
||||
def _build_channel() -> tuple[WhatsAppChannel, Path]:
|
||||
from nanobot.bus.queue import MessageBus
|
||||
from nanobot.channels.whatsapp.runtime import WhatsAppChannel
|
||||
|
||||
section = getattr(load_config().channels, "whatsapp", None)
|
||||
if section is not None and hasattr(section, "model_dump"):
|
||||
config = section.model_dump(mode="json", by_alias=True)
|
||||
elif isinstance(section, dict):
|
||||
config = dict(cast(dict[str, Any], section))
|
||||
else:
|
||||
config = {}
|
||||
channel = WhatsAppChannel(config, MessageBus())
|
||||
return channel, channel.connect_database_path()
|
||||
|
||||
@staticmethod
|
||||
def _local_state_present(path: Path) -> bool:
|
||||
try:
|
||||
return path.is_file() and path.stat().st_size > 0
|
||||
except OSError:
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
async def _commit_database(cls, pending: Path, target: Path) -> None:
|
||||
if not cls._local_state_present(pending):
|
||||
raise RuntimeError("WhatsApp connected but did not create a local session database")
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
for pending_file, target_file in cls._database_files(pending, target):
|
||||
if pending_file.exists():
|
||||
await cls._replace_database_file(pending_file, target_file)
|
||||
elif target_file != target and target_file.exists():
|
||||
target_file.unlink()
|
||||
|
||||
@staticmethod
|
||||
async def _replace_database_file(source: Path, target: Path) -> None:
|
||||
deadline = time.monotonic() + _DATABASE_REPLACE_TIMEOUT_SECONDS
|
||||
while True:
|
||||
try:
|
||||
os.replace(source, target)
|
||||
return
|
||||
except PermissionError as exc:
|
||||
if time.monotonic() >= deadline:
|
||||
raise RuntimeError(
|
||||
"WhatsApp paired, but its local session database is still in use"
|
||||
) from exc
|
||||
await asyncio.sleep(0.1)
|
||||
|
||||
@classmethod
|
||||
def _remove_database(cls, path: Path) -> None:
|
||||
for candidate, _ in cls._database_files(path, path):
|
||||
with suppress(OSError):
|
||||
candidate.unlink()
|
||||
|
||||
@staticmethod
|
||||
def _database_files(source: Path, target: Path) -> tuple[tuple[Path, Path], ...]:
|
||||
return (
|
||||
(source, target),
|
||||
(source.with_name(source.name + "-shm"), target.with_name(target.name + "-shm")),
|
||||
(source.with_name(source.name + "-wal"), target.with_name(target.name + "-wal")),
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _pending_payload(session: WhatsAppConnectSession) -> dict[str, Any]:
|
||||
payload: dict[str, Any] = {
|
||||
"session_id": session.id,
|
||||
"status": "pending",
|
||||
"interval_ms": 2000,
|
||||
"expires_at_ms": int((session.created_wall + 600) * 1000),
|
||||
"message": "Waiting for the WhatsApp scan.",
|
||||
}
|
||||
if session.qr_url:
|
||||
payload["qr_url"] = session.qr_url
|
||||
return payload
|
||||
|
||||
|
||||
__all__ = ["WhatsAppConnectStore"]
|
||||
Reference in new issue
Block a user