From 7e81c6bd1752c7532f7b326e9d026ed9091ed430 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toma=C5=BE=20Jerman?= Date: Wed, 24 Mar 2021 16:00:19 +0100 Subject: [PATCH] Workflow tweaks * Allow Reader for email subject, * Remove valid ID constraint check. --- automation/automation/email_handler.gen.go | 46 +++++++++++++++++++--- automation/automation/email_handler.go | 30 ++++++++++---- automation/automation/email_handler.yaml | 1 + pkg/expr/expr_types.go | 10 ++--- 4 files changed, 68 insertions(+), 19 deletions(-) diff --git a/automation/automation/email_handler.gen.go b/automation/automation/email_handler.gen.go index 24ab810fa..7f263de3c 100644 --- a/automation/automation/email_handler.gen.go +++ b/automation/automation/email_handler.gen.go @@ -42,8 +42,10 @@ func (h emailHandler) register() { type ( emailSendArgs struct { - hasSubject bool - Subject string + hasSubject bool + Subject interface{} + subjectString string + subjectStream io.Reader hasReplyTo bool ReplyTo interface{} @@ -79,6 +81,10 @@ type ( } ) +func (a emailSendArgs) GetSubject() (bool, string, io.Reader) { + return a.hasSubject, a.subjectString, a.subjectStream +} + func (a emailSendArgs) GetReplyTo() (bool, string, *sysTypes.User) { return a.hasReplyTo, a.replyToString, a.replyToUser } @@ -122,7 +128,7 @@ func (h emailHandler) Send() *atypes.Function { Parameters: []*atypes.Param{ { Name: "subject", - Types: []string{"String"}, + Types: []string{"String", "Reader"}, Meta: &atypes.ParamMeta{ Label: "Subject", }, @@ -188,6 +194,17 @@ func (h emailHandler) Send() *atypes.Function { return } + // Converting Subject argument + if args.hasSubject { + aux := expr.Must(expr.Select(in, "subject")) + switch aux.Type() { + case h.reg.Type("String").Type(): + args.subjectString = aux.Get().(string) + case h.reg.Type("Reader").Type(): + args.subjectStream = aux.Get().(io.Reader) + } + } + // Converting ReplyTo argument if args.hasReplyTo { aux := expr.Must(expr.Select(in, "replyTo")) @@ -265,8 +282,10 @@ func (h emailHandler) Send() *atypes.Function { type ( emailMessageArgs struct { - hasSubject bool - Subject string + hasSubject bool + Subject interface{} + subjectString string + subjectStream io.Reader hasReplyTo bool ReplyTo interface{} @@ -306,6 +325,10 @@ type ( } ) +func (a emailMessageArgs) GetSubject() (bool, string, io.Reader) { + return a.hasSubject, a.subjectString, a.subjectStream +} + func (a emailMessageArgs) GetReplyTo() (bool, string, *sysTypes.User) { return a.hasReplyTo, a.replyToString, a.replyToUser } @@ -349,7 +372,7 @@ func (h emailHandler) Message() *atypes.Function { Parameters: []*atypes.Param{ { Name: "subject", - Types: []string{"String"}, + Types: []string{"String", "Reader"}, Meta: &atypes.ParamMeta{ Label: "Subject", }, @@ -423,6 +446,17 @@ func (h emailHandler) Message() *atypes.Function { return } + // Converting Subject argument + if args.hasSubject { + aux := expr.Must(expr.Select(in, "subject")) + switch aux.Type() { + case h.reg.Type("String").Type(): + args.subjectString = aux.Get().(string) + case h.reg.Type("Reader").Type(): + args.subjectStream = aux.Get().(io.Reader) + } + } + // Converting ReplyTo argument if args.hasReplyTo { aux := expr.Must(expr.Select(in, "replyTo")) diff --git a/automation/automation/email_handler.go b/automation/automation/email_handler.go index 30a7fcfd7..d968d215a 100644 --- a/automation/automation/email_handler.go +++ b/automation/automation/email_handler.go @@ -3,12 +3,13 @@ package automation import ( "context" "fmt" - "github.com/cortezaproject/corteza-server/pkg/mail" - sysTypes "github.com/cortezaproject/corteza-server/system/types" - gomail "gopkg.in/mail.v2" "io" "io/ioutil" "strings" + + "github.com/cortezaproject/corteza-server/pkg/mail" + sysTypes "github.com/cortezaproject/corteza-server/system/types" + gomail "gopkg.in/mail.v2" ) type ( @@ -37,17 +38,32 @@ func EmailHandler(reg emailHandlerRegistry) *emailHandler { func (h emailHandler) send(_ context.Context, args *emailSendArgs) (err error) { msg := mail.New() - if err = h.procArgs(msg, args.Subject, args); err != nil { + + _, s, r := args.GetSubject() + if r != nil { + aux, _ := ioutil.ReadAll(r) + s = string(aux) + } + + if err = h.procArgs(msg, s, args); err != nil { return } return mail.Send(msg) } -func (h emailHandler) message(_ context.Context, args *emailMessageArgs) (r *emailMessageResults, err error) { +func (h emailHandler) message(_ context.Context, args *emailMessageArgs) (*emailMessageResults, error) { + var err error msg := mail.New() - if err = h.procArgs(msg, args.Subject, args); err != nil { - return + + _, s, r := args.GetSubject() + if r != nil { + aux, _ := ioutil.ReadAll(r) + s = string(aux) + } + + if err = h.procArgs(msg, s, args); err != nil { + return nil, err } return &emailMessageResults{Message: &emailMessage{msg: msg}}, nil diff --git a/automation/automation/email_handler.yaml b/automation/automation/email_handler.yaml index a932c8fb8..9c8b742df 100644 --- a/automation/automation/email_handler.yaml +++ b/automation/automation/email_handler.yaml @@ -11,6 +11,7 @@ messageParams: &messageParams label: Subject types: - { wf: String } + - { wf: Reader, suffix: Stream } replyTo: meta: diff --git a/pkg/expr/expr_types.go b/pkg/expr/expr_types.go index d643de0d9..dbc5c223c 100644 --- a/pkg/expr/expr_types.go +++ b/pkg/expr/expr_types.go @@ -5,15 +5,16 @@ import ( "context" "encoding/json" "fmt" - "github.com/cortezaproject/corteza-server/pkg/errors" - "github.com/cortezaproject/corteza-server/pkg/handle" - "github.com/spf13/cast" "io" "net/http" "net/url" "reflect" "strings" "time" + + "github.com/cortezaproject/corteza-server/pkg/errors" + "github.com/cortezaproject/corteza-server/pkg/handle" + "github.com/spf13/cast" ) type ( @@ -196,9 +197,6 @@ func CastToFloat(val interface{}) (out float64, err error) { func CastToID(val interface{}) (out uint64, err error) { out, err = cast.ToUint64E(UntypedValue(val)) - if out == 0 { - err = fmt.Errorf("invalid ID") - } return }