From 03bea8d9bd6cfd4af30f4d19af4523ee523905e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toma=C5=BE=20Jerman?= Date: Sun, 29 Aug 2021 10:38:23 +0200 Subject: [PATCH] Add function to add recipients --- automation/automation/email_handler.gen.go | 90 +++++++++++++++++++++- automation/automation/email_handler.go | 11 +++ automation/automation/email_handler.yaml | 21 ++++- 3 files changed, 118 insertions(+), 4 deletions(-) diff --git a/automation/automation/email_handler.gen.go b/automation/automation/email_handler.gen.go index 7f263de3c..433b5b588 100644 --- a/automation/automation/email_handler.gen.go +++ b/automation/automation/email_handler.gen.go @@ -35,6 +35,7 @@ func (h emailHandler) register() { h.SetHeaders(), h.SetHeader(), h.SetAddress(), + h.AddAddress(), h.Attach(), h.Embed(), ) @@ -809,7 +810,7 @@ type ( } ) -// SetAddress function Email address +// SetAddress function Email set address // // expects implementation of setAddress function: // func (h emailHandler) setAddress(ctx context.Context, args *emailSetAddressArgs) (err error) { @@ -821,8 +822,8 @@ func (h emailHandler) SetAddress() *atypes.Function { Kind: "function", Labels: map[string]string(nil), Meta: &atypes.FunctionMeta{ - Short: "Email address", - Description: "Adds new recipient, sender or reply-to address", + Short: "Email set address", + Description: "Sets the recipient, sender or reply-to addresses", }, Parameters: []*atypes.Param{ @@ -876,6 +877,89 @@ func (h emailHandler) SetAddress() *atypes.Function { } } +type ( + emailAddAddressArgs struct { + hasMessage bool + Message *emailMessage + + hasType bool + Type string + + hasAddress bool + Address string + + hasName bool + Name string + } +) + +// AddAddress function Email add address +// +// expects implementation of addAddress function: +// func (h emailHandler) addAddress(ctx context.Context, args *emailAddAddressArgs) (err error) { +// return +// } +func (h emailHandler) AddAddress() *atypes.Function { + return &atypes.Function{ + Ref: "emailAddAddress", + Kind: "function", + Labels: map[string]string(nil), + Meta: &atypes.FunctionMeta{ + Short: "Email add address", + Description: "Adds new recipient, sender or reply-to address", + }, + + Parameters: []*atypes.Param{ + { + Name: "message", + Types: []string{"EmailMessage"}, Required: true, + Meta: &atypes.ParamMeta{ + Label: "Message to be sent", + }, + }, + { + Name: "type", + Types: []string{"String"}, Required: true, + Meta: &atypes.ParamMeta{ + Label: "Type", + Description: "One of From", + }, + }, + { + Name: "address", + Types: []string{"String"}, Required: true, + Meta: &atypes.ParamMeta{ + Label: "Address", + }, + }, + { + Name: "name", + Types: []string{"String"}, + Meta: &atypes.ParamMeta{ + Label: "Name", + }, + }, + }, + + Handler: func(ctx context.Context, in *expr.Vars) (out *expr.Vars, err error) { + var ( + args = &emailAddAddressArgs{ + hasMessage: in.Has("message"), + hasType: in.Has("type"), + hasAddress: in.Has("address"), + hasName: in.Has("name"), + } + ) + + if err = in.Decode(args); err != nil { + return + } + + return out, h.addAddress(ctx, args) + }, + } +} + type ( emailAttachArgs struct { hasMessage bool diff --git a/automation/automation/email_handler.go b/automation/automation/email_handler.go index d968d215a..5dc543e59 100644 --- a/automation/automation/email_handler.go +++ b/automation/automation/email_handler.go @@ -117,6 +117,17 @@ func (h emailHandler) setAddress(_ context.Context, args *emailSetAddressArgs) ( return nil } +func (h emailHandler) addAddress(_ context.Context, args *emailAddAddressArgs) (err error) { + if args.Message.msg == nil { + return fmt.Errorf("email message not initialized") + } + + mm := args.Message.msg.GetHeader(args.Type) + mm = append(mm, args.Message.msg.FormatAddress(args.Address, args.Name)) + args.Message.msg.SetHeader(args.Type, mm...) + return nil +} + func (h emailHandler) attach(_ context.Context, args *emailAttachArgs) (err error) { if args.Message.msg == nil { return fmt.Errorf("email message not initialized") diff --git a/automation/automation/email_handler.yaml b/automation/automation/email_handler.yaml index 9c8b742df..dfcc90f90 100644 --- a/automation/automation/email_handler.yaml +++ b/automation/automation/email_handler.yaml @@ -134,7 +134,26 @@ functions: setAddress: kind: function meta: - short: Email address + short: Email set address + description: Sets the recipient, sender or reply-to addresses + params: + message: *messageParam + type: + required: true + meta: { label: Type, description: One of From, ReplyTo, To, CC, BCC } + types: [ { wf: String } ] + address: + required: true + meta: { label: Address } + types: [ { wf: String } ] + name: + meta: { label: Name } + types: [ { wf: String } ] + + addAddress: + kind: function + meta: + short: Email add address description: Adds new recipient, sender or reply-to address params: message: *messageParam