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