| 1 | package sheets |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "os" |
| 7 | "strconv" |
| 8 | "time" |
| 9 | |
| 10 | "amadan.net/oficina/home/connect" |
| 11 | "amadan.net/oficina/home/connect/linkstore" |
| 12 | "amadan.net/oficina/home/dashboard" |
| 13 | "amadan.net/oficina/home/roster" |
| 14 | ) |
| 15 | |
| 16 | func (a *App) configureDashboard(c *connect.DecodedCredentials) error { |
| 17 | team := os.Getenv("OFICINA_DASHBOARD_TEAM") |
| 18 | if team == "" { |
| 19 | return nil |
| 20 | } |
| 21 | db, e := a.db.DB() |
| 22 | if e != nil { |
| 23 | return e |
| 24 | } |
| 25 | 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) { |
| 26 | // In team mode this finds a Home-only member too (connect's |
| 27 | // HomeOnly hook). Identity only; Read asks the roster for standing. |
| 28 | p, e := a.home.Resolve(ctx, subject) |
| 29 | if errors.Is(e, linkstore.ErrStorage) { |
| 30 | return 0, dashboard.ErrUnavailable |
| 31 | } |
| 32 | return p.ID, e |
| 33 | }, Read: a.dashboardDocuments}) |
| 34 | if e != nil { |
| 35 | return e |
| 36 | } |
| 37 | a.dashboard = h |
| 38 | return nil |
| 39 | } |
| 40 | func (a *App) dashboardDocuments(ctx context.Context, id int64, subject string, _ time.Time) (dashboard.Response, error) { |
| 41 | var viewer Member |
| 42 | if e := a.db.WithContext(ctx).First(&viewer, id).Error; e != nil { |
| 43 | return dashboard.Response{}, e |
| 44 | } |
| 45 | if a.team != nil { |
| 46 | // The roster's standing for the subject that asked, never the |
| 47 | // stored team_role (team_access.go). |
| 48 | viewer = (&teamAccess{member: viewer, Access: a.team.Access(ctx, subject)}).actor() |
| 49 | } |
| 50 | rows, e := a.visibleSheetsLimited(ctx, viewer, dashboard.MaxItems+1) |
| 51 | if e != nil { |
| 52 | return dashboard.Response{}, e |
| 53 | } |
| 54 | // Unknown standing is unavailable, not a short list: the feed is read |
| 55 | // on another origin, by a reader who never sees the sheet list's |
| 56 | // partial notice (teammode-package.md §4, condition 3). Asked after |
| 57 | // listing, as the sheet list does, because a lease can lapse during it. |
| 58 | if a.team != nil && viewer.team.state() == roster.Unknown { |
| 59 | return dashboard.Response{}, dashboard.ErrUnavailable |
| 60 | } |
| 61 | out := dashboard.Response{Truncated: len(rows) > dashboard.MaxItems} |
| 62 | if out.Truncated { |
| 63 | rows = rows[:dashboard.MaxItems] |
| 64 | } |
| 65 | for _, s := range rows { |
| 66 | title, e := a.sealer.Open("sheet.title", s.TitleSealed) |
| 67 | if e != nil { |
| 68 | return dashboard.Response{}, e |
| 69 | } |
| 70 | key := strconv.FormatUint(s.ID, 10) |
| 71 | out.Items = append(out.Items, dashboard.Item{ID: key, Title: string(title), URL: a.origin + "/sheets/" + key, Updated: s.UpdatedAt}) |
| 72 | } |
| 73 | return out, nil |
| 74 | } |
| 75 | |