-
Notifications
You must be signed in to change notification settings - Fork 383
/
handlers.go
183 lines (153 loc) · 5.12 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
package bertybot
import (
"context"
"fmt"
"strings"
"go.uber.org/zap"
"berty.tech/berty/v2/go/pkg/bertymessenger"
)
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 *bertymessenger.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,
}
// raw messenger events
switch event.Type {
case bertymessenger.StreamEvent_TypeContactUpdated:
context.Contact = payload.(*bertymessenger.StreamEvent_ContactUpdated).Contact
context.ConversationPK = context.Contact.ConversationPublicKey
// specialized events
switch context.Contact.State {
case bertymessenger.Contact_IncomingRequest:
b.callHandlers(context, IncomingContactRequestHandler)
case bertymessenger.Contact_Accepted:
b.callHandlers(context, AcceptedContactHandler)
default:
return fmt.Errorf("unsupported contact state: %q", context.Contact.State)
}
b.callHandlers(context, ContactUpdatedHandler)
case bertymessenger.StreamEvent_TypeInteractionUpdated:
context.Interaction = payload.(*bertymessenger.StreamEvent_InteractionUpdated).Interaction
context.IsMe = context.Interaction.IsMe
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 bertymessenger.AppMessage_TypeUserMessage:
receivedMessage := payload.(*bertymessenger.AppMessage_UserMessage)
context.UserMessage = receivedMessage.GetBody()
if len(b.commands) > 0 && len(context.UserMessage) > 1 && strings.HasPrefix(context.UserMessage, "/") {
if !context.IsMe && !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 bertymessenger.StreamEvent_TypeConversationUpdated:
context.Conversation = payload.(*bertymessenger.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 bertymessenger.StreamEvent_TypeDeviceUpdated:
context.Device = payload.(*bertymessenger.StreamEvent_DeviceUpdated).Device
b.callHandlers(context, DeviceUpdatedHandler)
case bertymessenger.StreamEvent_TypeAccountUpdated:
context.Account = payload.(*bertymessenger.StreamEvent_AccountUpdated).Account
context.IsMe = true
b.callHandlers(context, AccountUpdatedHandler)
case bertymessenger.StreamEvent_TypeMemberUpdated:
context.Member = payload.(*bertymessenger.StreamEvent_MemberUpdated).Member
b.callHandlers(context, MemberUpdatedHandler)
case bertymessenger.StreamEvent_TypeListEnd:
b.callHandlers(context, EndOfReplayHandler)
case bertymessenger.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.skipMyself && context.IsMe {
return
}
if b.skipAcknowledge && !context.IsReplay && context.IsAck {
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)
}