/
push.go
136 lines (114 loc) · 3.52 KB
/
push.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
package node
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"berty.tech/core/api/p2p"
"berty.tech/core/entity"
"berty.tech/core/pkg/deviceinfo"
"berty.tech/core/pkg/errorcodes"
"berty.tech/core/push"
"github.com/pkg/errors"
"go.uber.org/zap"
)
func WithPushManager(pushManager *push.Manager) NewNodeOption {
return func(n *Node) {
n.pushManager = pushManager
}
}
func WithPushTokenSubscriber() NewNodeOption {
return func(n *Node) {
var err error
ctx := context.Background()
packageID := deviceinfo.PackageID()
go func() {
tokenSubscription := n.notificationDriver.SubscribeToken()
for {
select {
case token := <-tokenSubscription:
{
logger().Debug("node push token subscriber receive token",
zap.String("type", token.Type.String()),
zap.String("hash", token.Hash()),
)
currentToken := &entity.DevicePushConfig{}
if err = n.sql(ctx).First(¤tToken, &entity.DevicePushConfig{PushType: token.Type}).Error; err != nil {
logger().Info("unable to get push token", zap.Error(err))
}
pushID := &push.PushNativeIdentifier{
PackageID: packageID,
DeviceToken: token.Hash(),
}
pushIDBytes, err := pushID.Marshal()
if err != nil {
logger().Error("unable to serialize push id", zap.Error(err))
continue
}
if len(token.Value) > 0 && bytes.Compare(currentToken.PushID, pushIDBytes) == 0 {
continue
}
if len(currentToken.PushID) > 0 {
_, err = n.DevicePushConfigRemove(ctx, currentToken)
logger().Error("unable to delete existing push token", zap.Error(err))
}
if len(token.Value) > 0 {
_, err = n.DevicePushConfigCreate(ctx, &entity.DevicePushConfig{
RelayID: []byte{},
PushID: pushIDBytes,
PushType: token.Type,
})
logger().Error("unable to create push token", zap.Error(err))
}
}
case <-n.shutdown:
logger().Debug("node push token subscriber shutdown")
n.notificationDriver.UnsubscribeToken(tokenSubscription)
}
}
}()
}
}
func WithPushNotificationSubscriber() NewNodeOption {
return func(n *Node) {
ctx := context.Background()
go func() {
notificationSubscription := n.notificationDriver.Subscribe()
for {
select {
case notification := <-notificationSubscription:
{
logger().Debug("node receive push notification")
payload := push.Payload{}
if err := json.Unmarshal([]byte(notification.Body), &payload); err != nil {
logger().Error(errorcodes.ErrNodePushNotifSub.Wrap(err).Error())
continue
}
b64Envelope := payload.BertyEnvelope
if b64Envelope == "" {
logger().Error(errorcodes.ErrNodePushNotifSub.Wrap(errors.New("berty-envelope is missing")).Error())
continue
}
bytesEnvelope, err := base64.StdEncoding.DecodeString(string(b64Envelope))
if err != nil {
logger().Error(errorcodes.ErrNodePushNotifSub.Wrap(err).Error())
continue
}
envelope := &p2p.Envelope{}
if err := envelope.Unmarshal(bytesEnvelope); err != nil {
logger().Error(errorcodes.ErrNodePushNotifSub.Wrap(err).Error())
continue
}
if err := n.handleEnvelope(ctx, envelope); err != nil {
logger().Error(errorcodes.ErrNodePushNotifSub.Wrap(err).Error())
continue
}
}
case <-n.shutdown:
logger().Debug("node push notification subscriber shutdown")
n.notificationDriver.Unsubscribe(notificationSubscription)
}
}
}()
}
}