Files
Docwell/Server/internal/httpapi/spaces.go
T
tajniak81andClaude Opus 5.5 ba394b8fac Finish Phase 0: lint, codegen, contributor docs and the two remaining spikes
Repository basics:
- CONTRIBUTING.md and CODE_OF_CONDUCT.md (Contributor Covenant 2.1)
- SPDX AGPL-3.0-or-later headers on all hand-written sources, checked in CI
- golangci-lint (shared .golangci.yml), ESLint and Prettier, all run in CI

API codegen from api/openapi.yaml (npm run generate, drift fails CI):
- Go types via oapi-codegen in Server/internal/api
- a contract test that the router and the spec list the same routes
- Dart models via Phone-App/tool/generate_api.dart; the app uses them

Spike: shared Tiptap editor in a Flutter WebView (ADR-4)
- @docwell/editor-bridge builds the editor into one HTML page with a
  versioned JSON protocol; Phone-App hosts it with webview_flutter and a
  native toolbar. Integration test passes on an Android API 35 emulator.
- flutter_inappwebview dropped: its Android plugin fails on AGP 9.

Spike: Wails v3 desktop (ADR-11)
- system tray, docwell:// deep links (page and space only) with
  single-instance hand-off, and the built-in updater with a required
  ed25519 signing key. Verified on Windows.
- regenerated build assets register the scheme and drop template
  placeholders.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 16:11:07 +01:00

