File size: 1,635 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
package nfydest

import (
	"context"

	"github.com/target/goalert/notification/nfymsg"
)

// A MessageSender can send notifications.
type MessageSender interface {
	// Send should return nil error if the notification was sent successfully. It should be expected
	// that a returned error means that the notification should be attempted again.
	//
	// If the sent message can have its status tracked, a unique externalID should be returned.
	SendMessage(context.Context, nfymsg.Message) (*nfymsg.SentMessage, error)
}

func (r *Registry) SendMessage(ctx context.Context, msg nfymsg.Message) (*nfymsg.SentMessage, error) {
	p := r.Provider(msg.DestType())
	if p == nil {
		return nil, ErrUnknownType
	}

	s, ok := p.(MessageSender)
	if !ok {
		return nil, ErrUnsupported
	}

	info, err := p.TypeInfo(ctx)
	if err != nil {
		return nil, err
	}

	if !info.Enabled {
		return nil, ErrNotEnabled
	}

	switch msg.(type) {
	case nfymsg.Alert, nfymsg.AlertBundle:
		if !info.SupportsAlertNotifications {
			return nil, ErrUnsupported
		}
	case nfymsg.AlertStatus:
		if !info.SupportsStatusUpdates {
			return nil, ErrUnsupported
		}
	case nfymsg.Verification:
		if !info.SupportsUserVerification {
			return nil, ErrUnsupported
		}
	case nfymsg.Test:
	case nfymsg.SignalMessage:
		if !info.SupportsSignals {
			return nil, ErrUnsupported
		}
	case nfymsg.ScheduleOnCallUsers:
		if !info.SupportsOnCallNotify {
			return nil, ErrUnsupported
		}
	default:
		return nil, ErrUnsupported
	}

	if r.stubSender {
		return &nfymsg.SentMessage{
			ExternalID: "stub",
			State:      nfymsg.StateDelivered,
		}, nil
	}

	return s.SendMessage(ctx, msg)
}