| 1 | package docs |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "net/http" |
| 7 | "os" |
| 8 | "time" |
| 9 | |
| 10 | "amadan.net/oficina/home/dashboard" |
| 11 | "amadan.net/rastrillo/rastrillo/sessions" |
| 12 | ) |
| 13 | |
| 14 | type dashboardAccessKey struct{} |
| 15 | type dashboardAccess struct{ access *requestAccess } |
| 16 | |
| 17 | func (h *handlers) dashboardProvider(home *homeRoutes, c *homeSettings, list allowlist, db *sql.DB) (http.Handler, error) { |
| 18 | team := os.Getenv("OFICINA_DASHBOARD_TEAM") |
| 19 | if team == "" { |
| 20 | return nil, nil |
| 21 | } |
| 22 | // Resolve the same existing principal as the browser, without creating a |
| 23 | // local account or calling EnsureHome. The token itself confers no access. |
| 24 | resolve := func(ctx context.Context, subject string) (requestIdentity, error) { |
| 25 | r, _ := http.NewRequestWithContext(ctx, http.MethodGet, h.origin+"/", nil) |
| 26 | id, e := home.resolver.resolve(sessions.WithSession(r, sessions.Session{Subject: subject, Method: homeAssertionMethod})) |
| 27 | if e != nil { |
| 28 | return requestIdentity{}, e |
| 29 | } |
| 30 | if h.store.team == nil { |
| 31 | ok, e := list.allows(actorAddress(id.Actor)) |
| 32 | if e != nil { |
| 33 | return requestIdentity{}, dashboard.ErrUnavailable |
| 34 | } |
| 35 | if !ok { |
| 36 | return requestIdentity{}, dashboard.ErrRefused |
| 37 | } |
| 38 | } |
| 39 | return id, nil |
| 40 | } |
| 41 | provider, err := dashboard.NewProvider(dashboard.ProviderConfig{DB: db, Origin: h.origin, Issuer: c.issuer, Directory: h.directory, Keys: c.keys, Team: team, Kind: "documents", Resolve: func(ctx context.Context, subject string) (int64, error) { |
| 42 | id, e := resolve(ctx, subject) |
| 43 | return id.Actor.PersonID, e |
| 44 | }, Read: func(ctx context.Context, person int64, subject string, _ time.Time) (dashboard.Response, error) { |
| 45 | id, e := resolve(ctx, subject) |
| 46 | if e != nil || id.Actor.PersonID != person { |
| 47 | return dashboard.Response{}, dashboard.ErrRefused |
| 48 | } |
| 49 | var access *requestAccess |
| 50 | if h.store.team != nil { |
| 51 | access = h.store.team.resolveAccess(ctx, id) |
| 52 | ctx = withAccess(ctx, access) |
| 53 | ctx.Value(dashboardAccessKey{}).(*dashboardAccess).access = access |
| 54 | } |
| 55 | rows, e := h.store.listForLimited(ctx, id.Actor, dashboard.MaxItems+1) |
| 56 | if e != nil { |
| 57 | return dashboard.Response{}, e |
| 58 | } |
| 59 | // Home renders this feed, where no partial notice can be shown, so |
| 60 | // under Unknown it is unavailable rather than short (Paul's ruling |
| 61 | // on list pages, 2026-10-28, condition 3). Asked after listing. |
| 62 | if access != nil && access.Partial() { |
| 63 | return dashboard.Response{}, dashboard.ErrUnavailable |
| 64 | } |
| 65 | out := dashboard.Response{Truncated: len(rows) > dashboard.MaxItems} |
| 66 | if out.Truncated { |
| 67 | rows = rows[:dashboard.MaxItems] |
| 68 | } |
| 69 | for _, d := range rows { |
| 70 | out.Items = append(out.Items, dashboard.Item{ID: d.Sqid, Title: d.Title, URL: h.origin + "/d/" + d.Sqid, Updated: d.UpdatedAt}) |
| 71 | } |
| 72 | return out, nil |
| 73 | }, Write: func(w http.ResponseWriter, r *http.Request, body []byte) { |
| 74 | if h.store.team != nil { |
| 75 | access := r.Context().Value(dashboardAccessKey{}).(*dashboardAccess).access |
| 76 | if access == nil { |
| 77 | http.Error(w, "Dashboard unavailable", 503) |
| 78 | return |
| 79 | } |
| 80 | r = r.WithContext(withAccess(r.Context(), access)) |
| 81 | } |
| 82 | _ = writeGuardedBody(w, r, 200, body) |
| 83 | }}) |
| 84 | if err != nil { |
| 85 | return nil, err |
| 86 | } |
| 87 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 88 | provider.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), dashboardAccessKey{}, &dashboardAccess{}))) |
| 89 | }), nil |
| 90 | } |
| 91 | |