57 lines
1.5 KiB
Go
57 lines
1.5 KiB
Go
package server
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"pageswiki/internal/store"
|
|
"strings"
|
|
)
|
|
|
|
func (s *Server) handleRestoreRevisionAPI(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
w.Header().Set("Allow", "POST")
|
|
http.Error(w, "method not allowed", 405)
|
|
return
|
|
}
|
|
var input struct {
|
|
Revision string `json:"revision_uuid"`
|
|
Expected string `json:"base_revision_uuid"`
|
|
Message string `json:"message"`
|
|
}
|
|
if !taskJSON(w, r, &input) {
|
|
return
|
|
}
|
|
if input.Expected == "" {
|
|
http.Error(w, "base revision required", 428)
|
|
return
|
|
}
|
|
if len(input.Message) > 4096 {
|
|
http.Error(w, "message too long", 422)
|
|
return
|
|
}
|
|
metadata, err := s.store.GetRevisionMetadata(input.Revision)
|
|
if err != nil {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
if s.authorizeObjectForRequest(r, "read", metadata.ObjectUUID) != nil || s.authorizeObjectForRequest(r, "write", metadata.ObjectUUID) != nil {
|
|
http.Error(w, "forbidden", 403)
|
|
return
|
|
}
|
|
message := strings.TrimSpace(input.Message)
|
|
if message == "" {
|
|
message = "Restore revision " + metadata.UUID
|
|
}
|
|
saved, err := s.store.RestoreRevisionInDomain(r.Context(), s.domainUUIDForRequest(r), "", input.Revision, input.Expected, message)
|
|
if errors.Is(err, store.ErrRevisionConflict) {
|
|
http.Error(w, "revision changed; reload history", 412)
|
|
return
|
|
}
|
|
if err != nil {
|
|
http.Error(w, "restore failed", 500)
|
|
return
|
|
}
|
|
w.Header().Set("Cache-Control", "no-store")
|
|
writeEventJSON(w, 200, map[string]string{"revision_uuid": saved.UUID, "object_uuid": saved.ObjectUUID})
|
|
}
|