mirror of
https://github.com/cnlimiter/codex-register.git
synced 2026-06-27 18:22:03 +08:00
fix: 扩展邮箱验证码去重到现有账号链路
This commit is contained in:
@@ -1681,6 +1681,10 @@ class RegistrationEngine:
|
||||
try:
|
||||
# 获取默认 client_id
|
||||
settings = get_settings()
|
||||
metadata = dict(result.metadata or {})
|
||||
verification_state = self.email_service.export_verification_state(result.email or self.email)
|
||||
if verification_state["used_codes"] or verification_state["seen_messages"]:
|
||||
metadata["verification_state"] = verification_state
|
||||
|
||||
with get_db() as db:
|
||||
# 保存账户信息
|
||||
@@ -1699,7 +1703,7 @@ class RegistrationEngine:
|
||||
id_token=result.id_token,
|
||||
cookies=result.cookies,
|
||||
proxy_used=self.proxy_url,
|
||||
extra_data=result.metadata,
|
||||
extra_data=metadata,
|
||||
source=result.source
|
||||
)
|
||||
|
||||
|
||||
@@ -271,7 +271,12 @@ class DuckMailService(BaseEmailService):
|
||||
)
|
||||
messages = response.get("hydra:member", [])
|
||||
|
||||
for message in messages:
|
||||
ordered_messages = self._sort_items_by_message_time(
|
||||
messages,
|
||||
lambda item: item.get("createdAt") if isinstance(item, dict) else None,
|
||||
)
|
||||
|
||||
for message in ordered_messages:
|
||||
message_id = str(message.get("id") or "").strip()
|
||||
if not message_id or message_id in seen_message_ids:
|
||||
continue
|
||||
|
||||
@@ -316,7 +316,17 @@ class MeoMailEmailService(BaseEmailService):
|
||||
time.sleep(3)
|
||||
continue
|
||||
|
||||
for message in messages:
|
||||
ordered_messages = self._sort_items_by_message_time(
|
||||
messages,
|
||||
lambda item: (
|
||||
item.get("created_at")
|
||||
or item.get("createdAt")
|
||||
or item.get("received_at")
|
||||
or item.get("receivedAt")
|
||||
) if isinstance(item, dict) else None,
|
||||
)
|
||||
|
||||
for message in ordered_messages:
|
||||
message_id = message.get("id")
|
||||
if not message_id or message_id in seen_message_ids:
|
||||
continue
|
||||
|
||||
@@ -241,7 +241,12 @@ class TempmailService(BaseEmailService):
|
||||
time.sleep(3)
|
||||
continue
|
||||
|
||||
for msg in email_list:
|
||||
ordered_emails = self._sort_items_by_message_time(
|
||||
email_list,
|
||||
lambda item: item.get("date") if isinstance(item, dict) else None,
|
||||
)
|
||||
|
||||
for msg in ordered_emails:
|
||||
if not isinstance(msg, dict):
|
||||
continue
|
||||
|
||||
|
||||
@@ -1565,6 +1565,29 @@ def _build_inbox_config(db, service_type, email: str) -> dict:
|
||||
return cfg
|
||||
|
||||
|
||||
def _load_account_verification_state(account: Account) -> dict:
|
||||
"""从账号扩展信息中读取验证码去重状态。"""
|
||||
extra = account.extra_data or {}
|
||||
state = extra.get("verification_state") if isinstance(extra, dict) else {}
|
||||
if not isinstance(state, dict):
|
||||
state = {}
|
||||
return {
|
||||
"used_codes": [str(code) for code in (state.get("used_codes") or []) if code],
|
||||
"seen_messages": [str(marker) for marker in (state.get("seen_messages") or []) if marker],
|
||||
}
|
||||
|
||||
|
||||
def _save_account_verification_state(db, account: Account, service) -> None:
|
||||
"""将当前收件箱消费状态持久化到账号表,支持跨请求延续。"""
|
||||
state = service.export_verification_state(account.email)
|
||||
if not state["used_codes"] and not state["seen_messages"]:
|
||||
return
|
||||
|
||||
extra = dict(account.extra_data or {})
|
||||
extra["verification_state"] = state
|
||||
crud.update_account(db, account.id, extra_data=extra)
|
||||
|
||||
|
||||
@router.post("/{account_id}/inbox-code")
|
||||
async def get_account_inbox_code(account_id: int):
|
||||
"""查询账号邮箱收件箱最新验证码"""
|
||||
@@ -1586,6 +1609,10 @@ async def get_account_inbox_code(account_id: int):
|
||||
|
||||
try:
|
||||
svc = EmailServiceFactory.create(service_type, config)
|
||||
svc.load_verification_state(
|
||||
account.email,
|
||||
**_load_account_verification_state(account),
|
||||
)
|
||||
code = svc.get_verification_code(
|
||||
account.email,
|
||||
email_id=account.email_service_id,
|
||||
@@ -1597,4 +1624,6 @@ async def get_account_inbox_code(account_id: int):
|
||||
if not code:
|
||||
return {"success": False, "error": "未收到验证码邮件"}
|
||||
|
||||
_save_account_verification_state(db, account, svc)
|
||||
|
||||
return {"success": True, "code": code, "email": account.email}
|
||||
|
||||
Reference in New Issue
Block a user