package templates import ( "bytes" "fmt" "html/template" "time" "github.com/warmbly/warmbly/internal/observability/errs" ) const resetPasswordContent = `
Account
We received a request to reset your password. Click the button to choose a new one. If you didn't request this, you can safely ignore this email.
| Reset password |
Link expires in {{.Expiry}}
` var resetPasswordTmpl = template.Must(template.New("reset_password_content").Parse(resetPasswordContent)) // GenerateResetPasswordHTML takes the link's lifetime rather than quoting one, // because the two drifted: the mail promised four hours against a token that // died in one, so a link that read as good for the afternoon was refused. func GenerateResetPasswordHTML(firstName, url string, ttl time.Duration) (string, error) { data := struct { ResetURL string Expiry string }{ResetURL: url, Expiry: humanExpiry(ttl)} var buf bytes.Buffer if err := resetPasswordTmpl.Execute(&buf, data); err != nil { errs.CaptureException(err) return "", err } return renderEmail("Reset Your Password", buf.String()) } // humanExpiry renders a whole number of hours or minutes, which is every // lifetime this is ever handed. func humanExpiry(d time.Duration) string { plural := func(n int, unit string) string { if n == 1 { return fmt.Sprintf("1 %s", unit) } return fmt.Sprintf("%d %ss", n, unit) } if d >= time.Hour { return plural(int(d/time.Hour), "hour") } if d >= time.Minute { return plural(int(d/time.Minute), "minute") } return plural(int(d/time.Second), "second") }