| 1 | package calls |
| 2 | |
| 3 | // Team mode's invitations: following a meeting link, joining as a named |
| 4 | // guest, and the pending join a link's sign-in carries to /continue. A |
| 5 | // link stands on its meeting's host (team_host.go), and a guest's grant is |
| 6 | // written on the host's standing (team_guard.go). Nothing here runs |
| 7 | // outside team mode. |
| 8 | |
| 9 | import ( |
| 10 | "errors" |
| 11 | "net/http" |
| 12 | |
| 13 | "amadan.net/oficina/home/roster" |
| 14 | "amadan.net/oficina/home/teammode" |
| 15 | "amadan.net/rastrillo/rastrillo/sessions" |
| 16 | ) |
| 17 | |
| 18 | // teamLinkHost decides a followed link on its meeting's host: the host's |
| 19 | // Access when they stand, or the answer already written — the 404 every |
| 20 | // dead link gets when they have left (meetingStands has ended the |
| 21 | // meeting), a retryable 503 when it cannot be decided now. |
| 22 | func (a *app) teamLinkHost(w http.ResponseWriter, r *http.Request, m Meeting) (*teammode.Access, bool) { |
| 23 | host, st := a.meetingStands(r.Context(), m) |
| 24 | switch st { |
| 25 | case roster.Member: |
| 26 | return host, true |
| 27 | case roster.NotMember: |
| 28 | a.ErrorPage(w, r, http.StatusNotFound, "") |
| 29 | default: |
| 30 | a.teamUnavailable(w, r) |
| 31 | } |
| 32 | return nil, false |
| 33 | } |
| 34 | |
| 35 | // teamJoinCookieStands is the sign-in form's host check (POST /signin), |
| 36 | // made before the address is read: a join cookie whose link's host |
| 37 | // cannot be decided now is a retryable 503, as following the link itself |
| 38 | // is (nothing is written, nothing is mailed), rather than "check your |
| 39 | // email" with no mail behind it. A host who has left ends the meeting |
| 40 | // here (meetingStands), so the cookie's link is dead and mayRequestLink |
| 41 | // answers as it always has for a dead link. The work does not depend on |
| 42 | // the address, and the holding page already gives the same person the |
| 43 | // same 503, so it is no membership oracle. It reports whether the form |
| 44 | // may go on. |
| 45 | func (a *app) teamJoinCookieStands(w http.ResponseWriter, r *http.Request) bool { |
| 46 | ck, ok := a.readJoinCookie(r) |
| 47 | if !ok { |
| 48 | return true |
| 49 | } |
| 50 | _, m, live, err := a.store.liveLinkByHash(r.Context(), ck.TokenHash) |
| 51 | if err != nil || !live { |
| 52 | // Read failures and dead links are mayRequestLink's to answer, |
| 53 | // as before. |
| 54 | return true |
| 55 | } |
| 56 | if _, st := a.meetingStands(r.Context(), m); st == roster.Unknown { |
| 57 | a.teamUnavailable(w, r) |
| 58 | return false |
| 59 | } |
| 60 | return true |
| 61 | } |
| 62 | |
| 63 | // teamFollowSignedIn is followLink's signed-in half in team mode. A member |
| 64 | // goes to the holding screen; somebody the roster has taken off the team |
| 65 | // is signed out and told so, exactly as on any other page; anyone else — |
| 66 | // a guest, a local account with no Oficina link — is given a grant on the |
| 67 | // host's standing, as a non-member always was. |
| 68 | func (a *app) teamFollowSignedIn(w http.ResponseWriter, r *http.Request, s sessions.Session, link MeetingLink, m Meeting, host *teammode.Access) { |
| 69 | t, err := a.teamReqFor(r.Context(), s) |
| 70 | switch { |
| 71 | case errors.Is(err, errHomeRemoved): |
| 72 | a.endRemovedSession(w, r) |
| 73 | return |
| 74 | case errors.Is(err, errHomeIdentity): |
| 75 | a.ErrorPage(w, r, http.StatusNotFound, "") |
| 76 | return |
| 77 | case err != nil: |
| 78 | a.teamUnavailable(w, r) |
| 79 | return |
| 80 | } |
| 81 | if t.removed() { |
| 82 | a.endRemovedSession(w, r) |
| 83 | return |
| 84 | } |
| 85 | if t.person.Anonymous && m.RequireSignin { |
| 86 | a.setJoinCookie(w, link.TokenHash) |
| 87 | http.Redirect(w, r, signinPath, http.StatusSeeOther) |
| 88 | return |
| 89 | } |
| 90 | switch t.access.State() { |
| 91 | case roster.Member: |
| 92 | http.Redirect(w, r, "/m/"+m.Sqid+"/join", http.StatusSeeOther) |
| 93 | return |
| 94 | case roster.Unknown: |
| 95 | // Whether this is a member (who needs no grant) or a guest |
| 96 | // (who does) cannot be told now. |
| 97 | a.teamUnavailable(w, r) |
| 98 | return |
| 99 | } |
| 100 | _, live, err := a.store.mintGrantViaLink(teammode.WithAccess(r.Context(), host), link.ID, t.person.ID) |
| 101 | switch { |
| 102 | case isTeamUnavailable(err): |
| 103 | a.teamUnavailable(w, r) |
| 104 | return |
| 105 | case err != nil: |
| 106 | a.logger.Error("calls: mint grant", "err", err) |
| 107 | a.ErrorPage(w, r, http.StatusInternalServerError, "") |
| 108 | return |
| 109 | case !live: |
| 110 | a.ErrorPage(w, r, http.StatusNotFound, "") |
| 111 | return |
| 112 | } |
| 113 | http.Redirect(w, r, "/m/"+m.Sqid+"/join", http.StatusSeeOther) |
| 114 | } |
| 115 | |
| 116 | // teamContinuePending is /continue's pending branch in team mode: the |
| 117 | // person's own standing and the meeting host's are both decided before |
| 118 | // anything is consumed, so an undecidable roster leaves the pending row |
| 119 | // for a retry (it lives fifteen minutes) rather than spending it. A host |
| 120 | // who has left the team has ended the meeting, and consumePending then |
| 121 | // finds its link dead and falls through as it always did. It answers |
| 122 | // handled with no destination when it has written the 503 itself. |
| 123 | func (a *app) teamContinuePending(w http.ResponseWriter, r *http.Request, p Person, pend PendingJoin) (string, bool) { |
| 124 | ctx := r.Context() |
| 125 | _, m, live, err := a.store.liveLinkByID(ctx, pend.LinkID) |
| 126 | if err != nil { |
| 127 | a.logger.Error("calls: pending link", "err", err) |
| 128 | return "", false |
| 129 | } |
| 130 | // mint: the link is live and its host stands, so a non-member is |
| 131 | // given a grant on the host's standing. Otherwise the pending rows |
| 132 | // are consumed as bookkeeping and nothing is minted, as for a dead |
| 133 | // link before. |
| 134 | var host *teammode.Access |
| 135 | mint := false |
| 136 | if live { |
| 137 | var st roster.State |
| 138 | host, st = a.meetingStands(ctx, m) |
| 139 | switch st { |
| 140 | case roster.Unknown: |
| 141 | a.teamUnavailable(w, r) |
| 142 | return "", true |
| 143 | case roster.Member: |
| 144 | mint = true |
| 145 | } |
| 146 | } |
| 147 | member := true |
| 148 | if mint { |
| 149 | switch a.memberState(ctx, p) { |
| 150 | case roster.Unknown: |
| 151 | a.teamUnavailable(w, r) |
| 152 | return "", true |
| 153 | case roster.NotMember: |
| 154 | member = false |
| 155 | } |
| 156 | } |
| 157 | dest, err := a.store.consumePending(teammode.WithAccess(ctx, host), p, pend, member) |
| 158 | if isTeamUnavailable(err) { |
| 159 | a.teamUnavailable(w, r) |
| 160 | return "", true |
| 161 | } |
| 162 | if err != nil { |
| 163 | a.logger.Error("calls: consume pending", "err", err) |
| 164 | return "", false |
| 165 | } |
| 166 | return dest, dest != "" |
| 167 | } |
| 168 | |