/
event.go
329 lines (268 loc) · 7.77 KB
/
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package entity
import (
"encoding/json"
"fmt"
"strings"
"time"
"github.com/jinzhu/gorm"
opentracing "github.com/opentracing/opentracing-go"
"go.uber.org/zap"
context "golang.org/x/net/context"
"berty.tech/core/pkg/errorcodes"
"berty.tech/core/pkg/tracing"
"berty.tech/core/push"
)
func NewEvent() *Event {
return &Event{
Dispatches: make([]*EventDispatch, 0),
}
}
func (e Event) IsJustReceived() bool {
return e.Direction == Event_Incoming && (e.AckedAt == nil || e.AckedAt.IsZero())
}
func (e Event) Copy() *Event {
return &Event{
ID: e.ID,
SourceDeviceID: e.SourceDeviceID,
CreatedAt: e.CreatedAt,
UpdatedAt: e.UpdatedAt,
SentAt: e.SentAt,
ReceivedAt: e.ReceivedAt,
AckedAt: e.AckedAt,
Direction: e.Direction,
APIVersion: e.APIVersion,
Kind: e.Kind,
Attributes: e.Attributes,
SeenAt: e.SeenAt,
AckStatus: e.AckStatus,
Dispatches: e.Dispatches,
SourceContactID: e.SourceContactID,
TargetType: e.TargetType,
TargetAddr: e.TargetAddr,
Metadata: e.Metadata,
}
}
func (e Event) Author() string {
return strings.Split(e.ID, ":")[0]
}
func (e Event) ToJSON() string {
// FIXME: use jsonpb
out, _ := json.Marshal(e)
return string(out)
}
func (e Event) CreateSpan(ctx context.Context) (opentracing.Span, context.Context) {
tracer := opentracing.GlobalTracer()
caller := tracing.GetCallerName(1)
topic := fmt.Sprintf("event::/%s/%s", e.Direction.String(), e.Kind.String())
// Retrieve span context inside the event if needed
spanctx, err := tracer.Extract(opentracing.TextMap, e.TextMapReader())
var span opentracing.Span
if err != nil {
span = opentracing.StartSpan(topic, opentracing.ChildOf(spanctx))
} else {
span = opentracing.StartSpan(topic)
}
if e.ID != "" {
span.SetTag("event.id", e.ID)
}
span.SetTag("caller", caller)
span.SetTag("event.kind", e.Kind.String())
span.SetTag("event.SourceDeviceID", e.SourceDeviceID)
span.SetTag("event.TargetType", e.TargetType)
span.SetTag("event.TargetAddr", e.TargetAddr)
span.SetTag("event.Direction", e.Direction.String())
span.SetTag("len(event.Dispatches)", len(e.Dispatches))
if err := tracer.Inject(span.Context(), opentracing.TextMap, e.TextMapWriter()); err != nil {
logger().Error("failed to inject span context", zap.Error(err))
}
return span, opentracing.ContextWithSpan(ctx, span)
}
func (e Event) IsNode() {} // required by gqlgen
func (e Event) TextMapWriter() opentracing.TextMapWriter {
return (EventTextMap)(e)
}
func (e Event) TextMapReader() opentracing.TextMapReader {
return (EventTextMap)(e)
}
var _ opentracing.TextMapWriter = (*EventTextMap)(nil)
var _ opentracing.TextMapReader = (*EventTextMap)(nil)
type EventTextMap Event
func (tm EventTextMap) Set(key, val string) {
for _, data := range tm.Metadata {
if data.Key == key {
data.Values = []string{val}
return
}
}
tm.Metadata = append(tm.Metadata, &MetadataKeyValue{
Key: key,
Values: []string{val},
})
}
func (tm EventTextMap) ForeachKey(handler func(key, val string) error) (err error) {
for _, data := range tm.Metadata {
if len(data.Values) > 0 {
err = handler(data.Key, data.Values[0])
} else {
err = handler(data.Key, "")
}
if err != nil {
return
}
}
return
}
// FindNonAcknowledgedEventDestinations finds non acknowledged event destinations emitted before the supplied time value
func FindNonAcknowledgedEventDestinations(db *gorm.DB, before time.Time) ([]*Event, error) {
var events []*Event
err := db.
Table("event").
Select("target_type, target_addr").
Where("sent_at < :time", before).
Where("acked_at IS NULL").
Where(&Event{
Direction: Event_Outgoing,
}).
Group("target_type, target_addr").
Scan(&events).
Error
if err != nil {
return nil, errorcodes.ErrDb.Wrap(err)
}
return events, nil
}
// FindContactsWithNonAcknowledgedEvents finds non acknowledged event destinations as deviceIDs emitted before the supplied time value
func FindDevicesWithNonAcknowledgedEvents(db *gorm.DB, before time.Time) ([]string, error) {
var deviceIDs []string
err := db.
Model(&EventDispatch{}).
Joins("JOIN event ON event_dispatch.event_id = event.id").
Where("event.direction = ?", Event_Outgoing).
Where("event_dispatch.acked_at IS NULL").
Where("event_dispatch.sent_at < ?", before).
Group("event_dispatch.device_id").
Pluck("event_dispatch.device_id", &deviceIDs).
Error
if err != nil {
return nil, errorcodes.ErrDb.Wrap(err)
}
return deviceIDs, nil
}
func FindDispatchesWithNonAcknowledgedEvents(db *gorm.DB, before time.Time) ([]*EventDispatch, error) {
var dispatches []*EventDispatch
err := db.
Model(&EventDispatch{}).
Joins("JOIN event ON event_dispatch.event_id = event.id").
Where("event.direction = ?", Event_Outgoing).
Where("event_dispatch.acked_at IS NULL").
Where("event_dispatch.sent_at > ? OR event_dispatch.sent_at IS NULL", before).
Find(&dispatches).
Error
if err != nil {
return nil, errorcodes.ErrDb.Wrap(err)
}
return dispatches, nil
}
func FindNonAcknowledgedDispatchesForDestination(db *gorm.DB, deviceID string) ([]*EventDispatch, error) {
var dispatches []*EventDispatch
if err := db.Model(&EventDispatch{}).
Joins("JOIN event ON event_dispatch.event_id = event.id").
Where("event_dispatch.acked_at IS NULL").
Where("event.direction = ?", Event_Outgoing).
Where("event_dispatch.device_id = ?", deviceID).
Where("event_dispatch.acked_at IS NULL").
Find(&dispatches).
Error; err != nil {
return nil, err
}
return dispatches, nil
}
// FindNonAcknowledgedEventsForDestination finds non acknowledged events for the supplied destination (conversation/receiver)
func FindNonAcknowledgedEventsForDestination(db *gorm.DB, destination *Event) ([]*Event, error) {
var events []*Event
err := db.Find(&events, &Event{
TargetType: destination.TargetType,
TargetAddr: destination.TargetAddr,
Direction: Event_Outgoing,
}).Where("acked_at IS NULL").Error
if err != nil {
return nil, err
}
return events, nil
}
func (e Event) PushPriority() push.Priority {
switch e.Kind {
case Kind_ConversationInvite,
Kind_ConversationNewMessage,
Kind_ContactRequestAccepted:
return push.Priority_Normal
}
return push.Priority_Low
}
func (e Event) ToConversationID() string {
if e.TargetType == Event_ToSpecificConversation {
return e.TargetAddr
}
return ""
}
func (e Event) ToContactID() string {
// FIXME: query contact based on device
// -> if e.TargetType == Event_ToSpecificDevice {
if e.TargetType == Event_ToSpecificContact {
return e.TargetAddr
}
return ""
}
func (e Event) ToDeviceID() string {
if e.TargetType == Event_ToSpecificDevice {
return e.TargetAddr
}
return ""
}
func (e *Event) SetAckedAt(t time.Time) *Event {
e.AckedAt = &t
return e
}
func (e *Event) SetToContactID(id string) *Event {
e.TargetType = Event_ToSpecificContact
e.TargetAddr = id
return e
}
func (e *Event) SetToContact(conv *Contact) *Event {
return e.SetToContactID(conv.ID)
}
func (e *Event) SetToConversationID(id string) *Event {
e.TargetType = Event_ToSpecificConversation
e.TargetAddr = id
return e
}
func (e *Event) SetToConversation(conv *Conversation) *Event {
return e.SetToConversationID(conv.ID)
}
func (e *Event) SetToAllContacts() *Event {
e.TargetType = Event_ToAllContacts
return e
}
func (e *Event) SetToDeviceID(id string) *Event {
e.TargetType = Event_ToSpecificDevice
e.TargetAddr = id
return e
}
func (e *Event) SetToDevice(conv *Device) *Event {
return e.SetToDeviceID(conv.ID)
}
func (e *Event) SetErr(err error) *Event {
if err != nil {
// FIXME: enrich error -> consider extract the pkg/errorcodes logic somewhere
e.ErrProxy = &Err{
ErrMsg: err.Error(),
}
}
return e
}
func (e *Event) Err() error {
if e.ErrProxy != nil {
return e.ErrProxy
}
return nil
}