Spaces:
Sleeping
Sleeping
Commit ·
4a035f6
1
Parent(s): e61e751
Feat: Add /inbox + /envelope/{id}/read endpoints so Council can read mail
Browse files- app.py +19 -0
- envelope_store.py +27 -0
app.py
CHANGED
|
@@ -394,6 +394,25 @@ async def list_states():
|
|
| 394 |
return {"states": sorted(c_fed_id.VALID_STATES)}
|
| 395 |
|
| 396 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 397 |
@app.get("/witness-log")
|
| 398 |
async def get_witness_log(limit: int = 20):
|
| 399 |
"""View recent witness records. Most recent first."""
|
|
|
|
| 394 |
return {"states": sorted(c_fed_id.VALID_STATES)}
|
| 395 |
|
| 396 |
|
| 397 |
+
@app.get("/envelope/{envelope_id}/read")
|
| 398 |
+
async def read_envelope(envelope_id: str):
|
| 399 |
+
"""Read the full contents of a stored envelope by ID."""
|
| 400 |
+
result = envelopes.get_envelope(envelope_id)
|
| 401 |
+
if result is None:
|
| 402 |
+
raise HTTPException(status_code=404, detail=make_refusal(
|
| 403 |
+
RefusalReason.REFUSED_BY_POLICY,
|
| 404 |
+
f"Envelope '{envelope_id}' not found.",
|
| 405 |
+
NODE_SEAL_STR,
|
| 406 |
+
))
|
| 407 |
+
return result
|
| 408 |
+
|
| 409 |
+
|
| 410 |
+
@app.get("/inbox")
|
| 411 |
+
async def read_inbox(limit: int = 20, status: Optional[str] = None):
|
| 412 |
+
"""Browse queued envelopes. Most recent first. Filter by status if needed."""
|
| 413 |
+
return envelopes.list_envelopes(limit=limit, status_filter=status)
|
| 414 |
+
|
| 415 |
+
|
| 416 |
@app.get("/witness-log")
|
| 417 |
async def get_witness_log(limit: int = 20):
|
| 418 |
"""View recent witness records. Most recent first."""
|
envelope_store.py
CHANGED
|
@@ -95,6 +95,33 @@ class EnvelopeStore:
|
|
| 95 |
record["status_history"].append({"status": new_status, "at": now})
|
| 96 |
return record
|
| 97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
def get_status(self, envelope_id: str) -> Optional[dict]:
|
| 99 |
"""Get delivery status summary for an envelope."""
|
| 100 |
record = self._store.get(envelope_id)
|
|
|
|
| 95 |
record["status_history"].append({"status": new_status, "at": now})
|
| 96 |
return record
|
| 97 |
|
| 98 |
+
def get_envelope(self, envelope_id: str) -> Optional[dict]:
|
| 99 |
+
"""Retrieve full envelope record including payload."""
|
| 100 |
+
return self._store.get(envelope_id)
|
| 101 |
+
|
| 102 |
+
def list_envelopes(self, limit: int = 20, status_filter: Optional[str] = None) -> dict:
|
| 103 |
+
"""List envelopes, most recent first. Optionally filter by status."""
|
| 104 |
+
records = list(self._store.values())
|
| 105 |
+
if status_filter:
|
| 106 |
+
records = [r for r in records if r["status"] == status_filter]
|
| 107 |
+
records.sort(key=lambda r: r["received_at"], reverse=True)
|
| 108 |
+
page = records[:limit]
|
| 109 |
+
return {
|
| 110 |
+
"total": len(records),
|
| 111 |
+
"showing": len(page),
|
| 112 |
+
"envelopes": [
|
| 113 |
+
{
|
| 114 |
+
"envelope_id": r["envelope_id"],
|
| 115 |
+
"status": r["status"],
|
| 116 |
+
"sender_origin": r["sender_origin"],
|
| 117 |
+
"message_class": r["message_class"],
|
| 118 |
+
"received_at": r["received_at"],
|
| 119 |
+
"payload_preview": str(r["payload"].get("body", ""))[:120],
|
| 120 |
+
}
|
| 121 |
+
for r in page
|
| 122 |
+
],
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
def get_status(self, envelope_id: str) -> Optional[dict]:
|
| 126 |
"""Get delivery status summary for an envelope."""
|
| 127 |
record = self._store.get(envelope_id)
|