package web import ( "context" "fmt" "net/http" netmail "net/mail" "strings" "time" "amadan.net/oficina/base/internal/store" ) type personRow struct { ID string Initial string Name string Email string // Role is "owner", "member" (signed in at least once) or "invited" // (not yet), as the page's tag says. Role string Owner bool // Joined is true once they have signed in. Joined bool } type peoplePage struct { Name string peopleContent BadEmail bool MailFailed bool } // peopleContent is what People shows a member: everyone here, in the // order they were invited. The People page and the JSON (GET // /api/v1/people) both draw from it. type peopleContent struct { IsOwner bool People []personRow } func (a *app) peopleContent(ctx context.Context, me store.Member) (peopleContent, error) { ms, err := store.Members(ctx, a.db) if err != nil { return peopleContent{}, fmt.Errorf("members: %w", err) } c := peopleContent{IsOwner: me.Role == "owner"} for _, m := range ms { c.People = append(c.People, personFor(m)) } return c, nil } func personFor(m store.Member) personRow { label := m.Name if label == "" { label = m.Email } p := personRow{ ID: m.ID, Initial: strings.ToUpper(string([]rune(label)[:1])), Name: m.Name, Email: m.Email, Owner: m.Role == "owner", Joined: m.JoinedAt != nil, } switch { case p.Owner: p.Role = "owner" case p.Joined: p.Role = "member" default: p.Role = "invited" } return p } func (a *app) people(w http.ResponseWriter, r *http.Request) { me, _ := CurrentMember(r) c, err := a.peopleContent(r.Context(), me) if err != nil { a.logger.Error("people", "err", err) http.Error(w, "unavailable", http.StatusServiceUnavailable) return } w.Header().Set("Cache-Control", "no-store") a.render(w, r, "people", peoplePage{Name: a.cfg.Name, peopleContent: c, BadEmail: r.URL.Query().Get("problem") == "email", MailFailed: r.URL.Query().Get("problem") == "mail"}) } // inviteResult is how an invitation went, apart from Base failing to read // or write its own store. type inviteResult int const ( // inviteOK: the person is here, invited now or before. inviteOK inviteResult = iota // inviteBadEmail: what was given isn't one plain email address. inviteBadEmail // inviteMailFailed: they are invited, but the email wasn't delivered. // Inviting them again sends it. inviteMailFailed ) // invitePerson adds a teammate by address, trimmed and lowercased, and // emails them where to sign in. An address already here changes nothing. // It sends until one invitation has been delivered, so a failed send is // retried by inviting again. The caller checks that the member asking is // the owner. func (a *app) invitePerson(ctx context.Context, raw string) (store.Member, inviteResult, error) { email := strings.ToLower(strings.TrimSpace(raw)) addr, err := netmail.ParseAddress(email) if err != nil || addr.Address != email || addr.Name != "" { return store.Member{}, inviteBadEmail, nil } m, _, err := store.Invite(ctx, a.db, email) if err != nil { return store.Member{}, 0, fmt.Errorf("invite: %w", err) } if m.Role == "member" && m.JoinedAt == nil && m.InviteSentAt == nil { // The send outlives the request: a person who gives up waiting // mustn't leave a half-sent invitation. ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 20*time.Second) defer cancel() subject, body := invitationEmail(a.cfg.Name, a.cfg.Origin) if err := a.mailer.Send(ctx, email, subject, body); err != nil { a.logger.Error("invite: send", "err", err) return m, inviteMailFailed, nil } if err := store.MarkInviteSent(ctx, a.db, m.ID); err != nil { a.logger.Error("invite: mark sent", "err", err) } } return m, inviteOK, nil } // invite is the People page's form. Only the owner may. func (a *app) invite(w http.ResponseWriter, r *http.Request) { me, _ := CurrentMember(r) if me.Role != "owner" { http.Error(w, "forbidden", http.StatusForbidden) return } _, res, err := a.invitePerson(r.Context(), r.PostFormValue("email")) switch { case err != nil: a.logger.Error("invite", "err", err) http.Error(w, "unavailable", http.StatusServiceUnavailable) case res == inviteBadEmail: http.Redirect(w, r, "/people?problem=email", http.StatusSeeOther) case res == inviteMailFailed: http.Redirect(w, r, "/people?problem=mail", http.StatusSeeOther) default: http.Redirect(w, r, "/people", http.StatusSeeOther) } } func invitationEmail(name, origin string) (subject, body string) { subject = fmt.Sprintf("You're invited to %s", name) body = fmt.Sprintf("You've been invited to %s's Base, where the team's apps live.\n\nSign in with this email address to get started:\n%s/signin\n", name, origin) return }