summaryrefslogtreecommitdiff
path: root/notifications.go
diff options
context:
space:
mode:
authorJan Dittberner <jandd@cacert.org>2021-01-09 15:49:19 +0100
committerJan Dittberner <jandd@cacert.org>2021-01-09 15:49:19 +0100
commit03827874cfc51b4be6709459d5263ef87f31dbe4 (patch)
tree521618919516cf130aba35c0fca119d1e660d157 /notifications.go
parent594df29dc132c1573dddda3883eb4a31cdd99756 (diff)
downloadcacert-boardvoting-03827874cfc51b4be6709459d5263ef87f31dbe4.tar.gz
cacert-boardvoting-03827874cfc51b4be6709459d5263ef87f31dbe4.tar.xz
cacert-boardvoting-03827874cfc51b4be6709459d5263ef87f31dbe4.zip
Configure golangci-lint and apply suggestions
Diffstat (limited to 'notifications.go')
-rw-r--r--notifications.go90
1 files changed, 59 insertions, 31 deletions
diff --git a/notifications.go b/notifications.go
index ed77573..49fc543 100644
--- a/notifications.go
+++ b/notifications.go
@@ -1,27 +1,30 @@
/*
- Copyright 2017-2019 Jan Dittberner
+Copyright 2017-2021 Jan Dittberner
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this program except in compliance with the License.
- You may obtain a copy of the License at
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this program except in compliance with the License.
+You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
+ http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
*/
+
package main
import (
"bytes"
"fmt"
- "git.cacert.org/cacert-boardvoting/boardvoting"
+ "text/template"
+
"github.com/Masterminds/sprig"
"gopkg.in/gomail.v2"
- "text/template"
+
+ "git.cacert.org/cacert-boardvoting/boardvoting"
log "github.com/sirupsen/logrus"
)
@@ -37,7 +40,7 @@ type recipientData struct {
field, address, name string
}
-type notificationContent struct {
+type NotificationContent struct {
template string
data interface{}
subject string
@@ -46,32 +49,39 @@ type notificationContent struct {
}
type NotificationMail interface {
- GetNotificationContent() *notificationContent
+ GetNotificationContent() *NotificationContent
}
var NotifyMailChannel = make(chan NotificationMail, 1)
func MailNotifier(quitMailNotifier chan int) {
log.Info("Launched mail notifier")
+
for {
select {
case notification := <-NotifyMailChannel:
content := notification.GetNotificationContent()
+
mailText, err := buildMail(content.template, content.data)
if err != nil {
log.Errorf("building mail failed: %v", err)
+
continue
}
m := gomail.NewMessage()
m.SetAddressHeader("From", config.NotificationSenderAddress, "CAcert board voting system")
+
for _, recipient := range content.recipients {
m.SetAddressHeader(recipient.field, recipient.address, recipient.name)
}
+
m.SetHeader("Subject", content.subject)
+
for _, header := range content.headers {
m.SetHeader(header.name, header.value...)
}
+
m.SetBody("text/plain", mailText.String())
d := gomail.NewDialer(config.MailServer.Host, config.MailServer.Port, "", "")
@@ -80,6 +90,7 @@ func MailNotifier(quitMailNotifier chan int) {
}
case <-quitMailNotifier:
log.Info("Ending mail notifier")
+
return
}
}
@@ -90,6 +101,7 @@ func buildMail(templateName string, context interface{}) (mailText *bytes.Buffer
if err != nil {
return
}
+
t, err := template.New(templateName).Funcs(sprig.GenericFuncMap()).Parse(string(b))
if err != nil {
return
@@ -97,8 +109,10 @@ func buildMail(templateName string, context interface{}) (mailText *bytes.Buffer
mailText = bytes.NewBufferString("")
if err := t.Execute(mailText, context); err != nil {
- log.Errorf("Failed to execute template %s with context %+v: %v", templateName, context, err)
- return nil, err
+ return nil, fmt.Errorf(
+ "failed to execute template %s with context %+v: %w",
+ templateName, context, err,
+ )
}
return
@@ -122,6 +136,7 @@ func (n *decisionReplyBase) getHeaders() headerList {
headers = append(headers, headerData{
name: "In-Reply-To", value: []string{fmt.Sprintf("<%s>", n.decision.Tag)},
})
+
return headers
}
@@ -139,11 +154,12 @@ type notificationClosedDecision struct {
func NewNotificationClosedDecision(decision *Decision, voteSums *VoteSums, reasoning string) NotificationMail {
notification := &notificationClosedDecision{voteSums: *voteSums, reasoning: reasoning}
notification.decision = *decision
+
return notification
}
-func (n *notificationClosedDecision) GetNotificationContent() *notificationContent {
- return &notificationContent{
+func (n *notificationClosedDecision) GetNotificationContent() *NotificationContent {
+ return &NotificationContent{
template: "closed_motion_mail.txt",
data: struct {
*Decision
@@ -162,10 +178,11 @@ type NotificationCreateMotion struct {
voter Voter
}
-func (n *NotificationCreateMotion) GetNotificationContent() *notificationContent {
+func (n *NotificationCreateMotion) GetNotificationContent() *NotificationContent {
voteURL := fmt.Sprintf("%s/vote/%s", config.BaseURL, n.decision.Tag)
unvotedURL := fmt.Sprintf("%s/motions/?unvoted=1", config.BaseURL)
- return &notificationContent{
+
+ return &NotificationContent{
template: "create_motion_mail.txt",
data: struct {
*Decision
@@ -188,13 +205,15 @@ type notificationUpdateMotion struct {
func NewNotificationUpdateMotion(decision Decision, voter Voter) NotificationMail {
notification := notificationUpdateMotion{voter: voter}
notification.decision = decision
+
return &notification
}
-func (n *notificationUpdateMotion) GetNotificationContent() *notificationContent {
+func (n *notificationUpdateMotion) GetNotificationContent() *NotificationContent {
voteURL := fmt.Sprintf("%s/vote/%s", config.BaseURL, n.decision.Tag)
unvotedURL := fmt.Sprintf("%s/motions/?unvoted=1", config.BaseURL)
- return &notificationContent{
+
+ return &NotificationContent{
template: "update_motion_mail.txt",
data: struct {
*Decision
@@ -217,11 +236,12 @@ type notificationWithDrawMotion struct {
func NewNotificationWithDrawMotion(decision *Decision, voter *Voter) NotificationMail {
notification := &notificationWithDrawMotion{voter: *voter}
notification.decision = *decision
+
return notification
}
-func (n *notificationWithDrawMotion) GetNotificationContent() *notificationContent {
- return &notificationContent{
+func (n *notificationWithDrawMotion) GetNotificationContent() *NotificationContent {
+ return &NotificationContent{
template: "withdraw_motion_mail.txt",
data: struct {
*Decision
@@ -238,8 +258,8 @@ type RemindVoterNotification struct {
decisions []Decision
}
-func (n *RemindVoterNotification) GetNotificationContent() *notificationContent {
- return &notificationContent{
+func (n *RemindVoterNotification) GetNotificationContent() *NotificationContent {
+ return &NotificationContent{
template: "remind_voter_mail.txt",
data: struct {
Decisions []Decision
@@ -266,14 +286,21 @@ type notificationProxyVote struct {
justification string
}
-func NewNotificationProxyVote(decision *Decision, proxy *Voter, voter *Voter, vote *Vote, justification string) NotificationMail {
+func NewNotificationProxyVote(
+ decision *Decision,
+ proxy *Voter,
+ voter *Voter,
+ vote *Vote,
+ justification string,
+) NotificationMail {
notification := &notificationProxyVote{proxy: *proxy, voter: *voter, vote: *vote, justification: justification}
notification.decision = *decision
+
return notification
}
-func (n *notificationProxyVote) GetNotificationContent() *notificationContent {
- return &notificationContent{
+func (n *notificationProxyVote) GetNotificationContent() *NotificationContent {
+ return &NotificationContent{
template: "proxy_vote_mail.txt",
data: struct {
Proxy string
@@ -298,11 +325,12 @@ type notificationDirectVote struct {
func NewNotificationDirectVote(decision *Decision, voter *Voter, vote *Vote) NotificationMail {
notification := &notificationDirectVote{voter: *voter, vote: *vote}
notification.decision = *decision
+
return notification
}
-func (n *notificationDirectVote) GetNotificationContent() *notificationContent {
- return &notificationContent{
+func (n *notificationDirectVote) GetNotificationContent() *NotificationContent {
+ return &NotificationContent{
template: "direct_vote_mail.txt",
data: struct {
Vote VoteChoice