Adding changes to messages. Preparing to do a dashboard.

This commit is contained in:
Michael Woods
2025-12-21 22:22:27 -05:00
parent e76b19998d
commit 7472c08269
3 changed files with 64 additions and 2 deletions

View File

@@ -4,12 +4,24 @@ from typing import Optional
from datetime import datetime
from persistent.mapping import PersistentMapping
import persistent.list
import transaction
from pydantic import BaseModel, Field, validator
from packetserver.http.dependencies import get_current_http_user
from packetserver.http.auth import HttpUser
router = APIRouter(prefix="/api/v1", tags=["messages"])
# Simple request model (only allow setting to true)
class MarkRetrievedRequest(BaseModel):
retrieved: bool = Field(..., description="Set to true to mark as retrieved")
@validator("retrieved")
def must_be_true(cls, v):
if not v:
raise ValueError("retrieved must be true")
return v
@router.get("/messages")
async def get_messages(
@@ -96,7 +108,8 @@ async def get_message(
target_msg.retrieved = True
target_msg._p_changed = True
mailbox._p_changed = True
transaction.commit()
# Explicit transaction for the write
transaction.get().commit()
return {
"id": str(target_msg.msg_id),
@@ -107,4 +120,40 @@ async def get_message(
"retrieved": target_msg.retrieved,
"has_attachments": len(target_msg.attachments) > 0,
# Future: "attachments": [...] metadata
}
}
@router.patch("/messages/{msg_id}")
async def mark_message_retrieved(
msg_id: str = Path(..., description="Message UUID as string"),
payload: MarkRetrievedRequest = None,
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
mailbox = root.get('messages', {}).get(username)
if not mailbox:
raise HTTPException(status_code=404, detail="Mailbox not found")
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")
if target_msg.retrieved:
# Already marked idempotent success
return {"status": "already_retrieved", "id": msg_id}
target_msg.retrieved = True
target_msg._p_changed = True
mailbox._p_changed = True
transaction.get().commit()
return {"status": "marked_retrieved", "id": msg_id}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long