"""`/api/quilts*` HTTP routing for `StandaloneQuiltStore` (`node.protocols.textile.quilt_routes`).
- `POST /api/quilts` `{views: [MeshQuiltView...]}` publishes the caller's quilt views
- `GET /api/quilts/me?audience=self` the caller's own view (cross-device sync)
- `GET /api/quilts/<identity_id|did>` an owner's `public` view (channel pages)
- `POST /api/quilts/group` `{circle_id, view}` a member publishes their circle's quilt view
- `GET /api/quilts/group/<circle_id>?audience=circle|public` a circle's view (`circle` for members)
GET answers `{ok: true, view: null}` when there is no view the caller may read.
"""
from __future__ import annotations
import urllib.parse
from typing import Any, Callable, Dict, Mapping, Optional, Tuple
from fleetmesh_core.textile.principal import MeshAccessError, Principal
HttpResult = Tuple[int, Any, str, Dict[str, str]]
JSON_CT = "application/json; charset=utf-8"
[docs]def is_quilt_route(route: str) -> bool:
return route == "/api/quilts" or route.startswith("/api/quilts/")
[docs]def handle_quilt_http(
store: Any,
method: str,
route: str,
query: Any = "",
body: Optional[Mapping[str, Any]] = None,
principal: Principal = None,
circle_access: Optional[Callable[[str, Principal], Optional[Tuple[Mapping[str, Any], bool]]]] = None,
) -> Optional[HttpResult]:
"""`circle_access(circle_id, principal)` gives `(circle, is_member)`."""
store = getattr(store, "quilt_store", store)
qp = query if isinstance(query, Mapping) else urllib.parse.parse_qs(str(query or ""))
try:
if route == "/api/quilts/group" or route.startswith("/api/quilts/group/"):
return _group_quilt_http(store, method, route, qp, body or {}, principal, circle_access)
if method == "GET" and route.startswith("/api/quilts/"):
ref = urllib.parse.unquote(route[len("/api/quilts/") :].strip("/"))
if not ref or "/" in ref:
return None
audience = (qp.get("audience", [""])[0] or "").strip()
if ref == "me":
if not principal:
raise MeshAccessError(401, "authentication_required")
ref, audience = str(principal.get("identity_id") or principal.get("did") or ""), audience or "self"
view = store.get_view(ref, audience or "public", principal)
public = (view or {}).get("audience") == "public"
return (
200,
{"ok": True, "view": view},
JSON_CT,
{"Cache-Control": "public, max-age=30" if public else "private, no-store"},
)
if method == "POST" and route == "/api/quilts":
views = (body or {}).get("views")
stored = store.publish(list(views) if isinstance(views, list) else [], principal)
return 200, {"ok": True, "stored": stored}, JSON_CT, {}
return None
except MeshAccessError as exc:
return exc.status, {"ok": False, "error": exc.error}, JSON_CT, {}
def _group_quilt_http(
store: Any,
method: str,
route: str,
qp: Mapping[str, Any],
body: Mapping[str, Any],
principal: Principal,
circle_access: Any,
) -> Optional[HttpResult]:
if not callable(circle_access):
return None
if method == "POST" and route == "/api/quilts/group":
found = circle_access(str(body.get("circle_id") or "").strip(), principal)
if not found:
raise MeshAccessError(404, "circle_not_found")
if not found[1]:
raise MeshAccessError(403, "not_circle_member")
view = body.get("view")
stored = store.publish_group(found[0], view if isinstance(view, Mapping) else {}, principal)
return 200, {"ok": True, "stored": [stored]}, JSON_CT, {}
if method == "GET" and route.startswith("/api/quilts/group/"):
cid = urllib.parse.unquote(route[len("/api/quilts/group/") :].strip("/"))
found = circle_access(cid, principal) if cid and "/" not in cid else None
audience = (qp.get("audience", ["public"])[0] or "public").strip()
view = store.get_group_view(cid, audience, bool(found and found[1])) if found else None
public = (view or {}).get("audience") == "public"
return (
200,
{"ok": True, "view": view},
JSON_CT,
{"Cache-Control": "public, max-age=30" if public else "private, no-store"},
)
return None