forked from SkygearIO/skygear-server
/
notification.go
142 lines (117 loc) · 3.69 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
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright 2015-present Oursky Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package subscription
import (
"encoding/json"
"fmt"
"github.com/sirupsen/logrus"
"github.com/skygeario/skygear-server/pkg/server/pubsub"
"github.com/skygeario/skygear-server/pkg/server/push"
"github.com/skygeario/skygear-server/pkg/server/skydb"
)
// Notice encapsulates the information sent to subscribers when the content of
// a subscription has changed.
type Notice struct {
// SeqNum is a strictly increasing number between notice
SeqNum uint64
SubscriptionID string
Event skydb.RecordHookEvent
Record *skydb.Record
}
// Notifier is the interface implemented by an object that knows how to deliver
// a Notice to a device.
type Notifier interface {
// CanNotify returns whether the Notifier can send notice to the device.
CanNotify(device skydb.Device) bool
// Notify sends an notice to the device.
Notify(device skydb.Device, notice Notice) error
}
type pushNotifier struct {
sender push.Sender
}
// NewPushNotifier returns an Notifier which sends Notice
// using the given push.Sender.
func NewPushNotifier(sender push.Sender) Notifier {
return &pushNotifier{sender}
}
func (notifier *pushNotifier) CanNotify(device skydb.Device) bool {
return device.Type == "ios"
}
func (notifier *pushNotifier) Notify(device skydb.Device, notice Notice) error {
customMap := map[string]interface{}{
"aps": map[string]interface{}{
"content_available": 1,
},
"_skygear": map[string]interface{}{
"seq-num": notice.SeqNum,
"subscription-id": notice.SubscriptionID,
},
}
return notifier.sender.Send(push.MapMapper(customMap), device)
}
type hubNotifier pubsub.Hub
// NewHubNotifier returns an Notifier which sends Notice thru the supplied
// hub. The notice will be sent via the channel name "_sub_[DEVICE_ID]".
func NewHubNotifier(hub *pubsub.Hub) Notifier {
return (*hubNotifier)(hub)
}
func (n *hubNotifier) CanNotify(device skydb.Device) bool {
return true
}
func (n *hubNotifier) Notify(device skydb.Device, notice Notice) error {
data, err := json.Marshal(struct {
SeqNum uint64 `json:"seq-num"`
SubscriptionID string `json:"subscription-id"`
}{notice.SeqNum, notice.SubscriptionID})
if err == nil {
(*pubsub.Hub)(n).Broadcast <- pubsub.Parcel{
Channel: fmt.Sprintf("_sub_%s", device.ID),
Data: data,
}
}
return err
}
type multiNotifier []Notifier
// NewMultiNotifier returns a Notifier which sends Notice to multiple
// underlying Notifiers
func NewMultiNotifier(notifiers ...Notifier) Notifier {
return multiNotifier(notifiers)
}
func (ns multiNotifier) CanNotify(device skydb.Device) bool {
return true
}
func (ns multiNotifier) Notify(device skydb.Device, notice Notice) error {
n := len(ns)
errCh := make(chan error)
for _, notifier := range ns {
notifier := notifier
if notifier.CanNotify(device) {
go func() {
errCh <- notifier.Notify(device, notice)
}()
}
}
var lasterr error
for i := 0; i < n; i++ {
if err := <-errCh; err != nil {
lasterr = err
log.WithFields(logrus.Fields{
"device": device,
"notice": notice,
"err": err,
}).Errorf("multi-notifier: failed to send notice")
}
}
return lasterr
}