summaryrefslogtreecommitdiff
path: root/notifications.go
diff options
context:
space:
mode:
authorJan Dittberner <jan@dittberner.info>2017-04-21 02:25:49 +0200
committerJan Dittberner <jan@dittberner.info>2017-04-22 00:14:08 +0200
commit2cac50ee86e70428aa06c99f5e009ceaaf1dbc13 (patch)
treeada4eb330be62598445480b6f40a5633ddfa922f /notifications.go
parentb6ad5d8ad327066b21bdb690f5a5017f6bed9740 (diff)
downloadcacert-boardvoting-2cac50ee86e70428aa06c99f5e009ceaaf1dbc13.tar.gz
cacert-boardvoting-2cac50ee86e70428aa06c99f5e009ceaaf1dbc13.tar.xz
cacert-boardvoting-2cac50ee86e70428aa06c99f5e009ceaaf1dbc13.zip
Implement proxy voting
Diffstat (limited to 'notifications.go')
-rw-r--r--notifications.go143
1 files changed, 92 insertions, 51 deletions
diff --git a/notifications.go b/notifications.go
index d4b9715..499a689 100644
--- a/notifications.go
+++ b/notifications.go
@@ -13,28 +13,16 @@ type NotificationMail interface {
GetTemplate() string
GetSubject() string
GetHeaders() map[string]string
-}
-
-type VoterMail interface {
- GetData() interface{}
- GetTemplate() string
- GetSubject() string
GetRecipient() (string, string)
}
-var notifyMail = make(chan NotificationMail, 1)
-var voterMail = make(chan VoterMail, 1)
-var quitMailNotifier = make(chan int)
+var NotifyMailChannel = make(chan NotificationMail, 1)
-func CloseMailNotifier() {
- quitMailNotifier <- 1
-}
-
-func MailNotifier() {
+func MailNotifier(quitMailNotifier chan int) {
logger.Println("Launched mail notifier")
for {
select {
- case notification := <-notifyMail:
+ case notification := <-NotifyMailChannel:
mailText, err := buildMail(notification.GetTemplate(), notification.GetData())
if err != nil {
logger.Println("ERROR building mail:", err)
@@ -42,8 +30,9 @@ func MailNotifier() {
}
m := gomail.NewMessage()
- m.SetHeader("From", config.NoticeSenderAddress)
- m.SetHeader("To", config.BoardMailAddress)
+ m.SetHeader("From", config.NotificationSenderAddress)
+ address, name := notification.GetRecipient()
+ m.SetAddressHeader("To", address, name)
m.SetHeader("Subject", notification.GetSubject())
for header, value := range notification.GetHeaders() {
m.SetHeader(header, value)
@@ -54,24 +43,6 @@ func MailNotifier() {
if err := d.DialAndSend(m); err != nil {
logger.Println("ERROR sending mail:", err)
}
- case notification := <-voterMail:
- mailText, err := buildMail(notification.GetTemplate(), notification.GetData())
- if err != nil {
- logger.Println("ERROR building mail:", err)
- continue
- }
-
- m := gomail.NewMessage()
- m.SetHeader("From", config.ReminderSenderAddress)
- address, name := notification.GetRecipient()
- m.SetAddressHeader("To", address, name)
- m.SetHeader("Subject", notification.GetSubject())
- m.SetBody("text/plain", mailText.String())
-
- d := gomail.NewDialer(config.MailServer.Host, config.MailServer.Port, "", "")
- if err := d.DialAndSend(m); err != nil {
- logger.Println("ERROR sending mail:", err)
- }
case <-quitMailNotifier:
fmt.Println("Ending mail notifier")
return
@@ -92,11 +63,36 @@ func buildMail(templateName string, context interface{}) (mailText *bytes.Buffer
return
}
-type NotificationClosedDecision struct {
+type notificationBase struct{}
+
+func (n *notificationBase) GetRecipient() (address string, name string) {
+ address, name = config.BoardMailAddress, "CAcert board mailing list"
+ return
+}
+
+type decisionReplyBase struct {
decision Decision
+}
+
+func (n *decisionReplyBase) GetHeaders() map[string]string {
+ return map[string]string{
+ "References": fmt.Sprintf("<%s>", n.decision.Tag),
+ "In-Reply-To": fmt.Sprintf("<%s>", n.decision.Tag),
+ }
+}
+
+type NotificationClosedDecision struct {
+ notificationBase
+ decisionReplyBase
voteSums VoteSums
}
+func NewNotificationClosedDecision(decision *Decision, voteSums *VoteSums) *NotificationClosedDecision {
+ notification := &NotificationClosedDecision{voteSums: *voteSums}
+ notification.decision = *decision
+ return notification
+}
+
func (n *NotificationClosedDecision) GetData() interface{} {
return struct {
*Decision
@@ -110,11 +106,8 @@ func (n *NotificationClosedDecision) GetSubject() string {
return fmt.Sprintf("Re: %s - %s - finalised", n.decision.Tag, n.decision.Title)
}
-func (n *NotificationClosedDecision) GetHeaders() map[string]string {
- return map[string]string{"References": fmt.Sprintf("<%s>", n.decision.Tag)}
-}
-
type NotificationCreateMotion struct {
+ notificationBase
decision Decision
voter Voter
}
@@ -141,8 +134,15 @@ func (n *NotificationCreateMotion) GetHeaders() map[string]string {
}
type NotificationUpdateMotion struct {
- decision Decision
- voter Voter
+ notificationBase
+ decisionReplyBase
+ voter Voter
+}
+
+func NewNotificationUpdateMotion(decision Decision, voter Voter) *NotificationUpdateMotion {
+ notification := NotificationUpdateMotion{voter: voter}
+ notification.decision = decision
+ return &notification
}
func (n *NotificationUpdateMotion) GetData() interface{} {
@@ -162,13 +162,16 @@ func (n *NotificationUpdateMotion) GetSubject() string {
return fmt.Sprintf("Re: %s - %s", n.decision.Tag, n.decision.Title)
}
-func (n *NotificationUpdateMotion) GetHeaders() map[string]string {
- return map[string]string{"References": fmt.Sprintf("<%s>", n.decision.Tag)}
+type NotificationWithDrawMotion struct {
+ notificationBase
+ decisionReplyBase
+ voter Voter
}
-type NotificationWithDrawMotion struct {
- decision Decision
- voter Voter
+func NewNotificationWithDrawMotion(decision *Decision, voter *Voter) *NotificationWithDrawMotion {
+ notification := &NotificationWithDrawMotion{voter: *voter}
+ notification.decision = *decision
+ return notification
}
func (n *NotificationWithDrawMotion) GetData() interface{} {
@@ -184,10 +187,6 @@ func (n *NotificationWithDrawMotion) GetSubject() string {
return fmt.Sprintf("Re: %s - %s - withdrawn", n.decision.Tag, n.decision.Title)
}
-func (n *NotificationWithDrawMotion) GetHeaders() map[string]string {
- return map[string]string{"References": fmt.Sprintf("<%s>", n.decision.Tag)}
-}
-
type RemindVoterNotification struct {
voter Voter
decisions []Decision
@@ -205,7 +204,49 @@ func (n *RemindVoterNotification) GetTemplate() string { return "remind_voter_ma
func (n *RemindVoterNotification) GetSubject() string { return "Outstanding CAcert board votes" }
+func (n *RemindVoterNotification) GetHeaders() map[string]string {
+ return map[string]string{}
+}
+
func (n *RemindVoterNotification) GetRecipient() (address string, name string) {
address, name = n.voter.Reminder, n.voter.Name
return
}
+
+type voteNotificationBase struct{}
+
+func (n *voteNotificationBase) GetRecipient() (address string, name string) {
+ address, name = config.VoteNoticeAddress, "CAcert board votes mailing list"
+ return
+}
+
+type NotificationProxyVote struct {
+ voteNotificationBase
+ decisionReplyBase
+ proxy Voter
+ voter Voter
+ vote Vote
+ justification string
+}
+
+func NewNotificationProxyVote(decision *Decision, proxy *Voter, voter *Voter, vote *Vote, justification string) *NotificationProxyVote {
+ notification := &NotificationProxyVote{proxy: *proxy, voter: *voter, vote: *vote, justification: justification}
+ notification.decision = *decision
+ return notification
+}
+
+func (n *NotificationProxyVote) GetData() interface{} {
+ return struct {
+ Proxy string
+ Vote VoteChoice
+ Voter string
+ Decision *Decision
+ Justification string
+ }{n.proxy.Name, n.vote.Vote, n.voter.Name, &n.decision, n.justification}
+}
+
+func (n *NotificationProxyVote) GetTemplate() string { return "proxy_vote_mail.txt" }
+
+func (n *NotificationProxyVote) GetSubject() string {
+ return fmt.Sprintf("Re: %s - %s", n.decision.Tag, n.decision.Title)
+}