Merge branch 'feat/dashboard-feed' into main
Expose authorized recent spreadsheets to the Oficina dashboard
10 files changed,
+209
−64
go.mod+2 −1go.sum+2 −0internal/sheets/app.go+4 −18internal/sheets/dashboard.go+59 −0internal/sheets/dashboard_test.go+64 −0internal/sheets/grants.go+1 −43internal/sheets/home.go+1 −1internal/sheets/migrations.go+2 −1internal/sheets/session_guard.go+21 −0internal/sheets/visible_sheets.go+53 −0
CI passed — run details
landed from feat/dashboard-feed
diff --git a/go.mod b/go.mod| index 707f028..6b790a7 100644 |
| --- a/go.mod |
| +++ b/go.mod |
| @@ -5,6 +5,7 @@ go 1.25.0 |
| require ( |
| amadan.net/oficina/estilo.git v0.0.0-20261024233714-f14458a4683c |
| amadan.net/oficina/home/connect v0.0.0-20261024233715-ff16bf56bc4d |
| + amadan.net/oficina/home/dashboard v0.0.0-20261024234842-3774eb68f816 |
| amadan.net/oficina/home/handoff v0.0.0-20261024233715-ff16bf56bc4d |
| amadan.net/rastrillo/rastrillo v0.27.1-0.20261006204347-fb806a287060 |
| github.com/carlosframework/rastrillo v0.23.0 |
| @@ -29,7 +30,7 @@ require ( |
| require ( |
| amadan.net/rastrillo/native/server v0.0.0-20260923173546-2a5d07b2c8d3 |
| github.com/dustin/go-humanize v1.0.1 // indirect |
| - github.com/go-json-experiment/json v0.0.0-20250725192818-e39067aee2d2 // indirect |
| + github.com/go-json-experiment/json v0.0.0-20250725192818-e39067aee2d2 |
| github.com/google/uuid v1.6.0 // indirect |
| github.com/jinzhu/inflection v1.0.0 // indirect |
| github.com/jinzhu/now v1.1.5 // indirect |
diff --git a/go.sum b/go.sum| index a703876..263db62 100644 |
| --- a/go.sum |
| +++ b/go.sum |
| @@ -2,6 +2,8 @@ amadan.net/oficina/estilo.git v0.0.0-20261024233714-f14458a4683c h1:jPbFJwPi7lHe |
| amadan.net/oficina/estilo.git v0.0.0-20261024233714-f14458a4683c/go.mod h1:4u0jNBtIdMLOsJyrScfqoEuYDJF7BBM3qM9YWbzt6aw= |
| amadan.net/oficina/home/connect v0.0.0-20261024233715-ff16bf56bc4d h1:9sVZBph2xUuejZt73DYaoCfoyvrvZCnuPHcyMsg562E= |
| amadan.net/oficina/home/connect v0.0.0-20261024233715-ff16bf56bc4d/go.mod h1:ix81eFV87yE9f2SXiRuxYCVM4lAgTRC5YoQMiH3+TDk= |
| +amadan.net/oficina/home/dashboard v0.0.0-20261024234842-3774eb68f816 h1:AFDuOio0MxNxN9ZWVc3lul1QHkNjNUCO56kXpe/Fw3I= |
| +amadan.net/oficina/home/dashboard v0.0.0-20261024234842-3774eb68f816/go.mod h1:+7xsX+QyVFrKy5gDAGh+FZ5+75z4CibWTL0wl9DWAw0= |
| amadan.net/oficina/home/handoff v0.0.0-20261024233715-ff16bf56bc4d h1:8RTZkLQnyxvH2g0RMovhN/m2LN9D8qb+kBfqdBeqq+s= |
| amadan.net/oficina/home/handoff v0.0.0-20261024233715-ff16bf56bc4d/go.mod h1:YISpzFJ+iMgrM0rUL032OSzsAQbP4/nozvXLCCOcn1k= |
| amadan.net/rastrillo/native/server v0.0.0-20260923173546-2a5d07b2c8d3 h1:5EFx7BLJj/+G/rt8bTmeyMWVfF5s5Sns7ajTvX4w/I8= |
diff --git a/internal/sheets/app.go b/internal/sheets/app.go| index 86eb225..533f57f 100644 |
| --- a/internal/sheets/app.go |
| +++ b/internal/sheets/app.go |
| @@ -54,6 +54,7 @@ type Config struct { |
| // App holds the wiring. One per process. |
| type App struct { |
| + dashboard http.Handler |
| native *nativeauth.Auth |
| home *connect.Bridge |
| homeRoutes *connect.Routes |
| @@ -177,6 +178,9 @@ func (a *App) Router() http.Handler { |
| // (TestIndexIsNotACatchAll pins both halves). |
| r.NotFound(a.routeNotFound) |
| r.MethodNotAllowed(a.methodNotAllowed(r)) |
| + if a.dashboard != nil { |
| + r.Handle("/api/oficina/dashboard", a.dashboard) |
| + } |
| r.Get("/", a.handleIndex) |
| a.mountAuth(r) |
| if a.native != nil { |
| @@ -278,21 +282,3 @@ func (a *App) Router() http.Handler { |
| }) |
| return r |
| } |
| - |
| -// requireSession is auth.RequireSession in production and a |
| -// pass-through when there is no auth to require one with. |
| -// |
| -// The pass-through is for tests only, and it is deliberate rather than |
| -// accidental: a nil-auth App has no sessions at all, so requiring one |
| -// would make every route below untestable without a mailer, an SMTP |
| -// refusal and a full magic-link round trip. Production cannot reach it |
| -// — main exits before New if sign-in is not configured. |
| -func (a *App) requireSession(next http.Handler) http.Handler { |
| - if a.home != nil { |
| - return a.homeRequire(next) |
| - } |
| - if a.auth == nil { |
| - return next |
| - } |
| - return a.auth.RequireSession(a.rejectDisabledHome(next)) |
| -} |
diff --git a/internal/sheets/dashboard.go b/internal/sheets/dashboard.go| new file mode 100644 |
| index 0000000..6ea76e4 |
| --- /dev/null |
| +++ b/internal/sheets/dashboard.go |
| @@ -0,0 +1,59 @@ |
| +package sheets |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "os" |
| + "strconv" |
| + "time" |
| + |
| + "amadan.net/oficina/home/connect" |
| + "amadan.net/oficina/home/connect/linkstore" |
| + "amadan.net/oficina/home/dashboard" |
| +) |
| + |
| +func (a *App) configureDashboard(c *connect.DecodedCredentials) error { |
| + team := os.Getenv("OFICINA_DASHBOARD_TEAM") |
| + if team == "" { |
| + return nil |
| + } |
| + db, e := a.db.DB() |
| + if e != nil { |
| + return e |
| + } |
| + h, e := dashboard.NewProvider(dashboard.ProviderConfig{DB: db, Origin: a.origin, Issuer: c.Issuer, Directory: c.Directory, Keys: c.Keys, Team: team, Kind: "documents", Resolve: func(ctx context.Context, subject string) (int64, error) { |
| + p, e := a.home.Resolve(ctx, subject) |
| + if errors.Is(e, linkstore.ErrStorage) { |
| + return 0, dashboard.ErrUnavailable |
| + } |
| + return p.ID, e |
| + }, Read: a.dashboardDocuments}) |
| + if e != nil { |
| + return e |
| + } |
| + a.dashboard = h |
| + return nil |
| +} |
| +func (a *App) dashboardDocuments(ctx context.Context, id int64, _ string, _ time.Time) (dashboard.Response, error) { |
| + var viewer Member |
| + if e := a.db.WithContext(ctx).First(&viewer, id).Error; e != nil { |
| + return dashboard.Response{}, e |
| + } |
| + rows, e := a.visibleSheetsLimited(ctx, viewer, dashboard.MaxItems+1) |
| + if e != nil { |
| + return dashboard.Response{}, e |
| + } |
| + out := dashboard.Response{Truncated: len(rows) > dashboard.MaxItems} |
| + if out.Truncated { |
| + rows = rows[:dashboard.MaxItems] |
| + } |
| + for _, s := range rows { |
| + title, e := a.sealer.Open("sheet.title", s.TitleSealed) |
| + if e != nil { |
| + return dashboard.Response{}, e |
| + } |
| + key := strconv.FormatUint(s.ID, 10) |
| + out.Items = append(out.Items, dashboard.Item{ID: key, Title: string(title), URL: a.origin + "/sheets/" + key, Updated: s.UpdatedAt}) |
| + } |
| + return out, nil |
| +} |
diff --git a/internal/sheets/dashboard_test.go b/internal/sheets/dashboard_test.go| new file mode 100644 |
| index 0000000..bfecace |
| --- /dev/null |
| +++ b/internal/sheets/dashboard_test.go |
| @@ -0,0 +1,64 @@ |
| +package sheets |
| + |
| +import ( |
| + "net/http/httptest" |
| + "strings" |
| + "testing" |
| + "time" |
| + |
| + "amadan.net/oficina/home/dashboard" |
| + "github.com/go-json-experiment/json" |
| +) |
| + |
| +func TestDashboardExistingLinkAndCurrentAdmission(t *testing.T) { |
| + t.Setenv("OFICINA_DASHBOARD_TEAM", "blue") |
| + f := newHomeFixtureWith(t, "https://oficina.example.test") |
| + member := f.local(t, signedInAddress) |
| + sheet, e := f.app.createSheet("Linked workbook", member) |
| + if e != nil { |
| + t.Fatal(e) |
| + } |
| + subject := "pr_abcdefghijklmnopqrstuvwxyz" |
| + f.link(t, subject) |
| + call := func(sub string) *httptest.ResponseRecorder { |
| + jti, _ := dashboard.ID() |
| + nonce, _ := dashboard.ID() |
| + now := time.Now().Unix() |
| + token, e := dashboard.Sign(f.key, dashboard.Claims{Version: 1, Issuer: homeTestIssuer, Audience: testOrigin, Directory: "https://oficina.example.test", Subject: sub, Team: "blue", Purpose: "dashboard-read", KeyID: "key", ID: jti, Nonce: nonce, Issued: now, Expires: now + 120}) |
| + if e != nil { |
| + t.Fatal(e) |
| + } |
| + r := httptest.NewRequest("GET", testOrigin+dashboard.Path, nil) |
| + r.Header.Set("Authorization", "Bearer "+token) |
| + w := httptest.NewRecorder() |
| + f.app.Router().ServeHTTP(w, r) |
| + return w |
| + } |
| + w := call(subject) |
| + var data dashboard.Response |
| + if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &data) != nil || len(data.Items) != 1 || data.Items[0].Title != "Linked workbook" || !strings.HasSuffix(data.Items[0].URL, "/sheets/"+data.Items[0].ID) { |
| + t.Fatal("feed", w.Code, w.Body.String()) |
| + } |
| + if w := call("pr_bbbbbbbbbbbbbbbbbbbbbbbbbb"); w.Code != 403 { |
| + t.Fatal("unknown link admitted", w.Code) |
| + } |
| + f.app.admit = func(string) bool { return false } |
| + if e := f.app.db.Where("member_id=?", member.ID).Delete(&Grant{}).Error; e != nil { |
| + t.Fatal(e) |
| + } |
| + if w := call(subject); w.Code != 403 || strings.Contains(w.Body.String(), "Linked workbook") { |
| + t.Fatal("revoked viewer received data", w.Code) |
| + } |
| + _ = sheet |
| +} |
| + |
| +func TestDashboardGuestScope(t *testing.T) { |
| + a, cookies := signIn(t) |
| + shared, _ := newSheet(t, a, cookies, "Shared") |
| + newSheet(t, a, cookies, "Private") |
| + guest := guestOn(t, a, shared.ID, "reader@elsewhere.example", SheetViewer) |
| + data, e := a.dashboardDocuments(t.Context(), int64(guest.ID), "", time.Now()) |
| + if e != nil || len(data.Items) != 1 || data.Items[0].Title != "Shared" { |
| + t.Fatal("guest scope", e, data) |
| + } |
| +} |
diff --git a/internal/sheets/grants.go b/internal/sheets/grants.go| index ffbbf01..abbb4e1 100644 |
| --- a/internal/sheets/grants.go |
| +++ b/internal/sheets/grants.go |
| @@ -1,13 +1,12 @@ |
| package sheets |
| import ( |
| + "amadan.net/oficina/sheets/internal/sheets/store" |
| "errors" |
| "strings" |
| "gorm.io/gorm" |
| "gorm.io/gorm/clause" |
| - |
| - "amadan.net/oficina/sheets/internal/sheets/store" |
| ) |
| // errNoAccess is what every read answers when the viewer may not see a |
| @@ -106,47 +105,6 @@ func (a *App) sheetExists(id uint64) bool { |
| return n == 1 |
| } |
| -// visibleSheets returns the sheets a member may see. |
| -// |
| -// This is the hand-written grants join AGENTS.md requires, and the |
| -// reason it is hand-written rather than scope.Owned: scope.Owned is |
| -// single-column equality on user_id, our sheets are team-shared, and |
| -// applied here it would return only the sheets a person created — which |
| -// fails as a plausible short list rather than as an error. |
| -// |
| -// The query is deliberately one statement rather than "list all, then |
| -// filter in Go". A filter in Go reads every sheet on the instance into |
| -// memory before discarding most of them, which is both wasteful and the |
| -// shape where somebody later forgets the discard. |
| -func (a *App) visibleSheets(m Member) ([]store.Sheet, error) { |
| - if a.db == nil { |
| - return nil, errNoDB |
| - } |
| - // filling_since alongside deleted_at, everywhere the other is |
| - // checked: a sheet still being filled from an imported file is not |
| - // a thing anybody should see yet, and on this instance every team |
| - // member sees every sheet, so "not visible" has to mean not in this |
| - // query rather than not linked to. |
| - q := a.db.Model(&store.Sheet{}). |
| - Where("sheets.deleted_at IS NULL AND sheets.filling_since IS NULL") |
| - |
| - switch TeamRole(m.TeamRole) { |
| - case TeamOwner, TeamMember: |
| - // The whole instance. One team, one set of sheets. |
| - default: |
| - // A guest sees exactly what they were granted. The join is an |
| - // inner join precisely so a guest with no grants gets an empty |
| - // list rather than everything. |
| - q = q.Joins("JOIN grants ON grants.sheet_id = sheets.id AND grants.member_id = ?", m.ID) |
| - } |
| - |
| - var rows []store.Sheet |
| - if err := q.Order("sheets.updated_at DESC, sheets.id DESC").Find(&rows).Error; err != nil { |
| - return nil, err |
| - } |
| - return rows, nil |
| -} |
| - |
| // grantsOn lists who can reach a sheet, for the share screen. |
| type sharedWith struct { |
| Member Member |
diff --git a/internal/sheets/home.go b/internal/sheets/home.go| index edff9ce..8ae77e7 100644 |
| --- a/internal/sheets/home.go |
| +++ b/internal/sheets/home.go |
| @@ -81,7 +81,7 @@ func (a *App) ConfigureHome(raw *connect.Credentials) error { |
| } |
| a.home, a.homeRoutes, a.homeSessions = bridge, routes, sess |
| a.directory = settings.Directory |
| - return nil |
| + return a.configureDashboard(settings) |
| } |
| // The same current admission rule as Admits, using the link transaction's q. |
diff --git a/internal/sheets/migrations.go b/internal/sheets/migrations.go| index d66f731..1d3dc18 100644 |
| --- a/internal/sheets/migrations.go |
| +++ b/internal/sheets/migrations.go |
| @@ -2,6 +2,7 @@ package sheets |
| import ( |
| "amadan.net/oficina/home/connect/linkstore" |
| + "amadan.net/oficina/home/dashboard" |
| "amadan.net/oficina/home/handoff" |
| "embed" |
| @@ -38,7 +39,7 @@ var Schema = migrate.MustFromFS(migrationsFS, "sheets") |
| // sessions table, so sessions.Schema must apply first; migrate.Merge's |
| // argument order IS apply order. Sheets and optional Home protocol schemas |
| // follow the local authentication schemas. |
| -var BootSchema = migrate.Merge(sessions.Schema, auth.Schema, Schema, migrate.MustFromFS(handoff.Migrations, "oficina_handoff"), migrate.MustFromFS(linkstore.Migrations, linkstore.SchemaNamespace)) |
| +var BootSchema = migrate.Merge(migrate.MustFromFS(dashboard.Migrations, "oficina_dashboard"), sessions.Schema, auth.Schema, Schema, migrate.MustFromFS(handoff.Migrations, "oficina_handoff"), migrate.MustFromFS(linkstore.Migrations, linkstore.SchemaNamespace)) |
| // Models lists every GORM model `rastrillo migration generate`/`check` |
| // diff Schema against — Sheet, Tab, Row, Col, Cell, Format and Member |
diff --git a/internal/sheets/session_guard.go b/internal/sheets/session_guard.go| new file mode 100644 |
| index 0000000..767f0d8 |
| --- /dev/null |
| +++ b/internal/sheets/session_guard.go |
| @@ -0,0 +1,21 @@ |
| +package sheets |
| + |
| +import "net/http" |
| + |
| +// requireSession is auth.RequireSession in production and a |
| +// pass-through when there is no auth to require one with. |
| +// |
| +// The pass-through is for tests only, and it is deliberate rather than |
| +// accidental: a nil-auth App has no sessions at all, so requiring one |
| +// would make every route below untestable without a mailer, an SMTP |
| +// refusal and a full magic-link round trip. Production cannot reach it |
| +// — main exits before New if sign-in is not configured. |
| +func (a *App) requireSession(next http.Handler) http.Handler { |
| + if a.home != nil { |
| + return a.homeRequire(next) |
| + } |
| + if a.auth == nil { |
| + return next |
| + } |
| + return a.auth.RequireSession(a.rejectDisabledHome(next)) |
| +} |
diff --git a/internal/sheets/visible_sheets.go b/internal/sheets/visible_sheets.go| new file mode 100644 |
| index 0000000..71793b9 |
| --- /dev/null |
| +++ b/internal/sheets/visible_sheets.go |
| @@ -0,0 +1,53 @@ |
| +package sheets |
| + |
| +import ( |
| + "amadan.net/oficina/sheets/internal/sheets/store" |
| + "context" |
| +) |
| + |
| +// visibleSheets returns the sheets a member may see. |
| +// |
| +// This is the hand-written grants join AGENTS.md requires, and the |
| +// reason it is hand-written rather than scope.Owned: scope.Owned is |
| +// single-column equality on user_id, our sheets are team-shared, and |
| +// applied here it would return only the sheets a person created — which |
| +// fails as a plausible short list rather than as an error. |
| +// |
| +// The query is deliberately one statement rather than "list all, then |
| +// filter in Go". A filter in Go reads every sheet on the instance into |
| +// memory before discarding most of them, which is both wasteful and the |
| +// shape where somebody later forgets the discard. |
| +func (a *App) visibleSheets(m Member) ([]store.Sheet, error) { |
| + return a.visibleSheetsLimited(context.Background(), m, 0) |
| +} |
| +func (a *App) visibleSheetsLimited(ctx context.Context, m Member, limit int) ([]store.Sheet, error) { |
| + if a.db == nil { |
| + return nil, errNoDB |
| + } |
| + // filling_since alongside deleted_at, everywhere the other is |
| + // checked: a sheet still being filled from an imported file is not |
| + // a thing anybody should see yet, and on this instance every team |
| + // member sees every sheet, so "not visible" has to mean not in this |
| + // query rather than not linked to. |
| + q := a.db.WithContext(ctx).Model(&store.Sheet{}). |
| + Where("sheets.deleted_at IS NULL AND sheets.filling_since IS NULL") |
| + |
| + switch TeamRole(m.TeamRole) { |
| + case TeamOwner, TeamMember: |
| + // The whole instance. One team, one set of sheets. |
| + default: |
| + // A guest sees exactly what they were granted. The join is an |
| + // inner join precisely so a guest with no grants gets an empty |
| + // list rather than everything. |
| + q = q.Joins("JOIN grants ON grants.sheet_id = sheets.id AND grants.member_id = ?", m.ID) |
| + } |
| + |
| + if limit > 0 { |
| + q = q.Limit(limit) |
| + } |
| + var rows []store.Sheet |
| + if err := q.Order("sheets.updated_at DESC, sheets.id DESC").Find(&rows).Error; err != nil { |
| + return nil, err |
| + } |
| + return rows, nil |
| +} |