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