/
event.go
156 lines (133 loc) · 4.31 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
package node
import (
"context"
"encoding/json"
"fmt"
"time"
"berty.tech/core/api/node"
"berty.tech/core/api/p2p"
"berty.tech/core/entity"
"berty.tech/core/pkg/errorcodes"
"berty.tech/core/pkg/tracing"
"github.com/pkg/errors"
"go.uber.org/zap"
)
func (n *Node) AsyncWait(ctx context.Context) {
tracer := tracing.EnterFunc(ctx)
defer tracer.Finish()
// ctx = tracer.Context()
n.asyncWaitGroupInst.Wait()
}
// HandleEvent implements berty.entity.HandleEvent (synchronous unary)
func (n *Node) HandleEvent(ctx context.Context, input *entity.Event) (*node.Void, error) {
tracer := tracing.EnterFunc(ctx, input)
defer tracer.Finish()
ctx = tracer.Context()
defer n.asyncWaitGroup(ctx)()
return &node.Void{}, n.handleEvent(ctx, input)
}
func (n *Node) handleEvent(ctx context.Context, input *entity.Event) error {
tracer := tracing.EnterFunc(ctx, input)
defer tracer.Finish()
ctx = tracer.Context()
defer n.asyncWaitGroup(ctx)()
if input.SourceDeviceID == n.UserID() {
logger().Debug("skipping event created by myself",
zap.String("sender", input.SourceDeviceID),
zap.String("id", input.ID),
)
return nil
}
var count int
sql := n.sql(ctx)
if err := sql.
Model(&entity.Event{}).
Where(&entity.Event{ID: input.ID, SourceDeviceID: input.SourceDeviceID}).
Count(&count).
Error; err != nil {
return err
}
if count > 0 {
err := errors.New("event already handled")
logger().Warn(err.Error())
return err
}
if input.APIVersion != p2p.Version {
return fmt.Errorf("API versions differ, need to develop a migration tool")
}
now := time.Now().UTC()
input.Direction = entity.Event_Incoming // set direction to incoming
input.ReceivedAt = &now // set current date
// input.ReceiverID = "" // we should be able to remove this information
// debug
if ce := logger().Check(zap.DebugLevel, "handle event"); ce != nil {
var event string
if out, err := json.Marshal(input); err == nil {
event = string(out)
}
ce.Write(
zap.String("sender", input.SourceDeviceID),
zap.String("id", input.ID),
zap.String("event", event),
)
}
// FIXME: validate that event.Kind type matches with event.TargetType.
//
// i.e., ConversationNewMessage should have event.TargetType == ToSpecificConversation
// FIXME: validate that event is valid (seenAt etc should be nil)
handler, found := map[entity.Kind]EventHandler{
entity.Kind_ContactRequest: n.handleContactRequest,
entity.Kind_ContactRequestAccepted: n.handleContactRequestAccepted,
entity.Kind_ContactShareMe: n.handleContactShareMe,
entity.Kind_ConversationInvite: n.handleConversationInvite,
entity.Kind_ConversationUpdate: n.handleConversationUpdate,
entity.Kind_ConversationNewMessage: n.handleConversationNewMessage,
entity.Kind_ConversationRead: n.handleConversationRead,
entity.Kind_DevtoolsMapset: n.handleDevtoolsMapset,
entity.Kind_SenderAliasUpdate: n.handleSenderAliasUpdate,
entity.Kind_Ack: n.handleAck,
entity.Kind_Seen: n.handleSeen,
entity.Kind_DevicePushTo: n.handleDevicePushTo,
entity.Kind_DeviceUpdatePushConfig: n.handleDeviceUpdatePushConfig,
}[input.Kind]
var handlingError error
if !found {
handlingError = errorcodes.ErrUnimplemented.New()
} else {
handlingError = handler(ctx, input)
}
// no more action for ack events, stop here.
if input.Kind == entity.Kind_Ack {
return handlingError
}
// emits the event to the client (UI)
if handlingError == nil {
if err := n.EnqueueClientEvent(ctx, input); err != nil {
return err
}
} else {
n.LogBackgroundError(ctx, errors.Wrap(handlingError, "entity.Handle event"))
}
if input.Kind != entity.Kind_DevicePushTo {
if err := sql.Save(input).Error; err != nil {
return errorcodes.ErrDbUpdate.Wrap(err)
}
}
// asynchronously ack, maybe we can ignore this one?
if err := n.EnqueueOutgoingEventWithOptions(ctx,
n.NewEvent(ctx).
SetToContactID(input.SourceDeviceID).
SetAckAttrs(&entity.AckAttrs{IDs: []string{input.ID}}),
&OutgoingEventOptions{
DisableEventLogging: input.Kind == entity.Kind_DevicePushTo,
},
); err != nil {
return err
}
// update local event in db
input.SetAckedAt(now)
if err := sql.Save(input).Error; err != nil {
return errorcodes.ErrDbCreate.Wrap(err)
}
return handlingError
}