fix(backend): 累积的错误修复和细节修正
This commit is contained in:
@@ -22,7 +22,7 @@ NyaHome 是由 FastAPI 后端、Vue WebUI 实现的在线 AI 文学创作平台
|
|||||||
| 功能 | 阶段 | 优先级 |
|
| 功能 | 阶段 | 优先级 |
|
||||||
|---------------------------|-----|-----|
|
|---------------------------|-----|-----|
|
||||||
| **剧本市场** - 剧本分享、聊天室导出为剧本 | 规划中 | 低 |
|
| **剧本市场** - 剧本分享、聊天室导出为剧本 | 规划中 | 低 |
|
||||||
| **用户功能** - 绑定手机号、接收收集验证码 | 规划中 | 低 |
|
| **用户功能** - 绑定手机号、接收手机验证码 | 规划中 | 低 |
|
||||||
| **用户功能** - 第三方账户 Oauth 登录 | 规划中 | 低 |
|
| **用户功能** - 第三方账户 Oauth 登录 | 规划中 | 低 |
|
||||||
|
|
||||||
## 代码规范
|
## 代码规范
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ from pathlib import Path
|
|||||||
from typing import Mapping
|
from typing import Mapping
|
||||||
|
|
||||||
from nyahome.cli.cli import console
|
from nyahome.cli.cli import console
|
||||||
from nyahome.data import db_type_allowlist, db_driver_available
|
from nyahome.data import db_driver_available, db_type_allowlist
|
||||||
|
|
||||||
|
|
||||||
class CliWarning:
|
class CliWarning:
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ def set_config_item(
|
|||||||
"""
|
"""
|
||||||
config_manager.sync_load_config()
|
config_manager.sync_load_config()
|
||||||
if len(value) == 1:
|
if len(value) == 1:
|
||||||
value = value[0]
|
value: str | int | bool = value[0] # type: ignore[no-redef]
|
||||||
try:
|
try:
|
||||||
config_manager.set(key, value)
|
config_manager.set(key, value)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ logger = logging.getLogger(__name__)
|
|||||||
|
|
||||||
CONFIG_PATH = Path.cwd() / ".nyahome" / "config.json"
|
CONFIG_PATH = Path.cwd() / ".nyahome" / "config.json"
|
||||||
|
|
||||||
T = TypeVar("T")
|
T = TypeVar("T", str, int, bool, list)
|
||||||
|
|
||||||
|
|
||||||
class ConfigManager:
|
class ConfigManager:
|
||||||
@@ -88,6 +88,10 @@ class ConfigManager:
|
|||||||
Args:
|
Args:
|
||||||
key: 配置键名
|
key: 配置键名
|
||||||
value: 配置键的新值,可以是(且仅支持)字符串、整型以及列表。
|
value: 配置键的新值,可以是(且仅支持)字符串、整型以及列表。
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
AttributeError: 配置键名错误
|
||||||
|
TypeError: 配置键值类型错误
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
old_value = self.get(key)
|
old_value = self.get(key)
|
||||||
@@ -102,7 +106,7 @@ class ConfigManager:
|
|||||||
case bool():
|
case bool():
|
||||||
new_value = bool(value)
|
new_value = bool(value)
|
||||||
case list():
|
case list():
|
||||||
new_value = [x for x in value]
|
new_value = list(value)
|
||||||
case _:
|
case _:
|
||||||
raise TypeError(f"不支持 {type(old_value).__name__} 类型的设置项。({key})")
|
raise TypeError(f"不支持 {type(old_value).__name__} 类型的设置项。({key})")
|
||||||
setattr(self._config, key, new_value)
|
setattr(self._config, key, new_value)
|
||||||
@@ -114,6 +118,8 @@ class ConfigManager:
|
|||||||
Args:
|
Args:
|
||||||
key: 配置键名
|
key: 配置键名
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
AttributeError: 配置键名错误
|
||||||
"""
|
"""
|
||||||
ci = Config()
|
ci = Config()
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -42,17 +42,17 @@ class OtpMemoryStore(ABC):
|
|||||||
async def _cleanup(self) -> None:
|
async def _cleanup(self) -> None:
|
||||||
while True:
|
while True:
|
||||||
await asyncio.sleep(60)
|
await asyncio.sleep(60)
|
||||||
logger.debug(f"[{self.type_name}] 开始定时清理过期验证码。")
|
# logger.debug(f"[{self.type_name}] 开始定时清理过期验证码。")
|
||||||
expires = []
|
expires = []
|
||||||
count = 0
|
count = 0
|
||||||
for address, item in self._store.items():
|
for address, item in self._store.items():
|
||||||
if item.expire_time < time.time():
|
if item.expire_time < time.time():
|
||||||
logger.debug(f"[{self.type_name}] 移除过期的 {address}")
|
# logger.debug(f"[{self.type_name}] 移除过期的 {address}")
|
||||||
expires.append(address)
|
expires.append(address)
|
||||||
count += 1
|
count += 1
|
||||||
for address in expires:
|
for address in expires:
|
||||||
self._store.pop(address)
|
self._store.pop(address)
|
||||||
logger.debug(f"[{self.type_name}] 清理完成,清理了 {count} 个过期验证码。")
|
# logger.debug(f"[{self.type_name}] 清理完成,清理了 {count} 个过期验证码。")
|
||||||
|
|
||||||
def verify(self, address: str, user_id: int, verify_code: str) -> bool:
|
def verify(self, address: str, user_id: int, verify_code: str) -> bool:
|
||||||
item = self._store.get(address)
|
item = self._store.get(address)
|
||||||
|
|||||||
@@ -95,10 +95,10 @@ class EmailSenderQueue(TaskQueue):
|
|||||||
body=item.body,
|
body=item.body,
|
||||||
sender=config_manager.get("smtp_sender"),
|
sender=config_manager.get("smtp_sender"),
|
||||||
hostname=config_manager.get("smtp_hostname"),
|
hostname=config_manager.get("smtp_hostname"),
|
||||||
port=config_manager.get("smtp_port"),
|
port=config_manager.get("smtp_port", 465),
|
||||||
username=config_manager.get("smtp_username"),
|
username=config_manager.get("smtp_username"),
|
||||||
password=config_manager.get("smtp_password"),
|
password=config_manager.get("smtp_password"),
|
||||||
use_tls=config_manager.get("smtp_use_tls"),
|
use_tls=config_manager.get("smtp_use_tls", True),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user