forked from mtusi2026/mtusi2026lab3
552 lines
18 KiB
Python
552 lines
18 KiB
Python
import asyncio
|
|
import base64
|
|
from contextlib import asynccontextmanager
|
|
import datetime as dt
|
|
import json
|
|
import logging
|
|
import uuid
|
|
from typing import Any
|
|
|
|
import asyncpg
|
|
import redis.asyncio as redis
|
|
|
|
from app.auth import generate_session_token, hash_password, verify_password
|
|
from app.config import (
|
|
CACHE_URL,
|
|
DATABASE_URL,
|
|
SESSION_TTL_SECONDS,
|
|
)
|
|
from app.errors import (
|
|
ConflictError,
|
|
InvalidRequestError,
|
|
NotFoundError,
|
|
UnauthorizedError,
|
|
)
|
|
from app.schemas import (
|
|
ChatResponse,
|
|
MessageResponse,
|
|
SessionResponse,
|
|
UserResponse,
|
|
now_utc_iso,
|
|
)
|
|
|
|
logger = logging.getLogger("app.storage")
|
|
|
|
|
|
def encode_cursor(data: dict) -> str:
|
|
raw = json.dumps(data, separators=(",", ":")).encode("utf-8")
|
|
return base64.urlsafe_b64encode(raw).decode("utf-8").rstrip("=")
|
|
|
|
|
|
def decode_cursor(cursor_str: str) -> dict:
|
|
if not isinstance(cursor_str, str) or not cursor_str.strip():
|
|
raise InvalidRequestError("Некорректный курсор")
|
|
try:
|
|
padded = cursor_str + "=" * (-len(cursor_str) % 4)
|
|
raw = base64.urlsafe_b64decode(padded.encode("utf-8"))
|
|
data = json.loads(raw.decode("utf-8"))
|
|
if not isinstance(data, dict):
|
|
raise InvalidRequestError("Некорректный курсор")
|
|
return data
|
|
except InvalidRequestError:
|
|
raise
|
|
except Exception as exc:
|
|
raise InvalidRequestError("Некорректный курсор") from exc
|
|
|
|
|
|
def _format_dt(d: dt.datetime) -> str:
|
|
if d.tzinfo is None:
|
|
d = d.replace(tzinfo=dt.timezone.utc)
|
|
return d.astimezone(dt.timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%fZ")
|
|
|
|
|
|
def _to_text(val: Any) -> str:
|
|
if isinstance(val, (bytes, bytearray, memoryview)):
|
|
return bytes(val).decode("utf-8")
|
|
return str(val)
|
|
|
|
|
|
class PostgresStorage:
|
|
def __init__(self) -> None:
|
|
self.pg_pool: asyncpg.Pool | None = None
|
|
self.redis_client: redis.Redis | None = None
|
|
|
|
@asynccontextmanager
|
|
async def acquire_conn(self, timeout: float = 2.0):
|
|
if self.pg_pool is None:
|
|
raise asyncpg.InterfaceError("Пул подключений к PostgreSQL не инициализирован")
|
|
async with asyncio.timeout(4.0):
|
|
async with self.pg_pool.acquire(timeout=timeout) as conn:
|
|
yield conn
|
|
|
|
async def connect(self) -> None:
|
|
logger.info("Подключение к PostgreSQL: %s", DATABASE_URL.split("@")[-1])
|
|
self.pg_pool = await asyncpg.create_pool(
|
|
DATABASE_URL,
|
|
min_size=2,
|
|
max_size=20,
|
|
command_timeout=2.0,
|
|
timeout=2.0,
|
|
)
|
|
|
|
logger.info("Подключение к Redis: %s", CACHE_URL.split("@")[-1])
|
|
self.redis_client = redis.from_url(
|
|
CACHE_URL,
|
|
decode_responses=True,
|
|
socket_connect_timeout=3.0,
|
|
socket_timeout=3.0,
|
|
)
|
|
|
|
try:
|
|
async with self.acquire_conn() as conn:
|
|
col_type = await conn.fetchval(
|
|
"SELECT data_type FROM information_schema.columns WHERE table_name = 'messages' AND column_name = 'text'"
|
|
)
|
|
if col_type in ("text", "character varying"):
|
|
await conn.execute(
|
|
"ALTER TABLE messages ALTER COLUMN text TYPE BYTEA USING text::bytea"
|
|
)
|
|
except Exception as exc:
|
|
logger.warning("Не удалось проверить/обновить схему messages.text: %s", exc)
|
|
|
|
async def disconnect(self) -> None:
|
|
if self.pg_pool:
|
|
await self.pg_pool.close()
|
|
self.pg_pool = None
|
|
if self.redis_client:
|
|
await self.redis_client.close()
|
|
self.redis_client = None
|
|
|
|
async def check_health(self) -> bool:
|
|
if not self.pg_pool or not self.redis_client:
|
|
return False
|
|
try:
|
|
async with self.acquire_conn() as conn:
|
|
await conn.fetchval("SELECT 1")
|
|
await self.redis_client.ping()
|
|
return True
|
|
except Exception as exc:
|
|
logger.warning("Ошибка проверки health: %s", exc)
|
|
return False
|
|
|
|
# ------------------ Пользователи и аутентификация ------------------
|
|
|
|
async def create_user(
|
|
self, username: str, display_name: str, password: str
|
|
) -> UserResponse:
|
|
user_id = uuid.uuid4()
|
|
now = dt.datetime.now(dt.timezone.utc)
|
|
pwd_hash = hash_password(password)
|
|
|
|
try:
|
|
async with self.acquire_conn() as conn:
|
|
await conn.execute(
|
|
"""
|
|
INSERT INTO users (id, username, display_name, password_hash, created_at)
|
|
VALUES ($1, $2, $3, $4, $5)
|
|
""",
|
|
user_id,
|
|
username,
|
|
display_name,
|
|
pwd_hash,
|
|
now,
|
|
)
|
|
except asyncpg.UniqueViolationError:
|
|
raise ConflictError("Имя пользователя уже занято")
|
|
|
|
return UserResponse(
|
|
id=str(user_id),
|
|
username=username,
|
|
display_name=display_name,
|
|
created_at=_format_dt(now),
|
|
)
|
|
|
|
async def authenticate_user(
|
|
self, username: str, password: str
|
|
) -> UserResponse | None:
|
|
async with self.acquire_conn() as conn:
|
|
row = await conn.fetchrow(
|
|
"""
|
|
SELECT id, username, display_name, password_hash, created_at
|
|
FROM users
|
|
WHERE username = $1
|
|
""",
|
|
username,
|
|
)
|
|
|
|
if not row:
|
|
return None
|
|
|
|
if not verify_password(row["password_hash"], password):
|
|
return None
|
|
|
|
return UserResponse(
|
|
id=str(row["id"]),
|
|
username=row["username"],
|
|
display_name=_to_text(row["display_name"]),
|
|
created_at=_format_dt(row["created_at"]),
|
|
)
|
|
|
|
async def get_user_by_id(self, user_id: str) -> UserResponse | None:
|
|
try:
|
|
uid = uuid.UUID(user_id)
|
|
except ValueError:
|
|
return None
|
|
|
|
async with self.acquire_conn() as conn:
|
|
row = await conn.fetchrow(
|
|
"""
|
|
SELECT id, username, display_name, created_at
|
|
FROM users
|
|
WHERE id = $1
|
|
""",
|
|
uid,
|
|
)
|
|
|
|
if not row:
|
|
return None
|
|
|
|
return UserResponse(
|
|
id=str(row["id"]),
|
|
username=row["username"],
|
|
display_name=_to_text(row["display_name"]),
|
|
created_at=_format_dt(row["created_at"]),
|
|
)
|
|
|
|
# ------------------ Сессии (Redis) ------------------
|
|
|
|
async def create_session(self, user: UserResponse) -> SessionResponse:
|
|
assert self.redis_client is not None
|
|
token = generate_session_token()
|
|
now = dt.datetime.now(dt.timezone.utc)
|
|
expires_at = now + dt.timedelta(seconds=SESSION_TTL_SECONDS)
|
|
expires_at_str = _format_dt(expires_at)
|
|
|
|
session_data = json.dumps(
|
|
{
|
|
"user_id": user.id,
|
|
"expires_at": expires_at_str,
|
|
}
|
|
)
|
|
|
|
key = f"session:{token}"
|
|
await self.redis_client.set(key, session_data, ex=SESSION_TTL_SECONDS)
|
|
|
|
return SessionResponse(
|
|
token=token,
|
|
token_type="Bearer",
|
|
expires_at=expires_at_str,
|
|
user=user,
|
|
)
|
|
|
|
async def get_session_user(self, token: str) -> UserResponse | None:
|
|
if not token or not self.redis_client:
|
|
return None
|
|
|
|
key = f"session:{token}"
|
|
raw = await self.redis_client.get(key)
|
|
if not raw:
|
|
return None
|
|
|
|
try:
|
|
data = json.loads(raw)
|
|
user_id = data.get("user_id")
|
|
except Exception:
|
|
return None
|
|
|
|
if not user_id:
|
|
return None
|
|
|
|
return await self.get_user_by_id(user_id)
|
|
|
|
async def delete_session(self, token: str) -> bool:
|
|
if not token or not self.redis_client:
|
|
return False
|
|
key = f"session:{token}"
|
|
res = await self.redis_client.delete(key)
|
|
return res > 0
|
|
|
|
# ------------------ Чаты ------------------
|
|
|
|
async def create_chat(
|
|
self, caller_id: str, title: str, member_ids: list[str]
|
|
) -> ChatResponse:
|
|
if caller_id not in member_ids:
|
|
raise InvalidRequestError("Создатель чата должен быть в списке участников")
|
|
|
|
chat_id = uuid.uuid4()
|
|
now = dt.datetime.now(dt.timezone.utc)
|
|
|
|
member_uuids = [uuid.UUID(m) for m in member_ids]
|
|
|
|
async with self.acquire_conn() as conn:
|
|
# Проверяем существование всех участников
|
|
found = await conn.fetch(
|
|
"SELECT id FROM users WHERE id = ANY($1::uuid[])", member_uuids
|
|
)
|
|
if len(found) != len(member_uuids):
|
|
raise InvalidRequestError(
|
|
"Один или несколько участников чата не найдены"
|
|
)
|
|
|
|
async with conn.transaction():
|
|
await conn.execute(
|
|
"INSERT INTO chats (id, title, created_at) VALUES ($1, $2, $3)",
|
|
chat_id,
|
|
title,
|
|
now,
|
|
)
|
|
member_records = [(chat_id, m) for m in member_uuids]
|
|
await conn.executemany(
|
|
"INSERT INTO chat_members (chat_id, user_id) VALUES ($1, $2)",
|
|
member_records,
|
|
)
|
|
|
|
return ChatResponse(
|
|
id=str(chat_id),
|
|
title=title,
|
|
member_ids=member_ids,
|
|
created_at=_format_dt(now),
|
|
)
|
|
|
|
async def get_chat(self, caller_id: str, chat_id: str) -> ChatResponse:
|
|
try:
|
|
cid = uuid.UUID(chat_id)
|
|
c_caller = uuid.UUID(caller_id)
|
|
except ValueError:
|
|
raise NotFoundError("Чат не найден")
|
|
|
|
async with self.acquire_conn() as conn:
|
|
# Проверяем членство вызывающего
|
|
is_member = await conn.fetchval(
|
|
"SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2",
|
|
cid,
|
|
c_caller,
|
|
)
|
|
if not is_member:
|
|
raise NotFoundError("Чат не найден")
|
|
|
|
chat_row = await conn.fetchrow(
|
|
"SELECT id, title, created_at FROM chats WHERE id = $1", cid
|
|
)
|
|
if not chat_row:
|
|
raise NotFoundError("Чат не найден")
|
|
|
|
members_rows = await conn.fetch(
|
|
"SELECT user_id FROM chat_members WHERE chat_id = $1", cid
|
|
)
|
|
member_ids = [str(r["user_id"]) for r in members_rows]
|
|
|
|
return ChatResponse(
|
|
id=str(chat_row["id"]),
|
|
title=_to_text(chat_row["title"]),
|
|
member_ids=member_ids,
|
|
created_at=_format_dt(chat_row["created_at"]),
|
|
)
|
|
|
|
async def list_chats(
|
|
self, caller_id: str, limit: int = 50, cursor: str | None = None
|
|
) -> tuple[list[ChatResponse], str | None]:
|
|
try:
|
|
c_caller = uuid.UUID(caller_id)
|
|
except ValueError:
|
|
return [], None
|
|
|
|
c_created: dt.datetime | None = None
|
|
c_id: uuid.UUID | None = None
|
|
|
|
if cursor is not None:
|
|
c_data = decode_cursor(cursor)
|
|
if c_data.get("type") != "chats":
|
|
raise InvalidRequestError("Недействительный тип курсора")
|
|
c_created_str = c_data.get("created_at")
|
|
c_id_str = c_data.get("id")
|
|
if (
|
|
not isinstance(c_created_str, str)
|
|
or not isinstance(c_id_str, str)
|
|
or not c_created_str.strip()
|
|
or not c_id_str.strip()
|
|
):
|
|
raise InvalidRequestError("Поврежденный курсор")
|
|
try:
|
|
c_created = dt.datetime.fromisoformat(
|
|
c_created_str.replace("Z", "+00:00")
|
|
)
|
|
c_id = uuid.UUID(c_id_str)
|
|
except Exception:
|
|
raise InvalidRequestError("Некорректные параметры курсора")
|
|
|
|
async with self.acquire_conn() as conn:
|
|
query = """
|
|
SELECT c.id, c.title, c.created_at, array_agg(cm.user_id::text) as member_ids
|
|
FROM chats c
|
|
JOIN chat_members my_m ON c.id = my_m.chat_id AND my_m.user_id = $1
|
|
JOIN chat_members cm ON c.id = cm.chat_id
|
|
WHERE ($2::timestamptz IS NULL OR (c.created_at, c.id) > ($2::timestamptz, $3::uuid))
|
|
GROUP BY c.id, c.title, c.created_at
|
|
ORDER BY c.created_at ASC, c.id ASC
|
|
LIMIT $4
|
|
"""
|
|
rows = await conn.fetch(query, c_caller, c_created, c_id, limit + 1)
|
|
|
|
has_next = len(rows) > limit
|
|
items_rows = rows[:limit]
|
|
|
|
items = [
|
|
ChatResponse(
|
|
id=str(r["id"]),
|
|
title=_to_text(r["title"]),
|
|
member_ids=list(r["member_ids"]),
|
|
created_at=_format_dt(r["created_at"]),
|
|
)
|
|
for r in items_rows
|
|
]
|
|
|
|
next_cursor = None
|
|
if has_next and items:
|
|
last = items[-1]
|
|
next_cursor = encode_cursor(
|
|
{
|
|
"type": "chats",
|
|
"created_at": last.created_at,
|
|
"id": last.id,
|
|
}
|
|
)
|
|
|
|
return items, next_cursor
|
|
|
|
# ------------------ Сообщения ------------------
|
|
|
|
async def send_message(
|
|
self, caller_id: str, chat_id: str, text: str
|
|
) -> MessageResponse:
|
|
try:
|
|
cid = uuid.UUID(chat_id)
|
|
c_caller = uuid.UUID(caller_id)
|
|
except ValueError:
|
|
raise NotFoundError("Чат не найден")
|
|
|
|
msg_id = uuid.uuid4()
|
|
now = dt.datetime.now(dt.timezone.utc)
|
|
text_bytes = text.encode("utf-8")
|
|
|
|
async with self.acquire_conn() as conn:
|
|
is_member = await conn.fetchval(
|
|
"SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2",
|
|
cid,
|
|
c_caller,
|
|
)
|
|
if not is_member:
|
|
raise NotFoundError("Чат не найден")
|
|
|
|
await conn.execute(
|
|
"""
|
|
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
|
|
VALUES ($1, $2, $3, $4, $5)
|
|
""",
|
|
msg_id,
|
|
cid,
|
|
c_caller,
|
|
text_bytes,
|
|
now,
|
|
)
|
|
|
|
return MessageResponse(
|
|
id=str(msg_id),
|
|
chat_id=chat_id,
|
|
sender_id=caller_id,
|
|
text=text,
|
|
created_at=_format_dt(now),
|
|
)
|
|
|
|
async def list_messages(
|
|
self, caller_id: str, chat_id: str, limit: int = 50, cursor: str | None = None
|
|
) -> tuple[list[MessageResponse], str | None]:
|
|
try:
|
|
cid = uuid.UUID(chat_id)
|
|
c_caller = uuid.UUID(caller_id)
|
|
except ValueError:
|
|
raise NotFoundError("Чат не найден")
|
|
|
|
async with self.acquire_conn() as conn:
|
|
is_member = await conn.fetchval(
|
|
"SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2",
|
|
cid,
|
|
c_caller,
|
|
)
|
|
if not is_member:
|
|
raise NotFoundError("Чат не найден")
|
|
|
|
c_created: dt.datetime | None = None
|
|
c_id: uuid.UUID | None = None
|
|
|
|
if cursor is not None:
|
|
c_data = decode_cursor(cursor)
|
|
if c_data.get("type") != "messages" or c_data.get("chat_id") != chat_id:
|
|
raise InvalidRequestError(
|
|
"Курсор принадлежит другому чату или недействителен"
|
|
)
|
|
c_created_str = c_data.get("created_at")
|
|
c_id_str = c_data.get("id")
|
|
if (
|
|
not isinstance(c_created_str, str)
|
|
or not isinstance(c_id_str, str)
|
|
or not c_created_str.strip()
|
|
or not c_id_str.strip()
|
|
):
|
|
raise InvalidRequestError("Поврежденный курсор")
|
|
try:
|
|
c_created = dt.datetime.fromisoformat(
|
|
c_created_str.replace("Z", "+00:00")
|
|
)
|
|
c_id = uuid.UUID(c_id_str)
|
|
except Exception:
|
|
raise InvalidRequestError("Некорректные параметры курсора")
|
|
|
|
query = """
|
|
SELECT id, chat_id, sender_id, text, created_at
|
|
FROM messages
|
|
WHERE chat_id = $1
|
|
AND ($2::timestamptz IS NULL OR (created_at, id) > ($2::timestamptz, $3::uuid))
|
|
ORDER BY created_at ASC, id ASC
|
|
LIMIT $4
|
|
"""
|
|
rows = await conn.fetch(query, cid, c_created, c_id, limit + 1)
|
|
|
|
has_next = len(rows) > limit
|
|
items_rows = rows[:limit]
|
|
|
|
def _to_text(val: Any) -> str:
|
|
if isinstance(val, (bytes, bytearray, memoryview)):
|
|
return bytes(val).decode("utf-8")
|
|
return str(val)
|
|
|
|
items = [
|
|
MessageResponse(
|
|
id=str(r["id"]),
|
|
chat_id=str(r["chat_id"]),
|
|
sender_id=str(r["sender_id"]),
|
|
text=_to_text(r["text"]),
|
|
created_at=_format_dt(r["created_at"]),
|
|
)
|
|
for r in items_rows
|
|
]
|
|
|
|
next_cursor = None
|
|
if has_next and items:
|
|
last = items[-1]
|
|
next_cursor = encode_cursor(
|
|
{
|
|
"type": "messages",
|
|
"chat_id": chat_id,
|
|
"created_at": last.created_at,
|
|
"id": last.id,
|
|
}
|
|
)
|
|
|
|
return items, next_cursor
|
|
|
|
|
|
# Глобальный экземпляр хранилища
|
|
storage = PostgresStorage()
|