oficina / base Public

Clone
git clone https://amadan.net/oficina/base

Plain git — no account needed to clone.

Download

Download this file

1package web
2
3import (
4 "context"
5 "fmt"
6 "net/http"
7 netmail "net/mail"
8 "strings"
9 "time"
10
11 "amadan.net/oficina/base/internal/store"
12)
13
14type personRow struct {
15 ID string
16 Initial string
17 Name string
18 Email string
19 // Role is "owner", "member" (signed in at least once) or "invited"
20 // (not yet), as the page's tag says.
21 Role string
22 Owner bool
23 // Joined is true once they have signed in.
24 Joined bool
25}
26
27type peoplePage struct {
28 Name string
29 peopleContent
30 BadEmail bool
31 MailFailed bool
32}
33
34// peopleContent is what People shows a member: everyone here, in the
35// order they were invited. The People page and the JSON (GET
36// /api/v1/people) both draw from it.
37type peopleContent struct {
38 IsOwner bool
39 People []personRow
40}
41
42func (a *app) peopleContent(ctx context.Context, me store.Member) (peopleContent, error) {
43 ms, err := store.Members(ctx, a.db)
44 if err != nil {
45 return peopleContent{}, fmt.Errorf("members: %w", err)
46 }
47 c := peopleContent{IsOwner: me.Role == "owner"}
48 for _, m := range ms {
49 c.People = append(c.People, personFor(m))
50 }
51 return c, nil
52}
53
54func personFor(m store.Member) personRow {
55 label := m.Name
56 if label == "" {
57 label = m.Email
58 }
59 p := personRow{
60 ID: m.ID,
61 Initial: strings.ToUpper(string([]rune(label)[:1])),
62 Name: m.Name, Email: m.Email,
63 Owner: m.Role == "owner", Joined: m.JoinedAt != nil,
64 }
65 switch {
66 case p.Owner:
67 p.Role = "owner"
68 case p.Joined:
69 p.Role = "member"
70 default:
71 p.Role = "invited"
72 }
73 return p
74}
75
76func (a *app) people(w http.ResponseWriter, r *http.Request) {
77 me, _ := CurrentMember(r)
78 c, err := a.peopleContent(r.Context(), me)
79 if err != nil {
80 a.logger.Error("people", "err", err)
81 http.Error(w, "unavailable", http.StatusServiceUnavailable)
82 return
83 }
84 w.Header().Set("Cache-Control", "no-store")
85 a.render(w, r, "people", peoplePage{Name: a.cfg.Name, peopleContent: c,
86 BadEmail: r.URL.Query().Get("problem") == "email", MailFailed: r.URL.Query().Get("problem") == "mail"})
87}
88
89// inviteResult is how an invitation went, apart from Base failing to read
90// or write its own store.
91type inviteResult int
92
93const (
94 // inviteOK: the person is here, invited now or before.
95 inviteOK inviteResult = iota
96 // inviteBadEmail: what was given isn't one plain email address.
97 inviteBadEmail
98 // inviteMailFailed: they are invited, but the email wasn't delivered.
99 // Inviting them again sends it.
100 inviteMailFailed
101)
102
103// invitePerson adds a teammate by address, trimmed and lowercased, and
104// emails them where to sign in. An address already here changes nothing.
105// It sends until one invitation has been delivered, so a failed send is
106// retried by inviting again. The caller checks that the member asking is
107// the owner.
108func (a *app) invitePerson(ctx context.Context, raw string) (store.Member, inviteResult, error) {
109 email := strings.ToLower(strings.TrimSpace(raw))
110 addr, err := netmail.ParseAddress(email)
111 if err != nil || addr.Address != email || addr.Name != "" {
112 return store.Member{}, inviteBadEmail, nil
113 }
114 m, _, err := store.Invite(ctx, a.db, email)
115 if err != nil {
116 return store.Member{}, 0, fmt.Errorf("invite: %w", err)
117 }
118 if m.Role == "member" && m.JoinedAt == nil && m.InviteSentAt == nil {
119 // The send outlives the request: a person who gives up waiting
120 // mustn't leave a half-sent invitation.
121 ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 20*time.Second)
122 defer cancel()
123 subject, body := invitationEmail(a.cfg.Name, a.cfg.Origin)
124 if err := a.mailer.Send(ctx, email, subject, body); err != nil {
125 a.logger.Error("invite: send", "err", err)
126 return m, inviteMailFailed, nil
127 }
128 if err := store.MarkInviteSent(ctx, a.db, m.ID); err != nil {
129 a.logger.Error("invite: mark sent", "err", err)
130 }
131 }
132 return m, inviteOK, nil
133}
134
135// invite is the People page's form. Only the owner may.
136func (a *app) invite(w http.ResponseWriter, r *http.Request) {
137 me, _ := CurrentMember(r)
138 if me.Role != "owner" {
139 http.Error(w, "forbidden", http.StatusForbidden)
140 return
141 }
142 _, res, err := a.invitePerson(r.Context(), r.PostFormValue("email"))
143 switch {
144 case err != nil:
145 a.logger.Error("invite", "err", err)
146 http.Error(w, "unavailable", http.StatusServiceUnavailable)
147 case res == inviteBadEmail:
148 http.Redirect(w, r, "/people?problem=email", http.StatusSeeOther)
149 case res == inviteMailFailed:
150 http.Redirect(w, r, "/people?problem=mail", http.StatusSeeOther)
151 default:
152 http.Redirect(w, r, "/people", http.StatusSeeOther)
153 }
154}
155
156func invitationEmail(name, origin string) (subject, body string) {
157 subject = fmt.Sprintf("You're invited to %s", name)
158 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)
159 return
160}
161