-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdata.go
140 lines (118 loc) · 3.73 KB
/
data.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
package push
import (
"context"
"github.com/sirupsen/logrus"
"github.com/code-payments/code-server/pkg/code/common"
code_data "github.com/code-payments/code-server/pkg/code/data"
push_data "github.com/code-payments/code-server/pkg/code/data/push"
push_lib "github.com/code-payments/code-server/pkg/push"
)
type dataPushType string
const (
dataPushTypeKey = "code_notification_type"
chatMessageDataPush dataPushType = "ChatMessage"
executeSwapDataPush dataPushType = "ExecuteSwap"
)
// sendRawDataPushNotificationToOwner is a generic utility for sending raw data push
// notification to the devices linked to an owner account.
//
// todo: Duplicated code with other send push utitilies
func sendRawDataPushNotificationToOwner(
ctx context.Context,
data code_data.Provider,
pusher push_lib.Provider,
owner *common.Account,
notificationType dataPushType,
kvs map[string]string,
) error {
log := logrus.StandardLogger().WithFields(logrus.Fields{
"method": "sendRawDataPushNotificationToOwner",
"owner": owner.PublicKey().ToBase58(),
})
kvs[dataPushTypeKey] = string(notificationType)
pushTokenRecords, err := getPushTokensForOwner(ctx, data, owner)
if err != nil {
log.WithError(err).Warn("failure getting push tokens for owner")
return err
}
seenPushTokens := make(map[string]struct{})
for _, pushTokenRecord := range pushTokenRecords {
// Dedup push tokens, since they may appear more than once per app install
if _, ok := seenPushTokens[pushTokenRecord.PushToken]; ok {
continue
}
log := log.WithField("push_token", pushTokenRecord.PushToken)
// Try push
err := pusher.SendDataPush(
ctx,
pushTokenRecord.PushToken,
kvs,
)
if err != nil {
log.WithError(err).Warn("failure sending push notification")
onPushError(ctx, data, pusher, pushTokenRecord)
}
seenPushTokens[pushTokenRecord.PushToken] = struct{}{}
}
return nil
}
// sendMutableNotificationToOwner is a generic utility for sending mutable
// push notification to the devices linked to an owner account. It's a
// special data push where the notification content is replaced by the contents
// of a kv pair payload.
//
// todo: Duplicated code with other send push utitilies
func sendMutableNotificationToOwner(
ctx context.Context,
data code_data.Provider,
pusher push_lib.Provider,
owner *common.Account,
notificationType dataPushType,
titleKey string,
kvs map[string]string,
) error {
log := logrus.StandardLogger().WithFields(logrus.Fields{
"method": "sendMutableNotificationToOwner",
"owner": owner.PublicKey().ToBase58(),
})
kvs[dataPushTypeKey] = string(notificationType)
pushTokenRecords, err := getPushTokensForOwner(ctx, data, owner)
if err != nil {
log.WithError(err).Warn("failure getting push tokens for owner")
return err
}
seenPushTokens := make(map[string]struct{})
for _, pushTokenRecord := range pushTokenRecords {
// Dedup push tokens, since they may appear more than once per app install
if _, ok := seenPushTokens[pushTokenRecord.PushToken]; ok {
continue
}
log := log.WithField("push_token", pushTokenRecord.PushToken)
// Try push
var err error
switch pushTokenRecord.TokenType {
case push_data.TokenTypeFcmApns:
err = pusher.SendMutableAPNSPush(
ctx,
pushTokenRecord.PushToken,
titleKey,
string(notificationType),
titleKey, // All mutable pushes have a thread ID that's the title
kvs,
)
case push_data.TokenTypeFcmAndroid:
// todo: anything special required for Android?
err = pusher.SendDataPush(
ctx,
pushTokenRecord.PushToken,
kvs,
)
}
if err != nil {
log.WithError(err).Warn("failure sending push notification")
onPushError(ctx, data, pusher, pushTokenRecord)
}
seenPushTokens[pushTokenRecord.PushToken] = struct{}{}
}
return nil
}