-
Notifications
You must be signed in to change notification settings - Fork 384
/
push.go
82 lines (68 loc) · 2.27 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
package bertypush
import (
"context"
"go.uber.org/zap"
"berty.tech/berty/v2/go/internal/messengerdb"
"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
db *messengerdb.DBWrapper
}
type MessengerPushReceiver interface {
PushReceive(ctx context.Context, input []byte) (*messengertypes.PushReceive_Reply, error)
}
func NewPushReceiver(pushHandler PushHandler, evtHandler EventHandler, db *messengerdb.DBWrapper, logger *zap.Logger) MessengerPushReceiver {
if logger == nil {
logger = zap.NewNop()
}
return &messengerPushReceiver{
logger: logger,
pushHandler: pushHandler,
eventHandler: evtHandler,
db: db,
}
}
func (m *messengerPushReceiver) PushReceive(ctx context.Context, input []byte) (*messengertypes.PushReceive_Reply, error) {
clear, err := m.pushHandler.PushReceive(ctx, 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)
}
if i.Conversation.Type == messengertypes.Conversation_ContactType {
i.Conversation.Contact, err = m.db.GetContactByPK(i.Conversation.ContactPublicKey)
if err != nil {
m.logger.Error("failed to get push notif contact", zap.Error(err))
}
}
accountMuted, conversationMuted, err := m.db.GetMuteStatusForConversation(i.ConversationPublicKey)
if err != nil {
accountMuted = true
conversationMuted = true
}
hidePreview := true
account, err := m.db.GetAccount()
if err == nil && !account.HidePushPreviews {
hidePreview = false
}
return &messengertypes.PushReceive_Reply{
Data: &messengertypes.PushReceivedData{
ProtocolData: clear,
Interaction: i,
AlreadyReceived: clear.AlreadyReceived || !isNew,
ConversationMuted: conversationMuted,
AccountMuted: accountMuted,
HidePreview: hidePreview,
},
}, nil
}