-
-
Notifications
You must be signed in to change notification settings - Fork 274
/
notification.go
113 lines (89 loc) · 2.93 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
package bbgo
import (
"bytes"
"github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/util"
)
var Notification = &Notifiability{
SymbolChannelRouter: NewPatternChannelRouter(nil),
SessionChannelRouter: NewPatternChannelRouter(nil),
ObjectChannelRouter: NewObjectChannelRouter(),
}
func Notify(obj interface{}, args ...interface{}) {
Notification.Notify(obj, args...)
}
func NotifyTo(channel string, obj interface{}, args ...interface{}) {
Notification.NotifyTo(channel, obj, args...)
}
func SendPhoto(buffer *bytes.Buffer) {
Notification.SendPhoto(buffer)
}
func SendPhotoTo(channel string, buffer *bytes.Buffer) {
Notification.SendPhotoTo(channel, buffer)
}
type Notifier interface {
NotifyTo(channel string, obj interface{}, args ...interface{})
Notify(obj interface{}, args ...interface{})
SendPhotoTo(channel string, buffer *bytes.Buffer)
SendPhoto(buffer *bytes.Buffer)
}
type NullNotifier struct{}
func (n *NullNotifier) NotifyTo(channel string, obj interface{}, args ...interface{}) {}
func (n *NullNotifier) Notify(obj interface{}, args ...interface{}) {}
func (n *NullNotifier) SendPhoto(buffer *bytes.Buffer) {}
func (n *NullNotifier) SendPhotoTo(channel string, buffer *bytes.Buffer) {}
type Notifiability struct {
notifiers []Notifier
SessionChannelRouter *PatternChannelRouter `json:"-"`
SymbolChannelRouter *PatternChannelRouter `json:"-"`
ObjectChannelRouter *ObjectChannelRouter `json:"-"`
}
// RouteSymbol routes symbol name to channel
func (m *Notifiability) RouteSymbol(symbol string) (channel string, ok bool) {
if m.SymbolChannelRouter != nil {
return m.SymbolChannelRouter.Route(symbol)
}
return "", false
}
// RouteSession routes Session name to channel
func (m *Notifiability) RouteSession(session string) (channel string, ok bool) {
if m.SessionChannelRouter != nil {
return m.SessionChannelRouter.Route(session)
}
return "", false
}
// RouteObject routes object to channel
func (m *Notifiability) RouteObject(obj interface{}) (channel string, ok bool) {
if m.ObjectChannelRouter != nil {
return m.ObjectChannelRouter.Route(obj)
}
return "", false
}
// AddNotifier adds the notifier that implements the Notifier interface.
func (m *Notifiability) AddNotifier(notifier Notifier) {
m.notifiers = append(m.notifiers, notifier)
}
func (m *Notifiability) Notify(obj interface{}, args ...interface{}) {
if str, ok := obj.(string); ok {
simpleArgs := util.FilterSimpleArgs(args)
logrus.Infof(str, simpleArgs...)
}
for _, n := range m.notifiers {
n.Notify(obj, args...)
}
}
func (m *Notifiability) NotifyTo(channel string, obj interface{}, args ...interface{}) {
for _, n := range m.notifiers {
n.NotifyTo(channel, obj, args...)
}
}
func (m *Notifiability) SendPhoto(buffer *bytes.Buffer) {
for _, n := range m.notifiers {
n.SendPhoto(buffer)
}
}
func (m *Notifiability) SendPhotoTo(channel string, buffer *bytes.Buffer) {
for _, n := range m.notifiers {
n.SendPhotoTo(channel, buffer)
}
}