-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtext.go
128 lines (111 loc) · 3.65 KB
/
text.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
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"
"github.com/code-payments/code-server/pkg/code/localization"
push_lib "github.com/code-payments/code-server/pkg/push"
)
// sendLocalizedPushNotificationToOwner is a generic utility for sending a localized
// push notification to the devices linked to an owner account. Keys can be provided
// in either the iOS or Android format, and will be translated accordingly.
//
// todo: Duplicated code with other send push utitilies
func sendLocalizedPushNotificationToOwner(
ctx context.Context,
data code_data.Provider,
pusher push_lib.Provider,
owner *common.Account,
titleKey, bodyKey string,
bodyArgs ...string,
) error {
log := logrus.StandardLogger().WithFields(logrus.Fields{
"method": "sendLocalizedPushNotificationToOwner",
"owner": owner.PublicKey().ToBase58(),
})
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.SendLocalizedAPNSPush(
ctx,
pushTokenRecord.PushToken,
localization.GetIosLocalizationKey(titleKey),
localization.GetIosLocalizationKey(bodyKey),
bodyArgs...,
)
case push_data.TokenTypeFcmAndroid:
err = pusher.SendLocalizedAndroidPush(
ctx,
pushTokenRecord.PushToken,
localization.GetAndroidLocalizationKey(titleKey),
localization.GetAndroidLocalizationKey(bodyKey),
bodyArgs...,
)
default:
}
if err != nil {
log.WithError(err).Warn("failure sending push notification")
onPushError(ctx, data, pusher, pushTokenRecord)
}
seenPushTokens[pushTokenRecord.PushToken] = struct{}{}
}
return nil
}
// sendBasicPushNotificationToOwner is a generic utility for sending push notification
// to the devices linked to an owner account. This should be used early in features that
// don't have localization, since titles & body are the direct English text.
//
// todo: Duplicated code with other send push utitilies
func sendBasicPushNotificationToOwner(
ctx context.Context,
data code_data.Provider,
pusher push_lib.Provider,
owner *common.Account,
title, body string,
) error {
log := logrus.StandardLogger().WithFields(logrus.Fields{
"method": "sendPushNotificationToOwner",
"owner": owner.PublicKey().ToBase58(),
})
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.SendPush(
ctx,
pushTokenRecord.PushToken,
title,
body,
)
if err != nil {
log.WithError(err).Warn("failure sending push notification")
onPushError(ctx, data, pusher, pushTokenRecord)
}
seenPushTokens[pushTokenRecord.PushToken] = struct{}{}
}
return nil
}