/
messanger.go
182 lines (162 loc) · 4.24 KB
/
messanger.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package sendmsg
import (
"context"
"errors"
"github.com/demdxx/sendmsg/template"
)
var (
ErrTemplateStorageNotDefined = errors.New("template storage is not defined")
ErrRecipientsNotDefined = errors.New("recipients are not defined")
ErrSubjectNotDefined = errors.New("subject is not defined")
ErrContentNotDefined = errors.New("content is not defined")
ErrNoSenders = errors.New("no senders defined")
ErrMessageNotSent = errors.New("message not sent")
ErrTemplateNotFound = template.ErrTemplateNotFound
)
type SendOptions struct {
AllowedSenders []string
NotAllowedSenders []string
Message Message
TemplateName string
Vars map[string]any
Recipients []string
CC []string
BCC []string
Subject string
HTML string
Text string
Attaches []Attach
}
// IsAllowed checks if the sender is allowed to send the message
func (o *SendOptions) IsAllowed(sender string) bool {
for _, s := range o.AllowedSenders {
if s == sender {
return true
}
}
for _, s := range o.NotAllowedSenders {
if s == sender {
return false
}
}
return len(o.AllowedSenders) == 0
}
// GetMessage returns the message to send
func (o *SendOptions) GetMessage(storage template.Storage) (Message, error) {
if o.Message != nil {
return o.Message, nil
}
if len(o.Recipients) == 0 {
return nil, ErrRecipientsNotDefined
}
if o.TemplateName != "" {
if storage == nil {
return nil, ErrTemplateStorageNotDefined
}
template, err := storage.Template(context.Background(), o.TemplateName)
if err != nil {
return nil, err
}
return &TemplateMessage{
Template: template,
Recipients: o.Recipients,
CC: o.CC,
BCC: o.BCC,
Vars: o.Vars,
Attaches: o.Attaches,
}, nil
}
if o.Subject == "" {
return nil, ErrSubjectNotDefined
}
if o.HTML == "" || o.Text == "" {
return nil, ErrContentNotDefined
}
return &DefaultMessage{
Recipients: o.Recipients,
CC: o.CC,
BCC: o.BCC,
Subject: o.Subject,
HTML: o.HTML,
PlainText: o.Text,
Vars: o.Vars,
Attaches: o.Attaches,
}, nil
}
type SendOption func(*SendOptions)
// WithSender sets the list of allowed senders
func WithSender(senders ...string) SendOption {
return func(o *SendOptions) {
o.AllowedSenders = senders
}
}
// WithoutSender sets the list of not allowed senders
func WithoutSender(senders ...string) SendOption {
return func(o *SendOptions) {
o.NotAllowedSenders = senders
}
}
// WithMessage sets the message to send.
func WithMessage(message Message) SendOption {
return func(o *SendOptions) {
o.Message = message
}
}
// WithTemplate sets the template name to send.
func WithTemplate(name string) SendOption {
return func(o *SendOptions) {
o.TemplateName = name
if o.Message != nil {
panic("sendmsg: WithTemplate and WithMessage are mutually exclusive")
}
}
}
// WithRecipients sets the list of recipients
func WithRecipients(recipients, cc, bcc []string) SendOption {
return func(o *SendOptions) {
o.Recipients = recipients
o.CC = cc
o.BCC = bcc
if len(recipients) > 0 && o.Message != nil {
panic("sendmsg: WithRecipients and WithMessage are mutually exclusive")
}
}
}
// WithSubject sets the subject of the message
func WithSubject(subject string) SendOption {
return func(o *SendOptions) {
o.Subject = subject
if o.Message != nil {
panic("sendmsg: WithSubject and WithMessage are mutually exclusive")
}
}
}
// WithContent sets the content of the message
func WithContent(html, text string) SendOption {
return func(o *SendOptions) {
o.HTML = html
o.Text = text
if o.Message != nil {
panic("sendmsg: WithContent and WithMessage are mutually exclusive")
}
}
}
// WithAttaches sets the list of attachments
func WithAttaches(attaches ...Attach) SendOption {
return func(o *SendOptions) {
o.Attaches = attaches
if o.Message != nil {
panic("sendmsg: WithAttaches and WithMessage are mutually exclusive")
}
}
}
// WithVars sets the list of variables
func WithVars(vars map[string]any) SendOption {
return func(o *SendOptions) {
o.Vars = vars
}
}
// Messanger interface for sending messages into abstract channel
type Messanger interface {
Send(ctx context.Context, opts ...SendOption) error
}