161 lines
7.9 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
package httpapi
import (
"context"
"net/http"
"docwell/server/internal/access"
"docwell/server/internal/collab"
"docwell/server/internal/directory"
)
// SpaceService is the part of directory.Service that serves spaces and pages.
type SpaceService interface {
ListSpaces(ctx context.Context, c access.Caller, wsID string) ([]directory.Space, error)
CreateSpace(ctx context.Context, c access.Caller, wsID string, in directory.CreateSpaceInput) (directory.Space, error)
GetSpace(ctx context.Context, c access.Caller, id string) (directory.Space, error)
UpdateSpace(ctx context.Context, c access.Caller, id string, in directory.UpdateSpaceInput) (directory.Space, error)
DeleteSpace(ctx context.Context, c access.Caller, id string) error
ListSpaceMembers(ctx context.Context, c access.Caller, id string) ([]directory.Member, error)
SetSpaceMember(ctx context.Context, c access.Caller, id, email, role string) (directory.Member, error)
RemoveSpaceMember(ctx context.Context, c access.Caller, id, userID string) error
ListPages(ctx context.Context, c access.Caller, spaceID string) ([]directory.PageNode, error)
CreatePage(ctx context.Context, c access.Caller, spaceID string, in directory.CreatePageInput) (directory.Page, error)
GetPage(ctx context.Context, c access.Caller, id string) (directory.Page, error)
UpdatePage(ctx context.Context, c access.Caller, id string, in directory.UpdatePageInput) (directory.Page, error)
GetPageContent(ctx context.Context, c access.Caller, id string) (directory.PageContent, error)
SavePageContent(ctx context.Context, c access.Caller, id string, in directory.SavePageContentInput) (directory.PageContent, error)
CollabAccess(ctx context.Context, c access.Caller, id string) (canWrite bool, err error)
ListVersions(ctx context.Context, c access.Caller, id string) ([]collab.Version, error)
GetVersion(ctx context.Context, c access.Caller, id, versionID string) (directory.PageContent, error)
RestoreVersion(ctx context.Context, c access.Caller, id, versionID string) (directory.PageContent, error)
MovePage(ctx context.Context, c access.Caller, id string, in directory.MovePageInput) (directory.Page, error)
TrashPage(ctx context.Context, c access.Caller, id string) error
RestorePage(ctx context.Context, c access.Caller, id string) (directory.Page, error)
ListTrash(ctx context.Context, c access.Caller, spaceID string) ([]directory.TrashedPage, error)
PurgePage(ctx context.Context, c access.Caller, id string) error
}
func (a *api) spaceRoutes(mux *routeMux) {
// Spaces. Rights are resolved per space (see access.SpaceRole).
mux.HandleFunc("GET /api/v1/workspaces/{id}/spaces", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
list, err := a.dir.ListSpaces(r.Context(), caller(r), r.PathValue("id"))
a.respond(w, list, err)
}))
mux.HandleFunc("POST /api/v1/workspaces/{id}/spaces", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
var in directory.CreateSpaceInput
if !a.decode(w, r, &in) {
return
}
sp, err := a.dir.CreateSpace(r.Context(), caller(r), r.PathValue("id"), in)
a.respondStatus(w, http.StatusCreated, sp, err)
}))
mux.HandleFunc("GET /api/v1/spaces/{id}", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
sp, err := a.dir.GetSpace(r.Context(), caller(r), r.PathValue("id"))
a.respond(w, sp, err)
}))
mux.HandleFunc("PATCH /api/v1/spaces/{id}", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
var in directory.UpdateSpaceInput
if !a.decode(w, r, &in) {
return
}
sp, err := a.dir.UpdateSpace(r.Context(), caller(r), r.PathValue("id"), in)
a.respond(w, sp, err)
}))
mux.HandleFunc("DELETE /api/v1/spaces/{id}", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
a.respondNoContent(w, a.dir.DeleteSpace(r.Context(), caller(r), r.PathValue("id")))
}))
mux.HandleFunc("GET /api/v1/spaces/{id}/members", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
members, err := a.dir.ListSpaceMembers(r.Context(), caller(r), r.PathValue("id"))
a.respond(w, members, err)
}))
mux.HandleFunc("PUT /api/v1/spaces/{id}/members", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
var in struct{ Email, Role string }
if !a.decode(w, r, &in) {
return
}
m, err := a.dir.SetSpaceMember(r.Context(), caller(r), r.PathValue("id"), in.Email, in.Role)
a.respond(w, m, err)
}))
mux.HandleFunc("DELETE /api/v1/spaces/{id}/members/{userId}", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
a.respondNoContent(w, a.dir.RemoveSpaceMember(r.Context(), caller(r), r.PathValue("id"), r.PathValue("userId")))
}))
// Page tree and trash.
mux.HandleFunc("GET /api/v1/spaces/{id}/pages", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
pages, err := a.dir.ListPages(r.Context(), caller(r), r.PathValue("id"))
a.respond(w, pages, err)
}))
mux.HandleFunc("POST /api/v1/spaces/{id}/pages", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
var in directory.CreatePageInput
if !a.decode(w, r, &in) {
return
}
p, err := a.dir.CreatePage(r.Context(), caller(r), r.PathValue("id"), in)
a.respondStatus(w, http.StatusCreated, p, err)
}))
mux.HandleFunc("GET /api/v1/spaces/{id}/trash", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
pages, err := a.dir.ListTrash(r.Context(), caller(r), r.PathValue("id"))
a.respond(w, pages, err)
}))
mux.HandleFunc("GET /api/v1/pages/{id}", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
p, err := a.dir.GetPage(r.Context(), caller(r), r.PathValue("id"))
a.respond(w, p, err)
}))
mux.HandleFunc("PATCH /api/v1/pages/{id}", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
var in directory.UpdatePageInput
if !a.decode(w, r, &in) {
return
}
p, err := a.dir.UpdatePage(r.Context(), caller(r), r.PathValue("id"), in)
a.respond(w, p, err)
}))
mux.HandleFunc("GET /api/v1/pages/{id}/content", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
c, err := a.dir.GetPageContent(r.Context(), caller(r), r.PathValue("id"))
a.respond(w, c, err)
}))
mux.HandleFunc("PUT /api/v1/pages/{id}/content", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
var in directory.SavePageContentInput
if !a.decodeMax(w, r, &in, 10<<20) {
return
}
c, err := a.dir.SavePageContent(r.Context(), caller(r), r.PathValue("id"), in)
a.respond(w, c, err)
}))
mux.HandleFunc("GET /api/v1/pages/{id}/versions", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
v, err := a.dir.ListVersions(r.Context(), caller(r), r.PathValue("id"))
a.respond(w, v, err)
}))
mux.HandleFunc("GET /api/v1/pages/{id}/versions/{versionId}", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
c, err := a.dir.GetVersion(r.Context(), caller(r), r.PathValue("id"), r.PathValue("versionId"))
a.respond(w, c, err)
}))
mux.HandleFunc("POST /api/v1/pages/{id}/versions/{versionId}/restore", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
c, err := a.dir.RestoreVersion(r.Context(), caller(r), r.PathValue("id"), r.PathValue("versionId"))
a.respond(w, c, err)
}))
mux.HandleFunc("POST /api/v1/pages/{id}/move", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
var in directory.MovePageInput
if !a.decode(w, r, &in) {
return
}
p, err := a.dir.MovePage(r.Context(), caller(r), r.PathValue("id"), in)
a.respond(w, p, err)
}))
mux.HandleFunc("POST /api/v1/pages/{id}/restore", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
p, err := a.dir.RestorePage(r.Context(), caller(r), r.PathValue("id"))
a.respond(w, p, err)
}))
// DELETE moves to the trash; ?permanent=true purges a page that is already there.
mux.HandleFunc("DELETE /api/v1/pages/{id}", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Get("permanent") == "true" {
a.respondNoContent(w, a.dir.PurgePage(r.Context(), caller(r), r.PathValue("id")))
return
}
a.respondNoContent(w, a.dir.TrashPage(r.Context(), caller(r), r.PathValue("id")))
}))
}