Files
mtusi2026lab4/app/main.py
T

752 lines
30 KiB
Python

import base64
import io
import json
import os
import re
import secrets
import threading
import uuid
import hashlib
from datetime import datetime, timedelta, timezone
import redis
import boto3
from botocore.config import Config
from argon2 import PasswordHasher
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse, Response
from PIL import Image, UnidentifiedImageError
from psycopg.rows import dict_row
from psycopg_pool import ConnectionPool
DATABASE_URL = os.getenv('DATABASE_URL', 'postgresql://messenger:messenger@postgres:5432/messenger')
REDIS_URL = os.getenv('REDIS_URL', 'redis://redis:6379/0')
SESSION_TTL_SECONDS = int(os.getenv('SESSION_TTL_SECONDS', '3600'))
INSTANCE_ID = os.getenv('INSTANCE_ID', uuid.uuid4().hex[:12])
DB_POOL_SIZE = int(os.getenv('DB_POOL_SIZE', '10'))
S3_ENDPOINT = os.getenv('S3_ENDPOINT', 'http://minio:9000')
S3_REGION = os.getenv('S3_REGION', 'us-east-1')
S3_BUCKET = os.getenv('S3_BUCKET', 'messenger-attachments')
S3_ACCESS_KEY_ID = os.getenv('S3_ACCESS_KEY_ID', 'messenger-api')
S3_SECRET_ACCESS_KEY = os.getenv('S3_SECRET_ACCESS_KEY', 'messenger-api-secret-123')
MAX_UPLOAD_BYTES = int(os.getenv('MAX_UPLOAD_BYTES', '10485760'))
MAX_IMAGE_PIXELS = int(os.getenv('MAX_IMAGE_PIXELS', '40000000'))
password_hasher = PasswordHasher()
db_pool = ConnectionPool(
DATABASE_URL, min_size=1, max_size=DB_POOL_SIZE, timeout=5,
kwargs={'row_factory': dict_row}, open=False,
)
redis_client = redis.Redis.from_url(
REDIS_URL,
decode_responses=True,
socket_connect_timeout=1,
socket_timeout=1,
retry_on_timeout=False,
)
s3_client = boto3.client(
's3', endpoint_url=S3_ENDPOINT, region_name=S3_REGION,
aws_access_key_id=S3_ACCESS_KEY_ID, aws_secret_access_key=S3_SECRET_ACCESS_KEY,
config=Config(connect_timeout=2, read_timeout=5, retries={'max_attempts': 1}),
)
chat_cache_lock = threading.Lock()
chat_cache_sql_reads = 0
invalidated_chat_caches = set()
app = FastAPI(title='MTUSI Messenger Lab 4', version='1.4.0')
class IdempotencyConflict(Exception):
pass
def utc_now():
return datetime.now(timezone.utc)
def format_time(value):
return value.astimezone(timezone.utc).isoformat().replace('+00:00', 'Z')
def parse_uuid(value):
try:
return uuid.UUID(str(value))
except (TypeError, ValueError) as exc:
raise ValueError('invalid uuid') from exc
def user_response(row):
return {
'id': str(row['id']), 'username': row['username'],
'display_name': row['display_name'], 'created_at': format_time(row['created_at']),
}
def chat_response(row, member_ids):
return {
'id': str(row['id']), 'title': row['title'],
'member_ids': [str(item) for item in member_ids],
'created_at': format_time(row['created_at']),
}
def message_response(row, attachment_ids=None):
return {
'id': str(row['id']), 'chat_id': str(row['chat_id']),
'sender_id': str(row['sender_id']), 'text': row['text'],
'created_at': format_time(row['created_at']),
'attachment_ids': [str(item) for item in (attachment_ids or [])],
}
def attachment_response(row):
return {
'id': str(row['id']), 'chat_id': str(row['chat_id']),
'owner_id': str(row['owner_id']), 'filename': row['filename'],
'content_type': row['content_type'], 'size_bytes': row['size_bytes'],
'sha256': row['sha256'], 'status': row['status'], 'variants': [],
'error_code': row['error_code'], 'created_at': format_time(row['created_at']),
'updated_at': format_time(row['updated_at']),
}
def attachment_ids_for(connection, message_id):
rows = connection.execute(
'SELECT attachment_id FROM message_attachments WHERE message_id=%s ORDER BY attachment_id',
(message_id,),
).fetchall()
return [row['attachment_id'] for row in rows]
def safe_filename(value):
return (
isinstance(value, str) and 1 <= len(value) <= 255 and
not any(ord(char) < 32 for char in value) and '/' not in value and '\\' not in value
)
async def read_upload(request):
chunks, total = [], 0
async for chunk in request.stream():
total += len(chunk)
if total > MAX_UPLOAD_BYTES:
return None
chunks.append(chunk)
return b''.join(chunks)
def inspect_image(data, content_type):
try:
with Image.open(io.BytesIO(data)) as image:
actual = 'image/png' if image.format == 'PNG' else 'image/jpeg' if image.format == 'JPEG' else None
if actual != content_type:
raise ValueError('content type does not match image')
width, height = image.size
if width * height > MAX_IMAGE_PIXELS:
raise OverflowError('too many pixels')
image.verify()
except Image.DecompressionBombError as exc:
raise OverflowError('too many pixels') from exc
except (UnidentifiedImageError, OSError, ValueError) as exc:
raise ValueError('invalid image') from exc
def error_response(request, status_code, code, message, extra_headers=None):
request_id = getattr(request.state, 'request_id', uuid.uuid4().hex)
headers = {'X-Request-Id': request_id, 'X-Instance-Id': INSTANCE_ID}
if extra_headers:
headers.update(extra_headers)
return JSONResponse(
status_code=status_code,
content={'error': {'code': code, 'message': message, 'request_id': request_id}},
headers=headers,
)
@app.middleware('http')
async def request_context(request: Request, call_next):
request.state.request_id = request.headers.get('X-Request-Id') or uuid.uuid4().hex
try:
response = await call_next(request)
except Exception:
return error_response(request, 500, 'internal_error', 'Внутренняя ошибка')
response.headers['X-Request-Id'] = request.state.request_id
response.headers['X-Instance-Id'] = INSTANCE_ID
return response
@app.on_event('startup')
def start_application():
db_pool.open(wait=True)
with db_pool.connection() as connection:
connection.execute('SELECT 1')
@app.on_event('shutdown')
def stop_application():
db_pool.close()
async def read_json_body(request):
try:
value = await request.json()
except Exception as exc:
raise ValueError('malformed json') from exc
if not isinstance(value, dict):
raise ValueError('request body must be an object')
return value
def require_fields(data, required):
if set(data) != required:
raise ValueError('unexpected or missing fields')
def get_bearer_session(request):
authorization = request.headers.get('Authorization', '')
if not authorization.startswith('Bearer '):
return None
token = authorization.removeprefix('Bearer ')
if len(token) < 40:
return None
try:
value = redis_client.get('session:' + token)
return json.loads(value) if value else None
except Exception:
return None
def get_current_user(request):
session = get_bearer_session(request)
if session is None:
return None
with db_pool.connection() as connection:
return connection.execute(
'SELECT * FROM users WHERE id=%s', (session['user_id'],)
).fetchone()
def require_current_user(request):
user = get_current_user(request)
if user is None:
return None, error_response(
request, 401, 'unauthorized', 'Требуется действующая Bearer-сессия',
{'WWW-Authenticate': 'Bearer realm="messenger"'},
)
return user, None
def is_chat_member(connection, chat_id, user_id):
row = connection.execute(
'SELECT 1 FROM chat_members WHERE chat_id=%s AND user_id=%s',
(chat_id, user_id),
).fetchone()
return row is not None
def make_cursor(scope, owner_id, row):
created_at = row['created_at']
if isinstance(created_at, datetime):
created_at = format_time(created_at)
data = [scope, owner_id, created_at, str(row['id'])]
return base64.urlsafe_b64encode(json.dumps(data).encode()).decode().rstrip('=')
def read_cursor(cursor, expected_scope, expected_owner):
try:
padding = '=' * ((4 - len(cursor) % 4) % 4)
value = json.loads(base64.urlsafe_b64decode((cursor + padding).encode()))
if value[0] != expected_scope or value[1] != expected_owner:
raise ValueError
timestamp = datetime.fromisoformat(value[2].replace('Z', '+00:00'))
return timestamp, parse_uuid(value[3])
except (IndexError, TypeError, ValueError, json.JSONDecodeError) as exc:
raise ValueError('invalid cursor') from exc
def page_chats(items, limit, cursor_data, owner_id):
if cursor_data:
items = [
item for item in items
if (
datetime.fromisoformat(item['created_at'].replace('Z', '+00:00')),
uuid.UUID(item['id']),
) > cursor_data
]
page = items[:limit]
next_cursor = make_cursor('chats', owner_id, page[-1]) if len(items) > limit and page else None
return {'items': page, 'next_cursor': next_cursor}
def parse_limit(value):
try:
limit = int(value)
except (TypeError, ValueError) as exc:
raise ValueError('invalid limit') from exc
if not 1 <= limit <= 100:
raise ValueError('invalid limit')
return limit
@app.get('/health/live')
def health_live():
return {'status': 'ok'}
@app.get('/health/ready')
def health_ready(request: Request):
try:
with db_pool.connection() as connection:
connection.execute('SELECT 1')
redis_client.ping()
s3_client.head_bucket(Bucket=S3_BUCKET)
except Exception:
return error_response(request, 503, 'unavailable', 'Зависимость недоступна')
return {'status': 'ok'}
@app.get('/debug/cache-stats')
def cache_stats(request: Request):
if os.getenv('ENABLE_DEBUG_METRICS') != '1':
return error_response(request, 404, 'not_found', 'Ресурс не найден')
return {'chat_list_sql_reads': chat_cache_sql_reads}
@app.post('/api/v1/users', status_code=201)
async def create_user(request: Request):
try:
data = await read_json_body(request)
require_fields(data, {'username', 'display_name', 'password'})
username, display_name, password = data['username'], data['display_name'], data['password']
if not isinstance(username, str) or not re.fullmatch(r'[a-z0-9_]{3,32}', username):
raise ValueError('invalid username')
if not isinstance(display_name, str) or not display_name.strip() or not 1 <= len(display_name) <= 100:
raise ValueError('invalid display name')
if not isinstance(password, str) or not 12 <= len(password) <= 128:
raise ValueError('invalid password')
user_id, created_at = uuid.uuid4(), utc_now()
password_hash = password_hasher.hash(password)
with db_pool.connection() as connection:
try:
connection.execute(
'INSERT INTO users VALUES (%s,%s,%s,%s,%s)',
(user_id, username, display_name, password_hash, created_at),
)
connection.commit()
except Exception as exc:
connection.rollback()
if 'unique' in str(exc).lower():
return error_response(request, 409, 'conflict', 'Имя пользователя уже занято')
raise
return user_response({'id': user_id, 'username': username, 'display_name': display_name, 'created_at': created_at})
except ValueError:
return error_response(request, 400, 'invalid_request', 'Неверные поля запроса')
@app.post('/api/v1/auth/sessions', status_code=201)
def create_session(request: Request):
authorization = request.headers.get('Authorization', '')
try:
if not authorization.startswith('Basic '):
raise ValueError
credentials = base64.b64decode(authorization.removeprefix('Basic '), validate=True).decode()
username, password = credentials.split(':', 1)
with db_pool.connection() as connection:
user = connection.execute('SELECT * FROM users WHERE username=%s', (username,)).fetchone()
if user is None:
raise ValueError
password_hasher.verify(user['password_hash'], password)
except Exception:
return error_response(
request, 401, 'unauthorized', 'Неверные учётные данные',
{'WWW-Authenticate': 'Basic realm="messenger", charset="UTF-8"'},
)
token = secrets.token_urlsafe(32)
expires_at = utc_now() + timedelta(seconds=SESSION_TTL_SECONDS)
redis_client.setex(
'session:' + token,
SESSION_TTL_SECONDS,
json.dumps({'user_id': str(user['id']), 'expires_at': format_time(expires_at)}),
)
return {
'token': token, 'token_type': 'Bearer',
'expires_at': format_time(expires_at), 'user': user_response(user),
}
@app.delete('/api/v1/auth/sessions/current', status_code=204)
def logout(request: Request):
if get_bearer_session(request) is None:
return error_response(
request, 401, 'unauthorized', 'Сессия недействительна',
{'WWW-Authenticate': 'Bearer realm="messenger"'},
)
token = request.headers['Authorization'].removeprefix('Bearer ')
redis_client.delete('session:' + token)
return Response(status_code=204)
@app.get('/api/v1/users/me')
def get_me(request: Request):
user, failure = require_current_user(request)
return failure if failure else user_response(user)
@app.post('/api/v1/chats', status_code=201)
async def create_chat(request: Request):
user, failure = require_current_user(request)
if failure:
return failure
try:
data = await read_json_body(request)
require_fields(data, {'title', 'member_ids'})
title, member_values = data['title'], data['member_ids']
if not isinstance(title, str) or not title.strip() or not 1 <= len(title) <= 100:
raise ValueError('invalid title')
if not isinstance(member_values, list) or not 1 <= len(member_values) <= 100:
raise ValueError('invalid members')
if len(set(member_values)) != len(member_values):
raise ValueError('duplicate members')
member_ids = [parse_uuid(value) for value in member_values]
if user['id'] not in member_ids:
raise ValueError('current user must be a member')
chat_id, created_at = uuid.uuid4(), utc_now()
with db_pool.connection() as connection:
with connection.transaction():
count = connection.execute(
'SELECT count(*) AS n FROM users WHERE id = ANY(%s)', (member_ids,)
).fetchone()['n']
if count != len(member_ids):
raise ValueError('unknown member')
connection.execute('INSERT INTO chats VALUES (%s,%s,%s)', (chat_id, title, created_at))
for member_id in member_ids:
connection.execute('INSERT INTO chat_members VALUES (%s,%s)', (chat_id, member_id))
for member_id in member_ids:
try:
redis_client.delete('chats:' + str(member_id))
except Exception:
with chat_cache_lock:
invalidated_chat_caches.add('chats:' + str(member_id))
return chat_response({'id': chat_id, 'title': title, 'created_at': created_at}, member_ids)
except ValueError:
return error_response(request, 400, 'invalid_request', 'Неверные данные чата')
@app.get('/api/v1/chats')
def list_chats(request: Request, limit: str = '50', cursor: str | None = None):
global chat_cache_sql_reads
user, failure = require_current_user(request)
if failure:
return failure
try:
limit = parse_limit(limit)
except ValueError:
return error_response(request, 400, 'invalid_request', 'limit должен быть от 1 до 100')
cursor_data = None
if cursor is not None:
if not cursor:
return error_response(request, 400, 'invalid_request', 'Неверный cursor')
try:
cursor_data = read_cursor(cursor, 'chats', str(user['id']))
except ValueError:
return error_response(request, 400, 'invalid_request', 'Неверный cursor')
cache_key = 'chats:' + str(user['id'])
cache_needs_refresh = cache_key in invalidated_chat_caches
if not cache_needs_refresh:
try:
cached = redis_client.get(cache_key)
if cached:
return page_chats(json.loads(cached), limit, cursor_data, str(user['id']))
except Exception:
pass
with chat_cache_lock:
try:
cached = None if cache_needs_refresh else redis_client.get(cache_key)
except Exception:
cached = None
if cached:
chats = json.loads(cached)
else:
with db_pool.connection() as connection:
rows = connection.execute(
'SELECT c.* FROM chats c JOIN chat_members m ON m.chat_id=c.id '
'WHERE m.user_id=%s ORDER BY c.created_at,c.id', (user['id'],)
).fetchall()
chats = []
for row in rows:
members = connection.execute(
'SELECT user_id FROM chat_members WHERE chat_id=%s ORDER BY user_id', (row['id'],)
).fetchall()
chats.append(chat_response(row, [item['user_id'] for item in members]))
chat_cache_sql_reads += 1
try:
redis_client.setex(cache_key, 30, json.dumps(chats))
invalidated_chat_caches.discard(cache_key)
except Exception:
pass
return page_chats(chats, limit, cursor_data, str(user['id']))
@app.get('/api/v1/chats/{chat_id}')
def get_chat(chat_id: str, request: Request):
user, failure = require_current_user(request)
if failure:
return failure
try:
parsed_chat_id = parse_uuid(chat_id)
except ValueError:
return error_response(request, 400, 'invalid_request', 'Неверный UUID')
with db_pool.connection() as connection:
chat = connection.execute('SELECT * FROM chats WHERE id=%s', (parsed_chat_id,)).fetchone()
if chat is None or not is_chat_member(connection, parsed_chat_id, user['id']):
return error_response(request, 404, 'not_found', 'Чат не найден')
members = connection.execute('SELECT user_id FROM chat_members WHERE chat_id=%s', (parsed_chat_id,)).fetchall()
return chat_response(chat, [item['user_id'] for item in members])
@app.post('/api/v1/chats/{chat_id}/attachments', status_code=201)
async def upload_attachment(chat_id: str, request: Request):
user, failure = require_current_user(request)
if failure:
return failure
try:
parsed_chat_id = parse_uuid(chat_id)
filename = request.query_params.get('filename', '')
if not safe_filename(filename):
raise ValueError('invalid filename')
content_type = request.headers.get('Content-Type', '').split(';', 1)[0].strip().lower()
if content_type not in {'image/png', 'image/jpeg'}:
return error_response(request, 415, 'unsupported_media_type', 'Поддерживаются только PNG и JPEG')
data = await read_upload(request)
if data is None:
return error_response(request, 413, 'payload_too_large', 'Файл слишком большой')
if not data:
raise ValueError('empty file')
except OverflowError:
return error_response(request, 413, 'payload_too_large', 'Слишком много пикселей')
except ValueError:
return error_response(request, 400, 'invalid_request', 'Неверные данные вложения')
try:
inspect_image(data, content_type)
except OverflowError:
return error_response(request, 413, 'payload_too_large', 'Слишком много пикселей')
except ValueError:
return error_response(request, 415, 'unsupported_media_type', 'Файл не является корректным PNG или JPEG')
attachment_id = uuid.uuid4()
object_key = 'attachments/' + str(attachment_id)
now = utc_now()
digest = hashlib.sha256(data).hexdigest()
with db_pool.connection() as connection:
if not is_chat_member(connection, parsed_chat_id, user['id']):
return error_response(request, 404, 'not_found', 'Чат не найден')
try:
s3_client.put_object(
Bucket=S3_BUCKET, Key=object_key, Body=data,
ContentType=content_type, ContentLength=len(data),
)
with connection.transaction():
row = connection.execute(
'INSERT INTO attachments '
'(id,chat_id,owner_id,filename,content_type,size_bytes,sha256,object_key,status,error_code,created_at,updated_at) '
'VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) RETURNING *',
(attachment_id, parsed_chat_id, user['id'], filename, content_type,
len(data), digest, object_key, 'ready', None, now, now),
).fetchone()
except Exception:
try:
s3_client.delete_object(Bucket=S3_BUCKET, Key=object_key)
except Exception:
pass
return error_response(request, 503, 'unavailable', 'Хранилище вложений недоступно')
return attachment_response(row)
def get_attachment_for_user(connection, attachment_id, user_id):
row = connection.execute(
'SELECT a.* FROM attachments a JOIN chat_members m ON m.chat_id=a.chat_id '
'WHERE a.id=%s AND m.user_id=%s', (attachment_id, user_id),
).fetchone()
return row
@app.get('/api/v1/attachments/{attachment_id}')
def get_attachment(attachment_id: str, request: Request):
user, failure = require_current_user(request)
if failure:
return failure
try:
parsed_id = parse_uuid(attachment_id)
except ValueError:
return error_response(request, 400, 'invalid_request', 'Неверный UUID')
with db_pool.connection() as connection:
row = get_attachment_for_user(connection, parsed_id, user['id'])
if row is None:
return error_response(request, 404, 'not_found', 'Вложение не найдено')
return attachment_response(row)
@app.get('/api/v1/attachments/{attachment_id}/content')
def download_attachment(attachment_id: str, request: Request, variant: str = 'original'):
user, failure = require_current_user(request)
if failure:
return failure
if variant != 'original':
return error_response(request, 404, 'not_found', 'Вариант не найден')
try:
parsed_id = parse_uuid(attachment_id)
except ValueError:
return error_response(request, 400, 'invalid_request', 'Неверный UUID')
with db_pool.connection() as connection:
row = get_attachment_for_user(connection, parsed_id, user['id'])
if row is None:
return error_response(request, 404, 'not_found', 'Вложение не найдено')
try:
value = s3_client.get_object(Bucket=S3_BUCKET, Key=row['object_key'])
data = value['Body'].read(MAX_UPLOAD_BYTES + 1)
if len(data) > MAX_UPLOAD_BYTES:
return error_response(request, 503, 'unavailable', 'Некорректный объект в хранилище')
except Exception:
return error_response(request, 503, 'unavailable', 'Хранилище вложений недоступно')
return Response(content=data, media_type=row['content_type'])
@app.post('/api/v1/chats/{chat_id}/messages', status_code=201)
async def create_message(chat_id: str, request: Request):
user, failure = require_current_user(request)
if failure:
return failure
try:
parsed_chat_id = parse_uuid(chat_id)
idempotency_key = request.headers.get('Idempotency-Key', '')
if not re.fullmatch(r'[A-Za-z0-9._:-]{1,128}', idempotency_key):
raise ValueError('invalid idempotency key')
data = await read_json_body(request)
require_fields(data, {'text', 'attachment_ids'}) if 'attachment_ids' in data else require_fields(data, {'text'})
text = data['text']
attachment_values = data.get('attachment_ids', [])
if not isinstance(attachment_values, list) or len(attachment_values) > 10:
raise ValueError('invalid attachments')
attachment_ids = [parse_uuid(value) for value in attachment_values]
if len(set(attachment_ids)) != len(attachment_ids):
raise ValueError('duplicate attachments')
if not isinstance(text, str) or len(text) > 4000 or (not text.strip() and not attachment_ids):
raise ValueError('invalid text')
except ValueError:
return error_response(request, 400, 'invalid_request', 'Неверные данные сообщения')
body_hash = hashlib.sha256(json.dumps(
{'text': text, 'attachment_ids': sorted(str(value) for value in attachment_ids)},
sort_keys=True,
).encode()).hexdigest()
with db_pool.connection() as connection:
if not is_chat_member(connection, parsed_chat_id, user['id']):
return error_response(request, 404, 'not_found', 'Чат не найден')
try:
with connection.transaction():
idempotency = connection.execute(
'INSERT INTO message_idempotency '
'(user_id, chat_id, idempotency_key, body_hash) '
'VALUES (%s,%s,%s,%s) '
'ON CONFLICT (user_id, chat_id, idempotency_key) DO NOTHING '
'RETURNING message_id',
(user['id'], parsed_chat_id, idempotency_key, body_hash),
).fetchone()
if idempotency is None:
saved = connection.execute(
'SELECT message_id, body_hash FROM message_idempotency '
'WHERE user_id=%s AND chat_id=%s AND idempotency_key=%s',
(user['id'], parsed_chat_id, idempotency_key),
).fetchone()
if saved['body_hash'] != body_hash:
raise IdempotencyConflict
row = connection.execute(
'SELECT * FROM messages WHERE id=%s', (saved['message_id'],)
).fetchone()
return message_response(row, attachment_ids_for(connection, row['id']))
if attachment_ids:
rows = connection.execute(
'SELECT id FROM attachments WHERE id = ANY(%s) AND chat_id=%s AND status=%s',
(attachment_ids, parsed_chat_id, 'ready'),
).fetchall()
if len(rows) != len(attachment_ids):
raise ValueError('attachment is not available in this chat')
message_id, created_at = uuid.uuid4(), utc_now()
connection.execute(
'INSERT INTO messages VALUES (%s,%s,%s,%s,%s)',
(message_id, parsed_chat_id, user['id'], text, created_at),
)
connection.execute(
'UPDATE message_idempotency SET message_id=%s '
'WHERE user_id=%s AND chat_id=%s AND idempotency_key=%s',
(message_id, user['id'], parsed_chat_id, idempotency_key),
)
for attachment_id in attachment_ids:
connection.execute(
'INSERT INTO message_attachments (message_id, attachment_id) VALUES (%s,%s)',
(message_id, attachment_id),
)
row = {
'id': message_id, 'chat_id': parsed_chat_id,
'sender_id': user['id'], 'text': text, 'created_at': created_at,
}
except IdempotencyConflict:
return error_response(request, 409, 'conflict', 'Ключ уже использован для другого сообщения')
except ValueError:
return error_response(request, 404, 'not_found', 'Вложение не найдено')
return message_response(row, attachment_ids)
@app.get('/api/v1/chats/{chat_id}/messages')
def list_messages(chat_id: str, request: Request, limit: str = '50', cursor: str | None = None):
user, failure = require_current_user(request)
if failure:
return failure
try:
limit = parse_limit(limit)
except ValueError:
return error_response(request, 400, 'invalid_request', 'limit должен быть от 1 до 100')
try:
parsed_chat_id = parse_uuid(chat_id)
except ValueError:
return error_response(request, 400, 'invalid_request', 'Неверный UUID')
cursor_data = None
if cursor is not None:
if not cursor:
return error_response(request, 400, 'invalid_request', 'Неверный cursor')
try:
cursor_data = read_cursor(cursor, 'messages', str(parsed_chat_id))
except ValueError:
return error_response(request, 400, 'invalid_request', 'Неверный cursor')
with db_pool.connection() as connection:
if not is_chat_member(connection, parsed_chat_id, user['id']):
return error_response(request, 404, 'not_found', 'Чат не найден')
if cursor_data:
rows = connection.execute(
'SELECT * FROM messages WHERE chat_id=%s AND (created_at,id)>(%s,%s) '
'ORDER BY created_at,id LIMIT %s',
(parsed_chat_id, cursor_data[0], cursor_data[1], limit + 1),
).fetchall()
else:
rows = connection.execute(
'SELECT * FROM messages WHERE chat_id=%s ORDER BY created_at,id LIMIT %s',
(parsed_chat_id, limit + 1),
).fetchall()
items = [message_response(row, attachment_ids_for(connection, row['id'])) for row in rows[:limit]]
rows_to_return = rows[:limit]
next_cursor = make_cursor('messages', str(parsed_chat_id), rows_to_return[-1]) if len(rows) > limit else None
return {
'items': items,
'next_cursor': next_cursor,
}