Implement withdraw motion
[cacert-boardvoting.git] / actions.go
1 package main
2
3 import (
4 "bytes"
5 "fmt"
6 "github.com/Masterminds/sprig"
7 "gopkg.in/gomail.v2"
8 "text/template"
9 )
10
11 type templateBody string
12
13 func buildMail(templateName string, context interface{}) (mailText *bytes.Buffer, err error) {
14 t, err := template.New(templateName).Funcs(
15 sprig.GenericFuncMap()).ParseFiles(fmt.Sprintf("templates/%s", templateName))
16 if err != nil {
17 return
18 }
19
20 mailText = bytes.NewBufferString("")
21 t.Execute(mailText, context)
22
23 return
24 }
25
26 func CreateMotion(decision *Decision, voter *Voter) (err error) {
27 decision.ProponentId = voter.Id
28 err = decision.Create()
29 if err != nil {
30 logger.Println("Error saving motion:", err)
31 return
32 }
33
34 type mailContext struct {
35 Decision
36 Name string
37 VoteURL string
38 UnvotedURL string
39 }
40 voteURL := fmt.Sprintf("%s/vote", config.BaseURL)
41 unvotedURL := fmt.Sprintf("%s/motions/?unvoted=1", config.BaseURL)
42 context := mailContext{*decision, voter.Name, voteURL, unvotedURL}
43
44 mailText, err := buildMail("create_motion_mail.txt", context)
45 if err != nil {
46 logger.Println("Error", err)
47 return
48 }
49
50 m := gomail.NewMessage()
51 m.SetHeader("From", config.NoticeSenderAddress)
52 m.SetHeader("To", config.BoardMailAddress)
53 m.SetHeader("Subject", fmt.Sprintf("%s - %s", decision.Tag, decision.Title))
54 m.SetHeader("Message-ID", fmt.Sprintf("<%s>", decision.Tag))
55 m.SetBody("text/plain", mailText.String())
56
57 d := gomail.NewDialer(config.MailServer.Host, config.MailServer.Port, "", "")
58 if err := d.DialAndSend(m); err != nil {
59 logger.Println("Error sending mail", err)
60 }
61
62 return
63 }
64
65 func UpdateMotion(decision *Decision, voter *Voter) (err error) {
66 err = decision.Update()
67 if err != nil {
68 logger.Println("Error updating motion:", err)
69 return
70 }
71
72 type mailContext struct {
73 Decision
74 Name string
75 VoteURL string
76 UnvotedURL string
77 }
78 voteURL := fmt.Sprintf("%s/vote", config.BaseURL)
79 unvotedURL := fmt.Sprintf("%s/motions/?unvoted=1", config.BaseURL)
80 context := mailContext{*decision, voter.Name, voteURL, unvotedURL}
81
82 mailText, err := buildMail("update_motion_mail.txt", context)
83 if err != nil {
84 logger.Println("Error", err)
85 return
86 }
87
88 m := gomail.NewMessage()
89 m.SetHeader("From", config.NoticeSenderAddress)
90 m.SetHeader("To", config.BoardMailAddress)
91 m.SetHeader("Subject", fmt.Sprintf("Re: %s - %s", decision.Tag, decision.Title))
92 m.SetHeader("References", fmt.Sprintf("<%s>", decision.Tag))
93 m.SetBody("text/plain", mailText.String())
94
95 d := gomail.NewDialer(config.MailServer.Host, config.MailServer.Port, "", "")
96 if err := d.DialAndSend(m); err != nil {
97 logger.Println("Error sending mail", err)
98 }
99
100 return
101 }
102
103 func WithdrawMotion(decision *Decision, voter *Voter) (err error) {
104 err = decision.UpdateStatus()
105
106 type mailContext struct {
107 *Decision
108 Name string
109 }
110 context := mailContext{decision, voter.Name}
111
112 mailText, err := buildMail("withdraw_motion_mail.txt", context)
113 if err != nil {
114 logger.Println("Error", err)
115 return
116 }
117
118 m := gomail.NewMessage()
119 m.SetHeader("From", config.NoticeSenderAddress)
120 m.SetHeader("To", config.BoardMailAddress)
121 m.SetHeader("Subject", fmt.Sprintf("Re: %s - %s - withdrawn", decision.Tag, decision.Title))
122 m.SetHeader("References", fmt.Sprintf("<%s>", decision.Tag))
123 m.SetBody("text/plain", mailText.String())
124
125 d := gomail.NewDialer(config.MailServer.Host, config.MailServer.Port, "", "")
126 if err := d.DialAndSend(m); err != nil {
127 logger.Println("Error sending mail", err)
128 }
129
130 return
131 }