Merge branch 'feat/dashboard-feed' into main
Provide authorized recent presentations for the Oficina dashboard
10 files changed,
+126
−5
go.mod+2 −1go.sum+2 −0internal/slides/app.go+3 −0internal/slides/dashboard.go+49 −0internal/slides/dashboard_test.go+59 −0internal/slides/handlers.go+2 −0internal/slides/home.go+3 −0internal/slides/home_test.go+3 −2internal/slides/migrations.go+2 −1scripts/credits.py+1 −1
CI passed — run details
landed from feat/dashboard-feed
diff --git a/go.mod b/go.mod| index 54b3f0c..e34d496 100644 |
| --- a/go.mod |
| +++ b/go.mod |
| @@ -7,11 +7,13 @@ toolchain go1.26.5 |
| 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/chromedp/cdproto v0.0.0-20250724212937-08a3db8b4327 |
| github.com/chromedp/chromedp v0.14.2 |
| github.com/go-chi/chi/v5 v5.3.2 |
| + github.com/go-json-experiment/json v0.0.0-20250725192818-e39067aee2d2 |
| github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e |
| github.com/tetratelabs/wazero v1.12.0 |
| golang.org/x/image v0.44.0 |
| @@ -24,7 +26,6 @@ require ( |
| github.com/carlosframework/rastrillo v0.23.0 // indirect |
| github.com/chromedp/sysutil v1.1.0 // indirect |
| github.com/dustin/go-humanize v1.0.1 // indirect |
| - github.com/go-json-experiment/json v0.0.0-20250725192818-e39067aee2d2 // indirect |
| github.com/gobwas/httphead v0.1.0 // indirect |
| github.com/gobwas/pool v0.2.1 // indirect |
| github.com/gobwas/ws v1.4.0 // indirect |
diff --git a/go.sum b/go.sum| index 4614bc5..0268aa1 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/rastrillo v0.27.1-0.20261006204347-fb806a287060 h1:K2PXcCKfm+AYrqgH1ea6cNsFs4sN7MKLvq+uBY11NZE= |
diff --git a/internal/slides/app.go b/internal/slides/app.go| index 349aa82..fd303bd 100644 |
| --- a/internal/slides/app.go |
| +++ b/internal/slides/app.go |
| @@ -68,6 +68,9 @@ func New(d *db.DB, cfg Config) (*http.ServeMux, *app, error) { |
| r.NotFound(a.routeNotFound) |
| r.MethodNotAllowed(a.methodNotAllowed(r)) |
| + if a.dashboard != nil { |
| + r.Handle("/api/oficina/dashboard", a.dashboard) |
| + } |
| r.Get("/help/open-source", a.credits) |
| // The account menu's Appearance (estilo chrome). Inside the app-wide |
| // CSRF check above and outside the session group below: a session |
diff --git a/internal/slides/dashboard.go b/internal/slides/dashboard.go| new file mode 100644 |
| index 0000000..05eda2e |
| --- /dev/null |
| +++ b/internal/slides/dashboard.go |
| @@ -0,0 +1,49 @@ |
| +package slides |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "os" |
| + "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 |
| + } |
| + h, e := dashboard.NewProvider(dashboard.ProviderConfig{DB: a.db.Writer(), Origin: a.cfg.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 User |
| + if e := a.db.G.WithContext(ctx).First(&viewer, id).Error; e != nil { |
| + return dashboard.Response{}, e |
| + } |
| + var rows []Deck |
| + if e := a.visibleDecks(ctx, viewer).Order("decks.updated_at DESC, decks.id DESC").Limit(dashboard.MaxItems + 1).Find(&rows).Error; e != nil { |
| + return dashboard.Response{}, e |
| + } |
| + out := dashboard.Response{Truncated: len(rows) > dashboard.MaxItems} |
| + if out.Truncated { |
| + rows = rows[:dashboard.MaxItems] |
| + } |
| + for _, d := range rows { |
| + out.Items = append(out.Items, dashboard.Item{ID: d.Sqid, Title: d.Title, URL: a.cfg.Origin + "/d/" + d.Sqid, Updated: d.UpdatedAt}) |
| + } |
| + return out, nil |
| +} |
diff --git a/internal/slides/dashboard_test.go b/internal/slides/dashboard_test.go| new file mode 100644 |
| index 0000000..9d4a423 |
| --- /dev/null |
| +++ b/internal/slides/dashboard_test.go |
| @@ -0,0 +1,59 @@ |
| +package slides |
| + |
| +import ( |
| + "amadan.net/oficina/home/dashboard" |
| + "github.com/go-json-experiment/json" |
| + "net/http/httptest" |
| + "testing" |
| + "time" |
| +) |
| + |
| +func TestDashboardScopedDecksAndExistingLink(t *testing.T) { |
| + t.Setenv("OFICINA_DASHBOARD_TEAM", "blue") |
| + mux, a, u, key := homeFixtureDirectory(t, "https://oficina.example.com") |
| + subject := "pr_abcdefghijklmnopqrstuvwxyz" |
| + homeLinkedSubject(t, a, u, subject) |
| + for _, row := range []Deck{{Sqid: "own", Title: "Own", OwnerID: u.ID, Rung: "private"}, {Sqid: "private", Title: "Hidden", OwnerID: 999, Rung: "private"}, {Sqid: "shared", Title: "Shared", OwnerID: 999, Rung: "granted"}} { |
| + if e := a.db.G.Create(&row).Error; e != nil { |
| + t.Fatal(e) |
| + } |
| + } |
| + var shared Deck |
| + a.db.G.Where("sqid=?", "shared").First(&shared) |
| + if e := a.db.G.Create(&DeckGrant{DeckID: shared.ID, UserID: u.ID}).Error; e != nil { |
| + t.Fatal(e) |
| + } |
| + call := func(sub string) *httptest.ResponseRecorder { |
| + jti, _ := dashboard.ID() |
| + nonce, _ := dashboard.ID() |
| + now := time.Now().Unix() |
| + token, e := dashboard.Sign(key, dashboard.Claims{Version: 1, Issuer: "https://home.example.com", Audience: homeTestOrigin, Directory: "https://oficina.example.com", Subject: sub, Team: "blue", Purpose: "dashboard-read", KeyID: "test", ID: jti, Nonce: nonce, Issued: now, Expires: now + 120}) |
| + if e != nil { |
| + t.Fatal(e) |
| + } |
| + r := httptest.NewRequest("GET", homeTestOrigin+dashboard.Path, nil) |
| + r.Header.Set("Authorization", "Bearer "+token) |
| + w := httptest.NewRecorder() |
| + mux.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) != 2 { |
| + t.Fatal("feed", w.Code, w.Body.String()) |
| + } |
| + for _, item := range data.Items { |
| + if item.Title == "Hidden" { |
| + t.Fatal("private deck exposed") |
| + } |
| + } |
| + if w := call("pr_bbbbbbbbbbbbbbbbbbbbbbbbbb"); w.Code != 403 { |
| + t.Fatal("unknown link", w.Code) |
| + } |
| + if e := a.db.G.Where("id=?", u.ID).Delete(&User{}).Error; e != nil { |
| + t.Fatal(e) |
| + } |
| + if w := call(subject); w.Code != 403 { |
| + t.Fatal("deleted viewer", w.Code) |
| + } |
| +} |
diff --git a/internal/slides/handlers.go b/internal/slides/handlers.go| index 2de7561..a6c1575 100644 |
| --- a/internal/slides/handlers.go |
| +++ b/internal/slides/handlers.go |
| @@ -3,6 +3,7 @@ package slides |
| import ( |
| "context" |
| "log/slog" |
| + "net/http" |
| "time" |
| "amadan.net/oficina/home/connect" |
| @@ -26,6 +27,7 @@ import ( |
| // The handlers themselves live beside what they serve: sign-in in |
| // auth.go, the deck screens in decks.go. |
| type app struct { |
| + dashboard http.Handler |
| home *connect.Bridge |
| homeRoutes *connect.Routes |
| homeSessions *sessions.Sessions |
diff --git a/internal/slides/home.go b/internal/slides/home.go| index b9221a8..1140c98 100644 |
| --- a/internal/slides/home.go |
| +++ b/internal/slides/home.go |
| @@ -77,6 +77,9 @@ func (a *app) initHome(c *connect.DecodedCredentials) error { |
| } |
| a.home = b |
| a.directory = c.Directory |
| + if err := a.configureDashboard(c); err != nil { |
| + return err |
| + } |
| a.homeRoutes, err = connect.NewRoutes(b, connect.RoutesConfig{Namespace: "oficina/slides", Origin: a.cfg.Origin, Issuer: c.Issuer, DEK: c.DEK, SessionCookie: sess.CookieName(), ReadSession: read, SignOut: sess.SignOut, LocalSignin: a.signin, Protect: csrf.Protect(a.cfg.Origin), Render: a.renderHome, FormPolicy: connect.FormPolicy(c.Issuer)}) |
| return err |
| } |
diff --git a/internal/slides/home_test.go b/internal/slides/home_test.go| index 5f9a885..2009570 100644 |
| --- a/internal/slides/home_test.go |
| +++ b/internal/slides/home_test.go |
| @@ -77,7 +77,8 @@ func homeSession(t *testing.T, a *app, subject, method string) *http.Cookie { |
| t.Fatal("missing session cookie") |
| return nil |
| } |
| -func homeLinked(t *testing.T, a *app, u User) { |
| +func homeLinked(t *testing.T, a *app, u User) { homeLinkedSubject(t, a, u, "home-person") } |
| +func homeLinkedSubject(t *testing.T, a *app, u User, subject string) { |
| t.Helper() |
| ctx := context.Background() |
| now := time.Now() |
| @@ -102,7 +103,7 @@ func homeLinked(t *testing.T, a *app, u User) { |
| if err := store.BindNonce(ctx, init, local, nonce, now); err != nil { |
| t.Fatal(err) |
| } |
| - proof, err := store.Complete(ctx, init, local, linkstore.Home{Issuer: c.Issuer, Subject: "home-person", Nonce: nonce, AuthTime: now}, now) |
| + proof, err := store.Complete(ctx, init, local, linkstore.Home{Issuer: c.Issuer, Subject: subject, Nonce: nonce, AuthTime: now}, now) |
| if err != nil { |
| t.Fatal(err) |
| } |
diff --git a/internal/slides/migrations.go b/internal/slides/migrations.go| index 870ba98..9b1d37f 100644 |
| --- a/internal/slides/migrations.go |
| +++ b/internal/slides/migrations.go |
| @@ -1,6 +1,7 @@ |
| package slides |
| import ( |
| + "amadan.net/oficina/home/dashboard" |
| "embed" |
| rauth "amadan.net/rastrillo/rastrillo/auth" |
| @@ -37,4 +38,4 @@ var Schema = migrate.MustFromFS(migrationFS, "slides") |
| // must apply before auth.Schema, and this app's own Schema last. |
| // Get this line wrong and either boot silently skips a subsystem's |
| // tables, or generate/check try to drop them. |
| -var BootSchema = migrate.Merge(sessions.Schema, rauth.Schema, Schema) |
| +var BootSchema = migrate.Merge(migrate.MustFromFS(dashboard.Migrations, "oficina_dashboard"), sessions.Schema, rauth.Schema, Schema) |
diff --git a/scripts/credits.py b/scripts/credits.py| index ad816ae..cfb1951 100755 |
| --- a/scripts/credits.py |
| +++ b/scripts/credits.py |
| @@ -12,7 +12,7 @@ from html.parser import HTMLParser |
| ROOT = Path(__file__).resolve().parent.parent |
| OUT = ROOT / 'internal/slides/credits' |
| ENV = os.environ | {'GOPRIVATE': 'amadan.net/oficina/estilo.git'} |
| -EXCLUDE = ','.join(['amadan.net/oficina/estilo.git', 'amadan.net/oficina/home/handoff', 'amadan.net/oficina/home/connect']) |
| +EXCLUDE = ','.join(['amadan.net/oficina/home/dashboard', 'amadan.net/oficina/estilo.git', 'amadan.net/oficina/home/handoff', 'amadan.net/oficina/home/connect']) |
| def run(args, cwd=ROOT, env=ENV): |