Перенести API на PostgreSQL, добавить сессии Basic/Bearer и кэш Valkey

This commit is contained in:
Pavel Grigorev
2026-10-29 14:14:26 +03:00
parent 65003b6119
commit 0e70320df3
15 changed files with 792 additions and 0 deletions
+12
View File
@@ -18,3 +18,15 @@ __pycache__
node_modules
evidence
.DS_Store
tests
student-tests
contracts
scripts
examples
*.md
Makefile
lab.json
requirements-dev.txt
Dockerfile*
.dockerignore
.gitignore
+25
View File
@@ -0,0 +1,25 @@
FROM python:3.12.15-slim-trixie@sha256:a6e34c598f2467ed0e9a8d349809fcd8b5c603269512df273a0bb1784edc11b1
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
PIP_NO_CACHE_DIR=1 \
PIP_DISABLE_PIP_VERSION_CHECK=1 \
PIP_ROOT_USER_ACTION=ignore
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY migrations/ ./migrations/
COPY src/ ./src/
RUN useradd --uid 10001 --user-group --no-create-home --shell /usr/sbin/nologin app
USER app
EXPOSE 8080
HEALTHCHECK --interval=10s --timeout=3s --start-period=5s --retries=3 \
CMD ["python", "-c", "import os, urllib.request; urllib.request.urlopen('http://127.0.0.1:%s/health/ready' % os.environ.get('APP_PORT', '8080'), timeout=2)"]
CMD ["python", "src/main.py"]
+46
View File
@@ -0,0 +1,46 @@
CREATE TABLE users (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
username text NOT NULL UNIQUE CHECK (username ~ '^[a-z0-9_]{3,32}$'),
display_name text NOT NULL CHECK (char_length(display_name) BETWEEN 1 AND 100),
password_hash text NOT NULL,
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
);
CREATE TABLE chats (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
title text NOT NULL CHECK (char_length(title) BETWEEN 1 AND 100),
created_by uuid NOT NULL REFERENCES users (id),
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
);
CREATE TABLE chat_members (
chat_id uuid NOT NULL REFERENCES chats (id),
user_id uuid NOT NULL REFERENCES users (id),
position smallint NOT NULL CHECK (position >= 1),
PRIMARY KEY (chat_id, user_id),
UNIQUE (chat_id, position)
);
CREATE INDEX chat_members_user_idx ON chat_members (user_id, chat_id);
CREATE TABLE messages (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
chat_id uuid NOT NULL,
sender_id uuid NOT NULL,
text text NOT NULL CHECK (char_length(text) BETWEEN 1 AND 4000),
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
FOREIGN KEY (chat_id, sender_id) REFERENCES chat_members (chat_id, user_id)
);
CREATE INDEX messages_chat_history_idx ON messages (chat_id, created_at, id);
CREATE TABLE sessions (
token_hash bytea PRIMARY KEY CHECK (octet_length(token_hash) = 32),
user_id uuid NOT NULL REFERENCES users (id),
created_at timestamptz NOT NULL DEFAULT now(),
expires_at timestamptz NOT NULL,
CHECK (expires_at > created_at)
);
CREATE INDEX sessions_user_idx ON sessions (user_id);
CREATE INDEX sessions_expires_idx ON sessions (expires_at);
+19
View File
@@ -0,0 +1,19 @@
annotated-types==0.8.0
anyio==4.15.1
argon2-cffi==25.1.0
argon2-cffi-bindings==26.1.0
cffi==2.1.1
click==8.5.0
fastapi==0.115.6
h11==0.16.0
idna==3.20
psycopg==3.3.6
psycopg-binary==3.3.6
psycopg-pool==3.3.3
pycparser==3.12
pydantic==2.10.4
pydantic_core==2.27.2
redis==8.1.0
starlette==0.41.3
typing_extensions==4.16.0
uvicorn==0.34.0
+155
View File
@@ -0,0 +1,155 @@
from contextlib import asynccontextmanager
import logging
import time
from typing import Annotated, Optional
import uuid
from fastapi import Depends, FastAPI, Header, Query, Request, Response
from fastapi.exceptions import RequestValidationError
import psycopg
from starlette.exceptions import HTTPException as StarletteHTTPException
import auth
from cache import cache
from db import pool
from errors import ApiError, error_response
from models import CreateChat, CreateMessage, CreateUser
import store
log = logging.getLogger('messenger')
Limit = Annotated[int, Query(ge=1, le=100)]
Cursor = Annotated[Optional[str], Query(min_length=1)]
state = {'ready': False}
@asynccontextmanager
async def lifespan(app):
pool.open(wait=False)
log.info('session cache: %s', 'valkey' if cache.client else 'disabled')
state['ready'] = True
log.info('application started')
yield
state['ready'] = False
log.info('application stopping')
pool.close(timeout=2)
app = FastAPI(title='MTUSI Messenger lab2', lifespan=lifespan)
@app.middleware('http')
async def request_context(request: Request, call_next):
request.state.request_id = uuid.uuid4().hex
started = time.perf_counter()
try:
response = await call_next(request)
except Exception:
log.exception('unhandled error request_id=%s', request.state.request_id)
response = error_response(request, 500, 'Внутренняя ошибка сервера')
response.headers['X-Request-Id'] = request.state.request_id
log.info('%s %s -> %d %.1fms request_id=%s', request.method, request.url.path,
response.status_code, (time.perf_counter() - started) * 1000, request.state.request_id)
return response
@app.exception_handler(ApiError)
async def handle_api_error(request: Request, exc: ApiError):
return error_response(request, exc.status, exc.message, exc.headers)
@app.exception_handler(StarletteHTTPException)
async def handle_http_error(request: Request, exc: StarletteHTTPException):
messages = {404: 'Ресурс не найден', 405: 'Метод не поддерживается'}
return error_response(request, exc.status_code, messages.get(exc.status_code, 'Ошибка запроса'))
@app.exception_handler(RequestValidationError)
async def handle_validation_error(request: Request, exc: RequestValidationError):
errors = exc.errors()
where = '.'.join(str(part) for part in errors[0]['loc']) if errors else 'request'
return error_response(request, 400, 'Некорректный запрос: ' + where)
@app.exception_handler(psycopg.DataError)
async def handle_data_error(request: Request, exc: psycopg.DataError):
return error_response(request, 400, 'Некорректные данные')
@app.exception_handler(psycopg.OperationalError)
@app.exception_handler(psycopg.InterfaceError)
async def handle_database_unavailable(request: Request, exc: psycopg.Error):
log.warning('database unavailable: %s request_id=%s', type(exc).__name__, request.state.request_id)
return error_response(request, 503, 'Хранилище временно недоступно')
@app.get('/health/live')
async def liveness():
return {'status': 'ok'}
@app.get('/health/ready')
def readiness():
if not state['ready']:
raise ApiError(503, 'Сервис не готов')
with pool.connection(timeout=1) as conn:
conn.execute('SELECT 1')
return {'status': 'ok'}
def current_session(authorization: Annotated[Optional[str], Header()] = None):
return auth.authenticate(authorization)
def current_user(session: Annotated[dict, Depends(current_session)]):
return session['user']
@app.post('/api/v1/users', status_code=201)
def register_user(body: CreateUser):
return store.create_user(body.username, body.display_name, auth.hash_password(body.password))
@app.post('/api/v1/auth/sessions', status_code=201)
def create_session(request: Request, authorization: Annotated[Optional[str], Header()] = None):
if request.headers.get('content-length', '0') != '0' or 'transfer-encoding' in request.headers:
raise ApiError(400, 'Вход принимает только заголовок Authorization: Basic, без тела')
return auth.login(authorization)
@app.delete('/api/v1/auth/sessions/current', status_code=204)
def delete_session(session: Annotated[dict, Depends(current_session)]):
auth.logout(session)
return Response(status_code=204)
@app.get('/api/v1/users/me')
def get_me(user: Annotated[dict, Depends(current_user)]):
return user
@app.post('/api/v1/chats', status_code=201)
def create_chat(body: CreateChat, user: Annotated[dict, Depends(current_user)]):
return store.create_chat(user['id'], body.title, body.member_ids)
@app.get('/api/v1/chats')
def list_chats(user: Annotated[dict, Depends(current_user)], limit: Limit = 50, cursor: Cursor = None):
return store.list_chats(user['id'], cursor, limit)
@app.get('/api/v1/chats/{chat_id}')
def get_chat(chat_id: uuid.UUID, user: Annotated[dict, Depends(current_user)]):
return store.get_chat(str(chat_id), user['id'])
@app.post('/api/v1/chats/{chat_id}/messages', status_code=201)
def create_message(chat_id: uuid.UUID, body: CreateMessage, user: Annotated[dict, Depends(current_user)]):
return store.create_message(str(chat_id), user['id'], body.text)
@app.get('/api/v1/chats/{chat_id}/messages')
def list_messages(chat_id: uuid.UUID, user: Annotated[dict, Depends(current_user)],
limit: Limit = 50, cursor: Cursor = None):
return store.list_messages(str(chat_id), user['id'], cursor, limit)
+92
View File
@@ -0,0 +1,92 @@
import base64
import binascii
import hashlib
import re
import secrets
from argon2 import PasswordHasher
from argon2.exceptions import InvalidHashError, VerificationError
from cache import cache
from config import settings
from db import iso
from errors import BASIC_CHALLENGE, BEARER_CHALLENGE, unauthorized
import store
USERNAME = re.compile(r'^[a-z0-9_]{3,32}$')
hasher = PasswordHasher(time_cost=2, memory_cost=19456, parallelism=1)
DUMMY_HASH = hasher.hash('dummy-password-for-equal-timing')
def hash_password(password):
return hasher.hash(password)
def verify_password(password_hash, password):
try:
return hasher.verify(password_hash, password)
except (VerificationError, InvalidHashError):
return False
def token_hash(token):
return hashlib.sha256(token.encode('ascii')).digest()
def parse_credentials(header, scheme):
name, _, value = (header or '').strip().partition(' ')
value = value.strip()
if name.lower() != scheme or not value or ' ' in value:
return None
return value
def parse_basic(header):
value = parse_credentials(header, 'basic')
if value is None:
return None
try:
decoded = base64.b64decode(value, validate=True).decode('utf-8')
except (binascii.Error, UnicodeDecodeError):
return None
username, separator, password = decoded.partition(':')
if not separator or not USERNAME.match(username) or not 12 <= len(password) <= 128:
return None
return username, password
def login(header):
credentials = parse_basic(header)
row = store.find_credentials(credentials[0]) if credentials else None
if row is None:
verify_password(DUMMY_HASH, credentials[1] if credentials else 'x' * 12)
raise unauthorized(BASIC_CHALLENGE, 'Неверное имя пользователя или пароль')
if not verify_password(row['password_hash'], credentials[1]):
raise unauthorized(BASIC_CHALLENGE, 'Неверное имя пользователя или пароль')
token = secrets.token_urlsafe(32)
digest = token_hash(token)
expires_at = store.create_session(row['id'], digest, settings.session_ttl)
user = store.user_out(row)
cache.put(digest, user, expires_at)
return {'token': token, 'token_type': 'Bearer', 'expires_at': iso(expires_at), 'user': user}
def authenticate(header):
token = parse_credentials(header, 'bearer')
if token is None or not token.isascii():
raise unauthorized(BEARER_CHALLENGE)
digest = token_hash(token)
user = cache.get(digest)
if user is None:
row = store.session_user(digest)
if row is None:
raise unauthorized(BEARER_CHALLENGE)
user = store.user_out(row)
cache.put(digest, user, row['expires_at'])
return {'user': user, 'token_hash': digest}
def logout(session):
store.delete_session(session['token_hash'])
cache.delete(session['token_hash'])
+60
View File
@@ -0,0 +1,60 @@
from datetime import datetime, timezone
import json
import logging
import time
import redis
from config import settings
log = logging.getLogger('messenger.cache')
BYPASS_SECONDS = 5
class SessionCache:
def __init__(self, url, max_ttl):
self.client = redis.Redis.from_url(url, socket_timeout=0.2, socket_connect_timeout=0.2,
decode_responses=True) if url else None
self.max_ttl = max_ttl
self.bypass_until = 0.0
def _run(self, action):
if self.client is None or time.monotonic() < self.bypass_until:
return None
try:
return action(self.client)
except redis.RedisError as error:
log.warning('session cache unavailable (%s), using PostgreSQL for %ds', type(error).__name__, BYPASS_SECONDS)
self.bypass_until = time.monotonic() + BYPASS_SECONDS
return None
@staticmethod
def key(token_hash):
return 'session:' + token_hash.hex()
def get(self, token_hash):
raw = self._run(lambda c: c.get(self.key(token_hash)))
if not raw:
return None
entry = json.loads(raw)
if entry['expires_at'] <= time.time():
return None
return entry['user']
def put(self, token_hash, user, expires_at: datetime):
remaining = int((expires_at - datetime.now(timezone.utc)).total_seconds())
ttl = min(self.max_ttl, remaining)
if ttl <= 0:
return
value = json.dumps({'user': user, 'expires_at': expires_at.timestamp()}, ensure_ascii=False)
self._run(lambda c: c.set(self.key(token_hash), value, ex=ttl))
def delete(self, token_hash):
self._run(lambda c: c.delete(self.key(token_hash)))
def ping(self):
return bool(self._run(lambda c: c.ping()))
cache = SessionCache(settings.cache_url, settings.session_cache_ttl)
+37
View File
@@ -0,0 +1,37 @@
import os
def setting(name, default=None):
path = os.environ.get(name + '_FILE')
if path:
with open(path, encoding='utf-8') as file:
return file.read().strip()
return os.environ.get(name, default)
def positive_int(name, default):
value = int(os.environ.get(name, default))
if value <= 0:
raise SystemExit('%s must be a positive integer' % name)
return value
class Settings:
def __init__(self):
self.host = os.environ.get('APP_HOST', '0.0.0.0')
self.port = positive_int('APP_PORT', '8080')
self.env = os.environ.get('APP_ENV', 'lab')
self.shutdown_timeout = positive_int('APP_SHUTDOWN_TIMEOUT', '5')
self.database_url = setting('DATABASE_URL')
self.cache_url = setting('CACHE_URL') or None
self.session_ttl = positive_int('SESSION_TTL_SECONDS', '3600')
self.session_cache_ttl = positive_int('SESSION_CACHE_TTL_SECONDS', '60')
self.db_pool_min = positive_int('DB_POOL_MIN', '1')
self.db_pool_max = positive_int('DB_POOL_MAX', '10')
self.db_pool_timeout = float(os.environ.get('DB_POOL_TIMEOUT_SECONDS', '2'))
self.db_statement_timeout_ms = positive_int('DB_STATEMENT_TIMEOUT_MS', '5000')
if not self.database_url:
raise SystemExit('DATABASE_URL or DATABASE_URL_FILE is required')
settings = Settings()
+25
View File
@@ -0,0 +1,25 @@
from datetime import timezone
from psycopg.rows import dict_row
from psycopg_pool import ConnectionPool
from config import settings
pool = ConnectionPool(
settings.database_url,
min_size=settings.db_pool_min,
max_size=settings.db_pool_max,
timeout=settings.db_pool_timeout,
check=ConnectionPool.check_connection,
open=False,
name='messenger',
kwargs={
'connect_timeout': 3,
'row_factory': dict_row,
'options': '-c timezone=UTC -c statement_timeout=%d' % settings.db_statement_timeout_ms,
},
)
def iso(value):
return value.astimezone(timezone.utc).strftime('%Y-%m-%dT%H:%M:%S.%fZ')
+37
View File
@@ -0,0 +1,37 @@
from fastapi import Request
from fastapi.responses import JSONResponse
CODES = {
400: 'invalid_request',
401: 'unauthorized',
404: 'not_found',
409: 'conflict',
413: 'payload_too_large',
415: 'unsupported_media_type',
429: 'rate_limited',
500: 'internal_error',
503: 'unavailable',
}
BASIC_CHALLENGE = 'Basic realm="messenger", charset="UTF-8"'
BEARER_CHALLENGE = 'Bearer realm="messenger"'
class ApiError(Exception):
def __init__(self, status, message, headers=None):
super().__init__(message)
self.status = status
self.message = message
self.headers = headers
def unauthorized(challenge, message='Нужна действительная сессия'):
return ApiError(401, message, {'WWW-Authenticate': challenge})
def error_response(request: Request, status, message, headers=None):
request_id = getattr(request.state, 'request_id', '-')
body = {'error': {'code': CODES.get(status, 'invalid_request'), 'message': message, 'request_id': request_id}}
if status == 401 and not headers:
headers = {'WWW-Authenticate': BEARER_CHALLENGE}
return JSONResponse(body, status_code=status, headers=headers)
+22
View File
@@ -0,0 +1,22 @@
import logging
import sys
import uvicorn
from config import settings
def main():
logging.basicConfig(stream=sys.stdout, level=logging.INFO,
format='%(asctime)s %(levelname)s %(name)s: %(message)s')
logging.getLogger('messenger').info('config host=%s port=%d env=%s session_ttl=%ds session_cache_ttl=%ds',
settings.host, settings.port, settings.env,
settings.session_ttl, settings.session_cache_ttl)
from app import app
uvicorn.run(app, host=settings.host, port=settings.port, access_log=False, log_config=None,
timeout_graceful_shutdown=settings.shutdown_timeout)
if __name__ == '__main__':
main()
+47
View File
@@ -0,0 +1,47 @@
import logging
from pathlib import Path
import sys
import time
import psycopg
from config import settings
MIGRATIONS = Path(__file__).resolve().parent.parent / 'migrations'
LOCK_ID = 20260002
log = logging.getLogger('migrate')
def connect(attempts=30):
for attempt in range(1, attempts + 1):
try:
return psycopg.connect(settings.database_url, autocommit=True, connect_timeout=3)
except psycopg.OperationalError:
if attempt == attempts:
raise
log.info('database is not ready, retry %d/%d', attempt, attempts)
time.sleep(1)
def main():
logging.basicConfig(stream=sys.stdout, level=logging.INFO, format='%(asctime)s %(name)s: %(message)s')
with connect() as conn:
conn.execute('SELECT pg_advisory_lock(%s)', (LOCK_ID,))
conn.execute('CREATE TABLE IF NOT EXISTS schema_migrations ('
'version text PRIMARY KEY, applied_at timestamptz NOT NULL DEFAULT now())')
applied = {row[0] for row in conn.execute('SELECT version FROM schema_migrations')}
for path in sorted(MIGRATIONS.glob('*.sql')):
if path.stem in applied:
log.info('skip %s: already applied', path.stem)
continue
with conn.transaction():
conn.execute(path.read_text(encoding='utf-8'))
conn.execute('INSERT INTO schema_migrations (version) VALUES (%s)', (path.stem,))
log.info('applied %s', path.stem)
conn.execute('SELECT pg_advisory_unlock(%s)', (LOCK_ID,))
log.info('schema is up to date')
if __name__ == '__main__':
main()
+41
View File
@@ -0,0 +1,41 @@
from pydantic import BaseModel, ConfigDict, Field, field_validator
class StrictModel(BaseModel):
model_config = ConfigDict(extra='forbid', strict=True)
def not_blank(value):
if not value.strip():
raise ValueError('must not be blank')
return value
class CreateUser(StrictModel):
username: str = Field(pattern=r'^[a-z0-9_]{3,32}$')
display_name: str = Field(min_length=1, max_length=100)
password: str = Field(min_length=12, max_length=128)
@field_validator('display_name', 'password')
@classmethod
def not_blank_fields(cls, value):
return not_blank(value)
class CreateChat(StrictModel):
title: str = Field(min_length=1, max_length=100)
member_ids: list[str] = Field(min_length=1, max_length=100)
@field_validator('title')
@classmethod
def title_not_blank(cls, value):
return not_blank(value)
class CreateMessage(StrictModel):
text: str = Field(min_length=1, max_length=4000)
@field_validator('text')
@classmethod
def text_not_blank(cls, value):
return not_blank(value)
+31
View File
@@ -0,0 +1,31 @@
import base64
from datetime import datetime
import json
import uuid
from errors import ApiError
def encode_cursor(scope, item):
raw = json.dumps({'s': scope, 't': item['created_at'], 'i': item['id']}, separators=(',', ':'))
return base64.urlsafe_b64encode(raw.encode()).decode().rstrip('=')
def decode_cursor(scope, cursor):
try:
padded = cursor + '=' * (-len(cursor) % 4)
data = json.loads(base64.urlsafe_b64decode(padded.encode('ascii')))
if data['s'] != scope or not isinstance(data['t'], str) or not isinstance(data['i'], str):
raise ValueError
created_at = datetime.fromisoformat(data['t'])
if created_at.tzinfo is None:
raise ValueError
return created_at, uuid.UUID(data['i'])
except (ValueError, TypeError, KeyError, AttributeError):
raise ApiError(400, 'Некорректный cursor') from None
def page(items, scope, limit):
chunk = items[:limit]
has_more = len(items) > limit
return {'items': chunk, 'next_cursor': encode_cursor(scope, chunk[-1]) if has_more else None}
+143
View File
@@ -0,0 +1,143 @@
import uuid
from psycopg import errors
from db import iso, pool
from errors import ApiError
from pagination import decode_cursor, page
USER_COLUMNS = 'u.id, u.username, u.display_name, u.created_at'
MESSAGE_COLUMNS = 'id, chat_id, sender_id, text, created_at'
CHAT_SELECT = ('SELECT c.id, c.title, c.created_at, '
'ARRAY(SELECT m.user_id FROM chat_members m WHERE m.chat_id = c.id ORDER BY m.position) AS member_ids '
'FROM chats c ')
def user_out(row):
return {'id': str(row['id']), 'username': row['username'],
'display_name': row['display_name'], 'created_at': iso(row['created_at'])}
def chat_out(row):
return {'id': str(row['id']), 'title': row['title'],
'member_ids': [str(m) for m in row['member_ids']], 'created_at': iso(row['created_at'])}
def message_out(row):
return {'id': str(row['id']), 'chat_id': str(row['chat_id']), 'sender_id': str(row['sender_id']),
'text': row['text'], 'created_at': iso(row['created_at'])}
def create_user(username, display_name, password_hash):
try:
with pool.connection() as conn:
row = conn.execute(
'INSERT INTO users AS u (username, display_name, password_hash) VALUES (%s, %s, %s) '
'RETURNING ' + USER_COLUMNS,
(username, display_name, password_hash)).fetchone()
except errors.UniqueViolation:
raise ApiError(409, 'Имя пользователя уже занято') from None
return user_out(row)
def find_credentials(username):
with pool.connection() as conn:
return conn.execute('SELECT ' + USER_COLUMNS + ', u.password_hash FROM users u WHERE u.username = %s',
(username,)).fetchone()
def create_session(user_id, token_hash, ttl_seconds):
with pool.connection() as conn:
conn.execute('DELETE FROM sessions WHERE user_id = %s AND expires_at <= now()', (user_id,))
return conn.execute(
'INSERT INTO sessions (token_hash, user_id, expires_at) '
'VALUES (%s, %s, now() + make_interval(secs => %s)) RETURNING expires_at',
(token_hash, user_id, ttl_seconds)).fetchone()['expires_at']
def session_user(token_hash):
with pool.connection() as conn:
return conn.execute(
'SELECT ' + USER_COLUMNS + ', s.expires_at FROM sessions s JOIN users u ON u.id = s.user_id '
'WHERE s.token_hash = %s AND s.expires_at > now()',
(token_hash,)).fetchone()
def delete_session(token_hash):
with pool.connection() as conn:
conn.execute('DELETE FROM sessions WHERE token_hash = %s', (token_hash,))
def create_chat(creator_id, title, member_ids):
try:
members = [str(uuid.UUID(m)) for m in member_ids]
except ValueError:
raise ApiError(400, 'Неизвестный участник чата') from None
if len(set(members)) != len(members):
raise ApiError(400, 'Участники не должны повторяться')
if creator_id not in members:
raise ApiError(400, 'Создатель должен быть участником чата')
try:
with pool.connection() as conn, conn.transaction():
chat = conn.execute('INSERT INTO chats (title, created_by) VALUES (%s, %s) RETURNING id, title, created_at',
(title, creator_id)).fetchone()
conn.execute('INSERT INTO chat_members (chat_id, user_id, position) '
'SELECT %s, member, position FROM unnest(%s::uuid[]) WITH ORDINALITY AS t(member, position)',
(chat['id'], members))
except errors.ForeignKeyViolation:
raise ApiError(400, 'Неизвестный участник чата') from None
return chat_out({**chat, 'member_ids': members})
def get_chat(chat_id, user_id):
with pool.connection() as conn:
row = conn.execute(CHAT_SELECT + 'JOIN chat_members me ON me.chat_id = c.id AND me.user_id = %s '
'WHERE c.id = %s', (user_id, chat_id)).fetchone()
if row is None:
raise ApiError(404, 'Чат не найден')
return chat_out(row)
def list_chats(user_id, cursor, limit):
scope = 'user:' + user_id
sql = CHAT_SELECT + 'JOIN chat_members me ON me.chat_id = c.id WHERE me.user_id = %s '
params = [user_id]
if cursor:
sql += 'AND (c.created_at, c.id) > (%s, %s) '
params += decode_cursor(scope, cursor)
sql += 'ORDER BY c.created_at, c.id LIMIT %s'
with pool.connection() as conn:
rows = conn.execute(sql, params + [limit + 1]).fetchall()
return page([chat_out(r) for r in rows], scope, limit)
def is_member(conn, chat_id, user_id):
return conn.execute('SELECT 1 FROM chat_members WHERE chat_id = %s AND user_id = %s',
(chat_id, user_id)).fetchone() is not None
def create_message(chat_id, sender_id, text):
with pool.connection() as conn:
row = conn.execute(
'INSERT INTO messages (chat_id, sender_id, text) SELECT %s, %s, %s '
'WHERE EXISTS (SELECT 1 FROM chat_members WHERE chat_id = %s AND user_id = %s) '
'RETURNING ' + MESSAGE_COLUMNS,
(chat_id, sender_id, text, chat_id, sender_id)).fetchone()
if row is None:
raise ApiError(404, 'Чат не найден')
return message_out(row)
def list_messages(chat_id, user_id, cursor, limit):
scope = 'chat:' + chat_id
sql = 'SELECT ' + MESSAGE_COLUMNS + ' FROM messages WHERE chat_id = %s '
params = [chat_id]
if cursor:
sql += 'AND (created_at, id) > (%s, %s) '
params += decode_cursor(scope, cursor)
sql += 'ORDER BY created_at, id LIMIT %s'
with pool.connection() as conn:
if not is_member(conn, chat_id, user_id):
raise ApiError(404, 'Чат не найден')
rows = conn.execute(sql, params + [limit + 1]).fetchall()
return page([message_out(r) for r in rows], scope, limit)