forked from berty/weshnet
-
Notifications
You must be signed in to change notification settings - Fork 5
/
api_event.go
209 lines (176 loc) · 5.7 KB
/
api_event.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package weshnet
import (
"context"
"errors"
"fmt"
"github.com/libp2p/go-libp2p/p2p/host/eventbus"
"berty.tech/weshnet/pkg/errcode"
"berty.tech/weshnet/pkg/protocoltypes"
)
func checkParametersConsistency(sinceID, untilID []byte, sinceNow, untilNow, reverseOrder bool) error {
// Since can't be both set to an ID and to now
if sinceID != nil && sinceNow {
return errcode.ErrInvalidInput.Wrap(errors.New("params SinceNow and SinceID are both set"))
}
// Until can't be both set to an ID and to now
if untilID != nil && untilNow {
return errcode.ErrInvalidInput.Wrap(errors.New("params UntilNow and UntilID are both set"))
}
// Since and Until can't be both set to now at the same time
if sinceNow && untilNow {
return errcode.ErrInvalidInput.Wrap(errors.New("params SinceNow and UntilNow are both set"))
}
// Can't reverse events orders if subscribed to new events
if untilID == nil && !untilNow && reverseOrder {
return errcode.ErrInvalidInput.Wrap(errors.New("reverse chronological order requested while subscribing to new events"))
}
return nil
}
// GroupMetadataList replays previous and subscribes to new metadata events from the group
func (s *service) GroupMetadataList(req *protocoltypes.GroupMetadataList_Request, sub protocoltypes.ProtocolService_GroupMetadataListServer) error {
ctx, cancel := context.WithCancel(sub.Context())
defer cancel()
// Get group context / check if the group is opened
cg, err := s.GetContextGroupForID(req.GroupPK)
if err != nil {
return errcode.ErrGroupMemberUnknownGroupID.Wrap(err)
}
// Check parameters consistency
if err := checkParametersConsistency(req.SinceID, req.UntilID, req.SinceNow, req.UntilNow, req.ReverseOrder); err != nil {
return err
}
// Subscribe to new metadata events if requested
var newEvents <-chan interface{}
if req.UntilID == nil && !req.UntilNow {
sub, err := cg.MetadataStore().EventBus().Subscribe([]interface{}{
// new(stores.EventReplicated),
new(protocoltypes.GroupMetadataEvent),
}, eventbus.Name("weshnet/api/group-metadata-list"), eventbus.BufSize(32))
if err != nil {
return fmt.Errorf("unable to subscribe to new events")
}
defer sub.Close()
newEvents = sub.Out()
}
// Subscribe to previous metadata events and stream them if requested
previousEvents := make(chan protocoltypes.GroupMetadataEvent)
if !req.SinceNow {
pevt, err := cg.MetadataStore().ListEvents(ctx, req.SinceID, req.UntilID, req.ReverseOrder)
if err != nil {
return err
}
go func() {
for {
var evt *protocoltypes.GroupMetadataEvent
select {
case <-ctx.Done():
return
case evt = <-pevt:
}
if evt == nil {
// if we don't want to stream new event, cancel the process
if req.UntilNow {
cancel()
} else {
previousEvents <- protocoltypes.GroupMetadataEvent{EventContext: nil}
}
cg.logger.Debug("GroupMetadataList: previous events stream ended")
return
}
previousEvents <- *evt
}
}()
}
// Subscribe to new metadata events and stream them if requested
for {
var event interface{}
select {
case <-ctx.Done():
return nil
case event = <-previousEvents:
case event = <-newEvents:
}
msg := event.(protocoltypes.GroupMetadataEvent)
if msg.EventContext == nil {
continue
}
if err := sub.Send(&msg); err != nil {
return err
}
cg.logger.Info("service - metadata store - sent 1 event from log subscription")
}
}
// GroupMessageList replays previous and subscribes to new message events from the group
func (s *service) GroupMessageList(req *protocoltypes.GroupMessageList_Request, sub protocoltypes.ProtocolService_GroupMessageListServer) error {
ctx, cancel := context.WithCancel(sub.Context())
defer cancel()
// Get group context / check if the group is opened
cg, err := s.GetContextGroupForID(req.GroupPK)
if err != nil {
return errcode.ErrGroupMemberUnknownGroupID.Wrap(err)
}
// Check parameters consistency
if err := checkParametersConsistency(req.SinceID, req.UntilID, req.SinceNow, req.UntilNow, req.ReverseOrder); err != nil {
return err
}
// Subscribe to new message events if requested
var newEvents <-chan interface{}
if req.UntilID == nil && !req.UntilNow {
messageStoreSub, err := cg.MessageStore().EventBus().Subscribe([]interface{}{
new(protocoltypes.GroupMessageEvent),
}, eventbus.Name("weshnet/api/group-message-list"))
if err != nil {
return fmt.Errorf("unable to subscribe to new events")
}
defer messageStoreSub.Close()
newEvents = messageStoreSub.Out()
}
// Subscribe to previous message events and stream them if requested
previousEvents := make(chan protocoltypes.GroupMessageEvent)
if !req.SinceNow {
pevt, err := cg.MessageStore().ListEvents(ctx, req.SinceID, req.UntilID, req.ReverseOrder)
if err != nil {
return err
}
go func() {
for {
var evt *protocoltypes.GroupMessageEvent
select {
case <-ctx.Done():
return
case evt = <-pevt:
}
if evt == nil {
// if we don't want to stream new event, cancel the process
if req.UntilNow {
cancel()
} else {
previousEvents <- protocoltypes.GroupMessageEvent{EventContext: nil}
}
cg.logger.Debug("GroupMessageList: previous events stream ended")
return
}
previousEvents <- *evt
}
}()
}
// Subscribe to new message events and stream them if requested
// listPreviouseMessageDone := false
for {
var event interface{}
select {
case <-ctx.Done():
return nil
case event = <-previousEvents:
case event = <-newEvents:
}
msg := event.(protocoltypes.GroupMessageEvent)
if msg.EventContext == nil {
continue
}
if err := sub.Send(&msg); err != nil {
return err
}
cg.logger.Info("service - message store - sent 1 event from log subscription")
}
}