-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnotification.go
130 lines (108 loc) · 3.35 KB
/
notification.go
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package lime
import (
"encoding/json"
"errors"
"fmt"
)
// Notification provides information about events associated to a Message.
// It can be originated by an intermediate node, like a server, or by the destination of the message.
type Notification struct {
Envelope
// Event Related event to the notification
Event NotificationEvent
// In the case of a failed event, the Reason value brings more details about the problem.
Reason *Reason
}
func (not *Notification) SetEvent(event NotificationEvent) *Notification {
not.Event = event
return not
}
func (not *Notification) SetFailed(reason *Reason) *Notification {
not.Event = NotificationEventFailed
not.Reason = reason
return not
}
func (not Notification) MarshalJSON() ([]byte, error) {
raw, err := not.toRawEnvelope()
if err != nil {
return nil, err
}
return json.Marshal(raw)
}
func (not *Notification) UnmarshalJSON(b []byte) error {
raw := rawEnvelope{}
err := json.Unmarshal(b, &raw)
if err != nil {
return err
}
notification := Notification{}
err = notification.populate(&raw)
if err != nil {
return err
}
*not = notification
return nil
}
func (not *Notification) toRawEnvelope() (*rawEnvelope, error) {
raw, err := not.Envelope.toRawEnvelope()
if err != nil {
return nil, err
}
if not.Event != "" {
raw.Event = ¬.Event
}
raw.Reason = not.Reason
return raw, nil
}
func (not *Notification) populate(raw *rawEnvelope) error {
err := not.Envelope.populate(raw)
if err != nil {
return err
}
if raw.Event == nil {
return errors.New("notification event is required")
}
not.Event = *raw.Event
not.Reason = raw.Reason
return nil
}
// NotificationEvent represent the events that can happen in the message pipeline.
type NotificationEvent string
const (
// NotificationEventAccepted The message was received and accepted by the server.
// This event is similar To 'received' but is emitted by an intermediate node (hop) and not by the message's final destination.
NotificationEventAccepted = NotificationEvent("accepted")
// NotificationEventDispatched The message was dispatched to the destination by the server.
// This event is similar To the 'consumed' but is emitted by an intermediate node (hop) and not by the message's final destination.
NotificationEventDispatched = NotificationEvent("dispatched")
// NotificationEventReceived The node has received the message.
NotificationEventReceived = NotificationEvent("received")
// NotificationEventConsumed The node has consumed the Content of the message.
NotificationEventConsumed = NotificationEvent("consumed")
// NotificationEventFailed A problem occurred during the processing of the message.
// In this case, the reason property of the notification should be present.
NotificationEventFailed = NotificationEvent("failed")
)
func (e *NotificationEvent) Validate() error {
switch *e {
case NotificationEventAccepted, NotificationEventDispatched, NotificationEventReceived, NotificationEventConsumed, NotificationEventFailed:
return nil
}
return fmt.Errorf("invalid notification event '%v'", e)
}
func (e NotificationEvent) MarshalText() ([]byte, error) {
err := e.Validate()
if err != nil {
return []byte{}, err
}
return []byte(e), nil
}
func (e *NotificationEvent) UnmarshalText(text []byte) error {
event := NotificationEvent(text)
err := event.Validate()
if err != nil {
return err
}
*e = event
return nil
}