File size: 2,137 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package email

import (
	"context"
	"net/mail"

	"github.com/target/goalert/config"
	"github.com/target/goalert/gadb"
	"github.com/target/goalert/notification/nfydest"
	"github.com/target/goalert/validation"
)

const (
	DestTypeEmail     = "builtin-smtp-email"
	FieldEmailAddress = "email_address"
	FallbackIconURL   = "builtin://email"
)

var _ nfydest.Provider = (*Sender)(nil)

func NewEmailDest(address string) gadb.DestV1 {
	return gadb.NewDestV1(DestTypeEmail, FieldEmailAddress, address)
}

func (s *Sender) ID() string { return DestTypeEmail }
func (s *Sender) TypeInfo(ctx context.Context) (*nfydest.TypeInfo, error) {
	cfg := config.FromContext(ctx)
	return &nfydest.TypeInfo{
		Type:                       DestTypeEmail,
		Name:                       "Email",
		Enabled:                    cfg.SMTP.Enable,
		SupportsAlertNotifications: true,
		SupportsUserVerification:   true,
		SupportsStatusUpdates:      true,
		UserVerificationRequired:   true,
		RequiredFields: []nfydest.FieldConfig{{
			FieldID:            FieldEmailAddress,
			Label:              "Email Address",
			PlaceholderText:    "foobar@example.com",
			InputType:          "email",
			SupportsValidation: true,
		}},
		DynamicParams: []nfydest.DynamicParamConfig{{
			ParamID: "subject",
			Label:   "Subject",
			Hint:    "Subject of the email message.",
		}, {
			ParamID: "body",
			Label:   "Body",
			Hint:    "Body of the email message.",
		}},
	}, nil
}

func (s *Sender) ValidateField(ctx context.Context, fieldID, value string) error {
	switch fieldID {
	case FieldEmailAddress:
		_, err := mail.ParseAddress(value)
		if err != nil {
			return validation.WrapError(err)
		}

		return nil
	}

	return validation.NewGenericError("unknown field ID")
}

func (s *Sender) DisplayInfo(ctx context.Context, args map[string]string) (*nfydest.DisplayInfo, error) {
	if args == nil {
		args = make(map[string]string)
	}

	e, err := mail.ParseAddress(args[FieldEmailAddress])
	if err != nil {
		return nil, validation.WrapError(err)
	}
	return &nfydest.DisplayInfo{
		IconURL:     FallbackIconURL,
		IconAltText: "Email",
		Text:        e.Address,
	}, nil
}