-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbadge_count.go
101 lines (85 loc) · 2.91 KB
/
badge_count.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
package push
import (
"context"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/code-payments/code-server/pkg/code/common"
code_data "github.com/code-payments/code-server/pkg/code/data"
"github.com/code-payments/code-server/pkg/code/data/badgecount"
"github.com/code-payments/code-server/pkg/code/data/login"
push_data "github.com/code-payments/code-server/pkg/code/data/push"
push_lib "github.com/code-payments/code-server/pkg/push"
)
// UpdateBadgeCount updates the badge count for an owner account to the latest value
//
// todo: Duplicated code with other send push utitilies
func UpdateBadgeCount(
ctx context.Context,
data code_data.Provider,
pusher push_lib.Provider,
owner *common.Account,
) error {
log := logrus.StandardLogger().WithFields(logrus.Fields{
"method": "SetBadgeCount",
"owner": owner.PublicKey().ToBase58(),
})
// todo: Propagate this logic to other push sending utilities once login
// detection is made public.
loginRecord, err := data.GetLatestLoginByOwner(ctx, owner.PublicKey().ToBase58())
if err == login.ErrLoginNotFound {
return nil
} else if err != nil {
log.WithError(err).Warn("failure getting login record")
return err
}
var badgeCount int
badgeCountRecord, err := data.GetBadgeCount(ctx, owner.PublicKey().ToBase58())
if err == nil {
badgeCount = int(badgeCountRecord.BadgeCount)
} else if err != badgecount.ErrBadgeCountNotFound {
log.WithError(err).Warn("failure getting badge count record")
return err
}
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
}
switch pushTokenRecord.TokenType {
case push_data.TokenTypeFcmApns:
log := log.WithField("push_token", pushTokenRecord.PushToken)
// Legacy push tokens that don't map to an app install are skipped
if pushTokenRecord.AppInstallId == nil {
continue
}
// Only update the device with the latest app login for the owner
if *pushTokenRecord.AppInstallId != loginRecord.AppInstallId {
continue
}
log.Debugf("updating badge count on device to %d", badgeCount)
// Try push to update badge count
pushErr := pusher.SetAPNSBadgeCount(
ctx,
pushTokenRecord.PushToken,
badgeCount,
)
if pushErr != nil {
log.WithError(err).Warn("failure sending push notification")
isValid, err := onPushError(ctx, data, pusher, pushTokenRecord)
if isValid {
return errors.Wrap(pushErr, "error pushing to valid token")
} else if err != nil {
return errors.Wrap(err, "error handling push error")
}
}
}
seenPushTokens[pushTokenRecord.PushToken] = struct{}{}
}
return nil
}