|
30 | 30 | ) |
31 | 31 | from app.db.models import ChatConversation, ChatMessage, User |
32 | 32 | from app.schemas.chat import ChatConversationResponse, ChatMessageResponse |
| 33 | +from app.services.common import sanitize_pagination |
33 | 34 |
|
34 | 35 | logger = get_logger(__name__) |
35 | 36 |
|
36 | | - |
37 | | -def _sanitize_pagination(*, limit: int | None, offset: int | None) -> tuple[int, int]: |
38 | | - safe_limit = settings.DEFAULT_LIST_LIMIT if limit is None else limit |
39 | | - safe_limit = max(1, min(safe_limit, settings.MAX_LIST_LIMIT)) |
40 | | - safe_offset = 0 if offset is None else max(0, offset) |
41 | | - return safe_limit, safe_offset |
42 | | - |
43 | | - |
44 | 37 | def _conversation_list_cache_key(*, user_id: str, company_id: str) -> str: |
45 | 38 | return f"cache:chat:conversations:{company_id}:{user_id}" |
46 | 39 |
|
@@ -270,7 +263,7 @@ async def list_conversations( |
270 | 263 | detail="You do not have permission to perform this action.", |
271 | 264 | ) |
272 | 265 |
|
273 | | - safe_limit, safe_offset = _sanitize_pagination(limit=limit, offset=offset) |
| 266 | + safe_limit, safe_offset = sanitize_pagination(limit=limit, offset=offset) |
274 | 267 | cache_key = f"{_conversation_list_cache_key(user_id=current_user.id, company_id=current_user.company_id)}:{safe_limit}:{safe_offset}" |
275 | 268 | cached = await cache_get_json(key=cache_key) |
276 | 269 | if isinstance(cached, list): |
@@ -314,7 +307,7 @@ async def get_conversation_messages( |
314 | 307 | """Return persisted messages for one user-scoped conversation.""" |
315 | 308 | conversation = await _get_scoped_conversation(conversation_id=conversation_id, current_user=current_user, db=db) |
316 | 309 |
|
317 | | - safe_limit, safe_offset = _sanitize_pagination(limit=limit, offset=offset) |
| 310 | + safe_limit, safe_offset = sanitize_pagination(limit=limit, offset=offset) |
318 | 311 | cache_key = f"{_conversation_messages_cache_key(conversation_id=conversation.id)}:{safe_limit}:{safe_offset}" |
319 | 312 | cached = await cache_get_json(key=cache_key) |
320 | 313 | if isinstance(cached, list): |
|
0 commit comments