summaryrefslogtreecommitdiff
path: root/actions.go
diff options
context:
space:
mode:
Diffstat (limited to 'actions.go')
-rw-r--r--actions.go84
1 files changed, 72 insertions, 12 deletions
diff --git a/actions.go b/actions.go
index 0619587..a8e78fb 100644
--- a/actions.go
+++ b/actions.go
@@ -1,15 +1,32 @@
package main
import (
+ "bytes"
"fmt"
"github.com/Masterminds/sprig"
"os"
"text/template"
+ "gopkg.in/gomail.v2"
)
+type templateBody string
+
+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
+}
+
func CreateMotion(decision *Decision, voter *Voter) (err error) {
decision.ProponentId = voter.Id
- err = decision.Save()
+ err = decision.Create()
if err != nil {
logger.Println("Error saving motion:", err)
return
@@ -18,26 +35,69 @@ func CreateMotion(decision *Decision, voter *Voter) (err error) {
type mailContext struct {
Decision
Name string
- Sender string
- Recipient string
VoteURL string
UnvotedURL string
}
voteURL := fmt.Sprintf("%s/vote", config.BaseURL)
unvotedURL := fmt.Sprintf("%s/motions/?unvoted=1", config.BaseURL)
- context := mailContext{
- *decision, voter.Name, config.NoticeSenderAddress,
- config.BoardMailAddress, voteURL,
- unvotedURL}
+ context := mailContext{*decision, voter.Name, voteURL, unvotedURL}
- t, err := template.New("create_motion_mail.txt").Funcs(
- sprig.GenericFuncMap()).ParseFiles("templates/create_motion_mail.txt")
+ mailText, err := buildMail("create_motion_mail.txt", context)
if err != nil {
- logger.Fatal(err)
+ logger.Println("Error", err)
+ return
+ }
+
+ m := gomail.NewMessage()
+ m.SetHeader("From", config.NoticeSenderAddress)
+ m.SetHeader("To", config.BoardMailAddress)
+ m.SetHeader("Subject", fmt.Sprintf("%s - %s", decision.Tag, decision.Title))
+ m.SetHeader("Message-ID", fmt.Sprintf("<%s>", decision.Tag))
+ 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)
+ }
+
+ return
+}
+
+func UpdateMotion(decision *Decision, voter *Voter) (err error) {
+ err = decision.Update()
+ if err != nil {
+ logger.Println("Error updating motion:", err)
+ return
+ }
+
+ type mailContext struct {
+ Decision
+ Name string
+ VoteURL string
+ UnvotedURL string
+ }
+ voteURL := fmt.Sprintf("%s/vote", config.BaseURL)
+ unvotedURL := fmt.Sprintf("%s/motions/?unvoted=1", config.BaseURL)
+ context := mailContext{*decision, voter.Name, voteURL, unvotedURL}
+
+ mailText, err := buildMail("update_motion_mail.txt", context)
+ if err != nil {
+ logger.Println("Error", err)
+ return
+ }
+
+ m := gomail.NewMessage()
+ m.SetHeader("From", config.NoticeSenderAddress)
+ m.SetHeader("To", config.BoardMailAddress)
+ m.SetHeader("Subject", fmt.Sprintf("%s - %s", decision.Tag, decision.Title))
+ m.SetHeader("References", fmt.Sprintf("<%s>", decision.Tag))
+ 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)
}
- t.Execute(os.Stdout, context)
- // TODO: implement mail sending
return
}