| 1 | package calendar |
| 2 | |
| 3 | // Team mode's route gates, the roster's answer about somebody other than |
| 4 | // the request's own person, and how a Home-only person is named. Split |
| 5 | // from team_access.go so neither passes the 300-line cap. |
| 6 | |
| 7 | import ( |
| 8 | "context" |
| 9 | "net/http" |
| 10 | |
| 11 | "amadan.net/oficina/home/roster" |
| 12 | "amadan.net/oficina/home/teammode" |
| 13 | ) |
| 14 | |
| 15 | // standingNeed is what a route needs of the roster. |
| 16 | type standingNeed int |
| 17 | |
| 18 | const ( |
| 19 | // needMember: Member. Unknown is a retryable 503. Every item page, |
| 20 | // every form, every write and the native API. |
| 21 | needMember standingNeed = iota |
| 22 | // needListing: a view (day, week, month, agenda, the calendar list). |
| 23 | // Unknown renders it partial, with the notice; the page asks again |
| 24 | // after building it (requestUnknown). |
| 25 | needListing |
| 26 | ) |
| 27 | |
| 28 | // standing is the session group's gate, after requirePerson. Outside team |
| 29 | // mode it is no gate at all: next, unchanged. |
| 30 | func (a *App) standing(need standingNeed) func(http.Handler) http.Handler { |
| 31 | return func(next http.Handler) http.Handler { |
| 32 | if a.teamOn == nil { |
| 33 | return next |
| 34 | } |
| 35 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 36 | p, ok := teamPersonFrom(r.Context()) |
| 37 | if !ok { |
| 38 | // requirePerson puts the person on every request that |
| 39 | // reaches here; a route outside it is a wiring bug, and |
| 40 | // the closed answer is the one that cannot leak. |
| 41 | a.h.teamUnavailable(w, r) |
| 42 | return |
| 43 | } |
| 44 | switch p.access.State() { |
| 45 | case roster.Member: |
| 46 | case roster.NotMember: |
| 47 | // Only an unlinked local account gets this far |
| 48 | // (teamRequirePerson): it has no standing until it links. |
| 49 | if r.Method == http.MethodGet || r.Method == http.MethodHead { |
| 50 | w.Header().Set("Cache-Control", "no-store") |
| 51 | http.Redirect(w, r, "/account/home", http.StatusSeeOther) |
| 52 | return |
| 53 | } |
| 54 | a.h.errorPage(w, r, http.StatusNotFound, "") |
| 55 | return |
| 56 | default: |
| 57 | if need != needListing { |
| 58 | a.h.teamUnavailable(w, r) |
| 59 | return |
| 60 | } |
| 61 | } |
| 62 | next.ServeHTTP(w, r) |
| 63 | }) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | // standingOf builds the roster's answer about somebody other than the |
| 68 | // request's own person — a feed's minter, a booking page's host, a |
| 69 | // reminder's recipient — so what it says about them is what their own |
| 70 | // requests would be told. Nil outside team mode. A person the roster |
| 71 | // cannot be asked about (no link, no Home-only row) is NotMember; one |
| 72 | // whose subject cannot be read now is Unknown (a nil Access). |
| 73 | func (a *App) standingOf(ctx context.Context, person int64) *teammode.Access { |
| 74 | if a.team == nil { |
| 75 | return nil |
| 76 | } |
| 77 | subject, err := a.homeSubjectOf(ctx, person) |
| 78 | if err != nil { |
| 79 | return nil |
| 80 | } |
| 81 | return a.team.Access(ctx, subject) |
| 82 | } |
| 83 | |
| 84 | // teamLabel is how a Home-only person, who has no address, is shown where |
| 85 | // an address would be: the account menu. |
| 86 | const teamLabel = "Oficina account" |
| 87 | |
| 88 | // homeOnlyLabel is teamLabel and the last four characters of the person |
| 89 | // ref ("Oficina account …k7qz"), as Home's Team page shows a ref. |
| 90 | func homeOnlyLabel(subject string) string { |
| 91 | if len(subject) <= 4 { |
| 92 | return teamLabel |
| 93 | } |
| 94 | return teamLabel + " …" + subject[len(subject)-4:] |
| 95 | } |
| 96 | |
| 97 | // shownAddress is how a person is named where an address would be: their |
| 98 | // address, or for a Home-only person, who has none, the Home-only label. |
| 99 | // Outside team mode it is exactly Address. |
| 100 | func (p Person) shownAddress() string { |
| 101 | if p.homeOnly != "" { |
| 102 | return homeOnlyLabel(p.homeOnly) |
| 103 | } |
| 104 | return p.Address |
| 105 | } |
| 106 | |