| 1 | package calendar |
| 2 | |
| 3 | // Team mode's answers: the retryable 503 for Unknown standing, the partial |
| 4 | // list's notice, and the probe. None of it is reached outside team mode. |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "errors" |
| 9 | "net/http" |
| 10 | "strconv" |
| 11 | "strings" |
| 12 | "time" |
| 13 | |
| 14 | "amadan.net/oficina/home/roster" |
| 15 | "amadan.net/oficina/home/teammode" |
| 16 | ) |
| 17 | |
| 18 | // retryAfter is the roster's own retry interval, in whole seconds, as |
| 19 | // every team-mode 503 says it. |
| 20 | var retryAfter = strconv.Itoa(int(roster.RetryAfter / time.Second)) |
| 21 | |
| 22 | // teamUnavailable is the retryable answer for Unknown standing: the app's |
| 23 | // own 503 page, or a plain 503 for the native API and anything else that |
| 24 | // did not ask for a page — never cached, with Retry-After. A 404 would |
| 25 | // tell a team member their event is gone because Home was slow. |
| 26 | func (h *handlers) teamUnavailable(w http.ResponseWriter, r *http.Request) { |
| 27 | w.Header().Set("Cache-Control", "no-store") |
| 28 | w.Header().Set("Retry-After", retryAfter) |
| 29 | if strings.HasPrefix(r.URL.Path, "/api/") { |
| 30 | http.Error(w, "Team access could not be checked just now. Try again in a moment.", http.StatusServiceUnavailable) |
| 31 | return |
| 32 | } |
| 33 | h.errorPage(w, r, http.StatusServiceUnavailable, "") |
| 34 | } |
| 35 | |
| 36 | // teamUnavailablePlain is teamUnavailable for a reader that is not a |
| 37 | // person at a page: a subscription feed, the public booking routes. |
| 38 | func teamUnavailablePlain(w http.ResponseWriter) { |
| 39 | w.Header().Set("Cache-Control", "no-store") |
| 40 | w.Header().Set("Retry-After", retryAfter) |
| 41 | http.Error(w, "Unavailable just now. Try again in a moment.", http.StatusServiceUnavailable) |
| 42 | } |
| 43 | |
| 44 | // requestUnknown reports whether this request's own standing is Unknown |
| 45 | // now. It is also the partial-view rule (Paul, 2026-10-28, Home |
| 46 | // implementation/teammode-package.md §4): the page shell asks it as the |
| 47 | // page is drawn, after the view is built, of the same Access the view was |
| 48 | // built with — State latches, so a lease that lapsed while building marks |
| 49 | // the page partial. The store has already withheld the team rung |
| 50 | // (team_reach.go), so a partial view holds what the person reaches |
| 51 | // without standing, which in Calendar, with no shares, is nothing. |
| 52 | // NotMember is never partial: it never reaches a view. Always false outside team mode, and for a request with no person |
| 53 | // (a public page). It reads State, so a lease that lapsed since the gate |
| 54 | // latches here, which is the point: a read that found nothing because the |
| 55 | // lease ran out between the gate and the query must say "try again", not |
| 56 | // "not found". |
| 57 | func requestUnknown(r *http.Request) bool { |
| 58 | p, ok := teamPersonFrom(r.Context()) |
| 59 | return ok && p.access.State() == roster.Unknown |
| 60 | } |
| 61 | |
| 62 | // The partial notice (Paul, 2026-10-28): what a view says when the roster |
| 63 | // could not be read within its lease. Calendar has no shares, so the |
| 64 | // partial view is always empty, and the notice is what keeps that from |
| 65 | // reading as a free day. |
| 66 | const ( |
| 67 | partialTitle = "We couldn’t check your team access" |
| 68 | partialBody = "The team’s calendars can’t be shown until it’s checked, so this view is not your real schedule. Try again in a moment." |
| 69 | ) |
| 70 | |
| 71 | // guardRefused answers a write the guard refused, and reports whether it |
| 72 | // did. Standing lapsed between the route's gate and the commit: the same |
| 73 | // retryable 503 as Unknown anywhere else, and a 404 for a definite "no" |
| 74 | // (which one request's Access cannot reach once it has said Member, but |
| 75 | // costs nothing to answer correctly). Outside team mode no error is ever |
| 76 | // one of these. |
| 77 | func (h *handlers) guardRefused(w http.ResponseWriter, r *http.Request, err error) bool { |
| 78 | switch { |
| 79 | case errors.Is(err, teammode.ErrUnavailable): |
| 80 | h.teamUnavailable(w, r) |
| 81 | case errors.Is(err, teammode.ErrNotMember): |
| 82 | h.errorPage(w, r, http.StatusNotFound, "") |
| 83 | default: |
| 84 | return false |
| 85 | } |
| 86 | return true |
| 87 | } |
| 88 | |
| 89 | // standingFor is the answer about somebody other than the request's own |
| 90 | // person (App.standingOf), or nil outside team mode. |
| 91 | func (h *handlers) standingFor(ctx context.Context, person int64) *teammode.Access { |
| 92 | if h.standingOf == nil { |
| 93 | return nil |
| 94 | } |
| 95 | return h.standingOf(ctx, person) |
| 96 | } |
| 97 | |
| 98 | // teamProbe is GET /api/team, the readiness probe Alta polls at |
| 99 | // provisioning step 11 before it activates this instance in Home's |
| 100 | // directory: teammode's Probe, unauthenticated, no-store, |
| 101 | // {team_mode, consumer, roster} and no account data, and |
| 102 | // {"team_mode":false} with team mode off. |
| 103 | func (a *App) teamProbe(w http.ResponseWriter, r *http.Request) { |
| 104 | var rt *teammode.Runtime |
| 105 | if a.team != nil { |
| 106 | rt = a.team.Runtime |
| 107 | } |
| 108 | teammode.Probe(rt).ServeHTTP(w, r) |
| 109 | } |
| 110 | |
| 111 | // PartialTitle and PartialBody are the partial notice's words |
| 112 | // (team_answers.go), for the team-partial template. |
| 113 | func (PageData) PartialTitle() string { return partialTitle } |
| 114 | func (PageData) PartialBody() string { return partialBody } |
| 115 | |
| 116 | // importRefused is guardRefused for an import, which is one guarded write |
| 117 | // per event: standing that lapsed part-way stops the file with the |
| 118 | // retryable 503, and when some events had already landed the page says how |
| 119 | // many and that importing the same file again adds them a second time |
| 120 | // (import does not dedupe on UID: Fable #35 N1). Nothing landed is the |
| 121 | // plain retry. False for any other error. |
| 122 | func (h *handlers) importRefused(w http.ResponseWriter, r *http.Request, err error, res ImportResult) bool { |
| 123 | if !errors.Is(err, teammode.ErrUnavailable) || res.Created == 0 { |
| 124 | return h.guardRefused(w, r, err) |
| 125 | } |
| 126 | events := "events were" |
| 127 | if res.Created == 1 { |
| 128 | events = "event was" |
| 129 | } |
| 130 | w.Header().Set("Cache-Control", "no-store") |
| 131 | w.Header().Set("Retry-After", retryAfter) |
| 132 | renderStatus(w, "import_done", http.StatusServiceUnavailable, h.page(r, "Import stopped", map[string]any{ |
| 133 | "Result": res, |
| 134 | "Interrupted": strconv.Itoa(res.Created) + " " + events + " imported before your team access could no longer be checked. " + |
| 135 | "Importing the same file again will add those a second time; import the rest from a file without them, or try again in a moment and remove the duplicates.", |
| 136 | })) |
| 137 | return true |
| 138 | } |
| 139 | |