internal/control/notifications.go
53 lines · 1475 bytes
1package control
2
3import (
4 "fmt"
5 "strings"
6
7 "gitbay.org/gitbay/internal/store"
8)
9
10// notifyUsers enqueues activity mail for the given user ids, excluding the
11// acting user and anyone without a verified primary email. A best-effort
12// side channel: failures are ignored, the action itself already succeeded.
13// No-op when the instance has no SMTP.
14func notifyUsers(c *Ctx, userIDs []int64, subject, body string) {
15 if c.Cfg.Mail.SMTPHost == "" {
16 return
17 }
18 seen := map[int64]bool{c.User.ID: true}
19 for _, id := range userIDs {
20 if seen[id] {
21 continue
22 }
23 seen[id] = true
24 email, err := c.Store.PrimaryVerifiedEmail(id)
25 if err != nil || email == "" {
26 continue
27 }
28 c.Store.EnqueueMail(email, subject, body)
29 }
30}
31
32// notifyBody builds the standard notification body: who did what, an
33// excerpt, and the web link.
34func notifyBody(c *Ctx, action, excerpt, path string) string {
35 var b strings.Builder
36 fmt.Fprintf(&b, "%s %s\n", c.User.Username, action)
37 if e := strings.TrimSpace(excerpt); e != "" {
38 if len(e) > 500 {
39 e = e[:500] + "…"
40 }
41 fmt.Fprintf(&b, "\n%s\n", e)
42 }
43 fmt.Fprintf(&b, "\n%s/%s\n", strings.TrimSuffix(c.Cfg.Server.SiteURL, "/"), path)
44 return b.String()
45}
46
47func issueSubject(repo store.Repo, number int64, title string) string {
48 return fmt.Sprintf("[%s] #%d: %s", repo.Path(), number, title)
49}
50
51func mrSubject(repo store.Repo, number int64, title string) string {
52 return fmt.Sprintf("[%s] !%d: %s", repo.Path(), number, title)
53}