aboutsummaryrefslogtreecommitdiffstats
path: root/packages/meshbay-hub/src/meshbay_hub/api
diff options
context:
space:
mode:
Diffstat (limited to 'packages/meshbay-hub/src/meshbay_hub/api')
-rw-r--r--packages/meshbay-hub/src/meshbay_hub/api/groups.py51
-rw-r--r--packages/meshbay-hub/src/meshbay_hub/api/revocation.py11
-rw-r--r--packages/meshbay-hub/src/meshbay_hub/api/webapp.py73
3 files changed, 76 insertions, 59 deletions
diff --git a/packages/meshbay-hub/src/meshbay_hub/api/groups.py b/packages/meshbay-hub/src/meshbay_hub/api/groups.py
index 5bccf71..6dd4275 100644
--- a/packages/meshbay-hub/src/meshbay_hub/api/groups.py
+++ b/packages/meshbay-hub/src/meshbay_hub/api/groups.py
@@ -15,6 +15,57 @@ from meshbay_hub.db.models import (
router = APIRouter(prefix="/v1/groups", tags=["groups"])
+@router.get("/mine")
+async def my_groups(
+ current_user: User = Depends(get_current_user),
+ db: AsyncSession = Depends(get_db),
+):
+ """List groups the current user belongs to."""
+ result = await db.execute(
+ select(Group)
+ .join(GroupMember, Group.id == GroupMember.group_id)
+ .where(GroupMember.user_id == current_user.id, Group.status == "active")
+ .order_by(Group.name)
+ )
+ groups = result.scalars().all()
+ return {
+ "groups": [
+ {
+ "id": g.id,
+ "name": g.name,
+ "visibility": g.visibility,
+ "join_policy": g.join_policy,
+ "created_at": g.created_at.isoformat(),
+ "is_admin": g.admin_id == current_user.id,
+ }
+ for g in groups
+ ]
+ }
+
+
+@router.get("/{group_id}/nodes")
+async def group_online_nodes(
+ group_id: str,
+ current_user: User = Depends(get_current_user),
+ db: AsyncSession = Depends(get_db),
+):
+ """Return online nodes that serve a group (for WebRTC connection)."""
+ from meshbay_hub.api.revocation import get_online_nodes_for_group
+ from meshbay_hub.db.models import Node
+
+ group = await db.get(Group, group_id)
+ if not group:
+ raise HTTPException(status_code=404, detail="Group not found")
+
+ node_ids = get_online_nodes_for_group(group_id)
+ nodes = []
+ for nid in node_ids:
+ node = await db.get(Node, nid)
+ if node:
+ nodes.append({"node_id": nid, "pk_node": node.pk_node})
+ return {"nodes": nodes}
+
+
@router.get("")
async def list_public_groups(
db: AsyncSession = Depends(get_db),
diff --git a/packages/meshbay-hub/src/meshbay_hub/api/revocation.py b/packages/meshbay-hub/src/meshbay_hub/api/revocation.py
index 8f30745..8f65f89 100644
--- a/packages/meshbay-hub/src/meshbay_hub/api/revocation.py
+++ b/packages/meshbay-hub/src/meshbay_hub/api/revocation.py
@@ -51,6 +51,7 @@ router = APIRouter(tags=["revocation"])
# ── Connected node registry ───────────────────────────────────────────────────
_connected_nodes: dict[str, WebSocket] = {} # node_id → websocket
+_node_groups: dict[str, list[str]] = {} # node_id → [group_id, ...]
_punch_events: dict[str, asyncio.Event] = {} # node_id → signaling event
@@ -58,6 +59,10 @@ def get_connected_node_count() -> int:
return len(_connected_nodes)
+def get_online_nodes_for_group(group_id: str) -> list[str]:
+ return [nid for nid, gids in _node_groups.items() if group_id in gids]
+
+
async def broadcast_revocation(token: str) -> int:
"""Push a signed revocation token to all connected nodes. Returns count sent."""
payload = json.dumps({"type": "revocation", "token": token})
@@ -121,7 +126,10 @@ async def node_websocket(ws: WebSocket):
node_id = msg.get("node_id") or decoded.get("sub", "unknown")
_connected_nodes[node_id] = ws
- log.info("Node WS connected: %s", node_id[:8])
+ group_ids = msg.get("group_ids", [])
+ if group_ids:
+ _node_groups[node_id] = group_ids
+ log.info("Node WS connected: %s (groups=%d)", node_id[:8], len(group_ids))
await ws.send_text(json.dumps({"type": "auth_ok", "node_id": node_id}))
# Message loop — handle ping, punch_ready, etc.
@@ -145,6 +153,7 @@ async def node_websocket(ws: WebSocket):
finally:
if node_id:
_connected_nodes.pop(node_id, None)
+ _node_groups.pop(node_id, None)
# ── Admin revocation endpoint ─────────────────────────────────────────────────
diff --git a/packages/meshbay-hub/src/meshbay_hub/api/webapp.py b/packages/meshbay-hub/src/meshbay_hub/api/webapp.py
index 6005927..62917f4 100644
--- a/packages/meshbay-hub/src/meshbay_hub/api/webapp.py
+++ b/packages/meshbay-hub/src/meshbay_hub/api/webapp.py
@@ -1,47 +1,26 @@
"""
-Hub web application — serves the MeshBay web client at /.
+Hub web application — serves the MeshBay SPA and static assets.
-The web client (HTML/JS) is a single-page app that:
- - Logs in via the hub API
- - Discovers public groups
- - Connects to a node URL entered by the user
- - Browses the node's file index
- - Downloads or streams files via the node HTTP API
+The SPA (Preact + htm) handles:
+ - Authentication (login, register, token refresh)
+ - Group discovery and browsing
+ - WebRTC connection to nodes for P2P file transfer
+ - Dark/light theme with system preference detection
-Static files are served from meshbay_hub/static/.
-API routes remain at /v1/*.
+Static files are served from meshbay_hub/static/ via Starlette StaticFiles.
+The root route (/) returns the SPA HTML shell.
"""
from pathlib import Path
from fastapi import APIRouter
-from fastapi.responses import FileResponse, HTMLResponse
+from fastapi.responses import HTMLResponse
STATIC_DIR = Path(__file__).parent.parent / "static"
router = APIRouter(tags=["webapp"])
-@router.get("/app.js")
-async def app_js():
- return FileResponse(STATIC_DIR / "app.js", media_type="application/javascript")
-
-
-@router.get("/transport.js")
-async def transport_js():
- return FileResponse(STATIC_DIR / "transport.js", media_type="application/javascript")
-
-
-@router.get("/crypto.js")
-async def crypto_js():
- return FileResponse(STATIC_DIR / "crypto.js", media_type="application/javascript")
-
-
-@router.get("/webrtc-test.html")
-async def webrtc_test():
- return FileResponse(STATIC_DIR / "webrtc-test.html", media_type="text/html")
-
-
@router.get("/", response_class=HTMLResponse)
async def index():
return HTMLResponse(_HTML)
@@ -54,36 +33,14 @@ _HTML = """\
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>MeshBay</title>
- <style>
- *, *::before, *::after { box-sizing: border-box; }
- body { font-family: system-ui, sans-serif; margin: 0; background: #f8fafc; color: #1e293b; }
- #nav { background: #0f172a; color: #e2e8f0; padding: 12px 24px; }
- #nav b { color: #38bdf8; font-size: 1.2em; }
- #main { max-width: 960px; margin: 32px auto; padding: 0 16px; }
- h2 { color: #0f172a; margin-top: 1.5em; }
- input { padding: 8px 12px; border: 1px solid #cbd5e1; border-radius: 6px;
- font-size: 1em; margin: 4px; }
- button { padding: 8px 16px; background: #0ea5e9; color: #fff; border: none;
- border-radius: 6px; cursor: pointer; font-size: 0.9em; margin: 4px; }
- button:hover { background: #0284c7; }
- .card { background: #fff; border: 1px solid #e2e8f0; border-radius: 8px;
- padding: 16px; margin: 8px 0; cursor: pointer; }
- .card:hover { border-color: #0ea5e9; }
- .badge { background: #e0f2fe; color: #0284c7; padding: 2px 8px;
- border-radius: 12px; font-size: 0.8em; margin-left: 8px; }
- table { width: 100%; border-collapse: collapse; background: #fff;
- border: 1px solid #e2e8f0; border-radius: 8px; overflow: hidden; }
- th { background: #f1f5f9; padding: 10px; text-align: left; }
- td { padding: 10px; border-top: 1px solid #f1f5f9; }
- video { border-radius: 8px; box-shadow: 0 4px 16px rgba(0,0,0,.15); }
- a { color: #0ea5e9; text-decoration: none; }
- a:hover { text-decoration: underline; }
- </style>
+ <link rel="stylesheet" href="/style.css">
</head>
<body>
- <div id="nav"></div>
- <div id="main"><p>Loading…</p></div>
- <script src="/app.js"></script>
+ <div id="app"></div>
+ <script src="/keyderive.js"></script>
+ <script src="/crypto.js"></script>
+ <script src="/transport.js"></script>
+ <script type="module" src="/app.js"></script>
</body>
</html>
"""