forked from berty/berty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore_message.go
171 lines (135 loc) · 4.15 KB
/
store_message.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package bertyprotocol
import (
"context"
"fmt"
"encoding/base64"
"berty.tech/berty/v2/go/pkg/bertytypes"
"berty.tech/berty/v2/go/pkg/errcode"
ipfslog "berty.tech/go-ipfs-log"
"berty.tech/go-ipfs-log/identityprovider"
"berty.tech/go-orbit-db/address"
"berty.tech/go-orbit-db/iface"
"berty.tech/go-orbit-db/stores"
"berty.tech/go-orbit-db/stores/basestore"
"berty.tech/go-orbit-db/stores/operation"
coreapi "github.com/ipfs/interface-go-ipfs-core"
"github.com/libp2p/go-libp2p-core/crypto"
"go.uber.org/zap"
)
const groupMessageStoreType = "berty_group_messages"
type messageStore struct {
basestore.BaseStore
devKS DeviceKeystore
mks *MessageKeystore
g *bertytypes.Group
logger *zap.Logger
}
func (m *messageStore) setLogger(l *zap.Logger) {
if l == nil {
return
}
m.logger = l.With(zap.String("group-id", fmt.Sprintf("%.6s", base64.StdEncoding.EncodeToString(m.g.PublicKey))))
}
func (m *messageStore) openMessage(ctx context.Context, e ipfslog.Entry) (*bertytypes.GroupMessageEvent, error) {
if e == nil {
return nil, errcode.ErrInvalidInput
}
op, err := operation.ParseOperation(e)
if err != nil {
m.logger.Error("unable to parse operation", zap.Error(err))
return nil, err
}
ownPK := crypto.PubKey(nil)
md, inErr := m.devKS.MemberDeviceForGroup(m.g)
if inErr == nil {
ownPK = md.device.GetPublic()
}
headers, payload, err := m.mks.OpenEnvelope(ctx, m.g, ownPK, op.GetValue(), e.GetHash())
if err != nil {
m.logger.Error("unable to open envelope", zap.Error(err))
return nil, err
}
eventContext := newEventContext(e.GetHash(), e.GetNext(), m.g)
return &bertytypes.GroupMessageEvent{
EventContext: eventContext,
Headers: headers,
Message: payload,
}, err
}
func (m *messageStore) ListMessages(ctx context.Context) (<-chan *bertytypes.GroupMessageEvent, error) {
out := make(chan *bertytypes.GroupMessageEvent)
// FIXME: use iterator instead to reduce resource usage (require go-ipfs-log improvements)
go func() {
for _, e := range m.OpLog().GetEntries().Slice() {
evt, err := m.openMessage(ctx, e)
if err != nil {
m.logger.Error("unable to open message", zap.Error(err))
continue
}
out <- evt
}
close(out)
}()
return out, nil
}
func (m *messageStore) AddMessage(ctx context.Context, payload []byte) (operation.Operation, error) {
md, err := m.devKS.MemberDeviceForGroup(m.g)
if err != nil {
return nil, errcode.ErrInternal.Wrap(err)
}
env, err := m.mks.SealEnvelope(ctx, m.g, md.device, payload)
if err != nil {
return nil, errcode.ErrCryptoEncrypt.Wrap(err)
}
op := operation.NewOperation(nil, "ADD", env)
e, err := m.AddOperation(ctx, op, nil)
if err != nil {
return nil, errcode.ErrOrbitDBAppend.Wrap(err)
}
op, err = operation.ParseOperation(e)
if err != nil {
return nil, errcode.ErrOrbitDBDeserialization.Wrap(err)
}
return op, nil
}
func constructorFactoryGroupMessage(s *bertyOrbitDB) iface.StoreConstructor {
return func(ctx context.Context, ipfs coreapi.CoreAPI, identity *identityprovider.Identity, addr address.Address, options *iface.NewStoreOptions) (iface.Store, error) {
g, err := s.getGroupFromOptions(options)
if err != nil {
return nil, errcode.ErrInvalidInput.Wrap(err)
}
store := &messageStore{
devKS: s.deviceKeystore,
mks: s.messageKeystore,
g: g,
logger: zap.NewNop(),
}
options.Index = basestore.NewBaseIndex
if err := store.InitBaseStore(ctx, ipfs, identity, addr, options); err != nil {
return nil, errcode.ErrOrbitDBInit.Wrap(err)
}
go func() {
for e := range store.Subscribe(ctx) {
entry := ipfslog.Entry(nil)
switch evt := e.(type) {
case *stores.EventWrite:
entry = evt.Entry
case *stores.EventReplicateProgress:
entry = evt.Entry
}
if entry == nil {
continue
}
store.logger.Debug("received store event", zap.Any("raw event", e))
messageEvent, err := store.openMessage(ctx, entry)
if err != nil {
store.logger.Error("unable to open message", zap.Error(err))
continue
}
store.logger.Debug("received payload", zap.String("payload", string(messageEvent.Message)))
store.Emit(ctx, messageEvent)
}
}()
return store, nil
}
}