summaryrefslogtreecommitdiff
path: root/notifications.go
blob: beb9b3feba0ebe6df0a5ee0ea9908c399167f6aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main

import (
	"bytes"
	"fmt"
	"github.com/Masterminds/sprig"
	"gopkg.in/gomail.v2"
	"text/template"
)

type NotificationMail interface {
	GetData() interface{}
	GetTemplate() string
	GetSubject() string
	GetHeaders() map[string]string
}

var notifyMail = make(chan NotificationMail, 1)
var quitMailNotifier = make(chan int)

func CloseMailNotifier() {
	quitMailNotifier <- 1
}

func MailNotifier() {
	logger.Println("Launched mail notifier")
	for {
		select {
		case notification := <-notifyMail:
			mailText, err := buildMail(notification.GetTemplate(), notification.GetData())
			if err != nil {
				logger.Println("ERROR building mail:", err)
				continue
			}

			m := gomail.NewMessage()
			m.SetHeader("From", config.NoticeSenderAddress)
			m.SetHeader("To", config.BoardMailAddress)
			m.SetHeader("Subject", notification.GetSubject())
			for header, value := range notification.GetHeaders() {
				m.SetHeader(header, value)
			}
			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
		}
	}
}

func buildMail(templateName string, context interface{}) (mailText *bytes.Buffer, err error) {
	t, err := template.New(templateName).Funcs(
		sprig.GenericFuncMap()).ParseFiles(fmt.Sprintf("templates/%s", templateName))
	if err != nil {
		return
	}

	mailText = bytes.NewBufferString("")
	t.Execute(mailText, context)

	return
}

type NotificationClosedDecision struct {
	decision Decision
	voteSums VoteSums
}

func (n *NotificationClosedDecision) GetData() interface{} {
	return struct {
		*Decision
		*VoteSums
	}{&n.decision, &n.voteSums}
}

func (n *NotificationClosedDecision) GetTemplate() string {
	return "closed_motion_mail.txt"
}

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 {
	decision Decision
	voter    Voter
}

func (n *NotificationCreateMotion) GetData() interface{} {
	voteURL := fmt.Sprintf("%s/vote", config.BaseURL)
	unvotedURL := fmt.Sprintf("%s/motions/?unvoted=1", config.BaseURL)
	return struct {
		*Decision
		Name       string
		VoteURL    string
		UnvotedURL string
	}{&n.decision, n.voter.Name, voteURL, unvotedURL}
}

func (n *NotificationCreateMotion) GetTemplate() string {
	return "create_motion_mail.txt"
}

func (n *NotificationCreateMotion) GetSubject() string {
	return fmt.Sprintf("%s - %s", n.decision.Tag, n.decision.Title)
}

func (n *NotificationCreateMotion) GetHeaders() map[string]string {
	return map[string]string{"Message-ID": fmt.Sprintf("<%s>", n.decision.Tag)}
}

type NotificationUpdateMotion struct {
	decision Decision
	voter    Voter
}

func (n *NotificationUpdateMotion) GetData() interface{} {
	voteURL := fmt.Sprintf("%s/vote", config.BaseURL)
	unvotedURL := fmt.Sprintf("%s/motions/?unvoted=1", config.BaseURL)
	return struct {
		*Decision
		Name       string
		VoteURL    string
		UnvotedURL string
	}{&n.decision, n.voter.Name, voteURL, unvotedURL}
}

func (n *NotificationUpdateMotion) GetTemplate() string {
	return "update_motion_mail.txt"
}

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 {
	decision Decision
	voter    Voter
}

func (n *NotificationWithDrawMotion) GetData() interface{} {
	return struct {
		*Decision
		Name string
	}{&n.decision, n.voter.Name}
}

func (n *NotificationWithDrawMotion) GetTemplate() string {
	return "withdraw_motion_mail.txt"
}

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)}
}