-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtext.go
60 lines (50 loc) · 1.52 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
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_lib "github.com/code-payments/code-server/pkg/push"
)
// sendBasicPushNotificationToOwner is a generic utility for sending push notification
// to the devices linked to an owner account.
//
// 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
}