mirror of
https://github.com/ipnet-mesh/meshcore-hub.git
synced 2026-05-06 21:42:40 +02:00
fix: Handle empty channel_idx parameter in messages filter
Fixed parse error when clicking the filter button on messages screen with "All Channels" selected. The form was sending an empty string for channel_idx, but FastAPI expected either a valid integer or None. Changes: - Accept channel_idx as string in query parameter - Parse and validate channel_idx before passing to API - Treat empty strings as None to prevent validation errors - Add error handling for invalid integer values Fixes #25 Co-authored-by: JingleManSweep <jinglemansweep@users.noreply.github.com>
This commit is contained in:
@@ -15,7 +15,7 @@ router = APIRouter()
|
||||
async def messages_list(
|
||||
request: Request,
|
||||
message_type: str | None = Query(None, description="Filter by message type"),
|
||||
channel_idx: int | None = Query(None, description="Filter by channel"),
|
||||
channel_idx: str | None = Query(None, description="Filter by channel"),
|
||||
search: str | None = Query(None, description="Search in message text"),
|
||||
page: int = Query(1, ge=1, description="Page number"),
|
||||
limit: int = Query(50, ge=1, le=100, description="Items per page"),
|
||||
@@ -28,12 +28,20 @@ async def messages_list(
|
||||
# Calculate offset
|
||||
offset = (page - 1) * limit
|
||||
|
||||
# Parse channel_idx, treating empty string as None
|
||||
channel_idx_int: int | None = None
|
||||
if channel_idx and channel_idx.strip():
|
||||
try:
|
||||
channel_idx_int = int(channel_idx)
|
||||
except ValueError:
|
||||
logger.warning(f"Invalid channel_idx value: {channel_idx}")
|
||||
|
||||
# Build query params
|
||||
params: dict[str, int | str] = {"limit": limit, "offset": offset}
|
||||
if message_type:
|
||||
params["message_type"] = message_type
|
||||
if channel_idx is not None:
|
||||
params["channel_idx"] = channel_idx
|
||||
if channel_idx_int is not None:
|
||||
params["channel_idx"] = channel_idx_int
|
||||
|
||||
# Fetch messages from API
|
||||
messages = []
|
||||
@@ -62,7 +70,7 @@ async def messages_list(
|
||||
"limit": limit,
|
||||
"total_pages": total_pages,
|
||||
"message_type": message_type or "",
|
||||
"channel_idx": channel_idx,
|
||||
"channel_idx": channel_idx_int,
|
||||
"search": search or "",
|
||||
}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user