fix(dashboard): normalize recoverable layout preferences on save
Build and publish container images / images (push) Successful in 9m27s
Release container images / release (push) Successful in 20s

This commit is contained in:
Anya Tokugawa committed 2026-11-10 07:36:32 +09:00
1 parent 667776e43f
commit 38b0fe2a94
4 files changed
+43 -4

No files matched your search

+11 -1
View File
@@ -69,12 +69,22 @@ func TestOrganizerDashboardLayoutIsolationAndCAS(t *testing.T) {
if err = json.Unmarshal(w.Body.Bytes(), &layout); err != nil || len(layout.Widgets) != 0 {
t.Fatal("empty layout reset", layout, err)
}
for _, invalid := range [][]store.DashboardWidget{{{ID: "iframe", Width: 4, Height: 3}}, {{ID: "tasks", Width: 13, Height: 3, Tab: "inbox"}}, {{ID: "tasks", Width: 4, Height: 3, Tab: "secret"}}, {{ID: "progress", Width: 4, Height: 3}, {ID: "progress", Width: 4, Height: 3}}} {
for _, invalid := range [][]store.DashboardWidget{{{ID: "iframe", Width: 4, Height: 3}}} {
w = taskRequest(s, token, "PUT", "dashboard", map[string]any{"version": layout.Version, "widgets": invalid}, "")
if w.Code != 422 {
t.Fatal("invalid accepted", w.Code, w.Body)
}
}
for _, id := range []string{"commands", "tomorrow-schedule", "feeds"} {
input := []store.DashboardWidget{{ID: "tasks", Width: 13, Height: 0}, {ID: id, Width: 6, Height: 4, Tab: "obsolete"}, {ID: id, Width: 6, Height: 4}}
w = taskRequest(s, token, "PUT", "dashboard", map[string]any{"version": layout.Version, "widgets": input}, "")
if w.Code != 200 {
t.Fatal("normalizable layout rejected", w.Code, w.Body)
}
if err = json.Unmarshal(w.Body.Bytes(), &layout); err != nil || len(layout.Widgets) != 2 || layout.Widgets[0].Width != 12 || layout.Widgets[0].Height != 2 || layout.Widgets[0].Tab != "inbox" || layout.Widgets[1].Tab != "" {
t.Fatal("normalization", layout, err)
}
}
other, err := s.store.CreatePasswordAccount(domain, "dashboardother", "long test password", "Other", "")
if err != nil {
t.Fatal(err)
+30 -1
View File
@@ -53,6 +53,33 @@ type DashboardLayout struct {
Widgets []DashboardWidget `json:"widgets"`
}
func normalizeDashboard(items []DashboardWidget) ([]DashboardWidget, error) {
if len(items) > 100 {
return nil, ErrTaskInvalid
}
out := make([]DashboardWidget, 0, len(items))
seen := map[string]bool{}
for _, item := range items {
if seen[item.ID] {
continue
}
seen[item.ID] = true
item.Width = max(3, min(12, item.Width))
item.Height = max(2, min(10, item.Height))
if item.ID == "tasks" {
switch item.Tab {
case "inbox", "backlog", "someday", "review", "all", "done":
default:
item.Tab = "inbox"
}
} else {
item.Tab = ""
}
out = append(out, item)
}
return out, ValidateDashboard(out)
}
func (s *Store) Dashboard(ctx context.Context, domain, actor string) (DashboardLayout, error) {
p := DashboardLayout{Widgets: DefaultDashboard()}
if err := taskActor(ctx, s.DB, domain, actor, false); err != nil {
@@ -79,9 +106,11 @@ func (s *Store) SaveDashboard(ctx context.Context, domain, actor string, version
if version < 0 {
return p, ErrTaskInvalid
}
if err := ValidateDashboard(widgets); err != nil {
normalized, err := normalizeDashboard(widgets)
if err != nil {
return p, err
}
widgets = normalized
tx, err := s.DB.BeginTx(ctx, nil)
if err != nil {
return p, err