Pages are now shared Yjs documents. docwell-server relays the y-websocket protocol at /collab (token sent as the first message), flushes updates to page_yupdates every second, and compacts them into page_ydoc with yrs compiled to WebAssembly under wazero (ADR-3) after 30 s idle or when the last editor leaves. Compaction refreshes content_json/content_text and records page_versions; versions can be listed, previewed and restored. The editor connects through a small y-protocols provider with carets and presence. Pages saved before this change are seeded from content_json. The yrs module lives in Server/ycrdt-wasm and is built by go generate, CI and the Dockerfiles rather than committed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
159 lines
7.9 KiB
Go
159 lines
7.9 KiB
Go
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 *http.ServeMux) {
|
|
// 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")))
|
|
}))
|
|
}
|