Skip to content

Commit 287f26a

Browse files
committed
refactor ComposeNotification()
1 parent 3a48d3a commit 287f26a

File tree

3 files changed

+34
-69
lines changed

3 files changed

+34
-69
lines changed

core/notifier_email.go

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"errors"
55
"fmt"
66
"net/smtp"
7-
"strings"
87
"time"
98

109
"github.com/spf13/viper"
@@ -55,38 +54,6 @@ func (n EmailNotifier) New(config TransportConfig) (AbstractNotifier, error) {
5554
}, nil
5655
}
5756

58-
func (n EmailNotifier) ComposeNotification(box *Box, checks []CheckResult) Notification {
59-
errTexts := []string{}
60-
resolvedTexts := []string{}
61-
for _, check := range checks {
62-
if check.Status == CheckErr {
63-
errTexts = append(errTexts, check.String())
64-
} else {
65-
resolvedTexts = append(resolvedTexts, check.String())
66-
}
67-
}
68-
69-
var (
70-
resolved string
71-
resolvedList string
72-
errList string
73-
)
74-
if len(resolvedTexts) != 0 {
75-
resolvedList = fmt.Sprintf("Resolved issue(s):\n\n%s\n\n", strings.Join(resolvedTexts, "\n"))
76-
}
77-
if len(errTexts) != 0 {
78-
errList = fmt.Sprintf("New issue(s):\n\n%s\n\n", strings.Join(errTexts, "\n"))
79-
} else {
80-
resolved = "resolved "
81-
}
82-
83-
return Notification{
84-
Subject: fmt.Sprintf("Issues %swith your box \"%s\" on opensensemap.org!", resolved, box.Name),
85-
Body: fmt.Sprintf("A check at %s identified the following updates for your box \"%s\":\n\n%s%sYou may visit https://opensensemap.org/explore/%s for more details.\n\n--\nSent automatically by osem_notify (https://github.com/noerw/osem_notify)",
86-
time.Now().Round(time.Minute), box.Name, errList, resolvedList, box.Id),
87-
}
88-
}
89-
9057
func (n EmailNotifier) Submit(notification Notification) error {
9158
// :TransportConfSourceHack
9259
auth := smtp.PlainAuth(

core/notifier_xmpp.go

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package core
33
import (
44
"errors"
55
"fmt"
6-
"strings"
7-
"time"
86

97
xmpp "github.com/mattn/go-xmpp"
108
"github.com/spf13/viper"
@@ -54,38 +52,6 @@ func (n XmppNotifier) New(config TransportConfig) (AbstractNotifier, error) {
5452
}, nil
5553
}
5654

57-
func (n XmppNotifier) ComposeNotification(box *Box, checks []CheckResult) Notification {
58-
errTexts := []string{}
59-
resolvedTexts := []string{}
60-
for _, check := range checks {
61-
if check.Status == CheckErr {
62-
errTexts = append(errTexts, check.String())
63-
} else {
64-
resolvedTexts = append(resolvedTexts, check.String())
65-
}
66-
}
67-
68-
var (
69-
resolved string
70-
resolvedList string
71-
errList string
72-
)
73-
if len(resolvedTexts) != 0 {
74-
resolvedList = fmt.Sprintf("Resolved issue(s):\n\n%s\n\n", strings.Join(resolvedTexts, "\n"))
75-
}
76-
if len(errTexts) != 0 {
77-
errList = fmt.Sprintf("New issue(s):\n\n%s\n\n", strings.Join(errTexts, "\n"))
78-
} else {
79-
resolved = "resolved "
80-
}
81-
82-
return Notification{
83-
Subject: fmt.Sprintf("Issues %swith your box \"%s\" on opensensemap.org!", resolved, box.Name),
84-
Body: fmt.Sprintf("A check at %s identified the following updates for your box \"%s\":\n\n%s%sYou may visit https://opensensemap.org/explore/%s for more details.\n\n--\nSent automatically by osem_notify (https://github.com/noerw/osem_notify)",
85-
time.Now().Round(time.Minute), box.Name, errList, resolvedList, box.Id),
86-
}
87-
}
88-
8955
func (n XmppNotifier) Submit(notification Notification) error {
9056
// :TransportConfSourceHack
9157
xmppOpts := xmpp.Options{

core/notifiers.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ var Notifiers = map[string]AbstractNotifier{
1515

1616
type AbstractNotifier interface {
1717
New(config TransportConfig) (AbstractNotifier, error)
18-
ComposeNotification(box *Box, checks []CheckResult) Notification
1918
Submit(notification Notification) error
2019
}
2120

@@ -81,7 +80,7 @@ func (results BoxCheckResults) SendNotifications(notifyTypes []string, useCache
8180
continue
8281
}
8382

84-
notification := notifier.ComposeNotification(box, resultsDue)
83+
notification := ComposeNotification(box, resultsDue)
8584

8685
var submitErr error
8786
submitErr = notifier.Submit(notification)
@@ -116,3 +115,36 @@ func (results BoxCheckResults) SendNotifications(notifyTypes []string, useCache
116115
}
117116
return nil
118117
}
118+
119+
func ComposeNotification(box *Box, checks []CheckResult) Notification {
120+
errTexts := []string{}
121+
resolvedTexts := []string{}
122+
for _, check := range checks {
123+
if check.Status == CheckErr {
124+
errTexts = append(errTexts, check.String())
125+
} else {
126+
resolvedTexts = append(resolvedTexts, check.String())
127+
}
128+
}
129+
130+
var (
131+
resolved string
132+
resolvedList string
133+
errList string
134+
)
135+
if len(resolvedTexts) != 0 {
136+
resolvedList = fmt.Sprintf("Resolved issue(s):\n\n%s\n\n", strings.Join(resolvedTexts, "\n"))
137+
}
138+
if len(errTexts) != 0 {
139+
errList = fmt.Sprintf("New issue(s):\n\n%s\n\n", strings.Join(errTexts, "\n"))
140+
} else {
141+
resolved = "resolved "
142+
}
143+
144+
return Notification{
145+
Subject: fmt.Sprintf("Issues %swith your box \"%s\" on opensensemap.org!", resolved, box.Name),
146+
Body: fmt.Sprintf("A check at %s identified the following updates for your box \"%s\":\n\n%s%sYou may visit https://opensensemap.org/explore/%s for more details.\n\n--\nSent automatically by osem_notify (https://github.com/noerw/osem_notify)",
147+
time.Now().Round(time.Minute), box.Name, errList, resolvedList, box.Id),
148+
}
149+
}
150+

0 commit comments

Comments
 (0)