oficina / calendar Public

Add scoped read-only Oficina dashboard feed

Paul Campbell pushed by paul@keymail.dev 0cdee289170fc6b119cea2b215b56dd6085a939e parent 7a2ad04
7 files changed, +222 −9
  • go.mod +4 −1
  • go.sum +2 −0
  • internal/calendar/app.go +13 −7
  • internal/calendar/dashboard.go +87 −0
  • internal/calendar/dashboard_test.go +111 −0
  • internal/calendar/migrations.go +2 −1
  • internal/calendar/routes.go +3 −0

CI failed — run details

diff --git a/go.mod b/go.mod
index fb14bc4..895b8d7 100644
--- a/go.mod
+++ b/go.mod
@@ -42,7 +42,10 @@ require (
modernc.org/sqlite v1.55.0 // indirect
)
-require amadan.net/rastrillo/native/server v0.0.0-20260923173546-2a5d07b2c8d3
+require (
+ amadan.net/oficina/home/dashboard v0.0.0-20261024234842-3774eb68f816
+ amadan.net/rastrillo/native/server v0.0.0-20260923173546-2a5d07b2c8d3
+)
require (
amadan.net/rastrillo/rastrillo/money v0.1.0 // indirect
diff --git a/go.sum b/go.sum
index 19d841f..1c8112c 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/calendar/app.go b/internal/calendar/app.go
index 0a5d5a1..43dccb2 100644
--- a/internal/calendar/app.go
+++ b/internal/calendar/app.go
@@ -30,13 +30,14 @@ import (
// development mail recorder both hang off this struct, and every later
// test depends on them.
type App struct {
- native *nativeauth.Auth
- home *homeConnection
- cfg Config
- db *db.DB
- mux *http.ServeMux
- logger *slog.Logger
- store *Store
+ dashboard http.Handler
+ native *nativeauth.Auth
+ home *homeConnection
+ cfg Config
+ db *db.DB
+ mux *http.ServeMux
+ logger *slog.Logger
+ store *Store
// mailRecorder is non-nil only in development — see buildMailer.
mailRecorder *recordingMailer
@@ -176,6 +177,11 @@ func New(cfg Config) (*App, error) {
d.Close()
return nil, err
}
+ if err = a.configureDashboard(homeSettings); err != nil {
+ signinMail.close()
+ d.Close()
+ return nil, err
+ }
h.homeEnabled = true
h.directory = homeSettings.Directory
h.viewerFrom = func(r *http.Request) (rauth.Identity, bool) {
diff --git a/internal/calendar/dashboard.go b/internal/calendar/dashboard.go
new file mode 100644
index 0000000..8f75b5c
--- /dev/null
+++ b/internal/calendar/dashboard.go
@@ -0,0 +1,87 @@
+package calendar
+
+import (
+ "context"
+ "errors"
+ "os"
+ "sort"
+ "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
+ }
+ 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: "events", Resolve: func(ctx context.Context, subject string) (int64, error) {
+ p, e := a.home.bridge.Resolve(ctx, subject)
+ if errors.Is(e, linkstore.ErrStorage) {
+ return 0, dashboard.ErrUnavailable
+ }
+ return p.ID, e
+ }, Read: a.dashboardEvents})
+ if e != nil {
+ return e
+ }
+ a.dashboard = h
+ return nil
+}
+func (a *App) dashboardEvents(ctx context.Context, id int64, _ string, now time.Time) (dashboard.Response, error) {
+ var person Person
+ if e := a.db.G.WithContext(ctx).First(&person, id).Error; e != nil {
+ return dashboard.Response{}, e
+ }
+ zone := person.Zone
+ if zone == "" {
+ zone = "UTC"
+ }
+ loc, e := time.LoadLocation(zone)
+ if e != nil {
+ return dashboard.Response{}, e
+ }
+ local := now.In(loc)
+ from := time.Date(local.Year(), local.Month(), local.Day(), 0, 0, 0, 0, loc)
+ until := from.AddDate(0, 0, 8)
+ rows, e := a.store.EventsInRangeStrict(ctx, id, from.AddDate(0, 0, -2).Unix(), until.AddDate(0, 0, 2).Unix())
+ if e != nil {
+ return dashboard.Response{}, e
+ }
+ out := dashboard.Response{Timezone: zone}
+ for _, event := range rows {
+ if event.Status == "cancelled" || (!event.AllDay && (event.OccEndUTC <= now.Unix() || event.OccStartUTC >= until.Unix())) {
+ continue
+ }
+ item := dashboard.Item{ID: strconv.FormatInt(event.ID, 10) + ":" + strconv.FormatInt(event.OccStartUTC, 10), Title: event.Title, URL: a.cfg.Origin + eventHref(event), Start: time.Unix(event.OccStartUTC, 0).UTC(), End: time.Unix(event.OccEndUTC, 0).UTC(), AllDay: event.AllDay, Calendar: event.CalendarName}
+ if event.AllDay {
+ item.StartDate = event.StartLocal
+ if event.RRule != "" && event.SeriesID == nil && event.OccurrenceLocal != "" {
+ item.StartDate = event.OccurrenceLocal
+ }
+ masterStart, e1 := time.Parse(LayoutDate, event.StartLocal)
+ masterEnd, e2 := time.Parse(LayoutDate, event.EndLocal)
+ occurrence, e3 := time.Parse(LayoutDate, item.StartDate)
+ if e1 != nil || e2 != nil || e3 != nil {
+ return dashboard.Response{}, dashboard.ErrUnavailable
+ }
+ item.EndDate = occurrence.Add(masterEnd.Sub(masterStart)).Format(LayoutDate)
+ if item.EndDate <= from.Format(LayoutDate) || item.StartDate >= until.Format(LayoutDate) {
+ continue
+ }
+
+ item.Start, _ = time.ParseInLocation(LayoutDate, item.StartDate, loc)
+ item.End, _ = time.ParseInLocation(LayoutDate, item.EndDate, loc)
+ }
+ out.Items = append(out.Items, item)
+ }
+ sort.SliceStable(out.Items, func(i, j int) bool { return out.Items[i].Start.Before(out.Items[j].Start) })
+ if len(out.Items) > dashboard.MaxItems {
+ out.Truncated = true
+ out.Items = out.Items[:dashboard.MaxItems]
+ }
+ return out, nil
+}
diff --git a/internal/calendar/dashboard_test.go b/internal/calendar/dashboard_test.go
new file mode 100644
index 0000000..51d7f25
--- /dev/null
+++ b/internal/calendar/dashboard_test.go
@@ -0,0 +1,111 @@
+package calendar
+
+import (
+ "amadan.net/oficina/home/dashboard"
+ "github.com/go-json-experiment/json"
+ "net/http/httptest"
+ "testing"
+ "time"
+)
+
+func TestDashboardCalendarExistingLink(t *testing.T) {
+ t.Setenv("OFICINA_DASHBOARD_TEAM", "blue")
+ f := newHomeConsumerFixture(t, "https://oficina.example.test")
+ f.local(t)
+ if w := f.do("GET", "/day/2026-10-26", ""); w.Code != 200 {
+ t.Fatal("local calendar", w.Code)
+ }
+ subject := "pr_abcdefghijklmnopqrstuvwxyz"
+ start := homeRedirect(t, f.do("POST", "/account/home", ""))
+ homeRedirect(t, f.exchange(t, start, subject))
+ homeRedirect(t, f.do("POST", "/account/home/confirm", ""))
+ 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: homeTestOrigin, 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", homeTestOrigin+dashboard.Path, nil)
+ r.Header.Set("Authorization", "Bearer "+token)
+ w := httptest.NewRecorder()
+ f.app.mux.ServeHTTP(w, r)
+ return w
+ }
+ w := call(subject)
+ var data dashboard.Response
+ if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &data) != nil || data.Items == nil || data.Timezone == "" {
+ t.Fatal("feed", w.Code, w.Body.String())
+ }
+ if w := call("pr_bbbbbbbbbbbbbbbbbbbbbbbbbb"); w.Code != 403 {
+ t.Fatal("unknown link", w.Code)
+ }
+ delete(f.app.home.admitted.addrs, "alice@example.test")
+ if w := call(subject); w.Code != 403 {
+ t.Fatal("offboarded viewer", w.Code)
+ }
+}
+func TestDashboardCalendarCivilDatesRecurrenceAndScope(t *testing.T) {
+ f := newHomeRouterFixture(t)
+ f.local(t)
+ if w := f.do("GET", "/day/2026-10-26", ""); w.Code != 200 {
+ t.Fatal("local calendar", w.Code)
+ }
+ a := f.app
+ ctx := t.Context()
+ var p Person
+ if e := a.db.G.Where("address=?", "alice@example.test").First(&p).Error; e != nil {
+ t.Fatal(e)
+ }
+ if e := a.db.G.Model(&p).Update("zone", "America/Los_Angeles").Error; e != nil {
+ t.Fatal(e)
+ }
+ cal, e := a.store.DefaultCalendarID(ctx, p.ID)
+ if e != nil {
+ t.Fatal(e)
+ }
+ makeEvent := func(in EventInput) {
+ t.Helper()
+ in.CalendarID = cal
+ if _, e := a.store.CreateEvent(ctx, p.ID, in); e != nil {
+ t.Fatal(e)
+ }
+ }
+ makeEvent(EventInput{Title: "Yesterday", AllDay: true, StartLocal: "2026-10-25", EndLocal: "2026-10-26", TZ: "Pacific/Honolulu"})
+ makeEvent(EventInput{Title: "First day", AllDay: true, StartLocal: "2026-10-26", EndLocal: "2026-10-27", TZ: "Pacific/Kiritimati"})
+ makeEvent(EventInput{Title: "Tomorrow", AllDay: true, StartLocal: "2026-10-27", EndLocal: "2026-10-28", TZ: "Pacific/Kiritimati"})
+ makeEvent(EventInput{Title: "Last day", AllDay: true, StartLocal: "2026-11-02", EndLocal: "2026-11-03", TZ: "Pacific/Honolulu"})
+ makeEvent(EventInput{Title: "Outside", AllDay: true, StartLocal: "2026-11-03", EndLocal: "2026-11-04", TZ: "Pacific/Kiritimati"})
+ makeEvent(EventInput{Title: "Afternoon", StartLocal: "2026-10-26T15:00", EndLocal: "2026-10-26T16:00", TZ: "America/Los_Angeles"})
+ makeEvent(EventInput{Title: "Recurring", StartLocal: "2026-10-27T09:00", EndLocal: "2026-10-27T10:00", TZ: "America/Los_Angeles", RRule: "FREQ=DAILY;COUNT=2"})
+ other, e := a.store.EnsurePersonSetup(ctx, "other@example.test")
+ if e != nil {
+ t.Fatal(e)
+ }
+ otherCal, e := a.store.DefaultCalendarID(ctx, other.ID)
+ if e != nil {
+ t.Fatal(e)
+ }
+ if _, e := a.store.CreateEvent(ctx, other.ID, EventInput{CalendarID: otherCal, Title: "Private", StartLocal: "2026-10-26T16:00", EndLocal: "2026-10-26T17:00", TZ: "America/Los_Angeles"}); e != nil {
+ t.Fatal(e)
+ }
+ now := time.Date(2026, 10, 26, 12, 0, 0, 0, time.UTC)
+ data, e := a.dashboardEvents(ctx, p.ID, "", now)
+ if e != nil {
+ t.Fatal(e)
+ }
+ counts := map[string]int{}
+ for i, item := range data.Items {
+ counts[item.Title]++
+ if i > 0 && item.Start.Before(data.Items[i-1].Start) {
+ t.Fatal("agenda unordered")
+ }
+ }
+ if counts["Yesterday"] != 0 || counts["Outside"] != 0 || counts["Private"] != 0 || counts["First day"] != 1 || counts["Last day"] != 1 || counts["Recurring"] != 2 {
+ t.Fatal(counts)
+ }
+ if len(data.Items) < 3 || data.Items[0].Title != "First day" || data.Items[1].Title != "Afternoon" || data.Items[2].Title != "Tomorrow" {
+ t.Fatal("civil order", data.Items)
+ }
+}
diff --git a/internal/calendar/migrations.go b/internal/calendar/migrations.go
index a72e45d..8e9050d 100644
--- a/internal/calendar/migrations.go
+++ b/internal/calendar/migrations.go
@@ -1,6 +1,7 @@
package calendar
import (
+ "amadan.net/oficina/home/dashboard"
"embed"
"amadan.net/rastrillo/rastrillo/migrate"
@@ -36,4 +37,4 @@ var Schema = migrate.MustFromFS(migrationFS, "calendar")
// sessions table, so sessions.Schema must apply first. Get this line
// wrong and boot either skips a subsystem's tables or fails on a
// migration that references one that does not exist yet.
-var BootSchema = migrate.Merge(sessions.Schema, authSchema, Schema)
+var BootSchema = migrate.Merge(migrate.MustFromFS(dashboard.Migrations, "oficina_dashboard"), sessions.Schema, authSchema, Schema)
diff --git a/internal/calendar/routes.go b/internal/calendar/routes.go
index d77014f..69bf950 100644
--- a/internal/calendar/routes.go
+++ b/internal/calendar/routes.go
@@ -38,6 +38,9 @@ func (a *App) routes(h *handlers, authPlugin *rauth.Auth, list allowlist, origin
}
r.Handle("/native/*", a.native)
+ if a.dashboard != nil {
+ r.Handle("/api/oficina/dashboard", a.dashboard)
+ }
r.Get("/help/open-source", h.credits)
r.NotFound(h.notFound)