/
push.go
87 lines (71 loc) · 2.05 KB
/
push.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
package bertybridge
import (
"context"
"fmt"
"runtime"
"time"
push "berty.tech/berty/v2/go/framework/bertypush"
"berty.tech/berty/v2/go/internal/accountutils"
"berty.tech/berty/v2/go/pkg/accounttypes"
"berty.tech/berty/v2/go/pkg/pushtypes"
)
const (
ServicePushPayloadKey = pushtypes.ServicePushPayloadKey
StorageKeyName = accountutils.StorageKeyName
)
type (
DecryptedPush pushtypes.DecryptedPush
FormatedPush pushtypes.FormatedPush
)
type PushConfig struct {
p *push.Config
}
func NewPushConfig() *PushConfig {
return &PushConfig{
p: &push.Config{},
}
}
func (c *PushConfig) SetPreferredLanguages(lang string) { c.p.SetPreferredLanguages(lang) }
func (c *PushConfig) SetDriverLogger(logger NativeLoggerDriver) {
l := newLogger(logger)
c.p.SetLogger(l)
}
type PushStandalone struct {
p *push.PushStandalone
}
func NewPushStandalone(c *PushConfig) *PushStandalone {
s := push.NewPushStandalone(c.p)
return &PushStandalone{s}
}
func (s *PushStandalone) Decrypt(rootDir string, inputB64 string, storageKey []byte) (*FormatedPush, error) {
f, err := s.p.Decrypt(rootDir, inputB64, storageKey)
if err != nil {
return nil, err
}
return (*FormatedPush)(f), nil
}
func (b *Bridge) PushDecrypt(inputB64 string) (*FormatedPush, error) {
if b == nil || b.serviceAccount == nil {
return nil, fmt.Errorf("unable to call push receive on an empty bridge/serviceAccount instance")
}
var pushType pushtypes.PushServiceTokenType
switch runtime.GOOS {
case "android":
pushType = pushtypes.PushServiceTokenType_PushTokenFirebaseCloudMessaging
case "ios", "darwin":
pushType = pushtypes.PushServiceTokenType_PushTokenApplePushNotificationService
default:
pushType = pushtypes.PushServiceTokenType_PushTokenUndefined
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
res, err := b.serviceAccount.PushReceive(ctx, &accounttypes.PushReceive_Request{
Payload: inputB64,
TokenType: pushType,
})
if err != nil {
return nil, err
}
formatpush := res.GetPush()
return (*FormatedPush)(formatpush), nil
}