oficina / sheets Public

Add scoped read-only Oficina dashboard feed

Paul Campbell pushed by paul@keymail.dev a8f96769e890d0239280689f3d4212f97224407c parent 2586c65
8 files changed, +141 −3
  • go.mod +1 −0
  • go.sum +2 −0
  • internal/sheets/app.go +4 −0
  • internal/sheets/dashboard.go +59 −0
  • internal/sheets/dashboard_test.go +64 −0
  • internal/sheets/grants.go +8 −1
  • internal/sheets/home.go +1 −1
  • internal/sheets/migrations.go +2 −1

CI cancelled — superseded by a newer push — run details

diff --git a/go.mod b/go.mod
index 7603c81..3882000 100644
--- a/go.mod
+++ b/go.mod
@@ -5,6 +5,7 @@ go 1.25.0
require (
amadan.net/oficina/estilo.git v0.0.0-20261024225350-d53b8e9533a1
amadan.net/oficina/home/connect v0.0.0-20261001093757-2bec942ddd0c
+ amadan.net/oficina/home/dashboard v0.0.0-20261024234842-3774eb68f816
amadan.net/oficina/home/handoff v0.0.0-20260908102137-481d2d57f489
amadan.net/rastrillo/rastrillo v0.27.1-0.20261006204347-fb806a287060
github.com/carlosframework/rastrillo v0.23.0
diff --git a/go.sum b/go.sum
index 659544f..d739bbe 100644
--- a/go.sum
+++ b/go.sum
@@ -2,6 +2,8 @@ amadan.net/oficina/estilo.git v0.0.0-20261024225350-d53b8e9533a1 h1:Xaidrm9rzFyn
amadan.net/oficina/estilo.git v0.0.0-20261024225350-d53b8e9533a1/go.mod h1:4u0jNBtIdMLOsJyrScfqoEuYDJF7BBM3qM9YWbzt6aw=
amadan.net/oficina/home/connect v0.0.0-20261001093757-2bec942ddd0c h1:3yxO2D8BxDLMGfFV2o6Sp9Z91yLnjDCIuRKHVWORa8A=
amadan.net/oficina/home/connect v0.0.0-20261001093757-2bec942ddd0c/go.mod h1:YroavXe43Wietec+po8mtSk4a9Bir2h31Q3iAf4MWxI=
+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-20260908102137-481d2d57f489 h1:RPKtyry/71i06o8L+gjAaUyexbmG56kO1YPs601URAM=
amadan.net/oficina/home/handoff v0.0.0-20260908102137-481d2d57f489/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..360a426 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 {
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..8931a9b 100644
--- a/internal/sheets/grants.go
+++ b/internal/sheets/grants.go
@@ -1,6 +1,7 @@
package sheets
import (
+ "context"
"errors"
"strings"
@@ -119,6 +120,9 @@ func (a *App) sheetExists(id uint64) bool {
// 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
}
@@ -127,7 +131,7 @@ func (a *App) visibleSheets(m Member) ([]store.Sheet, error) {
// 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{}).
+ q := a.db.WithContext(ctx).Model(&store.Sheet{}).
Where("sheets.deleted_at IS NULL AND sheets.filling_since IS NULL")
switch TeamRole(m.TeamRole) {
@@ -140,6 +144,9 @@ func (a *App) visibleSheets(m Member) ([]store.Sheet, error) {
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
diff --git a/internal/sheets/home.go b/internal/sheets/home.go
index e744ad8..2eb1937 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