message changes

This commit is contained in:
Michael Woods
2025-12-21 21:56:31 -05:00
parent 0a82c4b90c
commit e76b19998d

View File

@@ -1,5 +1,5 @@
# packetserver/http/routers/messages.py # packetserver/http/routers/messages.py
from fastapi import APIRouter, Depends, Query, HTTPException from fastapi import APIRouter, Depends, Query, HTTPException, Path
from typing import Optional from typing import Optional
from datetime import datetime from datetime import datetime
from persistent.mapping import PersistentMapping from persistent.mapping import PersistentMapping
@@ -63,3 +63,48 @@ async def get_messages(
messages.sort(key=lambda m: m["sent_at"], reverse=True) messages.sort(key=lambda m: m["sent_at"], reverse=True)
return {"messages": messages[:limit], "total_returned": len(messages[:limit])} return {"messages": messages[:limit], "total_returned": len(messages[:limit])}
@router.get("/messages/{msg_id}")
async def get_message(
msg_id: str = Path(..., description="UUID of the message (as string)"),
mark_retrieved: bool = Query(False, description="If true, mark message as retrieved/read"),
current_user: HttpUser = Depends(get_current_http_user)
):
from packetserver.runners.http_server import get_db_connection
conn = get_db_connection()
root = conn.root()
username = current_user.username
messages_root = root.get('messages', {})
mailbox = messages_root.get(username)
if not mailbox:
raise HTTPException(status_code=404, detail="Mailbox not found")
# Find message by ID
target_msg = None
for msg in mailbox:
if str(msg.msg_id) == msg_id:
target_msg = msg
break
if not target_msg:
raise HTTPException(status_code=404, detail="Message not found")
# Optionally mark as retrieved
if mark_retrieved and not target_msg.retrieved:
target_msg.retrieved = True
target_msg._p_changed = True
mailbox._p_changed = True
transaction.commit()
return {
"id": str(target_msg.msg_id),
"from": target_msg.msg_from or "UNKNOWN",
"to": list(target_msg.msg_to),
"sent_at": target_msg.sent_at.isoformat() + "Z",
"text": target_msg.text,
"retrieved": target_msg.retrieved,
"has_attachments": len(target_msg.attachments) > 0,
# Future: "attachments": [...] metadata
}