Добавить X-Instance-Id, обязательный Idempotency-Key и мягкую остановку API
This commit is contained in:
@@ -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
@@ -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"]
|
||||
@@ -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);
|
||||
@@ -0,0 +1,14 @@
|
||||
ALTER TABLE users
|
||||
DROP CONSTRAINT users_display_name_check,
|
||||
ALTER COLUMN display_name TYPE bytea USING convert_to(display_name, 'UTF8'),
|
||||
ADD CONSTRAINT users_display_name_check CHECK (octet_length(display_name) BETWEEN 1 AND 400);
|
||||
|
||||
ALTER TABLE chats
|
||||
DROP CONSTRAINT chats_title_check,
|
||||
ALTER COLUMN title TYPE bytea USING convert_to(title, 'UTF8'),
|
||||
ADD CONSTRAINT chats_title_check CHECK (octet_length(title) BETWEEN 1 AND 400);
|
||||
|
||||
ALTER TABLE messages
|
||||
DROP CONSTRAINT messages_text_check,
|
||||
ALTER COLUMN text TYPE bytea USING convert_to(text, 'UTF8'),
|
||||
ADD CONSTRAINT messages_text_check CHECK (octet_length(text) BETWEEN 1 AND 16000);
|
||||
@@ -0,0 +1,6 @@
|
||||
CREATE TABLE session_revocations (
|
||||
token_hash bytea PRIMARY KEY CHECK (octet_length(token_hash) = 32),
|
||||
expires_at timestamptz NOT NULL
|
||||
);
|
||||
|
||||
CREATE INDEX session_revocations_expires_idx ON session_revocations (expires_at);
|
||||
@@ -0,0 +1,11 @@
|
||||
CREATE TABLE idempotency_keys (
|
||||
user_id uuid NOT NULL REFERENCES users (id),
|
||||
scope text NOT NULL,
|
||||
key text NOT NULL CHECK (key ~ '^[A-Za-z0-9._:-]{1,128}$'),
|
||||
request_hash bytea NOT NULL CHECK (octet_length(request_hash) = 32),
|
||||
message_id uuid REFERENCES messages (id),
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
PRIMARY KEY (user_id, scope, key)
|
||||
);
|
||||
|
||||
CREATE INDEX idempotency_keys_created_idx ON idempotency_keys (created_at);
|
||||
@@ -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
|
||||
Executable
+7
@@ -0,0 +1,7 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
docker compose stop api
|
||||
docker compose restart postgres valkey
|
||||
docker compose up -d --no-build --no-recreate --wait postgres valkey
|
||||
docker compose start api
|
||||
Executable
+14
@@ -0,0 +1,14 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
|
||||
cd "$(dirname "$0")/.."
|
||||
for id in $(docker compose ps -q api); do
|
||||
case "$id" in
|
||||
"$TARGET_INSTANCE_ID"*)
|
||||
docker stop -t 10 "$id" >/dev/null
|
||||
exit 0
|
||||
;;
|
||||
esac
|
||||
done
|
||||
echo "no running api replica matches TARGET_INSTANCE_ID" >&2
|
||||
exit 1
|
||||
+175
@@ -0,0 +1,175 @@
|
||||
from contextlib import asynccontextmanager
|
||||
import hashlib
|
||||
import json
|
||||
import logging
|
||||
import re
|
||||
import socket
|
||||
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.concurrency import run_in_threadpool
|
||||
from starlette.exceptions import HTTPException as StarletteHTTPException
|
||||
|
||||
import auth
|
||||
from cache import cache
|
||||
from config import settings
|
||||
from db import connection, pool, request_deadline
|
||||
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)]
|
||||
|
||||
INSTANCE_ID = socket.gethostname()
|
||||
IDEMPOTENCY_KEY = re.compile(r'^[A-Za-z0-9._:-]{1,128}$')
|
||||
|
||||
PUBLIC = {('POST', '/api/v1/users'), ('POST', '/api/v1/auth/sessions')}
|
||||
|
||||
state = {'ready': False}
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app):
|
||||
pool.open(wait=False)
|
||||
log.info('instance %s, session cache: %s', INSTANCE_ID, '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
|
||||
request_deadline.set({'deadline': None})
|
||||
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
|
||||
response.headers['X-Instance-Id'] = INSTANCE_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):
|
||||
if request.url.path.startswith('/api/v1/') and (request.method, request.url.path) not in PUBLIC:
|
||||
try:
|
||||
await run_in_threadpool(auth.authenticate, request.headers.get('authorization'))
|
||||
except ApiError as error:
|
||||
return error_response(request, error.status, error.message, error.headers)
|
||||
except (psycopg.OperationalError, psycopg.InterfaceError):
|
||||
return error_response(request, 503, 'Хранилище временно недоступно')
|
||||
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.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 connection(limit=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)],
|
||||
idempotency_key: Annotated[Optional[str], Header()] = None):
|
||||
if idempotency_key is None or not IDEMPOTENCY_KEY.match(idempotency_key):
|
||||
raise ApiError(400, 'Нужен заголовок Idempotency-Key: [A-Za-z0-9._:-]{1,128}')
|
||||
canonical = json.dumps({'text': body.text, 'attachment_ids': []}, ensure_ascii=False, sort_keys=True)
|
||||
return store.create_message(str(chat_id), user['id'], body.text, idempotency_key,
|
||||
hashlib.sha256(canonical.encode('utf-8')).digest(), settings.idempotency_ttl)
|
||||
|
||||
|
||||
@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)
|
||||
+120
@@ -0,0 +1,120 @@
|
||||
import base64
|
||||
import binascii
|
||||
import hashlib
|
||||
import re
|
||||
import secrets
|
||||
import threading
|
||||
|
||||
from argon2 import PasswordHasher
|
||||
from argon2.exceptions import InvalidHashError, VerificationError
|
||||
import psycopg
|
||||
|
||||
from cache import REVOKED, cache
|
||||
from config import settings
|
||||
from db import iso
|
||||
from errors import BASIC_CHALLENGE, BEARER_CHALLENGE, unauthorized
|
||||
import store
|
||||
|
||||
REVOCATION_MARGIN_SECONDS = 5
|
||||
|
||||
USERNAME = re.compile(r'^[a-z0-9_]{3,32}$')
|
||||
|
||||
hasher = PasswordHasher(time_cost=2, memory_cost=19456, parallelism=1)
|
||||
hashing = threading.BoundedSemaphore(settings.password_hash_concurrency)
|
||||
DUMMY_HASH = hasher.hash('dummy-password-for-equal-timing')
|
||||
|
||||
|
||||
def hash_password(password):
|
||||
with hashing:
|
||||
return hasher.hash(password)
|
||||
|
||||
|
||||
def verify_password(password_hash, password):
|
||||
try:
|
||||
with hashing:
|
||||
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)
|
||||
if cache.client is not None and not cache.trusted():
|
||||
sync_revocations()
|
||||
user = cache.get(digest)
|
||||
if user == REVOKED:
|
||||
raise unauthorized(BEARER_CHALLENGE)
|
||||
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 sync_revocations():
|
||||
errors_before = cache.errors
|
||||
pending = store.pending_revocations()
|
||||
if not cache.revoke_all(pending):
|
||||
return
|
||||
if pending:
|
||||
try:
|
||||
store.clear_revocations(pending)
|
||||
except psycopg.Error:
|
||||
pass
|
||||
cache.mark_synced(errors_before)
|
||||
|
||||
|
||||
def logout(session):
|
||||
store.revoke_session(session['token_hash'], cache.max_ttl + REVOCATION_MARGIN_SECONDS)
|
||||
if cache.revoke(session['token_hash']):
|
||||
try:
|
||||
store.clear_revocations([session['token_hash']])
|
||||
except psycopg.Error:
|
||||
pass
|
||||
@@ -0,0 +1,89 @@
|
||||
from datetime import datetime, timezone
|
||||
import json
|
||||
import logging
|
||||
import threading
|
||||
import time
|
||||
|
||||
import redis
|
||||
|
||||
from config import settings
|
||||
|
||||
log = logging.getLogger('messenger.cache')
|
||||
|
||||
BYPASS_SECONDS = 5
|
||||
REVOKED = 'revoked'
|
||||
FAILED = object()
|
||||
|
||||
|
||||
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
|
||||
self.lock = threading.Lock()
|
||||
self.errors = 0
|
||||
self.synced = False
|
||||
self.synced_at = 0.0
|
||||
|
||||
def _run(self, action, force=False):
|
||||
if self.client is None:
|
||||
return None
|
||||
if not force and time.monotonic() < self.bypass_until:
|
||||
return FAILED
|
||||
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
|
||||
with self.lock:
|
||||
self.errors += 1
|
||||
self.synced = False
|
||||
return FAILED
|
||||
|
||||
@staticmethod
|
||||
def key(token_hash):
|
||||
return 'session:' + token_hash.hex()
|
||||
|
||||
def mark_synced(self, errors_before):
|
||||
with self.lock:
|
||||
if self.errors == errors_before:
|
||||
self.synced = True
|
||||
self.synced_at = time.monotonic()
|
||||
|
||||
def trusted(self):
|
||||
return self.synced and time.monotonic() - self.synced_at < settings.revocation_sync_seconds
|
||||
|
||||
def get(self, token_hash):
|
||||
if not self.trusted():
|
||||
return None
|
||||
raw = self._run(lambda c: c.get(self.key(token_hash)))
|
||||
if raw is FAILED or not raw or raw == REVOKED:
|
||||
return REVOKED if raw == REVOKED else 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, nx=True))
|
||||
|
||||
def revoke(self, token_hash):
|
||||
return self._run(lambda c: c.set(self.key(token_hash), REVOKED, ex=self.max_ttl), force=True) is not FAILED
|
||||
|
||||
def revoke_all(self, token_hashes):
|
||||
def write(client):
|
||||
with client.pipeline(transaction=False) as pipe:
|
||||
for token_hash in token_hashes:
|
||||
pipe.set(self.key(token_hash), REVOKED, ex=self.max_ttl)
|
||||
pipe.execute()
|
||||
return True
|
||||
return not token_hashes or self._run(write) is not FAILED
|
||||
|
||||
|
||||
cache = SessionCache(settings.cache_url, settings.session_cache_ttl)
|
||||
@@ -0,0 +1,42 @@
|
||||
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', '3000')
|
||||
self.db_request_timeout = float(os.environ.get('DB_REQUEST_TIMEOUT_SECONDS', '4'))
|
||||
self.drain_seconds = float(os.environ.get('APP_DRAIN_SECONDS', '3'))
|
||||
self.idempotency_ttl = positive_int('IDEMPOTENCY_TTL_SECONDS', '86400')
|
||||
self.revocation_sync_seconds = float(os.environ.get('REVOCATION_SYNC_SECONDS', '1'))
|
||||
self.password_hash_concurrency = positive_int('PASSWORD_HASH_CONCURRENCY', '4')
|
||||
if not self.database_url:
|
||||
raise SystemExit('DATABASE_URL or DATABASE_URL_FILE is required')
|
||||
|
||||
|
||||
settings = Settings()
|
||||
@@ -0,0 +1,127 @@
|
||||
from contextlib import contextmanager
|
||||
import contextvars
|
||||
from datetime import timezone
|
||||
import itertools
|
||||
import logging
|
||||
import os
|
||||
import socket
|
||||
import threading
|
||||
import time
|
||||
|
||||
import psycopg
|
||||
from psycopg.rows import dict_row
|
||||
from psycopg_pool import ConnectionPool
|
||||
|
||||
from config import settings
|
||||
|
||||
log = logging.getLogger('messenger.db')
|
||||
|
||||
request_deadline = contextvars.ContextVar('request_deadline', default=None)
|
||||
|
||||
CHECK_SECONDS = 1
|
||||
|
||||
|
||||
def abort(conn):
|
||||
try:
|
||||
sock = socket.socket(fileno=os.dup(conn.fileno()))
|
||||
except (OSError, psycopg.Error):
|
||||
return
|
||||
try:
|
||||
sock.shutdown(socket.SHUT_RDWR)
|
||||
except OSError:
|
||||
pass
|
||||
finally:
|
||||
sock.close()
|
||||
|
||||
|
||||
class Watchdog:
|
||||
def __init__(self):
|
||||
self.cond = threading.Condition()
|
||||
self.armed = {}
|
||||
self.ids = itertools.count()
|
||||
threading.Thread(target=self.loop, name='db-watchdog', daemon=True).start()
|
||||
|
||||
@contextmanager
|
||||
def guard(self, conn, seconds):
|
||||
key = next(self.ids)
|
||||
with self.cond:
|
||||
self.armed[key] = (time.monotonic() + seconds, conn)
|
||||
self.cond.notify()
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
with self.cond:
|
||||
self.armed.pop(key, None)
|
||||
|
||||
def loop(self):
|
||||
with self.cond:
|
||||
while True:
|
||||
now = time.monotonic()
|
||||
for key, (deadline, conn) in list(self.armed.items()):
|
||||
if deadline <= now:
|
||||
del self.armed[key]
|
||||
log.warning('database did not answer in time, aborting connection')
|
||||
abort(conn)
|
||||
wake = min((deadline for deadline, _ in self.armed.values()), default=now + 60)
|
||||
self.cond.wait(max(wake - now, 0.01))
|
||||
|
||||
|
||||
watchdog = Watchdog()
|
||||
|
||||
|
||||
def check(conn):
|
||||
with watchdog.guard(conn, CHECK_SECONDS):
|
||||
ConnectionPool.check_connection(conn)
|
||||
|
||||
|
||||
pool = ConnectionPool(
|
||||
settings.database_url,
|
||||
min_size=settings.db_pool_min,
|
||||
max_size=settings.db_pool_max,
|
||||
timeout=settings.db_pool_timeout,
|
||||
check=check,
|
||||
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 remaining(limit=None):
|
||||
budget = request_deadline.get()
|
||||
if budget is None:
|
||||
left = settings.db_request_timeout
|
||||
else:
|
||||
if budget['deadline'] is None:
|
||||
budget['deadline'] = time.monotonic() + settings.db_request_timeout
|
||||
left = budget['deadline'] - time.monotonic()
|
||||
if limit is not None:
|
||||
left = min(left, limit)
|
||||
if left <= 0:
|
||||
raise psycopg.OperationalError('request deadline exceeded')
|
||||
return left
|
||||
|
||||
|
||||
@contextmanager
|
||||
def connection(limit=None):
|
||||
conn = pool.getconn(timeout=min(settings.db_pool_timeout, remaining(limit)))
|
||||
try:
|
||||
with watchdog.guard(conn, remaining(limit)):
|
||||
try:
|
||||
yield conn
|
||||
except BaseException:
|
||||
try:
|
||||
conn.rollback()
|
||||
except psycopg.Error:
|
||||
pass
|
||||
raise
|
||||
conn.commit()
|
||||
finally:
|
||||
pool.putconn(conn)
|
||||
|
||||
|
||||
def iso(value):
|
||||
return value.astimezone(timezone.utc).strftime('%Y-%m-%dT%H:%M:%S.%fZ')
|
||||
@@ -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)
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
import logging
|
||||
import sys
|
||||
import threading
|
||||
|
||||
import uvicorn
|
||||
|
||||
from config import settings
|
||||
|
||||
|
||||
class DrainingServer(uvicorn.Server):
|
||||
def handle_exit(self, sig, frame):
|
||||
from app import state
|
||||
if not state['ready'] or settings.drain_seconds <= 0:
|
||||
return super().handle_exit(sig, frame)
|
||||
state['ready'] = False
|
||||
logging.getLogger('messenger').info('signal %s: readiness off, draining for %.1fs', sig, settings.drain_seconds)
|
||||
threading.Timer(settings.drain_seconds, super().handle_exit, (sig, frame)).start()
|
||||
|
||||
|
||||
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
|
||||
config = uvicorn.Config(app, host=settings.host, port=settings.port, access_log=False, log_config=None,
|
||||
timeout_graceful_shutdown=settings.shutdown_timeout)
|
||||
DrainingServer(config).run()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -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()
|
||||
@@ -0,0 +1,45 @@
|
||||
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')
|
||||
try:
|
||||
value.encode('utf-8')
|
||||
except UnicodeEncodeError:
|
||||
raise ValueError('must be valid Unicode') from None
|
||||
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)
|
||||
@@ -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}
|
||||
+186
@@ -0,0 +1,186 @@
|
||||
import uuid
|
||||
|
||||
from psycopg import errors
|
||||
|
||||
from db import connection, iso
|
||||
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 utf8(value):
|
||||
return value.encode('utf-8')
|
||||
|
||||
|
||||
def unicode(value):
|
||||
return bytes(value).decode('utf-8')
|
||||
|
||||
|
||||
def user_out(row):
|
||||
return {'id': str(row['id']), 'username': row['username'],
|
||||
'display_name': unicode(row['display_name']), 'created_at': iso(row['created_at'])}
|
||||
|
||||
|
||||
def chat_out(row):
|
||||
return {'id': str(row['id']), 'title': unicode(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': unicode(row['text']), 'created_at': iso(row['created_at'])}
|
||||
|
||||
|
||||
def create_user(username, display_name, password_hash):
|
||||
try:
|
||||
with connection() as conn:
|
||||
row = conn.execute(
|
||||
'INSERT INTO users AS u (username, display_name, password_hash) VALUES (%s, %s, %s) '
|
||||
'RETURNING ' + USER_COLUMNS,
|
||||
(username, utf8(display_name), password_hash)).fetchone()
|
||||
except errors.UniqueViolation:
|
||||
raise ApiError(409, 'Имя пользователя уже занято') from None
|
||||
return user_out(row)
|
||||
|
||||
|
||||
def find_credentials(username):
|
||||
with 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 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 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 revoke_session(token_hash, keep_seconds):
|
||||
with connection() as conn:
|
||||
conn.execute('DELETE FROM sessions WHERE token_hash = %s', (token_hash,))
|
||||
conn.execute('INSERT INTO session_revocations (token_hash, expires_at) '
|
||||
'VALUES (%s, now() + make_interval(secs => %s)) '
|
||||
'ON CONFLICT (token_hash) DO UPDATE SET expires_at = EXCLUDED.expires_at',
|
||||
(token_hash, keep_seconds))
|
||||
|
||||
|
||||
def pending_revocations():
|
||||
with connection() as conn:
|
||||
conn.execute('DELETE FROM session_revocations WHERE expires_at <= now()')
|
||||
return [bytes(r['token_hash']) for r in conn.execute('SELECT token_hash FROM session_revocations').fetchall()]
|
||||
|
||||
|
||||
def clear_revocations(token_hashes):
|
||||
with connection() as conn:
|
||||
conn.execute('DELETE FROM session_revocations WHERE token_hash = ANY(%s)', (list(token_hashes),))
|
||||
|
||||
|
||||
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 connection() as conn, conn.transaction():
|
||||
chat = conn.execute('INSERT INTO chats (title, created_by) VALUES (%s, %s) RETURNING id, title, created_at',
|
||||
(utf8(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 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 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, key, request_hash, key_ttl_seconds):
|
||||
scope = 'POST /api/v1/chats/%s/messages' % chat_id
|
||||
with connection() as conn, conn.transaction():
|
||||
conn.execute('DELETE FROM idempotency_keys WHERE user_id = %s AND created_at < now() - make_interval(secs => %s)',
|
||||
(sender_id, key_ttl_seconds))
|
||||
claimed = conn.execute(
|
||||
'INSERT INTO idempotency_keys (user_id, scope, key, request_hash) VALUES (%s, %s, %s, %s) '
|
||||
'ON CONFLICT DO NOTHING RETURNING 1',
|
||||
(sender_id, scope, key, request_hash)).fetchone()
|
||||
if claimed is None:
|
||||
if not is_member(conn, chat_id, sender_id):
|
||||
raise ApiError(404, 'Чат не найден')
|
||||
stored = conn.execute(
|
||||
'SELECT k.request_hash, m.id, m.chat_id, m.sender_id, m.text, m.created_at '
|
||||
'FROM idempotency_keys k JOIN messages m ON m.id = k.message_id '
|
||||
'WHERE k.user_id = %s AND k.scope = %s AND k.key = %s',
|
||||
(sender_id, scope, key)).fetchone()
|
||||
if bytes(stored['request_hash']) != request_hash:
|
||||
raise ApiError(409, 'Idempotency-Key уже использован с другим запросом')
|
||||
return message_out(stored)
|
||||
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, utf8(text), chat_id, sender_id)).fetchone()
|
||||
if row is None:
|
||||
raise ApiError(404, 'Чат не найден')
|
||||
conn.execute('UPDATE idempotency_keys SET message_id = %s WHERE user_id = %s AND scope = %s AND key = %s',
|
||||
(row['id'], sender_id, scope, key))
|
||||
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 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)
|
||||
Reference in New Issue
Block a user