package templates import ( "bytes" "fmt" "html/template" "github.com/warmbly/warmbly/internal/observability/errs" ) // Notification digest: several pending notifications bundled into one email // by the flush loop, so a busy stretch produces a single message instead of // a stream. The newest DigestShown items render with their own links and the // rest as a count; titles and bodies are plain text, auto-escaped by html/template. // DigestItem is one bundled notification. type DigestItem struct { Title string Body string URL string } // DigestShown is how many items a digest lists before summing up the rest. const DigestShown = 9 // DigestSubject is the digest's subject line for count pending notifications. func DigestSubject(count int) string { return fmt.Sprintf("%d notifications waiting in %s", count, CompanyName()) } const digestContent = `

Digest

While you were away

You have {{.Count}} notifications waiting, bundled into one email. The newest are below.

{{range .Items}}

{{if .URL}}{{.Title}}{{else}}{{.Title}}{{end}}

{{if .Body}}

{{.Body}}

{{end}}
{{end}} {{if .More}}

And {{.More}} more waiting in {{.Brand}}.

{{end}}
Open {{.Brand}}

Updates you read in the app are never emailed, and the rest bundle on the cadence you chose. Manage both in your notification settings.

` var digestTmpl = template.Must(template.New("digest_content").Parse(digestContent)) // GenerateDigestHTML renders the bundled-notifications email; items are newest first. func GenerateDigestHTML(count int, items []DigestItem) (string, error) { brand := CompanyName() shown := items[:min(len(items), DigestShown)] data := struct { Count int Items []DigestItem More int AppURL string Brand string }{Count: count, Items: shown, More: count - len(shown), AppURL: AppURL(), Brand: brand} var buf bytes.Buffer if err := digestTmpl.Execute(&buf, data); err != nil { errs.CaptureException(err) return "", err } return renderEmail(DigestSubject(count), buf.String()) }