-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeedback.go
147 lines (123 loc) · 3.59 KB
/
feedback.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
package goautowp
import (
"fmt"
"github.com/autowp/goautowp/config"
"github.com/autowp/goautowp/email"
"github.com/autowp/goautowp/validation"
"google.golang.org/genproto/googleapis/rpc/errdetails"
)
// Feedback Main Object.
type Feedback struct {
captchaEnabled bool
config config.FeedbackConfig
recaptchaConfig config.RecaptchaConfig
emailSender email.Sender
}
// CreateFeedbackRequest CreateFeedbackRequest.
type CreateFeedbackRequest struct {
Name string `json:"name"`
Email string `json:"email"`
Message string `json:"message"`
Captcha string `json:"captcha"`
IP string
}
// NewFeedback constructor.
func NewFeedback(
config config.FeedbackConfig,
recaptchaConfig config.RecaptchaConfig,
captchaEnabled bool,
emailSender email.Sender,
) *Feedback {
return &Feedback{
config: config,
recaptchaConfig: recaptchaConfig,
captchaEnabled: captchaEnabled,
emailSender: emailSender,
}
}
func (s *Feedback) Create(request CreateFeedbackRequest) ([]*errdetails.BadRequest_FieldViolation, error) {
InvalidParams, err := request.Validate(s.captchaEnabled, request.IP)
if err != nil {
return nil, err
}
if len(InvalidParams) > 0 {
return InvalidParams, nil
}
message := fmt.Sprintf("Имя: %s\nE-mail: %s\nСообщение:\n%s", request.Name, request.Email, request.Message)
err = s.emailSender.Send(s.config.From, s.config.To, s.config.Subject, message, request.Email)
return nil, err
}
func (s *CreateFeedbackRequest) Validate(
captchaEnabled bool,
ip string,
) ([]*errdetails.BadRequest_FieldViolation, error) {
var (
result = make([]*errdetails.BadRequest_FieldViolation, 0)
problems []string
err error
)
nameInputFilter := validation.InputFilter{
Filters: []validation.FilterInterface{&validation.StringTrimFilter{}},
Validators: []validation.ValidatorInterface{&validation.NotEmpty{}},
}
s.Name, problems, err = nameInputFilter.IsValidString(s.Name)
if err != nil {
return nil, err
}
for _, fv := range problems {
result = append(result, &errdetails.BadRequest_FieldViolation{
Field: "name",
Description: fv,
})
}
emailInputFilter := validation.InputFilter{
Filters: []validation.FilterInterface{&validation.StringTrimFilter{}},
Validators: []validation.ValidatorInterface{&validation.NotEmpty{}, &validation.EmailAddress{}},
}
s.Email, problems, err = emailInputFilter.IsValidString(s.Email)
if err != nil {
return nil, err
}
for _, fv := range problems {
result = append(result, &errdetails.BadRequest_FieldViolation{
Field: "email",
Description: fv,
})
}
messageInputFilter := validation.InputFilter{
Filters: []validation.FilterInterface{&validation.StringTrimFilter{}},
Validators: []validation.ValidatorInterface{&validation.NotEmpty{}},
}
s.Message, problems, err = messageInputFilter.IsValidString(s.Message)
if err != nil {
return nil, err
}
for _, fv := range problems {
result = append(result, &errdetails.BadRequest_FieldViolation{
Field: "message",
Description: fv,
})
}
if captchaEnabled {
captchaInputFilter := validation.InputFilter{
Filters: []validation.FilterInterface{&validation.StringTrimFilter{}},
Validators: []validation.ValidatorInterface{
&validation.NotEmpty{},
&validation.Recaptcha{
ClientIP: ip,
},
},
}
s.Captcha, problems, err = captchaInputFilter.IsValidString(s.Captcha)
if err != nil {
return nil, err
}
for _, fv := range problems {
result = append(result, &errdetails.BadRequest_FieldViolation{
Field: "captcha",
Description: fv,
})
}
}
return result, nil
}