package calls // Team mode's invitations: following a meeting link, joining as a named // guest, and the pending join a link's sign-in carries to /continue. A // link stands on its meeting's host (team_host.go), and a guest's grant is // written on the host's standing (team_guard.go). Nothing here runs // outside team mode. import ( "errors" "net/http" "amadan.net/oficina/home/roster" "amadan.net/oficina/home/teammode" "amadan.net/rastrillo/rastrillo/sessions" ) // teamLinkHost decides a followed link on its meeting's host: the host's // Access when they stand, or the answer already written — the 404 every // dead link gets when they have left (meetingStands has ended the // meeting), a retryable 503 when it cannot be decided now. func (a *app) teamLinkHost(w http.ResponseWriter, r *http.Request, m Meeting) (*teammode.Access, bool) { host, st := a.meetingStands(r.Context(), m) switch st { case roster.Member: return host, true case roster.NotMember: a.ErrorPage(w, r, http.StatusNotFound, "") default: a.teamUnavailable(w, r) } return nil, false } // teamJoinCookieStands is the sign-in form's host check (POST /signin), // made before the address is read: a join cookie whose link's host // cannot be decided now is a retryable 503, as following the link itself // is (nothing is written, nothing is mailed), rather than "check your // email" with no mail behind it. A host who has left ends the meeting // here (meetingStands), so the cookie's link is dead and mayRequestLink // answers as it always has for a dead link. The work does not depend on // the address, and the holding page already gives the same person the // same 503, so it is no membership oracle. It reports whether the form // may go on. func (a *app) teamJoinCookieStands(w http.ResponseWriter, r *http.Request) bool { ck, ok := a.readJoinCookie(r) if !ok { return true } _, m, live, err := a.store.liveLinkByHash(r.Context(), ck.TokenHash) if err != nil || !live { // Read failures and dead links are mayRequestLink's to answer, // as before. return true } if _, st := a.meetingStands(r.Context(), m); st == roster.Unknown { a.teamUnavailable(w, r) return false } return true } // teamFollowSignedIn is followLink's signed-in half in team mode. A member // goes to the holding screen; somebody the roster has taken off the team // is signed out and told so, exactly as on any other page; anyone else — // a guest, a local account with no Oficina link — is given a grant on the // host's standing, as a non-member always was. func (a *app) teamFollowSignedIn(w http.ResponseWriter, r *http.Request, s sessions.Session, link MeetingLink, m Meeting, host *teammode.Access) { t, err := a.teamReqFor(r.Context(), s) switch { case errors.Is(err, errHomeRemoved): a.endRemovedSession(w, r) return case errors.Is(err, errHomeIdentity): a.ErrorPage(w, r, http.StatusNotFound, "") return case err != nil: a.teamUnavailable(w, r) return } if t.removed() { a.endRemovedSession(w, r) return } if t.person.Anonymous && m.RequireSignin { a.setJoinCookie(w, link.TokenHash) http.Redirect(w, r, signinPath, http.StatusSeeOther) return } switch t.access.State() { case roster.Member: http.Redirect(w, r, "/m/"+m.Sqid+"/join", http.StatusSeeOther) return case roster.Unknown: // Whether this is a member (who needs no grant) or a guest // (who does) cannot be told now. a.teamUnavailable(w, r) return } _, live, err := a.store.mintGrantViaLink(teammode.WithAccess(r.Context(), host), link.ID, t.person.ID) switch { case isTeamUnavailable(err): a.teamUnavailable(w, r) return case err != nil: a.logger.Error("calls: mint grant", "err", err) a.ErrorPage(w, r, http.StatusInternalServerError, "") return case !live: a.ErrorPage(w, r, http.StatusNotFound, "") return } http.Redirect(w, r, "/m/"+m.Sqid+"/join", http.StatusSeeOther) } // teamContinuePending is /continue's pending branch in team mode: the // person's own standing and the meeting host's are both decided before // anything is consumed, so an undecidable roster leaves the pending row // for a retry (it lives fifteen minutes) rather than spending it. A host // who has left the team has ended the meeting, and consumePending then // finds its link dead and falls through as it always did. It answers // handled with no destination when it has written the 503 itself. func (a *app) teamContinuePending(w http.ResponseWriter, r *http.Request, p Person, pend PendingJoin) (string, bool) { ctx := r.Context() _, m, live, err := a.store.liveLinkByID(ctx, pend.LinkID) if err != nil { a.logger.Error("calls: pending link", "err", err) return "", false } // mint: the link is live and its host stands, so a non-member is // given a grant on the host's standing. Otherwise the pending rows // are consumed as bookkeeping and nothing is minted, as for a dead // link before. var host *teammode.Access mint := false if live { var st roster.State host, st = a.meetingStands(ctx, m) switch st { case roster.Unknown: a.teamUnavailable(w, r) return "", true case roster.Member: mint = true } } member := true if mint { switch a.memberState(ctx, p) { case roster.Unknown: a.teamUnavailable(w, r) return "", true case roster.NotMember: member = false } } dest, err := a.store.consumePending(teammode.WithAccess(ctx, host), p, pend, member) if isTeamUnavailable(err) { a.teamUnavailable(w, r) return "", true } if err != nil { a.logger.Error("calls: consume pending", "err", err) return "", false } return dest, dest != "" }