-
Notifications
You must be signed in to change notification settings - Fork 0
/
gateway_events.go
331 lines (298 loc) · 11.8 KB
/
gateway_events.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
330
331
package main
import (
"log"
"strconv"
"strings"
"time"
"github.com/1egoman/slick/frontend" // The thing to draw to the screen
"github.com/1egoman/slick/gateway" // The thing to interface with slack
)
// Once conencted, listen for events from the active gateway. When an event comes in, act on it.
func gatewayEvents(state *State, term *frontend.TerminalDisplay) {
cachedUsers := make(map[string]*gateway.User)
// Keep track of how many connections are disconnected. If all connections are disconnected,
// then exit the goroutine.
totalDisconnected := 0
for totalDisconnected < len(state.Connections) {
for _, conn := range state.Connections {
if conn == nil {
// Sleep to lower the speed of the loop for debugging reasons.
time.Sleep(100 * time.Millisecond)
continue
}
if conn.Status() == gateway.DISCONNECTED {
totalDisconnected += 1
}
// Before events can run, confirm that the a channel is selected.
hasFetchedChannel := conn.SelectedChannel() != nil
// Is the channel empty? If so, move to the next ieration.
// We want the loop to always be running, so that if the reference that
// conn points to behind the scenes changes, the we won't be blocking
// listening for events on an old reference.
if len(conn.Incoming()) == 0 || !hasFetchedChannel {
// Sleep to lower the speed of the loop for debugging reasons.
time.Sleep(100 * time.Millisecond)
continue
}
// Now that we know there are events, grab one and handle it.
event := <-conn.Incoming()
log.Printf("Received event: %+v", event)
switch event.Type {
case "hello":
// Send an outgoing message
conn.Outgoing() <- gateway.Event{
Type: "ping",
Data: map[string]interface{}{
"type": "slick",
"when": int(time.Now().Unix()),
},
}
case "desktop_notification":
if title, ok := event.Data["title"].(string); ok {
if content, ok := event.Data["content"].(string); ok {
Notification(title, content)
}
}
// When a message is received for the selected channel, add to the message history
// "message" events come in when the gateway receives a message sent by someone else.
case "message":
if channel := conn.SelectedChannel(); event.Data["channel"] == channel.Id {
if event.Data["subtype"] == "message_deleted" {
// If a message was deleted, then delete the message from the message history
for index, msg := range conn.MessageHistory() {
if msg.Hash == event.Data["deleted_ts"] {
conn.DeleteMessageHistory(index)
}
}
} else {
// JUST A NORMAL MESSAGE!
// Find a hash for the message, just use the timestamp
// In message events, the timestamp is `ts`
// In pong events, the timestamp is `event_ts`
var messageHash string
if data, ok := event.Data["ts"].(string); ok {
messageHash = data
} else {
log.Fatal("No ts key in message so can't create a hash for this message!")
}
// See if the message is already in the history
alreadyInHistory := false
lastUnconfirmedMessageIndex := -1
for index, msg := range conn.MessageHistory() {
if msg.Hash == messageHash {
// Message with that hash is already in the history, no need to add
// again...
alreadyInHistory = true
break
} else if userId, ok := event.Data["user"].(string); ok && msg.Confirmed == false && msg.Sender != nil && msg.Sender.Id == userId {
lastUnconfirmedMessageIndex = index
}
}
if alreadyInHistory {
break
}
message, err := conn.ParseMessage(event.Data, cachedUsers)
if err == nil {
// Emit event to to be handled by lua scripts
EmitEvent(state, EVENT_MESSAGE_RECEIVED, map[string]string{
"text": message.Text,
"sender": message.Sender.Name,
"confirmed": strconv.FormatBool(message.Confirmed),
})
// Get the name of the channel the message was sent through
var messageChannel *gateway.Channel
if channelId, ok := event.Data["channel"].(string); ok {
for _, channel := range conn.Channels() {
if channel.Id == channelId {
messageChannel = &channel
break
}
}
}
// Send a notification, if applicable.
if self := conn.Self(); (message.Sender != nil && message.Sender.Id != self.Id) &&
ShouldMessageNotifyUser(message.Text, messageChannel, self) {
text := strings.Replace(message.Text, "<", "", -1)
text = strings.Replace(text, ">", "", -1)
Notification(messageChannel.Name, text)
}
// If an unconfirmed message was found that is thought to be the same
// message, then copy over this message into that spot and be done with
// it.
if lastUnconfirmedMessageIndex >= 0 {
history := conn.MessageHistory()
log.Printf("Message received is a confirmed version of message %+v. Replacing with confirmed version...", history[lastUnconfirmedMessageIndex])
history[lastUnconfirmedMessageIndex] = *message
conn.SetMessageHistory(history)
break
}
// Add message to history, if message was posted to the active channel.
if selectedChannel := conn.SelectedChannel(); selectedChannel != nil &&
messageChannel.Id == selectedChannel.Id {
conn.AppendMessageHistory(*message)
}
// If the user that sent the message was typing, they aren't anymore.
conn.TypingUsers().Remove(message.Sender.Name)
} else {
log.Fatalf(err.Error())
}
}
} else {
log.Println("Channel value", channel)
}
// case "reaction_added":
// // If a message was deleted, then delete the message from the message history
// for index, msg := range conn.MessageHistory() {
// if msg.Hash == event.Data["event_ts"] {
// for _, reaction := range msg.Reactions {
// event.Data["reaction"] // == "smile"
// event.Data["item_user"] // == "U0M9S59T2"
// }
// }
// }
// When a user starts typing, then display that they are typing.
case "user_typing":
if conn != nil {
if channel := conn.SelectedChannel(); event.Data["channel"] == channel.Id {
if userId, ok := event.Data["user"].(string); ok {
user, err := conn.UserById(userId)
if err != nil {
log.Println(err.Error())
} else if user != nil {
// Add user to the list of users typing.
conn.TypingUsers().Add(user.Name, time.Now())
} else {
log.Println("User in `user_typing` event was nil, ignoring...")
}
} else {
log.Println("User id in `user_typing` raw event was not coersable to string, ignoring...")
}
}
}
// When the user's presence value changes, update the active connection
// {"type":"presence_change","presence":"away","user":"U5FR33U4T"}
case "presence_change":
if conn != nil {
// Get user presence status
status := false
if value, ok := event.Data["presence"].(string); ok && value == "active" {
status = true
}
// Get user instance
if userId, ok := event.Data["user"].(string); ok {
user, err := conn.UserById(userId)
if err != nil {
log.Println(err.Error())
} else {
conn.SetUserOnline(user, status)
}
}
}
// When a reaction is added to a message, update our local copy.
// {"type":"reaction_added","user":"U5F7KC0CQ","item":{"type":"message","channel":"C5FAJ078R","ts":"1495901274.063169"},"reaction":"grinning","item_user":"U5F7KC0CQ","event_ts":"1495912992.765337","ts":"1495912992.765337"}
case "reaction_added":
// First, fetch a reference to the user that is in the message.
if userId, ok := event.Data["user"].(string); ok {
user, err := conn.UserById(userId)
if err != nil {
log.Println(err.Error())
} else {
log.Println("No error!")
// Next, fetch the message hash and emoji that was reacted with.
if item, ok := event.Data["item"].(map[string]interface{}); ok {
hash := item["ts"]
if emoji, ok := event.Data["reaction"].(string); ok {
// Loop through all messages to find the one that this event
// references.
messages := conn.MessageHistory()
for messageIndex, message := range messages {
if message.Hash == hash {
// Loop through each reaction on the message. If someone
// else already reacted with the emoji we reacted with, then
// add our username to that reaction.
foundReaction := false
for reactionIndex, reaction := range message.Reactions {
if reaction.Name == emoji {
log.Printf("Reaction %s found for %s, so adding user %+v...", emoji, hash, user)
messages[messageIndex].Reactions[reactionIndex].Users = append(
reaction.Users,
user,
)
foundReaction = true
break
}
}
// Otherwise, create a new reaction.
if !foundReaction {
log.Printf("No reaction %s found for %s, so adding...", emoji, hash)
messages[messageIndex].Reactions = append(
message.Reactions,
gateway.Reaction{
Name: emoji,
Users: []*gateway.User{user},
},
)
}
}
}
conn.SetMessageHistory(messages)
}
}
}
}
// When a reactino is removed from a message, update our local copy.
// {"type":"reaction_removed","user":"U5F7KC0CQ","item":{"type":"message","channel":"C5FAJ078R","ts":"1495901274.063169"},"reaction":"slightly_smiling_face","item_user":"U5F7KC0CQ","event_ts":"1495927732.484253","ts":"1495927732.484253"}
case "reaction_removed":
// First, fetch a reference to the user that is in the message.
if userId, ok := event.Data["user"].(string); ok {
user, err := conn.UserById(userId)
if err != nil {
log.Printf(err.Error())
} else {
// Next, fetch the message hash and emoji that was reacted with.
if item, ok := event.Data["item"].(map[string]interface{}); ok {
hash := item["ts"]
if emoji, ok := event.Data["reaction"].(string); ok {
// Loop through all messages to find the one that this event
// references.
messages := conn.MessageHistory()
for messageIndex, message := range messages {
if message.Hash == hash {
// Loop through each reaction on the message. If someone
// else already reacted with the emoji we reacted with, then
// add our username to that reaction.
for reactionIndex, reaction := range message.Reactions {
if reaction.Name == emoji {
log.Printf("Reaction %s found for %s, so removing user %+v...", emoji, hash, user)
// Delete every instance of the user in the reaction
for userIndex, u := range reaction.Users {
if u.Id == user.Id {
messages[messageIndex].Reactions[reactionIndex].Users = append(reaction.Users[:userIndex], reaction.Users[userIndex+1:]...)
}
}
// If all users have been removed from a reaction,
// then remove the reaction.
if len(messages[messageIndex].Reactions[reactionIndex].Users) == 0 {
log.Printf("Reaction %+v empty, so removing...", messages[messageIndex].Reactions[reactionIndex])
messages[messageIndex].Reactions = append(
message.Reactions[:reactionIndex],
message.Reactions[reactionIndex+1:]...,
)
}
break
}
}
}
}
conn.SetMessageHistory(messages)
}
}
}
}
case "":
log.Printf("Unknown event received: %+v", event)
}
render(state, term)
}
}
}