This commit is contained in:
Yuriy Yuriev
2026-07-21 00:22:37 +07:00
parent 9aa072792d
commit 19acea8481
2 changed files with 214 additions and 104 deletions
+43 -6
View File
@@ -23,15 +23,52 @@ class AuthStorage:
self.file_path.parent.mkdir(parents=True, exist_ok=True)
self._lock = asyncio.Lock()
async def register(self, user_id: int) -> bool:
async def register(
self, user_id: int, username: str = None, full_name: str = None
) -> bool:
"""Регистрирует пользователя и освежает его профиль.
Профиль перезаписывается и для уже известных пользователей — ник в
Telegram может смениться, а взять его неоткуда, кроме входящего
апдейта.
"""
async with self._lock:
data = await asyncio.to_thread(self._load_sync)
if str(user_id) in data:
return False
data[str(user_id)] = {"registered_at": datetime.now().isoformat()}
key = str(user_id)
is_new = key not in data
record = data.get(key, {})
if is_new:
record["registered_at"] = datetime.now().isoformat()
if username is not None:
record["username"] = username
if full_name is not None:
record["full_name"] = full_name
data[key] = record
await asyncio.to_thread(self._save_sync, data)
logger.info(f"User {user_id} registered")
return True
if is_new:
logger.info(f"User {user_id} registered ({username or full_name or 'no name'})")
return is_new
async def get_profile(self, user_id: int) -> dict:
async with self._lock:
data = await asyncio.to_thread(self._load_sync)
return data.get(str(user_id), {})
async def get_all_profiles(self) -> dict:
"""Возвращает {user_id: профиль} для всех пользователей."""
async with self._lock:
data = await asyncio.to_thread(self._load_sync)
return {int(k): v for k, v in data.items()}
@staticmethod
def display_name(profile: dict) -> str:
"""Человекочитаемое имя: @ник, иначе имя, иначе пусто."""
if not profile:
return ""
username = profile.get("username")
if username:
return f"@{username}"
return profile.get("full_name") or ""
async def delete_user(self, user_id: int) -> bool:
async with self._lock: