This repository has been archived by the owner on Jul 7, 2020. It is now read-only.
forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
push.go
796 lines (716 loc) · 24.8 KB
/
push.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
package chat
import (
"bytes"
"encoding/hex"
"errors"
"fmt"
"sync"
"time"
"strings"
"github.com/keybase/client/go/badges"
"github.com/keybase/client/go/chat/globals"
"github.com/keybase/client/go/chat/pager"
"github.com/keybase/client/go/chat/storage"
"github.com/keybase/client/go/chat/types"
"github.com/keybase/client/go/chat/utils"
"github.com/keybase/client/go/gregor"
"github.com/keybase/client/go/protocol/chat1"
"github.com/keybase/client/go/protocol/gregor1"
"github.com/keybase/client/go/protocol/keybase1"
"github.com/keybase/clockwork"
"github.com/keybase/go-codec/codec"
"golang.org/x/net/context"
)
type messageWaiterEntry struct {
vers chat1.InboxVers
cb chan struct{}
}
type gregorMessageOrderer struct {
globals.Contextified
utils.DebugLabeler
sync.Mutex
clock clockwork.Clock
waiters map[string][]messageWaiterEntry
}
func newGregorMessageOrderer(g *globals.Context) *gregorMessageOrderer {
return &gregorMessageOrderer{
Contextified: globals.NewContextified(g),
DebugLabeler: utils.NewDebugLabeler(g.GetLog(), "gregorMessageOrderer", false),
waiters: make(map[string][]messageWaiterEntry),
clock: clockwork.NewRealClock(),
}
}
func (g *gregorMessageOrderer) msgKey(uid gregor1.UID, vers chat1.InboxVers) string {
return fmt.Sprintf("%s:%d", uid, vers)
}
func (g *gregorMessageOrderer) isUIDKey(key string, uid gregor1.UID) bool {
toks := strings.Split(key, ":")
return toks[0] == uid.String()
}
func (g *gregorMessageOrderer) latestInboxVersion(ctx context.Context, uid gregor1.UID) (chat1.InboxVers, error) {
ibox := storage.NewInbox(g.G(), uid)
vers, err := ibox.Version(ctx)
if err != nil {
return 0, err
}
return vers, nil
}
func (g *gregorMessageOrderer) addToWaitersLocked(ctx context.Context, uid gregor1.UID, storedVers,
msgVers chat1.InboxVers) (res []messageWaiterEntry) {
for i := storedVers + 1; i < msgVers; i++ {
entry := messageWaiterEntry{
vers: msgVers,
cb: make(chan struct{}),
}
res = append(res, entry)
key := g.msgKey(uid, i)
g.waiters[key] = append(g.waiters[key], entry)
}
return res
}
func (g *gregorMessageOrderer) waitOnWaiters(ctx context.Context, vers chat1.InboxVers,
waiters []messageWaiterEntry) (res chan struct{}) {
res = make(chan struct{})
go func() {
for _, w := range waiters {
select {
case <-w.cb:
case <-ctx.Done():
g.Debug(ctx, "waitOnWaiters: context cancelled: waiter: %d target: %d", vers, w.vers)
}
}
close(res)
}()
return res
}
func (g *gregorMessageOrderer) cleanupAfterTimeoutLocked(uid gregor1.UID, vers chat1.InboxVers) {
for k, v := range g.waiters {
if g.isUIDKey(k, uid) {
var newv []messageWaiterEntry
for _, w := range v {
if w.vers != vers {
newv = append(newv, w)
}
}
if len(newv) == 0 {
delete(g.waiters, k)
} else {
g.waiters[k] = newv
}
}
}
}
func (g *gregorMessageOrderer) WaitForTurn(ctx context.Context, uid gregor1.UID,
newVers chat1.InboxVers) (res chan struct{}) {
res = make(chan struct{})
// Out of order update, we are going to wait a fixed amount of time for the correctly
// ordered update
deadline := g.clock.Now().Add(time.Second)
go func() {
defer close(res)
g.Lock()
vers, err := g.latestInboxVersion(ctx, uid)
if err != nil {
g.Debug(ctx, "WaitForTurn: failed to get current inbox version: %s", err.Error())
vers = newVers - 1
}
waiters := g.addToWaitersLocked(ctx, uid, vers, newVers)
g.Unlock()
if len(waiters) == 0 {
return
}
g.Debug(ctx, "WaitForTurn: out of order update received, waiting on %d updates: vers: %d newVers: %d", len(waiters), vers, newVers)
ctx, cancel := context.WithCancel(ctx)
defer cancel()
select {
case <-g.waitOnWaiters(ctx, newVers, waiters):
g.Debug(ctx, "WaitForTurn: cleared by earlier messages: vers: %d", newVers)
case <-g.clock.AfterTime(deadline):
g.Debug(ctx, "WaitForTurn: timeout reached, charging forward: vers: %d", newVers)
g.Lock()
g.cleanupAfterTimeoutLocked(uid, newVers)
g.Unlock()
}
}()
return res
}
func (g *gregorMessageOrderer) CompleteTurn(ctx context.Context, uid gregor1.UID, vers chat1.InboxVers) {
g.Lock()
defer g.Unlock()
key := g.msgKey(uid, vers)
waiters := g.waiters[key]
if len(waiters) > 0 {
g.Debug(ctx, "CompleteTurn: clearing %d messages on vers %d", len(waiters), vers)
}
for _, w := range waiters {
close(w.cb)
}
delete(g.waiters, key)
}
func (g *gregorMessageOrderer) SetClock(clock clockwork.Clock) {
g.clock = clock
}
type PushHandler struct {
globals.Contextified
utils.DebugLabeler
sync.Mutex
badger *badges.Badger
identNotifier *IdentifyNotifier
orderer *gregorMessageOrderer
typingMonitor *TypingMonitor
}
func NewPushHandler(g *globals.Context) *PushHandler {
return &PushHandler{
Contextified: globals.NewContextified(g),
DebugLabeler: utils.NewDebugLabeler(g.GetLog(), "PushHandler", false),
identNotifier: NewIdentifyNotifier(g),
orderer: newGregorMessageOrderer(g),
typingMonitor: NewTypingMonitor(g),
}
}
func (g *PushHandler) SetBadger(badger *badges.Badger) {
g.badger = badger
}
func (g *PushHandler) SetClock(clock clockwork.Clock) {
g.orderer.SetClock(clock)
}
func (g *PushHandler) shouldSendNotifications() bool {
return g.G().AppState.State() == keybase1.AppState_FOREGROUND
}
func (g *PushHandler) TlfFinalize(ctx context.Context, m gregor.OutOfBandMessage) (err error) {
defer g.Trace(ctx, func() error { return err }, "TlfFinalize")()
if m.Body() == nil {
return errors.New("gregor handler for chat.tlffinalize: nil message body")
}
var update chat1.TLFFinalizeUpdate
reader := bytes.NewReader(m.Body().Bytes())
dec := codec.NewDecoder(reader, &codec.MsgpackHandle{WriteExt: true})
err = dec.Decode(&update)
if err != nil {
return err
}
uid := gregor1.UID(m.UID().Bytes())
// Order updates based on inbox version of the update from the server
cb := g.orderer.WaitForTurn(ctx, uid, update.InboxVers)
bctx := BackgroundContext(ctx, g.G())
go func(ctx context.Context) {
defer g.Trace(ctx, func() error { return err }, "TlfFinalize(goroutine)")()
<-cb
g.Lock()
defer g.Unlock()
defer g.orderer.CompleteTurn(ctx, uid, update.InboxVers)
// Update inbox
var convs []chat1.ConversationLocal
if convs, err = g.G().InboxSource.TlfFinalize(ctx, m.UID().Bytes(), update.InboxVers,
update.ConvIDs, update.FinalizeInfo); err != nil {
g.Debug(ctx, "tlf finalize: unable to update inbox: %s", err.Error())
}
convMap := make(map[string]chat1.ConversationLocal)
for _, conv := range convs {
convMap[conv.GetConvID().String()] = conv
}
// Send notify for each conversation ID
for _, convID := range update.ConvIDs {
var conv *chat1.ConversationLocal
if mapConv, ok := convMap[convID.String()]; ok {
conv = &mapConv
} else {
conv = nil
}
if conv.GetTopicType() == chat1.TopicType_CHAT {
if g.shouldSendNotifications() {
g.G().NotifyRouter.HandleChatTLFFinalize(ctx, keybase1.UID(uid.String()),
convID, update.FinalizeInfo, g.presentUIItem(conv))
} else {
supdate := []chat1.ConversationStaleUpdate{chat1.ConversationStaleUpdate{
ConvID: convID,
UpdateType: chat1.StaleUpdateType_CLEAR,
}}
g.G().Syncer.SendChatStaleNotifications(ctx, uid, supdate, false)
}
}
}
}(bctx)
return nil
}
func (g *PushHandler) TlfResolve(ctx context.Context, m gregor.OutOfBandMessage) (err error) {
defer g.Trace(ctx, func() error { return err }, "TlfResolve")()
if m.Body() == nil {
return errors.New("gregor handler for chat.tlfresolve: nil message body")
}
var update chat1.TLFResolveUpdate
reader := bytes.NewReader(m.Body().Bytes())
dec := codec.NewDecoder(reader, &codec.MsgpackHandle{WriteExt: true})
err = dec.Decode(&update)
if err != nil {
return err
}
uid := gregor1.UID(m.UID().Bytes())
// Order updates based on inbox version of the update from the server
cb := g.orderer.WaitForTurn(ctx, uid, update.InboxVers)
bctx := BackgroundContext(ctx, g.G())
go func(ctx context.Context) {
defer g.Trace(ctx, func() error { return nil }, "TlfResolve(goroutine)")()
<-cb
g.Lock()
defer g.Unlock()
defer g.orderer.CompleteTurn(ctx, uid, update.InboxVers)
// Get and localize the conversation to get the new tlfname.
inbox, _, err := g.G().InboxSource.Read(ctx, uid, nil, true, &chat1.GetInboxLocalQuery{
ConvIDs: []chat1.ConversationID{update.ConvID},
}, nil)
if err != nil {
g.Debug(ctx, "resolve: unable to read conversation: %s", err.Error())
return
}
if len(inbox.Convs) != 1 {
g.Debug(ctx, "resolve: unable to find conversation")
return
}
updateConv := inbox.Convs[0]
resolveInfo := chat1.ConversationResolveInfo{
NewTLFName: updateConv.Info.TlfName,
}
if updateConv.GetTopicType() == chat1.TopicType_CHAT {
if g.shouldSendNotifications() {
g.G().NotifyRouter.HandleChatTLFResolve(ctx, keybase1.UID(uid.String()),
update.ConvID, resolveInfo)
} else {
supdate := []chat1.ConversationStaleUpdate{chat1.ConversationStaleUpdate{
ConvID: update.ConvID,
UpdateType: chat1.StaleUpdateType_CLEAR,
}}
g.G().Syncer.SendChatStaleNotifications(ctx, uid, supdate, false)
}
}
}(bctx)
return nil
}
func (g *PushHandler) shouldDisplayDesktopNotification(ctx context.Context,
uid gregor1.UID, conv *chat1.ConversationLocal, msg chat1.MessageUnboxed) bool {
if conv == nil || conv.Notifications == nil {
return false
}
if !utils.GetConversationStatusBehavior(conv.Info.Status).DesktopNotifications {
return false
}
if msg.IsValid() {
// No notifications for our own messages
if msg.Valid().ClientHeader.Sender.Eq(uid) {
return false
}
body := msg.Valid().MessageBody
typ, err := body.MessageType()
if err != nil {
g.Debug(ctx, "shouldDisplayDesktopNotification: failed to get message type: %s", err.Error())
return false
}
apptype := keybase1.DeviceType_DESKTOP
kind := chat1.NotificationKind_GENERIC
switch typ {
case chat1.MessageType_TEXT:
for _, at := range msg.Valid().AtMentions {
if at.Eq(uid) {
kind = chat1.NotificationKind_ATMENTION
break
}
}
chanMention := msg.Valid().ChannelMention
notifyFromChanMention := false
if chanMention == chat1.ChannelMention_HERE || chanMention == chat1.ChannelMention_ALL {
notifyFromChanMention = conv.Notifications.ChannelWide
}
return conv.Notifications.Settings[apptype][kind] || notifyFromChanMention
case chat1.MessageType_ATTACHMENT:
return conv.Notifications.Settings[apptype][kind]
default:
return false
}
}
return false
}
func (g *PushHandler) presentUIItem(conv *chat1.ConversationLocal) (res *chat1.InboxUIItem) {
if conv != nil {
pc := utils.PresentConversationLocal(*conv)
res = &pc
}
return res
}
func (g *PushHandler) Activity(ctx context.Context, m gregor.OutOfBandMessage) (err error) {
var identBreaks []keybase1.TLFIdentifyFailure
ctx = Context(ctx, g.G(), keybase1.TLFIdentifyBehavior_CHAT_GUI, &identBreaks,
g.identNotifier)
defer g.Trace(ctx, func() error { return err }, "Activity")()
if m.Body() == nil {
return errors.New("gregor handler for chat.activity: nil message body")
}
// Decode into generic form
var gm chat1.GenericPayload
uid := m.UID().Bytes()
reader := bytes.NewReader(m.Body().Bytes())
dec := codec.NewDecoder(reader, &codec.MsgpackHandle{WriteExt: true})
err = dec.Decode(&gm)
if err != nil {
g.Debug(ctx, "chat activity: failed to decode into generic payload: %s", err.Error())
return err
}
convID := gm.ConvID
g.Debug(ctx, "chat activity: action %s vers: %d convID: %s", gm.Action, gm.InboxVers, convID)
// Order updates based on inbox version of the update from the server
cb := g.orderer.WaitForTurn(ctx, uid, gm.InboxVers)
bctx := BackgroundContext(ctx, g.G())
go func(ctx context.Context) {
defer g.Trace(ctx, func() error { return nil }, "Activity(goroutine)")()
<-cb
g.Lock()
defer g.Unlock()
defer g.orderer.CompleteTurn(ctx, uid, gm.InboxVers)
var activity chat1.ChatActivity
var err error
var conv *chat1.ConversationLocal
action := gm.Action
reader.Reset(m.Body().Bytes())
switch action {
case types.ActionNewMessage:
var nm chat1.NewMessagePayload
err = dec.Decode(&nm)
if err != nil {
g.Debug(ctx, "chat activity: error decoding newMessage: %s", err.Error())
return
}
g.Debug(ctx, "chat activity: newMessage: convID: %s sender: %s",
nm.ConvID, nm.Message.ClientHeader.Sender)
if nm.Message.ClientHeader.OutboxID != nil {
g.Debug(ctx, "chat activity: newMessage: outboxID: %s",
hex.EncodeToString(*nm.Message.ClientHeader.OutboxID))
} else {
g.Debug(ctx, "chat activity: newMessage: outboxID is empty")
}
// Update typing status to stopped
g.typingMonitor.Update(ctx, chat1.TyperInfo{
Uid: keybase1.UID(nm.Message.ClientHeader.Sender.String()),
DeviceID: keybase1.DeviceID(nm.Message.ClientHeader.SenderDevice.String()),
}, convID, false)
decmsg, appended, pushErr := g.G().ConvSource.Push(ctx, nm.ConvID, gregor1.UID(uid), nm.Message)
if pushErr != nil {
g.Debug(ctx, "chat activity: unable to push message: %s", pushErr.Error())
}
if conv, err = g.G().InboxSource.NewMessage(ctx, uid, nm.InboxVers, nm.ConvID, nm.Message); err != nil {
g.Debug(ctx, "chat activity: unable to update inbox: %s", err.Error())
}
// If we have no error on this message, then notify the frontend
if pushErr == nil {
// Make a pagination object so client can use it in GetThreadLocal
pmsgs := []pager.Message{nm.Message}
pager := pager.NewThreadPager()
page, err := pager.MakePage(pmsgs, 1)
if err != nil {
g.Debug(ctx, "chat activity: error making page: %s", err.Error())
}
desktopNotification := g.shouldDisplayDesktopNotification(ctx, uid, conv, decmsg)
activity = chat1.NewChatActivityWithIncomingMessage(chat1.IncomingMessage{
Message: utils.PresentMessageUnboxed(decmsg),
ConvID: nm.ConvID,
Conv: g.presentUIItem(conv),
DisplayDesktopNotification: desktopNotification,
Pagination: utils.PresentPagination(page),
})
}
// If this message was not "appended", meaning there is a hole between what we have in cache,
// and this message, then we send out a notification that this thread should be considered
// stale.
// We also get here if we had an error unboxing the messages, it could be a temporal thing
// so the frontend should reload.
if !appended || pushErr != nil {
if !appended {
g.Debug(ctx, "chat activity: newMessage: non-append message, alerting")
}
if pushErr != nil {
g.Debug(ctx, "chat activity: newMessage: push error, alerting")
}
supdate := []chat1.ConversationStaleUpdate{chat1.ConversationStaleUpdate{
ConvID: nm.ConvID,
UpdateType: chat1.StaleUpdateType_CLEAR,
}}
g.G().Syncer.SendChatStaleNotifications(ctx, m.UID().Bytes(), supdate, true)
}
if g.badger != nil && nm.UnreadUpdate != nil {
g.badger.PushChatUpdate(*nm.UnreadUpdate, nm.InboxVers)
}
case types.ActionReadMessage:
var nm chat1.ReadMessagePayload
err = dec.Decode(&nm)
if err != nil {
g.Debug(ctx, "chat activity: error decoding: %s", err.Error())
return
}
g.Debug(ctx, "chat activity: readMessage: convID: %s msgID: %d",
nm.ConvID, nm.MsgID)
uid := m.UID().Bytes()
if conv, err = g.G().InboxSource.ReadMessage(ctx, uid, nm.InboxVers, nm.ConvID, nm.MsgID); err != nil {
g.Debug(ctx, "chat activity: unable to update inbox: %s", err.Error())
}
activity = chat1.NewChatActivityWithReadMessage(chat1.ReadMessageInfo{
MsgID: nm.MsgID,
ConvID: nm.ConvID,
Conv: g.presentUIItem(conv),
})
if g.badger != nil && nm.UnreadUpdate != nil {
g.badger.PushChatUpdate(*nm.UnreadUpdate, nm.InboxVers)
}
case types.ActionSetStatus:
var nm chat1.SetStatusPayload
err = dec.Decode(&nm)
if err != nil {
g.Debug(ctx, "chat activity: error decoding: %s", err.Error())
return
}
g.Debug(ctx, "chat activity: setStatus: convID: %s status: %d",
nm.ConvID, nm.Status)
uid := m.UID().Bytes()
if conv, err = g.G().InboxSource.SetStatus(ctx, uid, nm.InboxVers, nm.ConvID, nm.Status); err != nil {
g.Debug(ctx, "chat activity: unable to update inbox: %s", err.Error())
}
activity = chat1.NewChatActivityWithSetStatus(chat1.SetStatusInfo{
ConvID: nm.ConvID,
Status: nm.Status,
Conv: g.presentUIItem(conv),
})
if g.badger != nil && nm.UnreadUpdate != nil {
g.badger.PushChatUpdate(*nm.UnreadUpdate, nm.InboxVers)
}
case types.ActionSetAppNotificationSettings:
var nm chat1.SetAppNotificationSettingsPayload
err = dec.Decode(&nm)
if err != nil {
g.Debug(ctx, "chat activity: error decoding: %s", err.Error())
return
}
g.Debug(ctx, "chat activity: setAppNotificationSettings: convID: %s num settings: %d",
nm.ConvID, len(nm.Settings.Settings))
uid := m.UID().Bytes()
if _, err = g.G().InboxSource.SetAppNotificationSettings(ctx, uid, nm.InboxVers,
nm.ConvID, nm.Settings); err != nil {
g.Debug(ctx, "chat activity: unable to update inbox: %s", err.Error())
}
info := chat1.SetAppNotificationSettingsInfo{
ConvID: nm.ConvID,
Settings: nm.Settings,
}
activity = chat1.NewChatActivityWithSetAppNotificationSettings(info)
case types.ActionNewConversation:
var nm chat1.NewConversationPayload
err = dec.Decode(&nm)
if err != nil {
g.Debug(ctx, "chat activity: error decoding: %s", err.Error())
return
}
g.Debug(ctx, "chat activity: newConversation: convID: %s ", nm.ConvID)
uid := m.UID().Bytes()
// We need to get this conversation and then localize it
var inbox chat1.Inbox
if inbox, _, err = g.G().InboxSource.Read(ctx, uid, nil, false, &chat1.GetInboxLocalQuery{
ConvIDs: []chat1.ConversationID{nm.ConvID},
}, nil); err != nil {
g.Debug(ctx, "chat activity: unable to read conversation: %s", err.Error())
return
}
if len(inbox.Convs) != 1 {
g.Debug(ctx, "chat activity: unable to find conversation")
return
}
updateConv := inbox.ConvsUnverified[0]
if err = g.G().InboxSource.NewConversation(ctx, uid, nm.InboxVers, updateConv); err != nil {
g.Debug(ctx, "chat activity: unable to update inbox: %s", err.Error())
}
conv = &inbox.Convs[0]
activity = chat1.NewChatActivityWithNewConversation(chat1.NewConversationInfo{
Conv: *g.presentUIItem(conv),
})
if g.badger != nil && nm.UnreadUpdate != nil {
g.badger.PushChatUpdate(*nm.UnreadUpdate, nm.InboxVers)
}
default:
g.Debug(ctx, "unhandled chat.activity action %q", action)
}
g.notifyNewChatActivity(ctx, m.UID(), convID, conv, &activity)
}(bctx)
return nil
}
func (g *PushHandler) notifyNewChatActivity(ctx context.Context, uid gregor.UID,
convID chat1.ConversationID, conv *chat1.ConversationLocal, activity *chat1.ChatActivity) error {
kbUID, err := keybase1.UIDFromString(hex.EncodeToString(uid.Bytes()))
if err != nil {
return err
}
// Don't send any notifications for non-chat topic types
if conv != nil && conv.GetTopicType() != chat1.TopicType_CHAT {
return nil
}
if g.shouldSendNotifications() {
g.G().NotifyRouter.HandleNewChatActivity(ctx, kbUID, activity)
} else {
// If we are not in send notifications mode, then just label this conversation
// as stale, and we can reload the thread.
supdate := []chat1.ConversationStaleUpdate{chat1.ConversationStaleUpdate{
ConvID: convID,
UpdateType: chat1.StaleUpdateType_NEWACTIVITY,
}}
g.G().Syncer.SendChatStaleNotifications(ctx, uid.(gregor1.UID), supdate, false)
}
return nil
}
func (g *PushHandler) notifyJoinChannel(ctx context.Context, uid gregor1.UID,
conv chat1.ConversationLocal) {
kuid := keybase1.UID(uid.String())
if g.shouldSendNotifications() {
g.G().NotifyRouter.HandleChatJoinedConversation(ctx, kuid, *g.presentUIItem(&conv))
} else {
supdate := []chat1.ConversationStaleUpdate{chat1.ConversationStaleUpdate{
ConvID: conv.GetConvID(),
UpdateType: chat1.StaleUpdateType_NEWACTIVITY,
}}
g.G().Syncer.SendChatStaleNotifications(ctx, uid, supdate, false)
}
}
func (g *PushHandler) notifyLeftChannel(ctx context.Context, uid gregor1.UID,
convID chat1.ConversationID) {
kuid := keybase1.UID(uid.String())
if g.shouldSendNotifications() {
g.G().NotifyRouter.HandleChatLeftConversation(ctx, kuid, convID)
} else {
supdate := []chat1.ConversationStaleUpdate{chat1.ConversationStaleUpdate{
ConvID: convID,
UpdateType: chat1.StaleUpdateType_NEWACTIVITY,
}}
g.G().Syncer.SendChatStaleNotifications(ctx, uid, supdate, false)
}
}
func (g *PushHandler) notifyMembersUpdate(ctx context.Context, uid gregor1.UID,
member chat1.ConversationMember, joined bool) {
unameFailed := false
name, err := g.G().GetUPAKLoader().LookupUsername(ctx, keybase1.UID(member.Uid.String()))
if err != nil {
g.Debug(ctx, "notifyMembersUpdate: failed to lookup username for: %s msg: %s", member.Uid,
err.Error())
unameFailed = true
}
if !unameFailed {
activity := chat1.NewChatActivityWithMembersUpdate(chat1.MembersUpdateInfo{
ConvID: member.ConvID,
Member: name.String(),
Joined: joined,
})
g.notifyNewChatActivity(ctx, uid, member.ConvID, nil, &activity)
} else {
supdate := []chat1.ConversationStaleUpdate{chat1.ConversationStaleUpdate{
ConvID: member.ConvID,
UpdateType: chat1.StaleUpdateType_NEWACTIVITY,
}}
g.G().Syncer.SendChatStaleNotifications(ctx, uid, supdate, false)
}
}
func (g *PushHandler) Typing(ctx context.Context, m gregor.OutOfBandMessage) (err error) {
var identBreaks []keybase1.TLFIdentifyFailure
ctx = Context(ctx, g.G(), keybase1.TLFIdentifyBehavior_CHAT_GUI, &identBreaks,
g.identNotifier)
defer g.Trace(ctx, func() error { return err }, "Typing")()
if m.Body() == nil {
return errors.New("gregor handler for typing: nil message body")
}
var update chat1.RemoteUserTypingUpdate
reader := bytes.NewReader(m.Body().Bytes())
dec := codec.NewDecoder(reader, &codec.MsgpackHandle{WriteExt: true})
err = dec.Decode(&update)
if err != nil {
return err
}
// Lookup username and device name
kuid := keybase1.UID(update.Uid.String())
kdid := keybase1.DeviceID(update.DeviceID.String())
user, device, dtype, err := g.G().GetUPAKLoader().LookupUsernameAndDevice(ctx, kuid, kdid)
if err != nil {
g.Debug(ctx, "Typing: failed to lookup username/device: msg: %s", err.Error())
return err
}
// Fire off update with all relevant info
g.typingMonitor.Update(ctx, chat1.TyperInfo{
Uid: kuid,
DeviceID: kdid,
Username: user.String(),
DeviceName: device,
DeviceType: dtype,
}, update.ConvID, update.Typing)
return nil
}
func (g *PushHandler) MembershipUpdate(ctx context.Context, m gregor.OutOfBandMessage) (err error) {
var identBreaks []keybase1.TLFIdentifyFailure
ctx = Context(ctx, g.G(), keybase1.TLFIdentifyBehavior_CHAT_GUI, &identBreaks,
g.identNotifier)
defer g.Trace(ctx, func() error { return err }, "MembershipUpdate")()
if m.Body() == nil {
return errors.New("gregor handler for membership update: nil message body")
}
var update chat1.UpdateConversationMembership
reader := bytes.NewReader(m.Body().Bytes())
dec := codec.NewDecoder(reader, &codec.MsgpackHandle{WriteExt: true})
err = dec.Decode(&update)
if err != nil {
return err
}
uid := gregor1.UID(m.UID().Bytes())
// Order updates based on inbox version of the update from the server
cb := g.orderer.WaitForTurn(ctx, uid, update.InboxVers)
bctx := BackgroundContext(ctx, g.G())
go func(ctx context.Context) (err error) {
defer g.Trace(ctx, func() error { return err }, "MembershipUpdate(goroutine)")()
<-cb
g.Lock()
defer g.Unlock()
defer g.orderer.CompleteTurn(ctx, uid, update.InboxVers)
// Write out changes to local storage
updateRes, err := g.G().InboxSource.MembershipUpdate(ctx, uid, update.InboxVers, update.Joined,
update.Removed)
if err != nil {
g.Debug(ctx, "MembershipUpdate: failed to update membership on inbox: %s", err.Error())
return err
}
// Send out notifications
for _, c := range updateRes.UserJoinedConvs {
g.notifyJoinChannel(ctx, uid, c)
}
for _, c := range updateRes.UserRemovedConvs {
g.notifyLeftChannel(ctx, uid, c)
}
for _, cm := range updateRes.OthersJoinedConvs {
g.notifyMembersUpdate(ctx, uid, cm, true)
}
for _, cm := range updateRes.OthersRemovedConvs {
g.notifyMembersUpdate(ctx, uid, cm, false)
}
// Fire off badger update
if g.badger != nil && update.UnreadUpdate != nil {
g.badger.PushChatUpdate(*update.UnreadUpdate, update.InboxVers)
}
return nil
}(bctx)
return nil
}
func (g *PushHandler) HandleOobm(ctx context.Context, obm gregor.OutOfBandMessage) (bool, error) {
if obm.System() == nil {
return false, errors.New("nil system in out of band message")
}
switch obm.System().String() {
case types.PushActivity:
return true, g.Activity(ctx, obm)
case types.PushTLFFinalize:
return true, g.TlfFinalize(ctx, obm)
case types.PushTLFResolve:
return true, g.TlfResolve(ctx, obm)
case types.PushTyping:
return true, g.Typing(ctx, obm)
case types.PushMembershipUpdate:
return true, g.MembershipUpdate(ctx, obm)
}
return false, nil
}