Files
pages/internal/server/api_handlers.go
T
Anya Tokugawa d5d21b7841
Build and publish container images / images (push) Successful in 9m34s
Release container images / release (push) Successful in 36s
Refactor architecture into eight components and consolidate reliability work
Extract Grammaratur, Dregarnuhr, Geduldh, Mestionora, Ordoschnellic, Schutzaria, Kunstzeal and Seheweit behind compatible application contracts.

Include accumulated editor resilience, navigation and theme UX, remote CLI, storage/schema and DR groundwork, tests and design/operations documentation.

Verified: uncached full Go suite; go vet ./...; race tests for all eight components; 11 targeted Chromium tests; Node suites; 19 Python CLI tests; reproducible editor build. No push, release tag or deployment in this checkpoint.
2026-11-01 15:23:31 +09:00

170 lines
4.4 KiB
Go

package server
import (
"encoding/json"
"errors"
"net/http"
"pageswiki/internal/api"
"pageswiki/internal/store"
)
func (s *Server) handleCreateObjectAPI(
w http.ResponseWriter,
r *http.Request,
) {
var req struct {
Kind string `json:"kind"`
Title string `json:"title"`
Body string `json:"body"`
}
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
api.WriteError(
w,
400,
"bad_json",
err.Error(),
)
return
}
id, err := s.store.CreateObjectInDomain(s.domainUUIDForRequest(r), req.Kind)
if err != nil {
api.WriteError(
w,
500,
"create_failed",
err.Error(),
)
return
}
_ = json.NewEncoder(w).Encode(map[string]any{
"uuid": id,
})
}
func (s *Server) handleListObjectsAPI(
w http.ResponseWriter,
r *http.Request,
) {
s.serveObjectMetadataList(w, r)
}
func (s *Server) handleSaveRevisionAPI(
w http.ResponseWriter,
r *http.Request,
) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("Cache-Control", "private, no-store")
if r.Method != http.MethodPost {
w.Header().Set("Allow", "POST")
api.WriteError(w, 405, "method_not_allowed", "POST required")
return
}
var req struct {
ObjectUUID string `json:"object_uuid"`
Body string `json:"body"`
Message string `json:"message"`
Language string `json:"lang"`
ContentType string `json:"content_type"`
Filename string `json:"filename"`
BaseRevision *string `json:"base_revision_uuid"`
EditorPrincipal *string `json:"editor_principal_uuid"`
}
err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 4<<20)).Decode(&req)
if err != nil {
api.WriteError(
w,
400,
"bad_json",
err.Error(),
)
return
}
domainUUID := s.domainUUIDForRequest(r)
if req.EditorPrincipal != nil {
principal, e := s.store.PrincipalForSession(domainUUID, s.authTokenFromRequest(r))
id := ""
if e == nil && principal != nil {
id = principal.UUID
}
if id != *req.EditorPrincipal {
api.WriteError(w, 401, "editor_identity_changed", "editor identity changed; reopen with the original account")
return
}
}
if mode, _ := s.store.GetConfigInDomain(domainUUID, "authorization_mode"); mode == "enforcing" {
principal, authErr := s.store.PrincipalForSession(domainUUID, s.authTokenFromRequest(r))
if authErr != nil {
api.WriteError(w, http.StatusUnauthorized, "authentication_required", "authentication required")
return
}
var revisionCount int
_ = s.store.DB.QueryRow(`SELECT COUNT(*) FROM revisions WHERE object_uuid=?`, req.ObjectUUID).Scan(&revisionCount)
capability := "write"
if revisionCount == 0 {
capability = "create"
}
allowed := false
links, _ := s.store.ListPathLinksInDomain(domainUUID, req.ObjectUUID)
for _, link := range links {
if s.store.Authorize(store.AuthorizationRequest{DomainUUID: domainUUID, PrincipalUUID: principal.UUID, Capability: capability, TargetType: "path", TargetID: link.Path}) == nil {
allowed = true
break
}
}
if !allowed && s.authorizePrincipalObject(domainUUID, principal.UUID, capability, req.ObjectUUID) == nil {
allowed = true
}
if !allowed {
api.WriteError(w, http.StatusForbidden, "forbidden", "forbidden")
return
}
}
var revision *store.Revision
if req.BaseRevision != nil {
revision, err = s.store.SaveRevisionContentIfHead(req.ObjectUUID, []byte(req.Body), req.Message, req.Language, req.ContentType, req.Filename, *req.BaseRevision)
} else {
revision, err = s.store.SaveRevisionContent(req.ObjectUUID, []byte(req.Body), req.Message, req.Language, req.ContentType, req.Filename)
}
if errors.Is(err, store.ErrRevisionConflict) {
api.WriteError(w, http.StatusConflict, "revision_conflict", "document changed; reload the language head before saving")
return
}
if err != nil {
status := http.StatusInternalServerError
if errors.Is(err, store.ErrInvalidRevisionLanguage) ||
errors.Is(err, store.ErrPageNotFound) ||
errors.Is(err, store.ErrEmbeddedAssetUnavailable) {
status = http.StatusBadRequest
}
api.WriteError(
w,
status,
"save_failed",
err.Error(),
)
return
}
_ = json.NewEncoder(w).Encode(map[string]any{
"status": "ok",
"revision_uuid": revision.UUID,
"document_hash": revision.DocumentHash,
"lang": revision.Language,
"content_type": revision.ContentType,
"filename": revision.Filename,
"body": revision.Body,
})
}