-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.go
128 lines (109 loc) · 3.78 KB
/
common.go
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
package uniform
import (
"bytes"
"fmt"
"github.com/go-diary/diary"
"html/template"
"strings"
"time"
)
type EmailAttachment struct {
ContentType string `json:"content-type"`
Filename string `json:"filename"`
Data string `json:"data"`
}
func (c *conn) GeneratePdf(p diary.IPage, timeout time.Duration, serviceId string, html []byte) []byte {
var data []byte
subj := "pdf.convert" // broadcast to any available service
if serviceId != "" {
subj = fmt.Sprintf("%s.%s", serviceId, subj) // ask a specific service
}
if err := c.Request(p, subj, timeout, Request{
Model: html,
}, func(r IRequest, p diary.IPage) {
r.Read(&data)
}); err != nil {
panic(err)
}
return data
}
func (c *conn) SendEmail(p diary.IPage, timeout time.Duration, serviceId string, from, fromName, subject, body string, to ...string) {
c.SendEmailX(p, timeout, serviceId, from, fromName, subject, body, nil, to...)
}
func (c *conn) SendEmailX(p diary.IPage, timeout time.Duration, serviceId string, from, fromName, subject, body string, attachments []EmailAttachment, to ...string) {
subj := "email.send" // broadcast to any available service
if serviceId != "" {
subj = fmt.Sprintf("%s.%s", serviceId, subj) // ask a specific service
}
request := M{
"from": from,
"from-name": fromName,
"to": to,
"subject": subject,
"body": body,
}
if attachments != nil && len(attachments) > 0 {
request["attachments"] = attachments
}
if err := c.Request(p, subj, timeout, Request{
Model: request,
}, nil); err != nil {
panic(err)
}
}
func (c *conn) SendSms(p diary.IPage, timeout time.Duration, serviceId string, body string, to ...string) {
subj := "sms.send" // broadcast to any available service
if serviceId != "" {
subj = fmt.Sprintf("%s.%s", serviceId, subj) // ask a specific service
}
request := M{
"to": to,
"body": body,
}
if err := c.Request(p, subj, timeout, Request{
Model: request,
}, nil); err != nil {
panic(err)
}
}
func (c *conn) SendEmailTemplate(p diary.IPage, timeout time.Duration, asset func(string) []byte, serviceId string, from, fromName, path string, vars M, to ...string) {
c.SendEmailTemplateX(p, timeout, asset, serviceId, from, fromName, path, vars, nil, to...)
}
func (c *conn) SendEmailTemplateX(p diary.IPage, timeout time.Duration, asset func(string) []byte, serviceId string, from, fromName, path string, vars M, attachments []EmailAttachment, to ...string) {
path = strings.TrimPrefix(path, "/")
if !strings.HasPrefix(path, "emails/") {
path = fmt.Sprintf("emails/%s", path)
}
if !strings.HasSuffix(path, ".html") {
path = fmt.Sprintf("%s.html", path)
}
memory := bytes.NewBuffer([]byte{})
script := template.Must(template.New(path).Parse(string(asset(path))))
if err := script.Execute(memory, vars); err != nil {
panic(err)
}
body := memory.String()
memorySecondary := bytes.NewBuffer([]byte{})
script = template.Must(template.New(fmt.Sprintf("%s.subject", path)).Parse(string(asset(fmt.Sprintf("%s.subject", path)))))
if err := script.Execute(memorySecondary, vars); err != nil {
panic(err)
}
subject := memorySecondary.String()
c.SendEmailX(p, timeout, serviceId, from, fromName, subject, body, attachments, to...)
}
func (c *conn) SendSmsTemplate(p diary.IPage, timeout time.Duration, asset func(string) []byte, serviceId string, path string, vars M, to ...string) {
path = strings.TrimPrefix(path, "/")
if !strings.HasPrefix(path, "sms/") {
path = fmt.Sprintf("sms/%s", path)
}
if !strings.HasSuffix(path, ".txt") {
path = fmt.Sprintf("%s.txt", path)
}
memory := bytes.NewBuffer([]byte{})
script := template.Must(template.New(path).Parse(string(asset(path))))
if err := script.Execute(memory, vars); err != nil {
panic(err)
}
body := memory.String()
c.SendSms(p, timeout, serviceId, body, to...)
}