Add version history with rollback and per-note created/synced info
All checks were successful
ci / test (push) Successful in 3m8s
ci / release (push) Successful in 11m21s

Server (schema v2, auto-migrates): every accepted overwrite snapshots the
superseded revision into note_history (capped at 50 per note); new
GET /api/v1/notes/{id}/history endpoint; compact purges orphaned history.

Client: notes get a synced_at stamp on every confirmed server exchange;
an info footer under the editor and a Ctrl/Cmd+I panel show created/
modified/last-synced plus the revision list. Restoring a revision applies
it as a normal edit through the sync path, so rollback is non-destructive.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-25 22:20:19 +02:00
parent bd2b8e6241
commit c389bab46e
12 changed files with 563 additions and 9 deletions

View File

@@ -28,6 +28,7 @@ func (s *Server) Handler() http.Handler {
mux.HandleFunc("GET /api/v1/health", s.handleHealth)
mux.Handle("GET /api/v1/changes", s.auth(s.handleChanges))
mux.Handle("POST /api/v1/notes/batch", s.auth(s.handleBatch))
mux.Handle("GET /api/v1/notes/{id}/history", s.auth(s.handleHistory))
mux.Handle("POST /api/v1/import/simplenote", s.auth(s.handleImport))
mux.HandleFunc("OPTIONS /api/v1/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNoContent)
@@ -108,6 +109,16 @@ func (s *Server) handleBatch(w http.ResponseWriter, r *http.Request) {
writeJSON(w, map[string]any{"results": results})
}
func (s *Server) handleHistory(w http.ResponseWriter, r *http.Request) {
revs, err := s.Store.History(r.PathValue("id"))
if err != nil {
log.Printf("history: %v", err)
jsonError(w, http.StatusInternalServerError, "internal error")
return
}
writeJSON(w, map[string]any{"revisions": revs})
}
func (s *Server) handleImport(w http.ResponseWriter, r *http.Request) {
if err := r.ParseMultipartForm(64 << 20); err != nil {
jsonError(w, http.StatusBadRequest, "bad multipart form: "+err.Error())

View File

@@ -229,6 +229,45 @@ func TestTwoClientsConverge(t *testing.T) {
}
}
func TestHistoryEndpoint(t *testing.T) {
srv, tok := newTestServer(t)
a := newClient(t, srv.URL, tok)
a.edit("n1", "v1")
a.sync()
a.edit("n1", "v2")
a.notes["n1"].ModifiedAt = 2
a.sync()
var body struct {
Revisions []store.Revision `json:"revisions"`
}
if code := a.req("GET", "/api/v1/notes/n1/history", nil, &body); code != 200 {
t.Fatalf("history HTTP %d", code)
}
if len(body.Revisions) != 1 || body.Revisions[0].Content != "v1" {
t.Fatalf("history: %+v", body.Revisions)
}
// Unknown note → empty list, not an error.
if code := a.req("GET", "/api/v1/notes/nope/history", nil, &body); code != 200 {
t.Fatalf("unknown note HTTP %d", code)
}
if len(body.Revisions) != 0 {
t.Fatalf("want empty history, got %+v", body.Revisions)
}
// Auth required.
res, err := http.Get(srv.URL + "/api/v1/notes/n1/history")
if err != nil {
t.Fatal(err)
}
res.Body.Close()
if res.StatusCode != http.StatusUnauthorized {
t.Fatalf("want 401, got %d", res.StatusCode)
}
}
func TestDeleteEditConflictAcrossClients(t *testing.T) {
srv, tok := newTestServer(t)
a := newClient(t, srv.URL, tok)