-
Notifications
You must be signed in to change notification settings - Fork 387
/
handlers.go
184 lines (154 loc) · 5.15 KB
/
handlers.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
package bertybot
import (
"context"
"fmt"
"strings"
"go.uber.org/zap"
"berty.tech/berty/v2/go/pkg/messengertypes"
)
type HandlerType uint
const (
UnknownHandler HandlerType = iota
// general events
ErrorHandler
PreAnythingHandler
PostAnythingHandler
EndOfReplayHandler
// raw messenger events
ContactUpdatedHandler
InteractionUpdatedHandler
ConversationUpdatedHandler
AccountUpdatedHandler
MemberUpdatedHandler
DeviceUpdatedHandler
NotificationHandler
// specialized events
IncomingContactRequestHandler
AcceptedContactHandler
UserMessageHandler
NewConversationHandler
CommandHandler
CommandNotFoundHandler
)
type Handler func(ctx Context)
func (b *Bot) handleEvent(ctx context.Context, event *messengertypes.StreamEvent) error {
payload, err := event.UnmarshalPayload()
if err != nil {
return fmt.Errorf("unmarshal event payload failed: %w", err)
}
context := &Context{
Context: ctx,
EventPayload: payload,
EventType: event.Type,
IsReplay: b.isReplaying,
Client: b.client,
Logger: b.logger,
IsNew: event.IsNew,
}
// raw messenger events
switch event.Type {
case messengertypes.StreamEvent_TypeContactUpdated:
context.Contact = payload.(*messengertypes.StreamEvent_ContactUpdated).Contact
context.ConversationPK = context.Contact.ConversationPublicKey
// specialized events
switch context.Contact.State {
case messengertypes.Contact_IncomingRequest:
b.callHandlers(context, IncomingContactRequestHandler)
case messengertypes.Contact_Accepted:
b.callHandlers(context, AcceptedContactHandler)
default:
return fmt.Errorf("unsupported contact state: %q", context.Contact.State)
}
b.callHandlers(context, ContactUpdatedHandler)
case messengertypes.StreamEvent_TypeInteractionUpdated:
context.Interaction = payload.(*messengertypes.StreamEvent_InteractionUpdated).Interaction
context.IsMine = context.Interaction.IsMine
context.ConversationPK = context.Interaction.ConversationPublicKey
context.IsAck = context.Interaction.Acknowledged
payload, err := context.Interaction.UnmarshalPayload()
if err != nil {
return fmt.Errorf("unmarshal interaction payload failed: %w", err)
}
// specialized events
switch context.Interaction.Type {
case messengertypes.AppMessage_TypeUserMessage:
receivedMessage := payload.(*messengertypes.AppMessage_UserMessage)
context.UserMessage = receivedMessage.GetBody()
if len(b.commands) > 0 && len(context.UserMessage) > 1 && strings.HasPrefix(context.UserMessage, "/") {
if !context.IsMine && !context.IsReplay && !context.IsAck {
context.CommandArgs = strings.Split(context.UserMessage[1:], " ")
command, found := b.commands[context.CommandArgs[0]]
if found {
b.logger.Debug("found handler", zap.Strings("args", context.CommandArgs))
command.handler(*context)
b.callHandlers(context, CommandHandler)
} else {
b.callHandlers(context, CommandNotFoundHandler)
}
}
}
b.callHandlers(context, UserMessageHandler)
default:
return fmt.Errorf("unsupported interaction type: %q", context.Interaction.Type)
}
b.callHandlers(context, InteractionUpdatedHandler)
case messengertypes.StreamEvent_TypeConversationUpdated:
context.Conversation = payload.(*messengertypes.StreamEvent_ConversationUpdated).Conversation
context.ConversationPK = context.Conversation.PublicKey
b.store.mutex.Lock()
_, found := b.store.conversations[context.ConversationPK]
if !found {
b.store.conversations[context.ConversationPK] = context.Conversation
}
b.store.mutex.Unlock()
if !found {
b.callHandlers(context, NewConversationHandler)
}
b.callHandlers(context, ConversationUpdatedHandler)
case messengertypes.StreamEvent_TypeDeviceUpdated:
context.Device = payload.(*messengertypes.StreamEvent_DeviceUpdated).Device
b.callHandlers(context, DeviceUpdatedHandler)
case messengertypes.StreamEvent_TypeAccountUpdated:
context.Account = payload.(*messengertypes.StreamEvent_AccountUpdated).Account
context.IsMine = true
b.callHandlers(context, AccountUpdatedHandler)
case messengertypes.StreamEvent_TypeMemberUpdated:
context.Member = payload.(*messengertypes.StreamEvent_MemberUpdated).Member
b.callHandlers(context, MemberUpdatedHandler)
case messengertypes.StreamEvent_TypeListEnded:
b.callHandlers(context, EndOfReplayHandler)
case messengertypes.StreamEvent_TypeNotified:
b.callHandlers(context, NotificationHandler)
default:
return fmt.Errorf("unsupported event type: %q", event.Type)
}
b.callHandlers(context, PostAnythingHandler)
return nil
}
func (b *Bot) callHandlers(context *Context, typ HandlerType) {
if !b.withFromMyself && context.IsMine {
return
}
if !b.withEntityUpdates && !context.IsNew {
return
}
if !context.initialized {
context.initialized = true
b.callHandlers(context, PreAnythingHandler)
}
handlers, found := b.handlers[typ]
if !found {
return
}
copy := *context
for _, handler := range handlers {
copy.HandlerType = typ
handler(copy)
}
}
func (b *Bot) addHandler(typ HandlerType, handler Handler) {
if _, found := b.handlers[typ]; !found {
b.handlers[typ] = make([]Handler, 0)
}
b.handlers[typ] = append(b.handlers[typ], handler)
}