Skip to content

Commit

Permalink
Update to goflow v0.61.0 and provide email service to engine
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Nov 28, 2019
1 parent f0c5b98 commit 7cc2d94
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 123 deletions.
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ require (
github.com/fortytw2/leaktest v1.3.0 // indirect
github.com/getsentry/raven-go v0.1.2-0.20190125112653-238ebd86338d // indirect
github.com/go-chi/chi v3.3.3+incompatible
github.com/go-mail/mail v0.0.0-20180301192024-63235f23494b
github.com/go-playground/locales v0.12.1 // indirect
github.com/go-sql-driver/mysql v1.4.1 // indirect
github.com/gomodule/redigo v2.0.0+incompatible
Expand All @@ -23,7 +22,7 @@ require (
github.com/mattn/go-sqlite3 v1.10.0 // indirect
github.com/nyaruka/ezconf v0.2.1
github.com/nyaruka/gocommon v1.1.1
github.com/nyaruka/goflow v0.60.1
github.com/nyaruka/goflow v0.61.0
github.com/nyaruka/librato v1.0.0
github.com/nyaruka/logrus_sentry v0.8.2-0.20190129182604-c2962b80ba7d
github.com/nyaruka/null v1.2.0
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ github.com/getsentry/raven-go v0.1.2-0.20190125112653-238ebd86338d h1:CIp8WnfXz7
github.com/getsentry/raven-go v0.1.2-0.20190125112653-238ebd86338d/go.mod h1:KungGk8q33+aIAZUIVWZDr2OfAEBsO49PX4NzFV5kcQ=
github.com/go-chi/chi v3.3.3+incompatible h1:KHkmBEMNkwKuK4FdQL7N2wOeB9jnIx7jR5wsuSBEFI8=
github.com/go-chi/chi v3.3.3+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ=
github.com/go-mail/mail v0.0.0-20180301192024-63235f23494b h1:m35QDmGg5Pnb8U7OP/pETlSu9njctBR/SLucC28Wy6g=
github.com/go-mail/mail v0.0.0-20180301192024-63235f23494b/go.mod h1:VPWjmmNyRsWXQZHVHT3g0YbIINUkSmuKOiLIDkWbL6M=
github.com/go-mail/mail v2.3.1+incompatible h1:UzNOn0k5lpfVtO31cK3hn6I4VEVGhe3lX8AJBAxXExM=
github.com/go-mail/mail v2.3.1+incompatible/go.mod h1:VPWjmmNyRsWXQZHVHT3g0YbIINUkSmuKOiLIDkWbL6M=
github.com/go-playground/locales v0.11.2/go.mod h1:IUMDtCfWo/w/mtMfIE/IG2K+Ey3ygWanZIBtBW0W2TM=
github.com/go-playground/locales v0.12.1 h1:2FITxuFt/xuCNP1Acdhv62OzaCiviiE4kotfhkmOqEc=
github.com/go-playground/locales v0.12.1/go.mod h1:IUMDtCfWo/w/mtMfIE/IG2K+Ey3ygWanZIBtBW0W2TM=
Expand Down Expand Up @@ -70,8 +70,8 @@ github.com/nyaruka/ezconf v0.2.1 h1:TDXWoqjqYya1uhou1mAJZg7rgFYL98EB0Tb3+BWtUh0=
github.com/nyaruka/ezconf v0.2.1/go.mod h1:ey182kYkw2MIi4XiWe1FR/mzI33WCmTWuceDYYxgnQw=
github.com/nyaruka/gocommon v1.1.1 h1:RnQ+kMzN1lA+W0NpkBDd0mGU3UqadJygR3SMpITMYTQ=
github.com/nyaruka/gocommon v1.1.1/go.mod h1:QbdU2J9WBsqBmeZRuwndf2f6O7rD7mkC0bGn5UNnwjI=
github.com/nyaruka/goflow v0.60.1 h1:UJWiKIImt+2pJ5UudU2/nG+W3MugTMrSquZWceVqPfM=
github.com/nyaruka/goflow v0.60.1/go.mod h1:wI+5qZRajxxZvfYXqbmWauTWAJ0GOTTG3TZDkixU/E0=
github.com/nyaruka/goflow v0.61.0 h1:lgABUhP2vyUXQLeCDSuA5nLXLJgOy3b7wnMUliqW2ts=
github.com/nyaruka/goflow v0.61.0/go.mod h1:fb6eGAXiTL2hjbzMpXwSIlNx0O4IsR9XBunXBN+8pWM=
github.com/nyaruka/librato v1.0.0 h1:Vznj9WCeC1yZXbBYyYp40KnbmXLbEkjKmHesV/v2SR0=
github.com/nyaruka/librato v1.0.0/go.mod h1:pkRNLFhFurOz0QqBz6/DuTFhHHxAubWxs4Jx+J7yUgg=
github.com/nyaruka/logrus_sentry v0.8.2-0.20190129182604-c2962b80ba7d h1:hyp9u36KIwbTCo2JAJ+TuJcJBc+UZzEig7RI/S5Dvkc=
Expand Down
22 changes: 21 additions & 1 deletion goflow/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
var httpClient *http.Client
var eng, simulator flows.Engine
var engInit, simulatorInit sync.Once

var emailFactory engine.EmailServiceFactory
var classificationFactory engine.ClassificationServiceFactory
var airtimeFactory engine.AirtimeServiceFactory

Expand All @@ -34,6 +36,12 @@ func init() {
httpClient = &http.Client{Transport: t, Timeout: time.Duration(15 * time.Second)}
}

// RegisterEmailServiceFactory can be used by outside callers to register a email factory
// for use by the engine
func RegisterEmailServiceFactory(factory engine.EmailServiceFactory) {
emailFactory = factory
}

// RegisterClassificationServiceFactory can be used by outside callers to register a classification factory
// for use by the engine
func RegisterClassificationServiceFactory(factory engine.ClassificationServiceFactory) {
Expand All @@ -56,6 +64,7 @@ func Engine() flows.Engine {

eng = engine.NewBuilder().
WithWebhookServiceFactory(webhooks.NewServiceFactory(httpClient, webhookHeaders, 10000)).
WithEmailServiceFactory(emailFactory).
WithClassificationServiceFactory(classificationFactory).
WithAirtimeServiceFactory(airtimeFactory).
WithMaxStepsPerSprint(config.Mailroom.MaxStepsPerSprint).
Expand All @@ -76,14 +85,25 @@ func Simulator() flows.Engine {
simulator = engine.NewBuilder().
WithWebhookServiceFactory(webhooks.NewServiceFactory(httpClient, webhookHeaders, 10000)).
WithClassificationServiceFactory(classificationFactory). // simulated sessions do real classification
WithAirtimeServiceFactory(simulatorAirtimeServiceFactory). // but faked airtime transfers
WithEmailServiceFactory(simulatorEmailServiceFactory). // but faked emails
WithAirtimeServiceFactory(simulatorAirtimeServiceFactory). // and faked airtime transfers
WithMaxStepsPerSprint(config.Mailroom.MaxStepsPerSprint).
Build()
})

return simulator
}

func simulatorEmailServiceFactory(session flows.Session) (flows.EmailService, error) {
return &simulatorEmailService{}, nil
}

type simulatorEmailService struct{}

func (s *simulatorEmailService) Send(session flows.Session, addresses []string, subject, body string) error {
return nil
}

func simulatorAirtimeServiceFactory(session flows.Session) (flows.AirtimeService, error) {
return &simulatorAirtimeService{}, nil
}
Expand Down
115 changes: 0 additions & 115 deletions hooks/email_created.go

This file was deleted.

32 changes: 32 additions & 0 deletions hooks/email_sent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package hooks

import (
"context"

"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/flows/events"
"github.com/nyaruka/mailroom/models"

"github.com/gomodule/redigo/redis"
"github.com/jmoiron/sqlx"
"github.com/sirupsen/logrus"
)

func init() {
models.RegisterEventHook(events.TypeEmailSent, handleEmailSent)
}

// handleEmailCreated event queues an email to be sent later on
func handleEmailSent(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, org *models.OrgAssets, session *models.Session, e flows.Event) error {

// goflow now sends email so just log the event
event := e.(*events.EmailSentEvent)
logrus.WithFields(logrus.Fields{
"contact_uuid": session.ContactUUID(),
"session_id": session.ID(),
"body": event.Body,
"to": event.To,
}).Debug("email sent")

return nil
}
2 changes: 1 addition & 1 deletion hooks/email_created_test.go → hooks/email_sent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/nyaruka/mailroom/testsuite"
)

func TestEmailCreated(t *testing.T) {
func TestEmailSent(t *testing.T) {
// configure mailtrap for our org
db := testsuite.DB()
db.MustExec(`UPDATE orgs_org SET config = '{"smtp_server": "smtp://24f335c64dbc28:d7966a553e76f6@smtp.mailtrap.io:2525/?from=mailroom@foo.bar"}' WHERE id = 1;`)
Expand Down
18 changes: 18 additions & 0 deletions models/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/nyaruka/goflow/envs"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/services/airtime/dtone"
"github.com/nyaruka/goflow/services/email/smtp"
"github.com/nyaruka/mailroom/config"
"github.com/nyaruka/mailroom/goflow"

Expand All @@ -22,6 +23,12 @@ func init() {
// give airtime transfers an extra long timeout
httpClient := &http.Client{Timeout: time.Duration(120 * time.Second)}

goflow.RegisterEmailServiceFactory(
func(session flows.Session) (flows.EmailService, error) {
return orgFromSession(session).EmailService(httpClient)
},
)

goflow.RegisterAirtimeServiceFactory(
func(session flows.Session) (flows.AirtimeService, error) {
return orgFromSession(session).AirtimeService(httpClient)
Expand All @@ -34,6 +41,7 @@ type OrgID int
const (
NilOrgID = OrgID(0)

configSMTPServer = "smtp_server"
configDTOneLogin = "TRANSFERTO_ACCOUNT_LOGIN"
configDTOneToken = "TRANSFERTO_AIRTIME_API_TOKEN"
configDTOnecurrency = "TRANSFERTO_ACCOUNT_CURRENCY"
Expand Down Expand Up @@ -106,6 +114,16 @@ func (o *Org) ConfigValue(key string, def string) string {
return strVal
}

// EmailService returns the email service for this org
func (o *Org) EmailService(httpClient *http.Client) (flows.EmailService, error) {
connectionURL := o.ConfigValue(configSMTPServer, config.Mailroom.SMTPServer)

if connectionURL == "" {
return nil, errors.New("missing SMTP configuration")
}
return smtp.NewServiceFromURL(connectionURL)
}

// AirtimeService returns the airtime service for this org if one is configured
func (o *Org) AirtimeService(httpClient *http.Client) (flows.AirtimeService, error) {
login := o.ConfigValue(configDTOneLogin, "")
Expand Down

0 comments on commit 7cc2d94

Please sign in to comment.