Files
pages/internal/server/revision_api.go
Anya Tokugawa c1175d389f Implement Knowledge City platform, organizer and DAV integration
Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming.

Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
2026-09-27 14:44:43 +09:00

78 lines
2.2 KiB
Go

package server
import (
"encoding/json"
"errors"
"net/http"
"net/url"
"strconv"
"pageswiki/internal/store"
)
func revisionListOptions(r *http.Request) (string, int, error) {
before := r.URL.Query().Get("before")
if len(before) > 128 {
return "", 0, store.ErrRevisionCursor
}
limit := 100
if raw := r.URL.Query().Get("limit"); raw != "" {
var err error
limit, err = strconv.Atoi(raw)
if err != nil || limit < 1 || limit > 200 {
return "", 0, errors.New("limit must be between 1 and 200")
}
}
return before, limit, nil
}
func revisionPageURL(r *http.Request, before string) string {
query := r.URL.Query()
query.Del("before")
if before != "" {
query.Set("before", before)
}
return (&url.URL{Path: r.URL.Path, RawQuery: query.Encode()}).String()
}
func (s *Server) handleRevisionListAPI(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("Cache-Control", "private, no-store")
w.Header().Set("Vary", "Authorization, Cookie")
if r.Method != http.MethodGet && r.Method != http.MethodHead {
w.Header().Set("Allow", "GET, HEAD")
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
object := r.URL.Query().Get("uuid")
if object == "" || s.authorizeObjectForRequest(r, "read", object) != nil {
http.Error(w, "forbidden", http.StatusForbidden)
return
}
before, limit, err := revisionListOptions(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
page, err := s.store.ListRevisionPage(r.Context(), object, before, limit)
if err != nil {
if errors.Is(err, store.ErrRevisionCursor) {
http.Error(w, err.Error(), http.StatusBadRequest)
} else {
http.Error(w, "revision listing unavailable", http.StatusServiceUnavailable)
}
return
}
if page.Next != "" {
w.Header().Set("X-Next-Cursor", page.Next)
w.Header().Set("Link", "<"+revisionPageURL(r, page.Next)+">; rel=\"next\"")
}
out := make([]map[string]any, 0, len(page.Items))
for _, item := range page.Items {
out = append(out, map[string]any{"uuid": item.UUID, "message": item.Message, "created_at": item.CreatedAt, "lang": item.Language, "document_hash": item.DocumentHash})
}
if r.Method != http.MethodHead {
_ = json.NewEncoder(w).Encode(out)
}
}