-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutil.go
41 lines (34 loc) · 1.5 KB
/
util.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
package push
import (
"context"
"github.com/pkg/errors"
"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"
)
func getPushTokensForOwner(ctx context.Context, data code_data.Provider, owner *common.Account) ([]*push_data.Record, error) {
verificationRecord, err := data.GetLatestPhoneVerificationForAccount(ctx, owner.PublicKey().ToBase58())
if err != nil {
return nil, errors.Wrap(err, "error getting latest phone verification record")
}
dataContainerRecord, err := data.GetUserDataContainerByPhone(ctx, owner.PublicKey().ToBase58(), verificationRecord.PhoneNumber)
if err != nil {
return nil, errors.Wrap(err, "error getting data container record")
}
pushTokenRecords, err := data.GetAllValidPushTokensdByDataContainer(ctx, dataContainerRecord.ID)
if err == push_data.ErrTokenNotFound {
return nil, nil
} else if err != nil {
return nil, errors.Wrap(err, "error getting push token records")
}
return pushTokenRecords, nil
}
func onPushError(ctx context.Context, data code_data.Provider, pusher push_lib.Provider, pushTokenRecord *push_data.Record) (bool, error) {
// On failure, verify token validity, and cleanup if necessary
isValid, err := pusher.IsValidPushToken(ctx, pushTokenRecord.PushToken)
if err == nil && !isValid {
data.DeletePushToken(ctx, pushTokenRecord.PushToken)
}
return isValid, err
}