-
Notifications
You must be signed in to change notification settings - Fork 388
/
push.go
57 lines (46 loc) · 1.48 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
package bertypush
import (
"context"
"go.uber.org/zap"
"berty.tech/berty/v2/go/pkg/errcode"
"berty.tech/berty/v2/go/pkg/messengertypes"
"berty.tech/berty/v2/go/pkg/protocoltypes"
)
type EventHandler interface {
HandleOutOfStoreAppMessage(groupPK []byte, message *protocoltypes.OutOfStoreMessage, payload []byte) (*messengertypes.Interaction, bool, error)
}
type messengerPushReceiver struct {
logger *zap.Logger
pushHandler PushHandler
eventHandler EventHandler
}
type MessengerPushReceiver interface {
PushReceive(ctx context.Context, input []byte) (*messengertypes.PushReceive_Reply, error)
}
func NewPushReceiver(pushHandler PushHandler, evtHandler EventHandler, logger *zap.Logger) MessengerPushReceiver {
if logger == nil {
logger = zap.NewNop()
}
return &messengerPushReceiver{
logger: logger,
pushHandler: pushHandler,
eventHandler: evtHandler,
}
}
func (m *messengerPushReceiver) PushReceive(ctx context.Context, input []byte) (*messengertypes.PushReceive_Reply, error) {
clear, err := m.pushHandler.PushReceive(input)
if err != nil {
return nil, errcode.ErrInternal.Wrap(err)
}
i, isNew, err := m.eventHandler.HandleOutOfStoreAppMessage(clear.GroupPublicKey, clear.Message, clear.Cleartext)
if err != nil {
return nil, errcode.ErrInternal.Wrap(err)
}
return &messengertypes.PushReceive_Reply{
Data: &messengertypes.PushReceivedData{
ProtocolData: clear,
Interaction: i,
AlreadyReceived: clear.AlreadyReceived || !isNew,
},
}, nil
}