fix(webhooks): wire receiver to vector sync queue and fix registered URI

The /webhooks/nextcloud endpoint was a no-op stub that logged the
payload and returned 200 OK; webhook deletions never reached Qdrant.
Compounding that, _get_webhook_uri() registered the docker-compose
internal hostname (http://mcp:8000) with Nextcloud whenever
/.dockerenv existed — including ECS Fargate — so cloud deployments
were registering a URL NC could not resolve.

- New vector/webhook_parser.py extracts a DocumentTask from
  NodeCreatedEvent / NodeWrittenEvent / BeforeNodeDeletedEvent
  payloads scoped to */files/Notes/*.md (matching the registered
  preset filters).
- New vector/webhook_receiver.py pushes that task onto the same
  send-stream the scanner uses (app.state.document_send_stream),
  with 503 when sync is not running so NC retries delivery.
- _get_webhook_uri() now prefers NEXTCLOUD_MCP_SERVER_URL over the
  /.dockerenv branch, so the explicit public URL set on cloud tasks
  wins; docker-compose dev still falls back to the internal name when
  no public URL is configured.

Calendar / Tables event parsing is intentionally out of scope here.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Chris Coutinho
2026-04-30 03:13:50 +02:00
co-authored by Claude Opus 4.7
parent 111ae988f9
commit 2e2a098bee
7 changed files with 624 additions and 48 deletions
@@ -0,0 +1,86 @@
"""Parse Nextcloud webhook payloads into DocumentTask objects.
Maps Nextcloud webhook events to vector-sync DocumentTasks. The handler at
``/webhooks/nextcloud`` calls :func:`extract_document_task` and forwards any
non-None result to the same processor send-stream the scanner uses.
Currently scoped to file (note) events. Calendar / Tables events fall through
to ``None`` for now; those parsers can be added in follow-up changes.
See ADR-010 for the design and ``webhook-testing-findings.md`` for real
captured payloads.
"""
import logging
import re
from nextcloud_mcp_server.vector.scanner import DocumentTask
logger = logging.getLogger(__name__)
_FILE_EVENT_CREATED = "OCP\\Files\\Events\\Node\\NodeCreatedEvent"
_FILE_EVENT_WRITTEN = "OCP\\Files\\Events\\Node\\NodeWrittenEvent"
_FILE_EVENT_BEFORE_DELETED = "OCP\\Files\\Events\\Node\\BeforeNodeDeletedEvent"
# Matches paths inside any user's Notes folder ending in .md, e.g.
# "/admin/files/Notes/Sub/Note.md" or "/alice/files/Notes/foo.md".
_NOTES_PATH_RE = re.compile(r"^/[^/]+/files/Notes/.+\.md$")
def extract_document_task(payload: dict) -> DocumentTask | None:
"""Convert a Nextcloud webhook payload into a DocumentTask.
Returns None for any event we don't (yet) handle, or any event whose
target isn't a markdown file under a user's Notes folder. Callers should
treat None as "ignored" — not an error.
"""
try:
event = payload["event"]
event_class = event["class"]
user_id = payload["user"]["uid"]
except (KeyError, TypeError):
logger.debug("Webhook payload missing user/event/class fields")
return None
if event_class in (
_FILE_EVENT_CREATED,
_FILE_EVENT_WRITTEN,
_FILE_EVENT_BEFORE_DELETED,
):
return _parse_file_event(event_class, event, user_id, payload.get("time", 0))
logger.debug("Ignoring webhook for unsupported event: %s", event_class)
return None
def _parse_file_event(
event_class: str, event: dict, user_id: str, time: int
) -> DocumentTask | None:
node = event.get("node") or {}
path = node.get("path", "")
node_id = node.get("id")
if not _NOTES_PATH_RE.match(path):
# Not a note file — could be a parent folder, an unrelated file, etc.
return None
if node_id is None:
# BeforeNodeDeletedEvent should still carry node.id; if it doesn't
# we can't address the Qdrant points to delete. Skip rather than
# guess — the polling scanner will catch up via its grace period.
logger.warning(
"Webhook %s for note %s missing node.id; falling back to scanner",
event_class,
path,
)
return None
operation = "delete" if event_class == _FILE_EVENT_BEFORE_DELETED else "index"
return DocumentTask(
user_id=user_id,
doc_id=str(node_id),
doc_type="note",
operation=operation,
modified_at=int(time),
)
@@ -0,0 +1,82 @@
"""HTTP receiver for Nextcloud webhooks.
Routes inbound webhooks to the same processor send-stream the scanner uses.
The receiver is registered as a Starlette route at ``/webhooks/nextcloud``
in :mod:`nextcloud_mcp_server.app`.
"""
import logging
from starlette.requests import Request
from starlette.responses import JSONResponse
from nextcloud_mcp_server.vector.webhook_parser import extract_document_task
logger = logging.getLogger(__name__)
async def handle_nextcloud_webhook(request: Request) -> JSONResponse:
"""Receive a Nextcloud webhook and queue a DocumentTask for vector sync.
Returns quickly so NC's webhook worker is not blocked. The send-stream is
read from ``request.app.state.document_send_stream``; when vector sync
isn't running we return 503 so NC retries delivery.
"""
try:
payload = await request.json()
except Exception as e:
logger.warning(f"Webhook payload was not valid JSON: {e}")
return JSONResponse(
{"status": "error", "message": "invalid JSON"},
status_code=400,
)
task = extract_document_task(payload)
if task is None:
event_class = (payload.get("event") or {}).get("class", "<missing>")
logger.debug("Webhook ignored (unsupported event): %s", event_class)
return JSONResponse(
{"status": "ignored", "reason": "unsupported event"},
status_code=200,
)
send_stream = getattr(request.app.state, "document_send_stream", None)
if send_stream is None:
logger.warning(
"Webhook received but vector sync is not running; rejecting so NC retries"
)
return JSONResponse(
{"status": "unavailable", "reason": "vector sync not running"},
status_code=503,
)
try:
await send_stream.send(task)
except Exception as e:
logger.error(
"Failed to queue webhook task for %s_%s: %s",
task.doc_type,
task.doc_id,
e,
)
return JSONResponse(
{"status": "error", "message": "queue unavailable"},
status_code=500,
)
logger.info(
"Webhook queued %s_%s (%s) for user %s",
task.doc_type,
task.doc_id,
task.operation,
task.user_id,
)
return JSONResponse(
{
"status": "queued",
"doc_type": task.doc_type,
"doc_id": task.doc_id,
"operation": task.operation,
},
status_code=200,
)