mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-11 19:04:35 +00:00
integrate spam check on email (#270)
This commit is contained in:
@@ -8,12 +8,15 @@ import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
"github.com/micro/micro/v3/service"
|
||||
"github.com/micro/micro/v3/service/client"
|
||||
"github.com/micro/micro/v3/service/config"
|
||||
"github.com/micro/micro/v3/service/errors"
|
||||
log "github.com/micro/micro/v3/service/logger"
|
||||
"github.com/micro/micro/v3/service/store"
|
||||
pb "github.com/micro/services/email/proto"
|
||||
"github.com/micro/services/pkg/tenant"
|
||||
spampb "github.com/micro/services/spam/proto"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -31,7 +34,7 @@ type sendgridConf struct {
|
||||
EmailFrom string `json:"email_from"`
|
||||
}
|
||||
|
||||
func NewEmailHandler() *Email {
|
||||
func NewEmailHandler(svc *service.Service) *Email {
|
||||
c := sendgridConf{}
|
||||
val, err := config.Get("sendgridapi")
|
||||
if err != nil {
|
||||
@@ -46,11 +49,13 @@ func NewEmailHandler() *Email {
|
||||
}
|
||||
return &Email{
|
||||
c,
|
||||
spampb.NewSpamService("spam", svc.Client()),
|
||||
}
|
||||
}
|
||||
|
||||
type Email struct {
|
||||
config sendgridConf
|
||||
config sendgridConf
|
||||
spamSvc spampb.SpamService
|
||||
}
|
||||
|
||||
func (e *Email) Send(ctx context.Context, request *pb.SendRequest, response *pb.SendResponse) error {
|
||||
@@ -67,6 +72,19 @@ func (e *Email) Send(ctx context.Context, request *pb.SendRequest, response *pb.
|
||||
return errors.BadRequest("email.send.validation", "Missing email body")
|
||||
}
|
||||
|
||||
spamReq := &spampb.ClassifyRequest{
|
||||
TextBody: request.TextBody,
|
||||
HtmlBody: request.HtmlBody,
|
||||
To: request.To,
|
||||
From: request.From,
|
||||
Subject: request.Subject,
|
||||
}
|
||||
rsp, err := e.spamSvc.Classify(ctx, spamReq, client.WithAuthToken())
|
||||
if err != nil || rsp.IsSpam {
|
||||
log.Errorf("Error validating email %s %v", err, rsp)
|
||||
return errors.InternalServerError("email.send", "Error validating email")
|
||||
}
|
||||
|
||||
if err := e.sendEmail(ctx, request); err != nil {
|
||||
log.Errorf("Error sending email: %v\n", err)
|
||||
return errors.InternalServerError("email.sendemail", "Error sending email")
|
||||
|
||||
@@ -16,7 +16,7 @@ func main() {
|
||||
)
|
||||
|
||||
// Register handler
|
||||
pb.RegisterEmailHandler(srv.Server(), handler.NewEmailHandler())
|
||||
pb.RegisterEmailHandler(srv.Server(), handler.NewEmailHandler(srv))
|
||||
traceCloser := tracing.SetupOpentracing("email")
|
||||
defer traceCloser.Close()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user