Adding changes to messages. Preparing to do a dashboard.
This commit is contained in:
@@ -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),
|
||||
@@ -108,3 +121,39 @@ async def get_message(
|
||||
"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}
|
||||
6
packetserver/http/static/css/bootstrap.min.css
vendored
Normal file
6
packetserver/http/static/css/bootstrap.min.css
vendored
Normal file
File diff suppressed because one or more lines are too long
7
packetserver/http/static/js/bootstrap.bundle.min.js
vendored
Normal file
7
packetserver/http/static/js/bootstrap.bundle.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user