cacert-boardvoting/actions.go

131 lines
3.4 KiB
Go

package main
import (
"bytes"
"fmt"
"github.com/Masterminds/sprig"
"gopkg.in/gomail.v2"
"text/template"
)
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.Create()
if err != nil {
logger.Println("Error saving 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("create_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("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("Re: %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)
}
return
}
func WithdrawMotion(decision *Decision, voter *Voter) (err error) {
err = decision.UpdateStatus()
type mailContext struct {
*Decision
Name string
}
context := mailContext{decision, voter.Name}
mailText, err := buildMail("withdraw_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("Re: %s - %s - withdrawn", 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)
}
return
}