Files
2026-10-29 10:53:32 +03:00

538 lines
20 KiB
Python

import asyncio
import hashlib
import json
import secrets
import threading
import uuid
from dataclasses import dataclass
from datetime import datetime, timedelta, timezone
from typing import Dict, List, Optional, Sequence, Tuple
import asyncpg
from .cache import SessionCache
from .errors import conflict, invalid_request, not_found, unavailable
from .pagination import Key
from .passwords import hash_password, password_matches
@dataclass(frozen=True)
class User:
id: str
username: str
display_name: str
created_at: str
def to_dict(self) -> dict:
return {
'id': self.id,
'username': self.username,
'display_name': self.display_name,
'created_at': self.created_at,
}
@dataclass(frozen=True)
class Chat:
id: str
title: str
member_ids: Tuple[str, ...]
created_at: str
def to_dict(self) -> dict:
return {
'id': self.id,
'title': self.title,
'member_ids': list(self.member_ids),
'created_at': self.created_at,
}
@dataclass(frozen=True)
class Message:
id: str
chat_id: str
sender_id: str
text: str
created_at: str
def to_dict(self) -> dict:
return {
'id': self.id,
'chat_id': self.chat_id,
'sender_id': self.sender_id,
'text': self.text,
'created_at': self.created_at,
}
def _token_key(token: str) -> str:
return hashlib.sha256(token.encode('utf-8')).hexdigest()
def _stamp(moment: datetime) -> str:
return moment.astimezone(timezone.utc).isoformat(timespec='microseconds').replace('+00:00', 'Z')
def _encode_page(messages: Sequence[Message], has_more: bool) -> str:
return json.dumps(
{'items': [message.to_dict() for message in messages], 'has_more': has_more},
separators=(',', ':'),
)
def _decode_page(payload: Optional[str]) -> Optional[Tuple[List[Message], bool]]:
if not payload:
return None
try:
data = json.loads(payload)
messages = [
Message(item['id'], item['chat_id'], item['sender_id'], item['text'], item['created_at'])
for item in data['items']
]
return messages, bool(data['has_more'])
except (KeyError, TypeError, ValueError):
return None
def _parse_stamp(value: str) -> datetime:
if value.endswith('Z'):
value = value[:-1] + '+00:00'
return datetime.fromisoformat(value)
def _user(row: asyncpg.Record) -> User:
return User(str(row['id']), row['username'], row['display_name'], _stamp(row['created_at']))
def _as_uuid(value: str, message: str) -> uuid.UUID:
try:
return uuid.UUID(value)
except ValueError:
raise invalid_request(message) from None
def _db_down(exc: BaseException) -> bool:
return isinstance(exc, (
asyncpg.PostgresConnectionError,
asyncpg.InterfaceError,
asyncpg.CannotConnectNowError,
OSError,
TimeoutError,
))
_FIRST_PAGE_LIMIT = 50
# Opens the unguarded fill long enough for concurrent misses to each run their own SELECT.
_NAIVE_FILL_HOLD_SECONDS = 0.2
class Store:
"""PostgreSQL is the source of truth. Redis caches session lookup and the first history page."""
def __init__(
self,
pool: asyncpg.Pool,
cache: SessionCache,
session_ttl_seconds: int = 3600,
history_mode: str = 'protected',
) -> None:
self._pool = pool
self._cache = cache
self._session_ttl = timedelta(seconds=session_ttl_seconds)
self._history_mode = history_mode
self._lock = threading.Lock()
self._last_moment: Optional[datetime] = None
self._flights: Dict[str, asyncio.Future] = {}
self._flight_lock = asyncio.Lock()
def _now(self) -> datetime:
"""Strictly increasing server time, so append order always equals sort order."""
with self._lock:
moment = datetime.now(timezone.utc)
if self._last_moment is not None and moment <= self._last_moment:
moment = self._last_moment + timedelta(microseconds=1)
self._last_moment = moment
return moment
async def ready(self) -> None:
try:
async with self._pool.acquire() as conn:
await conn.execute('SELECT 1')
except Exception:
raise unavailable('Хранилище недоступно') from None
async def create_user(self, username: str, display_name: str, password: str) -> User:
password_hash = hash_password(password)
user_id = uuid.uuid4()
created = self._now()
try:
async with self._pool.acquire() 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,
password_hash,
created,
)
except asyncpg.UniqueViolationError:
raise conflict('Username уже занят') from None
return User(str(user_id), username, display_name, _stamp(created))
async def authenticate(self, username: str, password: str) -> Optional[User]:
try:
async with self._pool.acquire() as conn:
row = await conn.fetchrow(
'''
SELECT id, username, display_name, created_at, password_hash
FROM users
WHERE username = $1
''',
username,
)
except Exception as exc:
if _db_down(exc):
raise unavailable('Хранилище недоступно') from None
raise
stored_hash = row['password_hash'] if row is not None else None
if not password_matches(stored_hash, password) or row is None:
return None
return _user(row)
async def create_session(self, user_id: str) -> Tuple[str, str]:
token = secrets.token_urlsafe(32)
expires = datetime.now(timezone.utc) + self._session_ttl
key = _token_key(token)
try:
async with self._pool.acquire() as conn:
await conn.execute(
'''
INSERT INTO sessions (token_hash, user_id, expires_at)
VALUES ($1, $2, $3)
''',
key,
_as_uuid(user_id, 'Некорректный user_id'),
expires,
)
except Exception as exc:
if _db_down(exc):
raise unavailable('Хранилище недоступно') from None
raise
await self._cache.remember(key, expires, datetime.now(timezone.utc), only_if_absent=False)
return token, _stamp(expires)
async def user_for_token(self, token: str) -> Optional[User]:
now = datetime.now(timezone.utc)
key = _token_key(token)
cached = await self._cache.get(key)
try:
async with self._pool.acquire() as conn:
row = await conn.fetchrow(
'''
SELECT u.id, u.username, u.display_name, u.created_at, s.expires_at
FROM sessions s
JOIN users u ON u.id = s.user_id
WHERE s.token_hash = $1
''',
key,
)
if row is None or row['expires_at'] <= now:
if row is not None:
await conn.execute('DELETE FROM sessions WHERE token_hash = $1', key)
await self._cache.drop(key)
return None
if cached is None:
await self._cache.remember(key, row['expires_at'], now, only_if_absent=True)
return _user(row)
except Exception as exc:
if _db_down(exc):
raise unavailable('Хранилище недоступно') from None
raise
async def revoke_session(self, token: str) -> bool:
now = datetime.now(timezone.utc)
key = _token_key(token)
try:
async with self._pool.acquire() as conn:
row = await conn.fetchrow(
'''
DELETE FROM sessions
WHERE token_hash = $1 AND expires_at > $2
RETURNING token_hash
''',
key,
now,
)
except Exception as exc:
if _db_down(exc):
raise unavailable('Хранилище недоступно') from None
raise
await self._cache.drop(key)
return row is not None
async def create_chat(self, title: str, member_ids: Sequence[str]) -> Chat:
ids = [_as_uuid(member_id, 'Некорректный user_id') for member_id in member_ids]
chat_id = uuid.uuid4()
created = self._now()
async with self._pool.acquire() as conn:
async with conn.transaction():
found = await conn.fetch(
'SELECT id FROM users WHERE id = ANY($1::uuid[])',
ids,
)
if len(found) != len(ids):
raise invalid_request('Все участники должны быть существующими пользователями')
await conn.execute(
'INSERT INTO chats (id, title, created_at) VALUES ($1, $2, $3)',
chat_id,
title,
created,
)
await conn.execute(
'''
INSERT INTO chat_members (chat_id, user_id, position)
SELECT $1, member_id, (ordinality - 1)::integer
FROM unnest($2::uuid[]) WITH ORDINALITY AS members(member_id, ordinality)
''',
chat_id,
ids,
)
return Chat(str(chat_id), title, tuple(member_ids), _stamp(created))
async def find_chat(self, chat_id: str, member_id: str) -> Optional[Chat]:
"""A chat of somebody else is indistinguishable from a missing one."""
chat_uuid = _as_uuid(chat_id, 'Некорректный chat_id')
member_uuid = _as_uuid(member_id, 'Некорректный user_id')
async with self._pool.acquire() as conn:
row = await conn.fetchrow(
'''
SELECT c.id, c.title, c.created_at
FROM chats c
JOIN chat_members mine ON mine.chat_id = c.id AND mine.user_id = $2
WHERE c.id = $1
''',
chat_uuid,
member_uuid,
)
if row is None:
return None
members = await self._members(conn, [chat_uuid])
return Chat(str(row['id']), row['title'], members.get(chat_uuid, ()), _stamp(row['created_at']))
async def list_chats(self, member_id: str, limit: int, after: Optional[Key]) -> Tuple[List[Chat], bool]:
member_uuid = _as_uuid(member_id, 'Некорректный user_id')
after_at, after_id = self._cursor(after)
async with self._pool.acquire() as conn:
if after_at is None:
rows = await conn.fetch(
'''
SELECT c.id, c.title, c.created_at
FROM chats c
JOIN chat_members mine ON mine.chat_id = c.id AND mine.user_id = $1
ORDER BY c.created_at, c.id
LIMIT $2
''',
member_uuid,
limit + 1,
)
else:
rows = await conn.fetch(
'''
SELECT c.id, c.title, c.created_at
FROM chats c
JOIN chat_members mine ON mine.chat_id = c.id AND mine.user_id = $1
WHERE (c.created_at, c.id) > ($2::timestamptz, $3::uuid)
ORDER BY c.created_at, c.id
LIMIT $4
''',
member_uuid,
after_at,
after_id,
limit + 1,
)
page = rows[:limit]
members = await self._members(conn, [row['id'] for row in page])
chats = [
Chat(str(row['id']), row['title'], members.get(row['id'], ()), _stamp(row['created_at']))
for row in page
]
return chats, len(rows) > limit
async def add_message(self, chat_id: str, sender_id: str, text: str) -> Message:
message_id = uuid.uuid4()
created = self._now()
chat_uuid = _as_uuid(chat_id, 'Некорректный chat_id')
async with self._pool.acquire() as conn:
async with conn.transaction():
version = await conn.fetchval(
'''
UPDATE chats
SET history_version = history_version + 1
WHERE id = $1
RETURNING history_version
''',
chat_uuid,
)
if version is None:
raise not_found('Чат не найден')
await conn.execute(
'''
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
VALUES ($1, $2, $3, $4, $5)
''',
message_id,
chat_uuid,
_as_uuid(sender_id, 'Некорректный user_id'),
text,
created,
)
if self._history_mode == 'protected':
await self._cache.publish_history_version(str(chat_uuid), int(version))
return Message(str(message_id), str(chat_uuid), sender_id, text, _stamp(created))
async def list_messages(self, chat_id: str, limit: int, after: Optional[Key]) -> Tuple[List[Message], bool]:
chat_uuid = _as_uuid(chat_id, 'Некорректный chat_id')
after_at, after_id = self._cursor(after)
if after_at is None and limit == _FIRST_PAGE_LIMIT and self._history_mode != 'off':
if self._history_mode == 'protected':
return await self._protected_first_page(chat_uuid, limit)
return await self._naive_first_page(chat_uuid, limit)
return await self._fetch_messages(chat_uuid, limit, after_at, after_id)
async def _protected_first_page(self, chat_uuid: uuid.UUID, limit: int) -> Tuple[List[Message], bool]:
chat_id = str(chat_uuid)
version = await self._cache.history_version(chat_id)
if version is None:
version = await self._load_history_version(chat_uuid)
await self._cache.publish_history_version(chat_id, version)
key = 'history:%s:%s' % (chat_id, version)
cached = _decode_page(await self._cache.get_history_page(key))
if cached is not None:
return cached
return await self._singleflight(key, lambda: self._fill_page(key, chat_uuid, limit))
async def _naive_first_page(self, chat_uuid: uuid.UUID, limit: int) -> Tuple[List[Message], bool]:
key = 'history:%s' % chat_uuid
cached = _decode_page(await self._cache.get_history_page(key))
if cached is not None:
return cached
messages, has_more = await self._fetch_messages(chat_uuid, limit, None, None)
await asyncio.sleep(_NAIVE_FILL_HOLD_SECONDS)
await self._cache.set_history_page(key, _encode_page(messages, has_more))
return messages, has_more
async def _fill_page(self, key: str, chat_uuid: uuid.UUID, limit: int) -> Tuple[List[Message], bool]:
cached = _decode_page(await self._cache.get_history_page(key))
if cached is not None:
return cached
messages, has_more = await self._fetch_messages(chat_uuid, limit, None, None)
await self._cache.set_history_page(key, _encode_page(messages, has_more))
return messages, has_more
async def _singleflight(self, key: str, loader):
async with self._flight_lock:
flight = self._flights.get(key)
if flight is None:
flight = asyncio.get_running_loop().create_future()
self._flights[key] = flight
leader = True
else:
leader = False
if not leader:
return await flight
try:
value = await loader()
except Exception as exc:
if not flight.done():
flight.set_exception(exc)
raise
else:
if not flight.done():
flight.set_result(value)
return value
finally:
async with self._flight_lock:
if self._flights.get(key) is flight:
del self._flights[key]
async def _load_history_version(self, chat_uuid: uuid.UUID) -> int:
async with self._pool.acquire() as conn:
version = await conn.fetchval(
'SELECT history_version FROM chats WHERE id = $1',
chat_uuid,
)
if version is None:
raise not_found('Чат не найден')
return int(version)
async def _fetch_messages(
self,
chat_uuid: uuid.UUID,
limit: int,
after_at: Optional[datetime],
after_id: Optional[uuid.UUID],
) -> Tuple[List[Message], bool]:
async with self._pool.acquire() as conn:
if after_at is None:
rows = await conn.fetch(
'''
SELECT id, chat_id, sender_id, text, created_at
FROM messages
WHERE chat_id = $1
ORDER BY created_at, id
LIMIT $2
''',
chat_uuid,
limit + 1,
)
else:
rows = await conn.fetch(
'''
SELECT id, chat_id, sender_id, text, created_at
FROM messages
WHERE chat_id = $1
AND (created_at, id) > ($2::timestamptz, $3::uuid)
ORDER BY created_at, id
LIMIT $4
''',
chat_uuid,
after_at,
after_id,
limit + 1,
)
page = rows[:limit]
messages = [
Message(str(row['id']), str(row['chat_id']), str(row['sender_id']), row['text'], _stamp(row['created_at']))
for row in page
]
return messages, len(rows) > limit
async def _members(self, conn: asyncpg.Connection, chat_ids: Sequence[uuid.UUID]) -> Dict[uuid.UUID, Tuple[str, ...]]:
if not chat_ids:
return {}
rows = await conn.fetch(
'''
SELECT chat_id, user_id
FROM chat_members
WHERE chat_id = ANY($1::uuid[])
ORDER BY chat_id, position
''',
list(chat_ids),
)
grouped: Dict[uuid.UUID, List[str]] = {}
for row in rows:
grouped.setdefault(row['chat_id'], []).append(str(row['user_id']))
return {chat_id: tuple(member_ids) for chat_id, member_ids in grouped.items()}
def _cursor(self, after: Optional[Key]) -> Tuple[Optional[datetime], Optional[uuid.UUID]]:
if after is None:
return None, None
try:
return _parse_stamp(after[0]), uuid.UUID(after[1])
except ValueError:
raise invalid_request('Некорректный cursor') from None