feat: port lab3 backend foundation to lab4
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
from datetime import timezone
|
||||
from fastapi import APIRouter, Depends, Header, HTTPException, status
|
||||
from fastapi.security import HTTPBasic, HTTPBasicCredentials
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.api.deps import get_db
|
||||
from app.api.users import user_to_response
|
||||
from app.services.auth_service import (
|
||||
AuthService,
|
||||
InvalidCredentialsError,
|
||||
InvalidSessionError,
|
||||
)
|
||||
|
||||
|
||||
router = APIRouter(prefix="/api/v1/auth", tags=["Authentication"])
|
||||
|
||||
basic_security = HTTPBasic(auto_error=False)
|
||||
|
||||
|
||||
|
||||
@router.post("/sessions", status_code=status.HTTP_201_CREATED)
|
||||
def create_session(
|
||||
credentials: HTTPBasicCredentials = Depends(basic_security),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
if credentials is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Basic authentication required",
|
||||
headers={
|
||||
"WWW-Authenticate": 'Basic realm="messenger", charset="UTF-8"'
|
||||
},
|
||||
)
|
||||
|
||||
try:
|
||||
result = AuthService(db).create_session(
|
||||
username=credentials.username,
|
||||
password=credentials.password,
|
||||
)
|
||||
|
||||
expires_at = result["expires_at"]
|
||||
|
||||
if expires_at.tzinfo is None:
|
||||
expires_at = expires_at.replace(tzinfo=timezone.utc)
|
||||
else:
|
||||
expires_at = expires_at.astimezone(timezone.utc)
|
||||
|
||||
return {
|
||||
"token": result["token"],
|
||||
"token_type": result["token_type"],
|
||||
"expires_at": expires_at.isoformat(
|
||||
timespec="microseconds"
|
||||
).replace("+00:00", "Z"),
|
||||
"user": user_to_response(result["user"]),
|
||||
}
|
||||
|
||||
except InvalidCredentialsError:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Invalid username or password",
|
||||
headers={
|
||||
"WWW-Authenticate": 'Basic realm="messenger", charset="UTF-8"'
|
||||
},
|
||||
) from None
|
||||
|
||||
|
||||
|
||||
@router.delete("/sessions/current", status_code=status.HTTP_204_NO_CONTENT)
|
||||
def delete_current_session(
|
||||
authorization: str | None = Header(default=None),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
if not authorization or not authorization.startswith("Bearer "):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Bearer token required",
|
||||
headers={"WWW-Authenticate": 'Bearer realm="messenger"'},
|
||||
)
|
||||
|
||||
token = authorization[7:].strip()
|
||||
|
||||
if not token:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Bearer token required",
|
||||
headers={"WWW-Authenticate": 'Bearer realm="messenger"'},
|
||||
)
|
||||
|
||||
try:
|
||||
AuthService(db).delete_session(token)
|
||||
except InvalidSessionError:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Invalid or expired session",
|
||||
headers={"WWW-Authenticate": 'Bearer realm="messenger"'},
|
||||
) from None
|
||||
@@ -0,0 +1,90 @@
|
||||
import uuid
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, status
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.api.deps import get_db, get_current_user
|
||||
from app.db.models import User as UserModel
|
||||
from app.models import Chat as ChatResponse, CreateChat
|
||||
from app.services.chat_service import (
|
||||
ChatService,
|
||||
ChatNotFoundError,
|
||||
InvalidChatDataError,
|
||||
)
|
||||
|
||||
|
||||
router = APIRouter(prefix="/api/v1/chats", tags=["Chats"])
|
||||
|
||||
|
||||
def chat_to_response(chat) -> ChatResponse:
|
||||
return ChatResponse(
|
||||
id=str(chat.id),
|
||||
title=chat.title,
|
||||
member_ids=[str(member.user_id) for member in chat.members],
|
||||
created_at=chat.created_at,
|
||||
)
|
||||
|
||||
|
||||
@router.post("", response_model=ChatResponse, status_code=status.HTTP_201_CREATED)
|
||||
def create_chat(
|
||||
body: CreateChat,
|
||||
db: Session = Depends(get_db),
|
||||
user: UserModel = Depends(get_current_user),
|
||||
):
|
||||
try:
|
||||
chat = ChatService(db).create_chat(
|
||||
title=body.title,
|
||||
member_ids=body.member_ids,
|
||||
creator_id=user.id,
|
||||
)
|
||||
return chat_to_response(chat)
|
||||
|
||||
except InvalidChatDataError as exc:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail=str(exc),
|
||||
) from None
|
||||
|
||||
|
||||
@router.get("")
|
||||
def list_chats(
|
||||
limit: int = Query(default=50, ge=1, le=100),
|
||||
cursor: str | None = None,
|
||||
db: Session = Depends(get_db),
|
||||
user: UserModel = Depends(get_current_user),
|
||||
):
|
||||
if cursor is not None:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="Chat cursor pagination is not implemented yet",
|
||||
)
|
||||
|
||||
chats = ChatService(db).list_chats(user.id)
|
||||
chats = chats[:limit]
|
||||
|
||||
return {
|
||||
"items": [chat_to_response(chat) for chat in chats],
|
||||
"next_cursor": None,
|
||||
}
|
||||
|
||||
|
||||
@router.get("/{chat_id}", response_model=ChatResponse)
|
||||
def get_chat(
|
||||
chat_id: str,
|
||||
db: Session = Depends(get_db),
|
||||
user: UserModel = Depends(get_current_user),
|
||||
):
|
||||
try:
|
||||
parsed_id = uuid.UUID(chat_id)
|
||||
except ValueError:
|
||||
raise HTTPException(status_code=400, detail="Invalid chat UUID") from None
|
||||
|
||||
try:
|
||||
chat = ChatService(db).get_chat(parsed_id, user.id)
|
||||
return chat_to_response(chat)
|
||||
|
||||
except ChatNotFoundError:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail="Chat not found",
|
||||
) from None
|
||||
@@ -0,0 +1,40 @@
|
||||
from fastapi import Depends, HTTPException, status
|
||||
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.db.session import SessionLocal
|
||||
from app.services.auth_service import AuthService, InvalidSessionError
|
||||
|
||||
|
||||
bearer_scheme = HTTPBearer(auto_error=False)
|
||||
|
||||
|
||||
def get_db():
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
def get_current_user(
|
||||
credentials: HTTPAuthorizationCredentials | None = Depends(bearer_scheme),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
if credentials is None or credentials.scheme.lower() != "bearer":
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Bearer token required",
|
||||
headers={"WWW-Authenticate": "Bearer"},
|
||||
)
|
||||
|
||||
try:
|
||||
user = AuthService(db).authenticate(credentials.credentials)
|
||||
return user
|
||||
|
||||
except InvalidSessionError:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Invalid or expired token",
|
||||
headers={"WWW-Authenticate": "Bearer"},
|
||||
) from None
|
||||
@@ -0,0 +1,73 @@
|
||||
import re
|
||||
import uuid
|
||||
|
||||
from fastapi import APIRouter, Depends, Header, HTTPException, Query, status
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.api.deps import get_db, get_current_user
|
||||
from app.db.models import User as UserModel
|
||||
from app.models import CreateMessage, Message as MessageResponse
|
||||
from app.services.message_service import (
|
||||
MessageService, MessageNotFoundError, InvalidMessageDataError,
|
||||
IdempotencyConflictError,
|
||||
)
|
||||
|
||||
router = APIRouter(prefix="/api/v1/chats/{chat_id}/messages", tags=["Messages"])
|
||||
IDEMPOTENCY_KEY_RE = re.compile(r"^[A-Za-z0-9._:-]{1,128}$")
|
||||
|
||||
|
||||
def message_to_response(message) -> MessageResponse:
|
||||
return MessageResponse(
|
||||
id=str(message.id), chat_id=str(message.chat_id),
|
||||
sender_id=str(message.sender_id), text=message.text,
|
||||
created_at=message.created_at,
|
||||
)
|
||||
|
||||
|
||||
def parse_chat_id(chat_id: str) -> uuid.UUID:
|
||||
try:
|
||||
return uuid.UUID(chat_id)
|
||||
except ValueError:
|
||||
raise HTTPException(status_code=400, detail="Invalid chat UUID") from None
|
||||
|
||||
|
||||
@router.post("", response_model=MessageResponse, status_code=status.HTTP_201_CREATED)
|
||||
def create_message(
|
||||
chat_id: str, body: CreateMessage,
|
||||
idempotency_key: str = Header(..., alias="Idempotency-Key", min_length=1, max_length=128),
|
||||
db: Session = Depends(get_db),
|
||||
user: UserModel = Depends(get_current_user),
|
||||
):
|
||||
if not IDEMPOTENCY_KEY_RE.fullmatch(idempotency_key):
|
||||
raise HTTPException(status_code=400, detail="Invalid Idempotency-Key")
|
||||
parsed_id = parse_chat_id(chat_id)
|
||||
try:
|
||||
message = MessageService(db).create_message(
|
||||
chat_id=parsed_id, sender_id=user.id, text=body.text,
|
||||
idempotency_key=idempotency_key,
|
||||
)
|
||||
return message_to_response(message)
|
||||
except MessageNotFoundError:
|
||||
raise HTTPException(status_code=404, detail="Chat not found") from None
|
||||
except InvalidMessageDataError as exc:
|
||||
raise HTTPException(status_code=400, detail=str(exc)) from None
|
||||
except IdempotencyConflictError:
|
||||
raise HTTPException(status_code=409, detail="Idempotency-Key was already used with a different request") from None
|
||||
|
||||
|
||||
@router.get("")
|
||||
def list_messages(
|
||||
chat_id: str, limit: int = Query(default=50, ge=1, le=100),
|
||||
cursor: str | None = None, db: Session = Depends(get_db),
|
||||
user: UserModel = Depends(get_current_user),
|
||||
):
|
||||
parsed_id = parse_chat_id(chat_id)
|
||||
try:
|
||||
messages, next_cursor = MessageService(db).get_history(
|
||||
chat_id=parsed_id, user_id=user.id, limit=limit, cursor=cursor
|
||||
)
|
||||
return {"items": [message_to_response(item) for item in messages], "next_cursor": next_cursor}
|
||||
except MessageNotFoundError:
|
||||
raise HTTPException(status_code=404, detail="Chat not found") from None
|
||||
except InvalidMessageDataError as exc:
|
||||
raise HTTPException(status_code=400, detail=str(exc)) from None
|
||||
@@ -0,0 +1,55 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.api.deps import get_db, get_current_user
|
||||
from app.db.models import User as UserModel
|
||||
from app.models import CreateUser, User
|
||||
from app.services.user_service import (
|
||||
UserService,
|
||||
UsernameAlreadyExistsError,
|
||||
)
|
||||
|
||||
|
||||
router = APIRouter(prefix="/api/v1/users", tags=["Users"])
|
||||
|
||||
|
||||
def user_to_response(user: UserModel) -> User:
|
||||
return User(
|
||||
id=str(user.id),
|
||||
username=user.username,
|
||||
display_name=user.display_name,
|
||||
created_at=user.created_at,
|
||||
)
|
||||
|
||||
|
||||
@router.post("", response_model=User, status_code=status.HTTP_201_CREATED)
|
||||
def register_user(
|
||||
body: CreateUser,
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
try:
|
||||
user = UserService(db).register(
|
||||
username=body.username,
|
||||
display_name=body.display_name,
|
||||
password=body.password,
|
||||
)
|
||||
return user_to_response(user)
|
||||
|
||||
except UsernameAlreadyExistsError:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail="Username already exists",
|
||||
) from None
|
||||
|
||||
except ValueError as exc:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail=str(exc),
|
||||
) from None
|
||||
|
||||
|
||||
@router.get("/me", response_model=User)
|
||||
def get_me(
|
||||
current_user: UserModel = Depends(get_current_user),
|
||||
):
|
||||
return user_to_response(current_user)
|
||||
@@ -0,0 +1,24 @@
|
||||
import os
|
||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
app_name: str = "Messenger API"
|
||||
app_env: str = "lab"
|
||||
database_url: str = "postgresql+psycopg://messenger:messenger@postgres:5432/messenger"
|
||||
db_pool_size: int = 5
|
||||
db_max_overflow: int = 5
|
||||
db_pool_timeout: int = 10
|
||||
session_ttl_seconds: int = 3600
|
||||
cache_url: str = "redis://redis:6379/0"
|
||||
message_cache_ttl_seconds: int = 60
|
||||
idempotency_ttl_seconds: int = 86400
|
||||
instance_id: str = os.getenv("INSTANCE_ID") or os.getenv("HOSTNAME") or "api-local"
|
||||
shutdown_grace_seconds: int = 20
|
||||
|
||||
model_config = SettingsConfigDict(
|
||||
env_file=".env", env_file_encoding="utf-8", extra="ignore"
|
||||
)
|
||||
|
||||
|
||||
settings = Settings()
|
||||
@@ -0,0 +1,5 @@
|
||||
from sqlalchemy.orm import DeclarativeBase
|
||||
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
pass
|
||||
@@ -0,0 +1,215 @@
|
||||
import uuid
|
||||
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from sqlalchemy import (
|
||||
DateTime,
|
||||
ForeignKey,
|
||||
Index,
|
||||
String,
|
||||
Text,
|
||||
UniqueConstraint,
|
||||
)
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from app.db.base import Base
|
||||
|
||||
|
||||
def utc_now() -> datetime:
|
||||
return datetime.now(timezone.utc)
|
||||
|
||||
|
||||
class User(Base):
|
||||
__tablename__ = "users"
|
||||
|
||||
id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
primary_key=True,
|
||||
default=uuid.uuid4,
|
||||
)
|
||||
username: Mapped[str] = mapped_column(
|
||||
String(32),
|
||||
unique=True,
|
||||
nullable=False,
|
||||
index=True,
|
||||
)
|
||||
display_name: Mapped[str] = mapped_column(
|
||||
String(100),
|
||||
nullable=False,
|
||||
)
|
||||
password_hash: Mapped[str] = mapped_column(
|
||||
String(255),
|
||||
nullable=False,
|
||||
)
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True),
|
||||
default=utc_now,
|
||||
nullable=False,
|
||||
)
|
||||
|
||||
memberships: Mapped[list["ChatMember"]] = relationship(
|
||||
back_populates="user",
|
||||
cascade="all, delete-orphan",
|
||||
)
|
||||
messages: Mapped[list["Message"]] = relationship(
|
||||
back_populates="sender",
|
||||
)
|
||||
|
||||
|
||||
class Chat(Base):
|
||||
__tablename__ = "chats"
|
||||
|
||||
id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
primary_key=True,
|
||||
default=uuid.uuid4,
|
||||
)
|
||||
title: Mapped[str] = mapped_column(
|
||||
String(100),
|
||||
nullable=False,
|
||||
)
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True),
|
||||
default=utc_now,
|
||||
nullable=False,
|
||||
)
|
||||
|
||||
members: Mapped[list["ChatMember"]] = relationship(
|
||||
back_populates="chat",
|
||||
cascade="all, delete-orphan",
|
||||
)
|
||||
messages: Mapped[list["Message"]] = relationship(
|
||||
back_populates="chat",
|
||||
cascade="all, delete-orphan",
|
||||
)
|
||||
|
||||
|
||||
class ChatMember(Base):
|
||||
__tablename__ = "chat_members"
|
||||
|
||||
chat_id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
ForeignKey("chats.id", ondelete="CASCADE"),
|
||||
primary_key=True,
|
||||
)
|
||||
user_id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
ForeignKey("users.id", ondelete="CASCADE"),
|
||||
primary_key=True,
|
||||
)
|
||||
|
||||
chat: Mapped["Chat"] = relationship(
|
||||
back_populates="members",
|
||||
)
|
||||
user: Mapped["User"] = relationship(
|
||||
back_populates="memberships",
|
||||
)
|
||||
|
||||
|
||||
class Message(Base):
|
||||
__tablename__ = "messages"
|
||||
|
||||
id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
primary_key=True,
|
||||
default=uuid.uuid4,
|
||||
)
|
||||
chat_id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
ForeignKey("chats.id", ondelete="CASCADE"),
|
||||
nullable=False,
|
||||
)
|
||||
sender_id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
ForeignKey("users.id"),
|
||||
nullable=False,
|
||||
)
|
||||
text: Mapped[str] = mapped_column(
|
||||
Text,
|
||||
nullable=False,
|
||||
)
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True),
|
||||
default=utc_now,
|
||||
nullable=False,
|
||||
)
|
||||
|
||||
chat: Mapped["Chat"] = relationship(
|
||||
back_populates="messages",
|
||||
)
|
||||
sender: Mapped["User"] = relationship(
|
||||
back_populates="messages",
|
||||
)
|
||||
|
||||
__table_args__ = (
|
||||
Index(
|
||||
"ix_messages_chat_created_id",
|
||||
"chat_id",
|
||||
"created_at",
|
||||
"id",
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
class Session(Base):
|
||||
__tablename__ = "sessions"
|
||||
|
||||
id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
primary_key=True,
|
||||
default=uuid.uuid4,
|
||||
)
|
||||
token_hash: Mapped[str] = mapped_column(
|
||||
String(64),
|
||||
unique=True,
|
||||
nullable=False,
|
||||
index=True,
|
||||
)
|
||||
user_id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
ForeignKey("users.id", ondelete="CASCADE"),
|
||||
nullable=False,
|
||||
)
|
||||
expires_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True),
|
||||
nullable=False,
|
||||
)
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True),
|
||||
default=utc_now,
|
||||
nullable=False,
|
||||
)
|
||||
|
||||
user: Mapped["User"] = relationship()
|
||||
|
||||
class IdempotencyRecord(Base):
|
||||
__tablename__ = "idempotency_records"
|
||||
|
||||
id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
|
||||
)
|
||||
user_id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True), ForeignKey("users.id", ondelete="CASCADE"), nullable=False
|
||||
)
|
||||
method: Mapped[str] = mapped_column(String(10), nullable=False)
|
||||
path: Mapped[str] = mapped_column(String(255), nullable=False)
|
||||
idempotency_key: Mapped[str] = mapped_column(String(128), nullable=False)
|
||||
request_hash: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||
# NULL is visible only inside the creating transaction; the message and
|
||||
# idempotency row are committed atomically.
|
||||
message_id: Mapped[uuid.UUID | None] = mapped_column(
|
||||
UUID(as_uuid=True), ForeignKey("messages.id", ondelete="CASCADE"), nullable=True
|
||||
)
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), default=utc_now, nullable=False
|
||||
)
|
||||
expires_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
||||
|
||||
__table_args__ = (
|
||||
UniqueConstraint(
|
||||
"user_id", "method", "path", "idempotency_key",
|
||||
name="uq_idempotency_scope_key",
|
||||
),
|
||||
Index("ix_idempotency_records_expires_at", "expires_at"),
|
||||
)
|
||||
@@ -0,0 +1,15 @@
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
from app.core.config import settings
|
||||
|
||||
engine = create_engine(
|
||||
settings.database_url,
|
||||
pool_pre_ping=True,
|
||||
pool_size=settings.db_pool_size,
|
||||
max_overflow=settings.db_max_overflow,
|
||||
pool_timeout=settings.db_pool_timeout,
|
||||
pool_recycle=1800,
|
||||
)
|
||||
|
||||
SessionLocal = sessionmaker(bind=engine, autoflush=False, autocommit=False)
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
import logging
|
||||
import uuid
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
from fastapi import FastAPI, HTTPException, Request
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from fastapi.responses import JSONResponse
|
||||
from pathlib import Path
|
||||
from sqlalchemy import text
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
|
||||
from app.api.auth import router as auth_router
|
||||
from app.api.users import router as users_router
|
||||
from app.api.chats import router as chats_router
|
||||
from app.api.messages import router as messages_router
|
||||
from app.core.config import settings
|
||||
from app.db.session import engine
|
||||
|
||||
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s %(message)s")
|
||||
logger = logging.getLogger(__name__)
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
logger.info("Application startup instance_id=%s", settings.instance_id)
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
engine.dispose()
|
||||
logger.info("Application shutdown instance_id=%s", settings.instance_id)
|
||||
|
||||
app = FastAPI(title=settings.app_name, version="1.3.0", lifespan=lifespan)
|
||||
app.include_router(users_router)
|
||||
app.include_router(auth_router)
|
||||
app.include_router(chats_router)
|
||||
app.include_router(messages_router)
|
||||
|
||||
@app.middleware("http")
|
||||
async def response_headers(request: Request, call_next):
|
||||
request_id = str(uuid.uuid4())
|
||||
request.state.request_id = request_id
|
||||
try:
|
||||
response = await call_next(request)
|
||||
except Exception:
|
||||
logger.exception("Unhandled request error; request_id=%s", request_id)
|
||||
response = JSONResponse(status_code=500, content={"error": {"code": "internal_error", "message": "Unexpected error", "request_id": request_id}})
|
||||
response.headers["X-Request-Id"] = request_id
|
||||
response.headers["X-Instance-Id"] = settings.instance_id
|
||||
response.headers["X-Content-Type-Options"] = "nosniff"
|
||||
return response
|
||||
|
||||
@app.exception_handler(HTTPException)
|
||||
async def http_exception_handler(request: Request, exc: HTTPException):
|
||||
request_id = getattr(request.state, "request_id", str(uuid.uuid4()))
|
||||
code = {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"}.get(exc.status_code, "internal_error")
|
||||
headers = dict(exc.headers or {})
|
||||
headers["X-Request-Id"] = request_id
|
||||
headers["X-Instance-Id"] = settings.instance_id
|
||||
return JSONResponse(status_code=exc.status_code, content={"error": {"code": code, "message": exc.detail if isinstance(exc.detail, str) else "Request failed", "request_id": request_id}}, headers=headers)
|
||||
|
||||
@app.exception_handler(RequestValidationError)
|
||||
async def validation_exception_handler(request: Request, exc: RequestValidationError):
|
||||
request_id = getattr(request.state, "request_id", str(uuid.uuid4()))
|
||||
return JSONResponse(status_code=400, content={"error": {"code": "invalid_request", "message": "Validation error", "request_id": request_id}}, headers={"X-Request-Id": request_id, "X-Instance-Id": settings.instance_id})
|
||||
|
||||
@app.get("/health/live")
|
||||
def liveness():
|
||||
return {"status": "ok"}
|
||||
|
||||
DRAIN_FILE = Path("/tmp/api-draining")
|
||||
@app.get("/health/ready")
|
||||
def readiness():
|
||||
if DRAIN_FILE.exists():
|
||||
raise HTTPException(
|
||||
status_code=503,
|
||||
detail="Instance is draining",
|
||||
)
|
||||
|
||||
try:
|
||||
with engine.connect() as connection:
|
||||
connection.execute(text("SELECT 1"))
|
||||
return {"status": "ok"}
|
||||
except SQLAlchemyError:
|
||||
logger.exception("PostgreSQL readiness check failed")
|
||||
raise HTTPException(
|
||||
status_code=503,
|
||||
detail="Database unavailable",
|
||||
) from None
|
||||
+110
@@ -0,0 +1,110 @@
|
||||
|
||||
from datetime import datetime, timezone
|
||||
from typing import List, Optional
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
||||
|
||||
|
||||
class BaseConfig(BaseModel):
|
||||
model_config = ConfigDict(
|
||||
from_attributes=True,
|
||||
json_encoders={
|
||||
datetime: lambda value: (
|
||||
value.astimezone(timezone.utc)
|
||||
.isoformat(timespec="microseconds")
|
||||
.replace("+00:00", "Z")
|
||||
)
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
class CreateUser(BaseConfig):
|
||||
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")
|
||||
@classmethod
|
||||
def validate_display_name(cls, value: str) -> str:
|
||||
value = value.strip()
|
||||
if not value:
|
||||
raise ValueError("display_name must not be blank")
|
||||
return value
|
||||
|
||||
|
||||
class User(BaseConfig):
|
||||
id: str
|
||||
username: str
|
||||
display_name: str
|
||||
created_at: datetime
|
||||
|
||||
|
||||
class CreateChat(BaseConfig):
|
||||
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 validate_title(cls, value: str) -> str:
|
||||
value = value.strip()
|
||||
if not value:
|
||||
raise ValueError("title must not be blank")
|
||||
return value
|
||||
|
||||
@field_validator("member_ids")
|
||||
@classmethod
|
||||
def validate_unique_members(cls, value: List[str]) -> List[str]:
|
||||
if len(value) != len(set(value)):
|
||||
raise ValueError("member_ids must be unique")
|
||||
return value
|
||||
|
||||
|
||||
class Chat(BaseConfig):
|
||||
id: str
|
||||
title: str
|
||||
member_ids: List[str]
|
||||
created_at: datetime
|
||||
|
||||
|
||||
class CreateMessage(BaseConfig):
|
||||
text: str = Field(..., min_length=1, max_length=4000)
|
||||
|
||||
@field_validator("text")
|
||||
@classmethod
|
||||
def validate_text(cls, value: str) -> str:
|
||||
value = value.strip()
|
||||
if not value:
|
||||
raise ValueError("text must not be blank")
|
||||
return value
|
||||
|
||||
|
||||
class Message(BaseConfig):
|
||||
id: str
|
||||
chat_id: str
|
||||
sender_id: str
|
||||
text: str
|
||||
created_at: datetime
|
||||
|
||||
|
||||
class Health(BaseConfig):
|
||||
status: str = "ok"
|
||||
|
||||
|
||||
class ErrorDetail(BaseConfig):
|
||||
code: str
|
||||
message: str
|
||||
request_id: str
|
||||
|
||||
|
||||
class ErrorResponse(BaseConfig):
|
||||
error: ErrorDetail
|
||||
|
||||
|
||||
class ChatPage(BaseConfig):
|
||||
items: List[Chat]
|
||||
next_cursor: Optional[str] = None
|
||||
|
||||
|
||||
class MessagePage(BaseConfig):
|
||||
items: List[Message]
|
||||
next_cursor: Optional[str] = None
|
||||
@@ -0,0 +1,71 @@
|
||||
import uuid
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session, selectinload
|
||||
|
||||
from app.db.models import Chat, ChatMember
|
||||
|
||||
|
||||
class ChatRepository:
|
||||
|
||||
def __init__(self, db: Session):
|
||||
self.db = db
|
||||
|
||||
def get_by_id(self, chat_id: uuid.UUID) -> Chat | None:
|
||||
statement = (
|
||||
select(Chat)
|
||||
.options(selectinload(Chat.members))
|
||||
.where(Chat.id == chat_id)
|
||||
)
|
||||
|
||||
return self.db.scalar(statement)
|
||||
|
||||
def get_by_user(
|
||||
self,
|
||||
user_id: uuid.UUID,
|
||||
limit: int = 50,
|
||||
) -> list[Chat]:
|
||||
statement = (
|
||||
select(Chat)
|
||||
.join(ChatMember, ChatMember.chat_id == Chat.id)
|
||||
.where(ChatMember.user_id == user_id)
|
||||
.order_by(Chat.created_at.asc(), Chat.id.asc())
|
||||
.limit(limit)
|
||||
)
|
||||
|
||||
return list(self.db.scalars(statement).all())
|
||||
|
||||
def is_member(
|
||||
self,
|
||||
chat_id: uuid.UUID,
|
||||
user_id: uuid.UUID,
|
||||
) -> bool:
|
||||
statement = select(ChatMember).where(
|
||||
ChatMember.chat_id == chat_id,
|
||||
ChatMember.user_id == user_id,
|
||||
)
|
||||
|
||||
return self.db.scalar(statement) is not None
|
||||
|
||||
def create(
|
||||
self,
|
||||
title: str,
|
||||
member_ids: list[uuid.UUID],
|
||||
) -> Chat:
|
||||
chat = Chat(title=title)
|
||||
|
||||
self.db.add(chat)
|
||||
self.db.flush()
|
||||
|
||||
memberships = [
|
||||
ChatMember(
|
||||
chat_id=chat.id,
|
||||
user_id=user_id,
|
||||
)
|
||||
for user_id in member_ids
|
||||
]
|
||||
|
||||
self.db.add_all(memberships)
|
||||
self.db.flush()
|
||||
|
||||
return chat
|
||||
@@ -0,0 +1,71 @@
|
||||
import uuid
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import and_, or_, select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.db.models import Message
|
||||
|
||||
|
||||
class MessageRepository:
|
||||
|
||||
def __init__(self, db: Session):
|
||||
self.db = db
|
||||
|
||||
def get_by_id(
|
||||
self,
|
||||
message_id: uuid.UUID,
|
||||
) -> Message | None:
|
||||
statement = select(Message).where(
|
||||
Message.id == message_id
|
||||
)
|
||||
|
||||
return self.db.scalar(statement)
|
||||
|
||||
def create(
|
||||
self,
|
||||
chat_id: uuid.UUID,
|
||||
sender_id: uuid.UUID,
|
||||
text: str,
|
||||
) -> Message:
|
||||
message = Message(
|
||||
chat_id=chat_id,
|
||||
sender_id=sender_id,
|
||||
text=text,
|
||||
)
|
||||
|
||||
self.db.add(message)
|
||||
self.db.flush()
|
||||
|
||||
return message
|
||||
|
||||
def get_history(
|
||||
self,
|
||||
chat_id: uuid.UUID,
|
||||
limit: int = 50,
|
||||
cursor_created_at: datetime | None = None,
|
||||
cursor_id: uuid.UUID | None = None,
|
||||
) -> list[Message]:
|
||||
statement = select(Message).where(
|
||||
Message.chat_id == chat_id
|
||||
)
|
||||
|
||||
if cursor_created_at is not None and cursor_id is not None:
|
||||
statement = statement.where(
|
||||
or_(
|
||||
Message.created_at > cursor_created_at,
|
||||
and_(
|
||||
Message.created_at == cursor_created_at,
|
||||
Message.id > cursor_id,
|
||||
),
|
||||
)
|
||||
)
|
||||
|
||||
statement = (
|
||||
statement
|
||||
.order_by(Message.created_at.asc(), Message.id.asc())
|
||||
.limit(limit)
|
||||
)
|
||||
|
||||
return list(self.db.scalars(statement).all())
|
||||
@@ -0,0 +1,78 @@
|
||||
import uuid
|
||||
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from sqlalchemy import delete, select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.db.models import Session as UserSession
|
||||
|
||||
|
||||
class SessionRepository:
|
||||
|
||||
def __init__(self, db: Session):
|
||||
self.db = db
|
||||
|
||||
def create(
|
||||
self,
|
||||
user_id: uuid.UUID,
|
||||
token_hash: str,
|
||||
expires_at: datetime,
|
||||
) -> UserSession:
|
||||
session = UserSession(
|
||||
user_id=user_id,
|
||||
token_hash=token_hash,
|
||||
expires_at=expires_at,
|
||||
)
|
||||
|
||||
self.db.add(session)
|
||||
self.db.flush()
|
||||
|
||||
return session
|
||||
|
||||
def get_by_token_hash(
|
||||
self,
|
||||
token_hash: str,
|
||||
) -> UserSession | None:
|
||||
statement = select(UserSession).where(
|
||||
UserSession.token_hash == token_hash
|
||||
)
|
||||
|
||||
return self.db.scalar(statement)
|
||||
|
||||
def get_valid_session(
|
||||
self,
|
||||
token_hash: str,
|
||||
) -> UserSession | None:
|
||||
now = datetime.now(timezone.utc)
|
||||
|
||||
statement = select(UserSession).where(
|
||||
UserSession.token_hash == token_hash,
|
||||
UserSession.expires_at > now,
|
||||
)
|
||||
|
||||
return self.db.scalar(statement)
|
||||
|
||||
def delete_by_token_hash(
|
||||
self,
|
||||
token_hash: str,
|
||||
) -> bool:
|
||||
statement = (
|
||||
delete(UserSession)
|
||||
.where(UserSession.token_hash == token_hash)
|
||||
.returning(UserSession.id)
|
||||
)
|
||||
|
||||
return self.db.scalar(statement) is not None
|
||||
|
||||
def delete_expired(self) -> int:
|
||||
now = datetime.now(timezone.utc)
|
||||
|
||||
statement = (
|
||||
delete(UserSession)
|
||||
.where(UserSession.expires_at <= now)
|
||||
)
|
||||
|
||||
result = self.db.execute(statement)
|
||||
|
||||
return result.rowcount
|
||||
@@ -0,0 +1,41 @@
|
||||
import uuid
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.db.models import User
|
||||
|
||||
|
||||
class UserRepository:
|
||||
|
||||
def __init__(self, db: Session):
|
||||
self.db = db
|
||||
|
||||
def get_by_id(self, user_id: uuid.UUID) -> User | None:
|
||||
statement = select(User).where(User.id == user_id)
|
||||
|
||||
return self.db.scalar(statement)
|
||||
|
||||
def get_by_username(self, username: str) -> User | None:
|
||||
statement = select(User).where(
|
||||
User.username == username
|
||||
)
|
||||
|
||||
return self.db.scalar(statement)
|
||||
|
||||
def create(
|
||||
self,
|
||||
username: str,
|
||||
display_name: str,
|
||||
password_hash: str,
|
||||
) -> User:
|
||||
user = User(
|
||||
username=username,
|
||||
display_name=display_name,
|
||||
password_hash=password_hash,
|
||||
)
|
||||
|
||||
self.db.add(user)
|
||||
self.db.flush()
|
||||
|
||||
return user
|
||||
@@ -0,0 +1,159 @@
|
||||
import hashlib
|
||||
import secrets
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
from redis import Redis
|
||||
from redis.backoff import NoBackoff
|
||||
from redis.exceptions import RedisError
|
||||
from redis.retry import Retry
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.config import settings
|
||||
from app.db.models import User
|
||||
from app.repositories.session_repository import SessionRepository
|
||||
from app.repositories.user_repository import UserRepository
|
||||
from app.services.user_service import password_hasher
|
||||
|
||||
|
||||
class InvalidCredentialsError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidSessionError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class AuthService:
|
||||
def __init__(self, db: Session):
|
||||
self.db = db
|
||||
self.sessions = SessionRepository(db)
|
||||
self.users = UserRepository(db)
|
||||
|
||||
self._redis_available = True
|
||||
|
||||
self.redis = Redis.from_url(
|
||||
settings.cache_url,
|
||||
decode_responses=True,
|
||||
socket_connect_timeout=0.5,
|
||||
socket_timeout=0.5,
|
||||
retry_on_timeout=False,
|
||||
retry=Retry(NoBackoff(), 0),
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _hash_token(token: str) -> str:
|
||||
return hashlib.sha256(token.encode("utf-8")).hexdigest()
|
||||
|
||||
def create_session(self, username: str, password: str) -> dict:
|
||||
user = self.users.get_by_username(username)
|
||||
|
||||
if user is None or not password_hasher.verify(
|
||||
password, user.password_hash
|
||||
):
|
||||
raise InvalidCredentialsError("Invalid username or password")
|
||||
|
||||
token = secrets.token_urlsafe(32)
|
||||
token_hash = self._hash_token(token)
|
||||
|
||||
expires_at = datetime.now(timezone.utc) + timedelta(
|
||||
seconds=settings.session_ttl_seconds
|
||||
)
|
||||
|
||||
try:
|
||||
self.sessions.create(
|
||||
user_id=user.id,
|
||||
token_hash=token_hash,
|
||||
expires_at=expires_at,
|
||||
)
|
||||
self.db.commit()
|
||||
except Exception:
|
||||
self.db.rollback()
|
||||
raise
|
||||
|
||||
self._cache_session(token_hash, user.id, expires_at)
|
||||
|
||||
return {
|
||||
"token": token,
|
||||
"token_type": "Bearer",
|
||||
"expires_at": expires_at,
|
||||
"user": user,
|
||||
}
|
||||
|
||||
def _cache_session(
|
||||
self,
|
||||
token_hash: str,
|
||||
user_id,
|
||||
expires_at,
|
||||
) -> None:
|
||||
if not self._redis_available:
|
||||
return
|
||||
|
||||
ttl = max(
|
||||
1,
|
||||
int((expires_at - datetime.now(timezone.utc)).total_seconds()),
|
||||
)
|
||||
|
||||
try:
|
||||
self.redis.setex(
|
||||
f"session:{token_hash}",
|
||||
ttl,
|
||||
str(user_id),
|
||||
)
|
||||
except RedisError:
|
||||
self._redis_available = False
|
||||
|
||||
def authenticate(self, token: str) -> User:
|
||||
token_hash = self._hash_token(token)
|
||||
cached_user_id = None
|
||||
|
||||
if self._redis_available:
|
||||
try:
|
||||
cached_user_id = self.redis.get(
|
||||
f"session:{token_hash}"
|
||||
)
|
||||
except RedisError:
|
||||
self._redis_available = False
|
||||
|
||||
session = self.sessions.get_valid_session(token_hash)
|
||||
|
||||
if session is None:
|
||||
if self._redis_available:
|
||||
try:
|
||||
self.redis.delete(f"session:{token_hash}")
|
||||
except RedisError:
|
||||
self._redis_available = False
|
||||
|
||||
raise InvalidSessionError(
|
||||
"Session is invalid or expired"
|
||||
)
|
||||
|
||||
if cached_user_id is None:
|
||||
self._cache_session(
|
||||
token_hash,
|
||||
session.user_id,
|
||||
session.expires_at,
|
||||
)
|
||||
|
||||
return session.user
|
||||
|
||||
def delete_session(self, token: str) -> None:
|
||||
token_hash = self._hash_token(token)
|
||||
|
||||
try:
|
||||
deleted = self.sessions.delete_by_token_hash(token_hash)
|
||||
|
||||
if not deleted:
|
||||
raise InvalidSessionError(
|
||||
"Session is invalid or expired"
|
||||
)
|
||||
|
||||
self.db.commit()
|
||||
except Exception:
|
||||
self.db.rollback()
|
||||
raise
|
||||
|
||||
if self._redis_available:
|
||||
try:
|
||||
self.redis.delete(f"session:{token_hash}")
|
||||
except RedisError:
|
||||
self._redis_available = False
|
||||
@@ -0,0 +1,94 @@
|
||||
import uuid
|
||||
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.db.models import Chat
|
||||
from app.repositories.chat_repository import ChatRepository
|
||||
from app.repositories.user_repository import UserRepository
|
||||
|
||||
|
||||
class ChatNotFoundError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class ChatAccessError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidChatDataError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class ChatService:
|
||||
def __init__(self, db: Session):
|
||||
self.db = db
|
||||
self.chats = ChatRepository(db)
|
||||
self.users = UserRepository(db)
|
||||
|
||||
def create_chat(
|
||||
self,
|
||||
title: str,
|
||||
member_ids: list[str],
|
||||
creator_id: uuid.UUID,
|
||||
) -> Chat:
|
||||
title = title.strip()
|
||||
|
||||
if not title or len(title) > 100:
|
||||
raise InvalidChatDataError("Invalid chat title")
|
||||
|
||||
if not member_ids or len(member_ids) > 100:
|
||||
raise InvalidChatDataError("Invalid members")
|
||||
|
||||
try:
|
||||
parsed_ids = [uuid.UUID(str(value)) for value in member_ids]
|
||||
except (ValueError, TypeError, AttributeError):
|
||||
raise InvalidChatDataError("Invalid member UUID") from None
|
||||
|
||||
if len(parsed_ids) != len(set(parsed_ids)):
|
||||
raise InvalidChatDataError("Duplicate members")
|
||||
|
||||
if creator_id not in parsed_ids:
|
||||
raise InvalidChatDataError("Creator must be a member")
|
||||
|
||||
for user_id in parsed_ids:
|
||||
if self.users.get_by_id(user_id) is None:
|
||||
raise InvalidChatDataError("Unknown member")
|
||||
|
||||
try:
|
||||
chat = self.chats.create(
|
||||
title=title,
|
||||
member_ids=parsed_ids,
|
||||
)
|
||||
self.db.commit()
|
||||
self.db.refresh(chat)
|
||||
return chat
|
||||
|
||||
except IntegrityError as exc:
|
||||
self.db.rollback()
|
||||
raise InvalidChatDataError("Unable to create chat") from exc
|
||||
|
||||
except Exception:
|
||||
self.db.rollback()
|
||||
raise
|
||||
|
||||
def get_chat(self, chat_id: uuid.UUID, user_id: uuid.UUID) -> Chat:
|
||||
chat = self.chats.get_by_id(chat_id)
|
||||
|
||||
if chat is None:
|
||||
raise ChatNotFoundError()
|
||||
|
||||
if not self.chats.is_member(chat_id, user_id):
|
||||
raise ChatNotFoundError()
|
||||
|
||||
return chat
|
||||
|
||||
def list_chats(self, user_id: uuid.UUID) -> list[Chat]:
|
||||
return self.chats.get_by_user(user_id)
|
||||
|
||||
def check_membership(
|
||||
self,
|
||||
chat_id: uuid.UUID,
|
||||
user_id: uuid.UUID,
|
||||
) -> bool:
|
||||
return self.chats.is_member(chat_id, user_id)
|
||||
@@ -0,0 +1,418 @@
|
||||
import base64
|
||||
import hashlib
|
||||
import json
|
||||
import time
|
||||
import uuid
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from types import SimpleNamespace
|
||||
|
||||
from redis import Redis
|
||||
from redis.backoff import NoBackoff
|
||||
from redis.exceptions import RedisError
|
||||
from redis.retry import Retry
|
||||
from sqlalchemy import delete, select, update
|
||||
from sqlalchemy.dialects.postgresql import insert as pg_insert
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.config import settings
|
||||
from app.db.models import IdempotencyRecord, Message
|
||||
from app.repositories.chat_repository import ChatRepository
|
||||
from app.repositories.message_repository import MessageRepository
|
||||
|
||||
|
||||
class MessageNotFoundError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class MessageAccessError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidMessageDataError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class IdempotencyConflictError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class MessageService:
|
||||
def __init__(self, db: Session):
|
||||
self.db = db
|
||||
self.messages = MessageRepository(db)
|
||||
self.chats = ChatRepository(db)
|
||||
|
||||
self._redis_available = True
|
||||
|
||||
self.redis = Redis.from_url(
|
||||
settings.cache_url,
|
||||
decode_responses=True,
|
||||
socket_connect_timeout=0.5,
|
||||
socket_timeout=0.5,
|
||||
retry_on_timeout=False,
|
||||
retry=Retry(NoBackoff(), 0),
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def encode_cursor(message: Message) -> str:
|
||||
payload = {
|
||||
"created_at": message.created_at.isoformat(),
|
||||
"id": str(message.id),
|
||||
}
|
||||
|
||||
raw = json.dumps(payload).encode("utf-8")
|
||||
|
||||
return (
|
||||
base64.urlsafe_b64encode(raw)
|
||||
.decode("ascii")
|
||||
.rstrip("=")
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def decode_cursor(cursor: str | None):
|
||||
if not cursor:
|
||||
return None, None
|
||||
|
||||
try:
|
||||
padded = cursor + "=" * (-len(cursor) % 4)
|
||||
raw = base64.urlsafe_b64decode(padded.encode("ascii"))
|
||||
payload = json.loads(raw.decode("utf-8"))
|
||||
|
||||
created_at = datetime.fromisoformat(payload["created_at"])
|
||||
message_id = uuid.UUID(payload["id"])
|
||||
|
||||
if created_at.tzinfo is None:
|
||||
raise ValueError("Cursor timestamp must include timezone")
|
||||
|
||||
return created_at, message_id
|
||||
|
||||
except (ValueError, TypeError, KeyError, UnicodeError):
|
||||
raise InvalidMessageDataError("Invalid cursor") from None
|
||||
|
||||
def _version_key(self, chat_id: uuid.UUID) -> str:
|
||||
return f"messages:version:{chat_id}"
|
||||
|
||||
def _cache_key(
|
||||
self,
|
||||
chat_id: uuid.UUID,
|
||||
version: int,
|
||||
limit: int,
|
||||
cursor: str | None,
|
||||
) -> str:
|
||||
cursor_hash = hashlib.sha256(
|
||||
(cursor or "first-page").encode("utf-8")
|
||||
).hexdigest()[:16]
|
||||
|
||||
return (
|
||||
f"messages:{chat_id}:v{version}:"
|
||||
f"limit:{limit}:cursor:{cursor_hash}"
|
||||
)
|
||||
|
||||
def _get_version(self, chat_id: uuid.UUID) -> int:
|
||||
if not self._redis_available:
|
||||
return 0
|
||||
|
||||
try:
|
||||
value = self.redis.get(self._version_key(chat_id))
|
||||
return int(value or 0)
|
||||
except (RedisError, ValueError):
|
||||
self._redis_available = False
|
||||
return 0
|
||||
|
||||
def _bump_version(self, chat_id: uuid.UUID) -> None:
|
||||
if not self._redis_available:
|
||||
return
|
||||
|
||||
try:
|
||||
self.redis.incr(self._version_key(chat_id))
|
||||
except RedisError:
|
||||
self._redis_available = False
|
||||
|
||||
@staticmethod
|
||||
def _serialize(messages, next_cursor):
|
||||
return json.dumps({
|
||||
"items": [
|
||||
{
|
||||
"id": str(message.id),
|
||||
"chat_id": str(message.chat_id),
|
||||
"sender_id": str(message.sender_id),
|
||||
"text": message.text,
|
||||
"created_at": message.created_at.isoformat(),
|
||||
}
|
||||
for message in messages
|
||||
],
|
||||
"next_cursor": next_cursor,
|
||||
})
|
||||
|
||||
@staticmethod
|
||||
def _deserialize(payload: str):
|
||||
data = json.loads(payload)
|
||||
|
||||
messages = [
|
||||
SimpleNamespace(
|
||||
id=uuid.UUID(item["id"]),
|
||||
chat_id=uuid.UUID(item["chat_id"]),
|
||||
sender_id=uuid.UUID(item["sender_id"]),
|
||||
text=item["text"],
|
||||
created_at=datetime.fromisoformat(item["created_at"]),
|
||||
)
|
||||
for item in data["items"]
|
||||
]
|
||||
|
||||
return messages, data["next_cursor"]
|
||||
|
||||
def _read_cache(self, key: str):
|
||||
if not self._redis_available:
|
||||
return None
|
||||
|
||||
try:
|
||||
payload = self.redis.get(key)
|
||||
|
||||
if payload is None:
|
||||
return None
|
||||
|
||||
return self._deserialize(payload)
|
||||
|
||||
except RedisError:
|
||||
self._redis_available = False
|
||||
return None
|
||||
except (ValueError, TypeError, KeyError):
|
||||
return None
|
||||
|
||||
def _load_from_database(
|
||||
self,
|
||||
chat_id,
|
||||
limit,
|
||||
cursor_created_at,
|
||||
cursor_id,
|
||||
):
|
||||
rows = self.messages.get_history(
|
||||
chat_id=chat_id,
|
||||
limit=limit + 1,
|
||||
cursor_created_at=cursor_created_at,
|
||||
cursor_id=cursor_id,
|
||||
)
|
||||
|
||||
has_more = len(rows) > limit
|
||||
items = rows[:limit]
|
||||
|
||||
next_cursor = (
|
||||
self.encode_cursor(items[-1])
|
||||
if has_more and items
|
||||
else None
|
||||
)
|
||||
|
||||
return items, next_cursor
|
||||
|
||||
def create_message(
|
||||
self,
|
||||
chat_id: uuid.UUID,
|
||||
sender_id: uuid.UUID,
|
||||
text: str,
|
||||
idempotency_key: str,
|
||||
) -> Message:
|
||||
text = text.strip()
|
||||
if not text or len(text) > 4000:
|
||||
raise InvalidMessageDataError("Invalid message text")
|
||||
|
||||
# Access is checked for every retry, before consulting the key.
|
||||
if not self.chats.is_member(chat_id, sender_id):
|
||||
raise MessageNotFoundError()
|
||||
|
||||
method = "POST"
|
||||
path = f"/api/v1/chats/{chat_id}/messages"
|
||||
request_hash = hashlib.sha256(
|
||||
json.dumps({"text": text}, sort_keys=True, separators=(",", ":")).encode("utf-8")
|
||||
).hexdigest()
|
||||
now = datetime.now(timezone.utc)
|
||||
expires_at = now + timedelta(seconds=max(settings.idempotency_ttl_seconds, 86400))
|
||||
scope = (
|
||||
IdempotencyRecord.user_id == sender_id,
|
||||
IdempotencyRecord.method == method,
|
||||
IdempotencyRecord.path == path,
|
||||
IdempotencyRecord.idempotency_key == idempotency_key,
|
||||
)
|
||||
|
||||
try:
|
||||
# An expired key may be reused. Deletion and claim are serialized by
|
||||
# PostgreSQL's unique constraint for the scope.
|
||||
self.db.execute(
|
||||
delete(IdempotencyRecord).where(*scope, IdempotencyRecord.expires_at <= now)
|
||||
)
|
||||
claim = (
|
||||
pg_insert(IdempotencyRecord)
|
||||
.values(
|
||||
id=uuid.uuid4(), user_id=sender_id, method=method, path=path,
|
||||
idempotency_key=idempotency_key, request_hash=request_hash,
|
||||
message_id=None, created_at=now, expires_at=expires_at,
|
||||
)
|
||||
.on_conflict_do_nothing(constraint="uq_idempotency_scope_key")
|
||||
.returning(IdempotencyRecord.id)
|
||||
)
|
||||
claimed_id = self.db.execute(claim).scalar_one_or_none()
|
||||
|
||||
if claimed_id is None:
|
||||
existing = self.db.execute(
|
||||
select(IdempotencyRecord).where(*scope).with_for_update()
|
||||
).scalar_one_or_none()
|
||||
if existing is None:
|
||||
# Rare expiration race: retry once in a fresh transaction.
|
||||
self.db.rollback()
|
||||
return self.create_message(chat_id, sender_id, text, idempotency_key)
|
||||
if existing.request_hash != request_hash:
|
||||
self.db.rollback()
|
||||
raise IdempotencyConflictError()
|
||||
if existing.message_id is None:
|
||||
raise RuntimeError("Committed idempotency record has no message")
|
||||
message = self.db.get(Message, existing.message_id)
|
||||
if message is None:
|
||||
raise RuntimeError("Idempotency record references a missing message")
|
||||
self.db.commit()
|
||||
return message
|
||||
|
||||
message = self.messages.create(
|
||||
chat_id=chat_id, sender_id=sender_id, text=text
|
||||
)
|
||||
self.db.execute(
|
||||
update(IdempotencyRecord)
|
||||
.where(IdempotencyRecord.id == claimed_id)
|
||||
.values(message_id=message.id)
|
||||
)
|
||||
self.db.commit()
|
||||
self.db.refresh(message)
|
||||
self._bump_version(chat_id)
|
||||
return message
|
||||
except IdempotencyConflictError:
|
||||
raise
|
||||
except Exception:
|
||||
self.db.rollback()
|
||||
raise
|
||||
|
||||
def get_history(
|
||||
self,
|
||||
chat_id: uuid.UUID,
|
||||
user_id: uuid.UUID,
|
||||
limit: int = 50,
|
||||
cursor: str | None = None,
|
||||
):
|
||||
if not 1 <= limit <= 100:
|
||||
raise InvalidMessageDataError(
|
||||
"Limit must be between 1 and 100"
|
||||
)
|
||||
|
||||
if not self.chats.is_member(chat_id, user_id):
|
||||
raise MessageNotFoundError()
|
||||
|
||||
cursor_created_at, cursor_id = self.decode_cursor(cursor)
|
||||
|
||||
version = self._get_version(chat_id)
|
||||
|
||||
cache_key = self._cache_key(
|
||||
chat_id,
|
||||
version,
|
||||
limit,
|
||||
cursor,
|
||||
)
|
||||
|
||||
cached = self._read_cache(cache_key)
|
||||
|
||||
if cached is not None:
|
||||
return cached
|
||||
|
||||
if not self._redis_available:
|
||||
return self._load_from_database(
|
||||
chat_id,
|
||||
limit,
|
||||
cursor_created_at,
|
||||
cursor_id,
|
||||
)
|
||||
|
||||
lock_key = f"lock:{cache_key}"
|
||||
lock_token = str(uuid.uuid4())
|
||||
|
||||
try:
|
||||
acquired = self.redis.set(
|
||||
lock_key,
|
||||
lock_token,
|
||||
nx=True,
|
||||
ex=10,
|
||||
)
|
||||
except RedisError:
|
||||
self._redis_available = False
|
||||
return self._load_from_database(
|
||||
chat_id,
|
||||
limit,
|
||||
cursor_created_at,
|
||||
cursor_id,
|
||||
)
|
||||
|
||||
if not acquired:
|
||||
for _ in range(10):
|
||||
time.sleep(0.05)
|
||||
|
||||
cached = self._read_cache(cache_key)
|
||||
|
||||
if cached is not None:
|
||||
return cached
|
||||
|
||||
if not self._redis_available:
|
||||
break
|
||||
|
||||
return self._load_from_database(
|
||||
chat_id,
|
||||
limit,
|
||||
cursor_created_at,
|
||||
cursor_id,
|
||||
)
|
||||
|
||||
try:
|
||||
cached = self._read_cache(cache_key)
|
||||
|
||||
if cached is not None:
|
||||
return cached
|
||||
|
||||
if not self._redis_available:
|
||||
return self._load_from_database(
|
||||
chat_id,
|
||||
limit,
|
||||
cursor_created_at,
|
||||
cursor_id,
|
||||
)
|
||||
|
||||
items, next_cursor = self._load_from_database(
|
||||
chat_id,
|
||||
limit,
|
||||
cursor_created_at,
|
||||
cursor_id,
|
||||
)
|
||||
|
||||
if self._redis_available and self._get_version(chat_id) == version:
|
||||
try:
|
||||
self.redis.setex(
|
||||
cache_key,
|
||||
settings.message_cache_ttl_seconds,
|
||||
self._serialize(items, next_cursor),
|
||||
)
|
||||
except RedisError:
|
||||
self._redis_available = False
|
||||
|
||||
return items, next_cursor
|
||||
|
||||
finally:
|
||||
if self._redis_available:
|
||||
try:
|
||||
self.redis.eval(
|
||||
"""
|
||||
if redis.call('get', KEYS[1]) == ARGV[1]
|
||||
then
|
||||
return redis.call('del', KEYS[1])
|
||||
else
|
||||
return 0
|
||||
end
|
||||
""",
|
||||
1,
|
||||
lock_key,
|
||||
lock_token,
|
||||
)
|
||||
except RedisError:
|
||||
self._redis_available = False
|
||||
@@ -0,0 +1,81 @@
|
||||
import re
|
||||
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.orm import Session
|
||||
from pwdlib import PasswordHash
|
||||
|
||||
from app.db.models import User
|
||||
from app.repositories.user_repository import UserRepository
|
||||
|
||||
|
||||
password_hasher = PasswordHash.recommended()
|
||||
|
||||
|
||||
class UsernameAlreadyExistsError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class UserService:
|
||||
|
||||
def __init__(self, db: Session):
|
||||
self.db = db
|
||||
self.users = UserRepository(db)
|
||||
|
||||
def register(
|
||||
self,
|
||||
username: str,
|
||||
display_name: str,
|
||||
password: str,
|
||||
) -> User:
|
||||
|
||||
if not re.fullmatch(r"[a-z0-9_]{3,32}", username):
|
||||
raise ValueError("Invalid username")
|
||||
|
||||
if not display_name or not display_name.strip():
|
||||
raise ValueError("Display name cannot be empty")
|
||||
|
||||
if len(display_name) > 100:
|
||||
raise ValueError("Display name is too long")
|
||||
|
||||
if not 12 <= len(password) <= 128:
|
||||
raise ValueError("Invalid password length")
|
||||
|
||||
if self.users.get_by_username(username) is not None:
|
||||
raise UsernameAlreadyExistsError(
|
||||
"Username already exists"
|
||||
)
|
||||
|
||||
password_hash = password_hasher.hash(password)
|
||||
|
||||
try:
|
||||
user = self.users.create(
|
||||
username=username,
|
||||
display_name=display_name,
|
||||
password_hash=password_hash,
|
||||
)
|
||||
|
||||
self.db.commit()
|
||||
self.db.refresh(user)
|
||||
|
||||
return user
|
||||
|
||||
except IntegrityError as exc:
|
||||
self.db.rollback()
|
||||
|
||||
raise UsernameAlreadyExistsError(
|
||||
"Username already exists"
|
||||
) from exc
|
||||
|
||||
except Exception:
|
||||
self.db.rollback()
|
||||
raise
|
||||
|
||||
def verify_password(
|
||||
self,
|
||||
user: User,
|
||||
password: str,
|
||||
) -> bool:
|
||||
return password_hasher.verify(
|
||||
password,
|
||||
user.password_hash,
|
||||
)
|
||||
+193
@@ -0,0 +1,193 @@
|
||||
import base64
|
||||
import bisect
|
||||
import binascii
|
||||
import json
|
||||
import os
|
||||
import threading
|
||||
import uuid
|
||||
from datetime import datetime, timezone
|
||||
from typing import Dict, List, Optional, Tuple
|
||||
|
||||
from .models import Chat, Message, User
|
||||
|
||||
|
||||
Key = Tuple[datetime, str]
|
||||
|
||||
|
||||
def _encode_cursor(kind: str, scope: str, item) -> str:
|
||||
payload = {
|
||||
"v": 1,
|
||||
"kind": kind,
|
||||
"scope": scope,
|
||||
"created_at": item.created_at.isoformat(),
|
||||
"id": item.id,
|
||||
}
|
||||
raw = json.dumps(payload, separators=(",", ":")).encode("utf-8")
|
||||
return base64.urlsafe_b64encode(raw).decode("ascii").rstrip("=")
|
||||
|
||||
|
||||
def _decode_cursor(cursor: str, kind: str, scope: str) -> Key:
|
||||
try:
|
||||
if not cursor or len(cursor) > 4096:
|
||||
raise ValueError
|
||||
padded = cursor + "=" * (-len(cursor) % 4)
|
||||
raw = base64.b64decode(padded, altchars=b"-_", validate=True)
|
||||
payload = json.loads(raw.decode("utf-8"))
|
||||
if not isinstance(payload, dict) or payload.get("v") != 1:
|
||||
raise ValueError
|
||||
if payload.get("kind") != kind or payload.get("scope") != scope:
|
||||
raise ValueError
|
||||
item_id = payload.get("id")
|
||||
timestamp = payload.get("created_at")
|
||||
if not isinstance(item_id, str) or not isinstance(timestamp, str):
|
||||
raise ValueError
|
||||
created_at = datetime.fromisoformat(timestamp)
|
||||
if created_at.tzinfo is None:
|
||||
raise ValueError
|
||||
created_at = created_at.astimezone(timezone.utc)
|
||||
return created_at, item_id
|
||||
except (
|
||||
ValueError,
|
||||
TypeError,
|
||||
KeyError,
|
||||
UnicodeDecodeError,
|
||||
json.JSONDecodeError,
|
||||
binascii.Error,
|
||||
):
|
||||
raise ValueError("invalid_request") from None
|
||||
|
||||
|
||||
class InMemoryStore:
|
||||
def __init__(self):
|
||||
self._lock = threading.RLock()
|
||||
self.users: Dict[str, User] = {}
|
||||
self.chats: Dict[str, Chat] = {}
|
||||
self.messages: Dict[str, List[Message]] = {}
|
||||
self.usernames: Dict[str, str] = {}
|
||||
self.message_keys: Dict[str, List[Key]] = {}
|
||||
self.message_index_enabled = os.getenv("MESSAGE_INDEX_ENABLED", "1") != "0"
|
||||
|
||||
def create_user(self, username: str, display_name: str) -> User:
|
||||
with self._lock:
|
||||
if username in self.usernames:
|
||||
raise ValueError("conflict")
|
||||
user_id = str(uuid.uuid4())
|
||||
user = User(
|
||||
id=user_id,
|
||||
username=username,
|
||||
display_name=display_name,
|
||||
created_at=datetime.now(timezone.utc),
|
||||
)
|
||||
self.users[user_id] = user
|
||||
self.usernames[username] = user_id
|
||||
return user
|
||||
|
||||
def get_user(self, user_id: str) -> Optional[User]:
|
||||
with self._lock:
|
||||
return self.users.get(user_id)
|
||||
|
||||
def create_chat(self, title: str, member_ids: List[str], creator_id: str) -> Chat:
|
||||
with self._lock:
|
||||
if creator_id not in member_ids or len(member_ids) != len(set(member_ids)):
|
||||
raise ValueError("invalid_request")
|
||||
if any(member_id not in self.users for member_id in member_ids):
|
||||
raise ValueError("invalid_request")
|
||||
|
||||
chat_id = str(uuid.uuid4())
|
||||
chat = Chat(
|
||||
id=chat_id,
|
||||
title=title,
|
||||
member_ids=list(member_ids),
|
||||
created_at=datetime.now(timezone.utc),
|
||||
)
|
||||
self.chats[chat_id] = chat
|
||||
self.messages[chat_id] = []
|
||||
self.message_keys[chat_id] = []
|
||||
return chat
|
||||
|
||||
def get_chat(self, chat_id: str, user_id: str) -> Optional[Chat]:
|
||||
with self._lock:
|
||||
chat = self.chats.get(chat_id)
|
||||
if chat is None or user_id not in chat.member_ids:
|
||||
return None
|
||||
return chat
|
||||
|
||||
def list_chats(
|
||||
self, user_id: str, limit: int, cursor: Optional[str]
|
||||
) -> Tuple[List[Chat], Optional[str]]:
|
||||
with self._lock:
|
||||
user_chats = [chat for chat in self.chats.values() if user_id in chat.member_ids]
|
||||
user_chats.sort(key=lambda chat: (chat.created_at, chat.id))
|
||||
keys = [(chat.created_at, chat.id) for chat in user_chats]
|
||||
|
||||
start_index = 0
|
||||
if cursor:
|
||||
target_key = _decode_cursor(cursor, kind="chats", scope=user_id)
|
||||
start_index = bisect.bisect_right(keys, target_key)
|
||||
|
||||
page_with_extra = user_chats[start_index : start_index + limit + 1]
|
||||
has_next = len(page_with_extra) > limit
|
||||
page = page_with_extra[:limit]
|
||||
next_cursor = None
|
||||
if has_next and page:
|
||||
next_cursor = _encode_cursor("chats", user_id, page[-1])
|
||||
return page, next_cursor
|
||||
|
||||
def create_message(self, chat_id: str, sender_id: str, text: str) -> Message:
|
||||
with self._lock:
|
||||
chat = self.chats.get(chat_id)
|
||||
if chat is None or sender_id not in chat.member_ids:
|
||||
raise ValueError("not_found")
|
||||
|
||||
message = Message(
|
||||
id=str(uuid.uuid4()),
|
||||
chat_id=chat_id,
|
||||
sender_id=sender_id,
|
||||
text=text,
|
||||
created_at=datetime.now(timezone.utc),
|
||||
)
|
||||
messages = self.messages[chat_id]
|
||||
keys = self.message_keys[chat_id]
|
||||
key = (message.created_at, message.id)
|
||||
|
||||
if self.message_index_enabled:
|
||||
index = bisect.bisect_right(keys, key)
|
||||
keys.insert(index, key)
|
||||
messages.insert(index, message)
|
||||
else:
|
||||
current_keys = [(item.created_at, item.id) for item in messages]
|
||||
index = bisect.bisect_right(current_keys, key)
|
||||
messages.insert(index, message)
|
||||
keys.insert(index, key)
|
||||
return message
|
||||
|
||||
def list_messages(
|
||||
self,
|
||||
chat_id: str,
|
||||
user_id: str,
|
||||
limit: int,
|
||||
cursor: Optional[str],
|
||||
) -> Tuple[List[Message], Optional[str]]:
|
||||
with self._lock:
|
||||
chat = self.chats.get(chat_id)
|
||||
if chat is None or user_id not in chat.member_ids:
|
||||
raise ValueError("not_found")
|
||||
|
||||
messages = self.messages[chat_id]
|
||||
if self.message_index_enabled:
|
||||
keys = self.message_keys[chat_id]
|
||||
else:
|
||||
keys = [(item.created_at, item.id) for item in messages]
|
||||
|
||||
start_index = 0
|
||||
if cursor:
|
||||
target_key = _decode_cursor(cursor, kind="messages", scope=chat_id)
|
||||
start_index = bisect.bisect_right(keys, target_key)
|
||||
|
||||
page_with_extra = messages[start_index : start_index + limit + 1]
|
||||
has_next = len(page_with_extra) > limit
|
||||
page = page_with_extra[:limit]
|
||||
next_cursor = None
|
||||
if has_next and page:
|
||||
next_cursor = _encode_cursor("messages", chat_id, page[-1])
|
||||
return page, next_cursor
|
||||
Reference in New Issue
Block a user