/
helper.go
1115 lines (1002 loc) · 38.1 KB
/
helper.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
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package chat
import (
"encoding/hex"
"errors"
"fmt"
"math"
"sort"
"time"
"github.com/keybase/client/go/chat/globals"
"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/libkb"
"github.com/keybase/client/go/protocol/chat1"
"github.com/keybase/client/go/protocol/gregor1"
"github.com/keybase/client/go/protocol/keybase1"
"github.com/keybase/client/go/teams"
"golang.org/x/net/context"
)
type Helper struct {
globals.Contextified
utils.DebugLabeler
ri func() chat1.RemoteInterface
}
func NewHelper(g *globals.Context, ri func() chat1.RemoteInterface) *Helper {
return &Helper{
Contextified: globals.NewContextified(g),
DebugLabeler: utils.NewDebugLabeler(g.GetLog(), "Helper", false),
ri: ri,
}
}
func (h *Helper) SendTextByID(ctx context.Context, convID chat1.ConversationID,
trip chat1.ConversationIDTriple, tlfName string, text string) error {
return h.SendMsgByID(ctx, convID, trip, tlfName, chat1.NewMessageBodyWithText(chat1.MessageText{
Body: text,
}), chat1.MessageType_TEXT)
}
func (h *Helper) SendMsgByID(ctx context.Context, convID chat1.ConversationID,
trip chat1.ConversationIDTriple, tlfName string, body chat1.MessageBody, msgType chat1.MessageType) error {
boxer := NewBoxer(h.G())
sender := NewBlockingSender(h.G(), boxer, h.ri)
msg := chat1.MessagePlaintext{
ClientHeader: chat1.MessageClientHeader{
Conv: trip,
TlfName: tlfName,
MessageType: msgType,
},
MessageBody: body,
}
_, _, err := sender.Send(ctx, convID, msg, 0, nil)
return err
}
func (h *Helper) SendTextByIDNonblock(ctx context.Context, convID chat1.ConversationID,
trip chat1.ConversationIDTriple, tlfName string, text string) error {
return h.SendMsgByIDNonblock(ctx, convID, trip, tlfName, chat1.NewMessageBodyWithText(chat1.MessageText{
Body: text,
}), chat1.MessageType_TEXT)
}
func (h *Helper) SendMsgByIDNonblock(ctx context.Context, convID chat1.ConversationID,
trip chat1.ConversationIDTriple, tlfName string, body chat1.MessageBody, msgType chat1.MessageType) error {
boxer := NewBoxer(h.G())
baseSender := NewBlockingSender(h.G(), boxer, h.ri)
sender := NewNonblockingSender(h.G(), baseSender)
msg := chat1.MessagePlaintext{
ClientHeader: chat1.MessageClientHeader{
Conv: trip,
TlfName: tlfName,
MessageType: msgType,
},
MessageBody: body,
}
_, _, err := sender.Send(ctx, convID, msg, 0, nil)
return err
}
func (h *Helper) SendTextByName(ctx context.Context, name string, topicName *string,
membersType chat1.ConversationMembersType, ident keybase1.TLFIdentifyBehavior, text string) error {
boxer := NewBoxer(h.G())
sender := NewBlockingSender(h.G(), boxer, h.ri)
helper := newSendHelper(h.G(), name, topicName, membersType, ident, sender, h.ri)
return helper.SendText(ctx, text)
}
func (h *Helper) SendMsgByName(ctx context.Context, name string, topicName *string,
membersType chat1.ConversationMembersType, ident keybase1.TLFIdentifyBehavior, body chat1.MessageBody,
msgType chat1.MessageType) error {
boxer := NewBoxer(h.G())
sender := NewBlockingSender(h.G(), boxer, h.ri)
helper := newSendHelper(h.G(), name, topicName, membersType, ident, sender, h.ri)
return helper.SendBody(ctx, body, msgType)
}
func (h *Helper) SendTextByNameNonblock(ctx context.Context, name string, topicName *string,
membersType chat1.ConversationMembersType, ident keybase1.TLFIdentifyBehavior, text string) error {
boxer := NewBoxer(h.G())
baseSender := NewBlockingSender(h.G(), boxer, h.ri)
sender := NewNonblockingSender(h.G(), baseSender)
helper := newSendHelper(h.G(), name, topicName, membersType, ident, sender, h.ri)
return helper.SendText(ctx, text)
}
func (h *Helper) SendMsgByNameNonblock(ctx context.Context, name string, topicName *string,
membersType chat1.ConversationMembersType, ident keybase1.TLFIdentifyBehavior, body chat1.MessageBody,
msgType chat1.MessageType) error {
boxer := NewBoxer(h.G())
baseSender := NewBlockingSender(h.G(), boxer, h.ri)
sender := NewNonblockingSender(h.G(), baseSender)
helper := newSendHelper(h.G(), name, topicName, membersType, ident, sender, h.ri)
return helper.SendBody(ctx, body, msgType)
}
func (h *Helper) FindConversations(ctx context.Context, useLocalData bool, name string, topicName *string,
topicType chat1.TopicType, membersType chat1.ConversationMembersType, vis keybase1.TLFVisibility) ([]chat1.ConversationLocal, error) {
kuid, err := CurrentUID(h.G())
if err != nil {
return nil, err
}
uid := gregor1.UID(kuid.ToBytes())
oneChat := true
var tname string
if topicName != nil {
tname = *topicName
}
convs, err := FindConversations(ctx, h.G(), h.DebugLabeler, useLocalData, h.ri, uid, name, topicType,
membersType, vis, tname, &oneChat)
return convs, err
}
func (h *Helper) FindConversationsByID(ctx context.Context, convIDs []chat1.ConversationID) ([]chat1.ConversationLocal, error) {
kuid, err := CurrentUID(h.G())
if err != nil {
return nil, err
}
uid := gregor1.UID(kuid.ToBytes())
query := &chat1.GetInboxLocalQuery{
ConvIDs: convIDs,
}
inbox, err := h.G().InboxSource.Read(ctx, uid, nil, true, query, nil)
if err != nil {
return nil, err
}
return inbox.Convs, nil
}
// GetChannelTopicName gets the name of a team channel even if it's not in the inbox.
func (h *Helper) GetChannelTopicName(ctx context.Context, teamID keybase1.TeamID,
topicType chat1.TopicType, convID chat1.ConversationID) (topicName string, err error) {
defer h.Trace(ctx, func() error { return err }, "ChatHelper.GetChannelTopicName")()
h.Debug(ctx, "for teamID:%v convID:%v", teamID.String(), convID.String())
kuid, err := CurrentUID(h.G())
if err != nil {
return topicName, err
}
uid := gregor1.UID(kuid.ToBytes())
tlfID, err := chat1.TeamIDToTLFID(teamID)
if err != nil {
return topicName, err
}
// First try the inbox
query := &chat1.GetInboxLocalQuery{
ConvIDs: []chat1.ConversationID{convID},
TopicType: &topicType,
}
inbox, err := h.G().InboxSource.Read(ctx, uid, nil, true, query, nil)
if err != nil {
return topicName, err
}
h.Debug(ctx, "found inbox convs: %v", len(inbox.Convs))
for _, conv := range inbox.Convs {
if conv.GetConvID().Eq(convID) && conv.GetMembersType() == chat1.ConversationMembersType_TEAM {
return utils.GetTopicName(conv), nil
}
}
// Fallback to TeamChannelSource
h.Debug(ctx, "using TeamChannelSource")
topicName, err = h.G().TeamChannelSource.GetChannelTopicName(ctx, uid, tlfID, topicType, convID)
return topicName, err
}
func (h *Helper) UpgradeKBFSToImpteam(ctx context.Context, tlfName string, tlfID chat1.TLFID, public bool) (err error) {
ctx = Context(ctx, h.G(), keybase1.TLFIdentifyBehavior_CHAT_GUI, nil, NewCachingIdentifyNotifier(h.G()))
defer h.Trace(ctx, func() error { return err }, "ChatHelper.UpgradeKBFSToImpteam(%s,%s,%v)",
tlfID, tlfName, public)()
var cryptKeys []keybase1.CryptKey
nis := NewKBFSNameInfoSource(h.G())
keys, err := nis.AllCryptKeys(ctx, tlfName, public)
if err != nil {
return err
}
for _, key := range keys[chat1.ConversationMembersType_KBFS] {
cryptKeys = append(cryptKeys, keybase1.CryptKey{
KeyGeneration: key.Generation(),
Key: key.Material(),
})
}
ni, err := nis.LookupID(ctx, tlfName, public)
if err != nil {
return err
}
tlfName = ni.CanonicalName
h.Debug(ctx, "UpgradeKBFSToImpteam: upgrading: TlfName: %s TLFID: %s public: %v keys: %d",
tlfName, tlfID, public, len(cryptKeys))
return teams.UpgradeTLFIDToImpteam(ctx, h.G().ExternalG(), tlfName, keybase1.TLFID(tlfID.String()),
public, keybase1.TeamApplication_CHAT, cryptKeys)
}
func (h *Helper) GetMessages(ctx context.Context, uid gregor1.UID, convID chat1.ConversationID,
msgIDs []chat1.MessageID, resolveSupersedes bool) ([]chat1.MessageUnboxed, error) {
return GetMessages(ctx, h.G(), uid, convID, msgIDs, resolveSupersedes)
}
func GetMessages(ctx context.Context, g *globals.Context, uid gregor1.UID, convID chat1.ConversationID,
msgIDs []chat1.MessageID, resolveSupersedes bool) ([]chat1.MessageUnboxed, error) {
conv, err := GetUnverifiedConv(ctx, g, uid, convID, true /* useLocalData */)
if err != nil {
return nil, err
}
// use ConvSource to get the messages, to try the cache first
messages, err := g.ConvSource.GetMessages(ctx, conv, uid, msgIDs, nil)
if err != nil {
return nil, err
}
// unless arg says not to, transform the superseded messages
if resolveSupersedes {
messages, err = g.ConvSource.TransformSupersedes(ctx, conv, uid, messages)
if err != nil {
return nil, err
}
}
return messages, nil
}
type sendHelper struct {
utils.DebugLabeler
name string
membersType chat1.ConversationMembersType
ident keybase1.TLFIdentifyBehavior
sender types.Sender
ri func() chat1.RemoteInterface
topicName *string
convID chat1.ConversationID
triple chat1.ConversationIDTriple
globals.Contextified
}
func newSendHelper(g *globals.Context, name string, topicName *string,
membersType chat1.ConversationMembersType, ident keybase1.TLFIdentifyBehavior, sender types.Sender,
ri func() chat1.RemoteInterface) *sendHelper {
return &sendHelper{
Contextified: globals.NewContextified(g),
DebugLabeler: utils.NewDebugLabeler(g.GetLog(), "sendHelper", false),
name: name,
topicName: topicName,
membersType: membersType,
ident: ident,
sender: sender,
ri: ri,
}
}
func (s *sendHelper) SendText(ctx context.Context, text string) error {
body := chat1.NewMessageBodyWithText(chat1.MessageText{Body: text})
return s.SendBody(ctx, body, chat1.MessageType_TEXT)
}
func (s *sendHelper) SendBody(ctx context.Context, body chat1.MessageBody, mtype chat1.MessageType) error {
ctx = Context(ctx, s.G(), s.ident, nil, NewCachingIdentifyNotifier(s.G()))
if err := s.conversation(ctx); err != nil {
return err
}
return s.deliver(ctx, body, mtype)
}
func (s *sendHelper) conversation(ctx context.Context) error {
kuid, err := CurrentUID(s.G())
if err != nil {
return err
}
uid := gregor1.UID(kuid.ToBytes())
conv, err := NewConversation(ctx, s.G(), uid, s.name, s.topicName,
chat1.TopicType_CHAT, s.membersType, keybase1.TLFVisibility_PRIVATE, s.remoteInterface)
if err != nil {
return err
}
s.convID = conv.GetConvID()
s.triple = conv.Info.Triple
s.name = conv.Info.TlfName
return nil
}
func (s *sendHelper) deliver(ctx context.Context, body chat1.MessageBody, mtype chat1.MessageType) error {
msg := chat1.MessagePlaintext{
ClientHeader: chat1.MessageClientHeader{
Conv: s.triple,
TlfName: s.name,
MessageType: mtype,
},
MessageBody: body,
}
_, _, err := s.sender.Send(ctx, s.convID, msg, 0, nil)
return err
}
func (s *sendHelper) remoteInterface() chat1.RemoteInterface {
return s.ri()
}
func CurrentUID(g *globals.Context) (keybase1.UID, error) {
uid := g.Env.GetUID()
if uid.IsNil() {
return "", libkb.LoginRequiredError{}
}
return uid, nil
}
type recentConversationParticipants struct {
globals.Contextified
utils.DebugLabeler
}
func newRecentConversationParticipants(g *globals.Context) *recentConversationParticipants {
return &recentConversationParticipants{
Contextified: globals.NewContextified(g),
DebugLabeler: utils.NewDebugLabeler(g.GetLog(), "recentConversationParticipants", false),
}
}
func (r *recentConversationParticipants) getActiveScore(ctx context.Context, conv chat1.Conversation) float64 {
mtime := conv.GetMtime()
diff := time.Now().Sub(mtime.Time())
weeksAgo := diff.Seconds() / (time.Hour.Seconds() * 24 * 7)
val := 10.0 - math.Pow(1.6, weeksAgo)
if val < 1.0 {
val = 1.0
}
return val
}
func (r *recentConversationParticipants) get(ctx context.Context, myUID gregor1.UID) (res []gregor1.UID, err error) {
_, convs, err := storage.NewInbox(r.G()).ReadAll(ctx, myUID)
if err != nil {
if _, ok := err.(storage.MissError); ok {
r.Debug(ctx, "get: no inbox, returning blank results")
return nil, nil
}
return nil, err
}
r.Debug(ctx, "get: convs: %d", len(convs))
m := make(map[string]float64)
for _, conv := range convs {
for _, uid := range conv.Conv.Metadata.ActiveList {
if uid.Eq(myUID) {
continue
}
m[uid.String()] += r.getActiveScore(ctx, conv.Conv)
}
}
for suid := range m {
uid, _ := hex.DecodeString(suid)
res = append(res, gregor1.UID(uid))
}
// Sort by the most appearances in the active lists
sort.Slice(res, func(i, j int) bool {
return m[res[i].String()] > m[res[j].String()]
})
return res, nil
}
func RecentConversationParticipants(ctx context.Context, g *globals.Context, myUID gregor1.UID) ([]gregor1.UID, error) {
ctx = Context(ctx, g, keybase1.TLFIdentifyBehavior_CHAT_GUI, nil, NewCachingIdentifyNotifier(g))
return newRecentConversationParticipants(g).get(ctx, myUID)
}
var errGetUnverifiedConvNotFound = errors.New("GetUnverifiedConv: conversation not found")
var errGetVerifiedConvNotFound = errors.New("GetVerifiedConv: conversation not found")
func GetUnverifiedConv(ctx context.Context, g *globals.Context, uid gregor1.UID,
convID chat1.ConversationID, useLocalData bool) (chat1.Conversation, error) {
inbox, err := g.InboxSource.ReadUnverified(ctx, uid, useLocalData, &chat1.GetInboxQuery{
ConvIDs: []chat1.ConversationID{convID},
}, nil)
if err != nil {
return chat1.Conversation{}, fmt.Errorf("GetUnverifiedConv: %s", err.Error())
}
if len(inbox.ConvsUnverified) == 0 {
return chat1.Conversation{}, errGetUnverifiedConvNotFound
}
if !inbox.ConvsUnverified[0].GetConvID().Eq(convID) {
return chat1.Conversation{}, fmt.Errorf("GetUnverifiedConv: convID mismatch: %s != %s",
inbox.ConvsUnverified[0].GetConvID(), convID)
}
return inbox.ConvsUnverified[0].Conv, nil
}
func GetVerifiedConv(ctx context.Context, g *globals.Context, uid gregor1.UID,
convID chat1.ConversationID, useLocalData bool) (res chat1.ConversationLocal, err error) {
inbox, err := g.InboxSource.Read(ctx, uid, nil, useLocalData, &chat1.GetInboxLocalQuery{
ConvIDs: []chat1.ConversationID{convID},
}, nil)
if err != nil {
return res, fmt.Errorf("GetVerifiedConv: %s", err.Error())
}
if len(inbox.Convs) == 0 {
return res, errGetVerifiedConvNotFound
}
if !inbox.Convs[0].GetConvID().Eq(convID) {
return res, fmt.Errorf("GetVerifiedConv: convID mismatch: %s != %s",
inbox.Convs[0].GetConvID(), convID)
}
return inbox.Convs[0], nil
}
func PresentConversationLocalWithFetchRetry(ctx context.Context, g *globals.Context,
uid gregor1.UID, conv chat1.ConversationLocal) (res *chat1.InboxUIItem) {
if conv.Error != nil {
// If we get a transient failure, add this to the retrier queue
if conv.Error.Typ == chat1.ConversationErrorType_TRANSIENT {
g.FetchRetrier.Failure(ctx, uid,
NewConversationRetry(g, conv.GetConvID(), &conv.Info.Triple.Tlfid, InboxLoad))
}
} else {
pc := utils.PresentConversationLocal(conv, g.Env.GetUsername().String())
res = &pc
}
return res
}
func GetTopicNameState(ctx context.Context, g *globals.Context, debugger utils.DebugLabeler,
convs []chat1.ConversationLocal,
uid gregor1.UID, tlfID chat1.TLFID, topicType chat1.TopicType,
membersType chat1.ConversationMembersType) (res chat1.TopicNameState, err error) {
var pairs chat1.ConversationIDMessageIDPairs
sort.Sort(utils.ConvLocalByConvID(convs))
for _, conv := range convs {
msg, err := conv.GetMaxMessage(chat1.MessageType_METADATA)
if err != nil {
debugger.Debug(ctx, "GetTopicNameState: skipping convID: %s, no metadata message",
conv.GetConvID())
continue
}
if !msg.IsValid() {
debugger.Debug(ctx, "GetTopicNameState: skipping convID: %s, invalid metadata message: %v",
conv.GetConvID(), msg.DebugString())
continue
}
pairs.Pairs = append(pairs.Pairs, chat1.ConversationIDMessageIDPair{
ConvID: conv.GetConvID(),
MsgID: msg.GetMessageID(),
})
}
if res, err = utils.CreateTopicNameState(pairs); err != nil {
debugger.Debug(ctx, "GetTopicNameState: failed to create topic name state: %s", err.Error())
return res, err
}
return res, nil
}
func FindConversations(ctx context.Context, g *globals.Context, debugger utils.DebugLabeler,
useLocalData bool, ri func() chat1.RemoteInterface, uid gregor1.UID, tlfName string,
topicType chat1.TopicType,
membersTypeIn chat1.ConversationMembersType, vis keybase1.TLFVisibility, topicName string,
oneChatPerTLF *bool) (res []chat1.ConversationLocal, err error) {
findConvosWithMembersType := func(membersType chat1.ConversationMembersType) (res []chat1.ConversationLocal, err error) {
// Don't look for KBFS conversations anymore, they have mostly been converted, and it is better
// to just not search for them than to create a double conversation. Make an exception for
// public conversations.
if g.GetEnv().GetChatMemberType() != "kbfs" && membersType == chat1.ConversationMembersType_KBFS &&
vis == keybase1.TLFVisibility_PRIVATE {
return nil, nil
}
// Make sure team topic name makes sense
if topicName == "" && membersType == chat1.ConversationMembersType_TEAM {
topicName = globals.DefaultTeamTopic
}
query := &chat1.GetInboxLocalQuery{
Name: &chat1.NameQuery{
Name: tlfName,
MembersType: membersType,
},
TlfVisibility: &vis,
TopicName: &topicName,
TopicType: &topicType,
OneChatTypePerTLF: oneChatPerTLF,
}
inbox, err := g.InboxSource.Read(ctx, uid, nil, useLocalData, query, nil)
if err != nil {
// don't error out if the TLF name is just unknown, treat it as a complete miss
if _, ok := err.(UnknownTLFNameError); !ok {
return res, err
}
inbox.Convs = nil
}
// If we have inbox hits, return those
if len(inbox.Convs) > 0 {
debugger.Debug(ctx, "FindConversations: found conversations in inbox: tlfName: %s num: %d",
tlfName, len(inbox.Convs))
res = inbox.Convs
} else if membersType == chat1.ConversationMembersType_TEAM {
// If this is a team chat that we are looking for, then let's try searching all
// chats on the team to see if any match the arguments before giving up.
// No need to worry (yet) about conflicting with public code path, since there
// are not any public team chats.
// Fetch the TLF ID from specified name
nameInfo, err := CreateNameInfoSource(ctx, g, membersType).LookupID(ctx, tlfName, false)
if err != nil {
debugger.Debug(ctx, "FindConversations: failed to get TLFID from name: %s", err.Error())
return res, err
}
tlfConvs, err := g.TeamChannelSource.GetChannelsFull(ctx, uid, nameInfo.ID, topicType)
if err != nil {
debugger.Debug(ctx, "FindConversations: failed to list TLF conversations: %s", err.Error())
return res, err
}
for _, tlfConv := range tlfConvs {
if utils.GetTopicName(tlfConv) == topicName {
res = append(res, tlfConv)
}
}
if len(res) > 0 {
debugger.Debug(ctx, "FindConversations: found team channels: num: %d", len(res))
}
} else if vis == keybase1.TLFVisibility_PUBLIC {
debugger.Debug(ctx, "FindConversations: no conversations found in inbox, trying public chats")
// Check for offline and return an error
if g.InboxSource.IsOffline(ctx) {
return res, OfflineError{}
}
// If we miss the inbox, and we are looking for a public TLF, let's try and find
// any conversation that matches
nameInfo, err := GetInboxQueryNameInfo(ctx, g, query)
if err != nil {
return res, err
}
// Call into gregor to try and find some public convs
pubConvs, err := ri().GetPublicConversations(ctx, chat1.GetPublicConversationsArg{
TlfID: nameInfo.ID,
TopicType: topicType,
SummarizeMaxMsgs: true,
})
if err != nil {
return res, err
}
// Localize the convs (if any)
if len(pubConvs.Conversations) > 0 {
localizer := NewBlockingLocalizer(g)
convsLocal, err := localizer.Localize(ctx, uid, types.Inbox{
ConvsUnverified: utils.RemoteConvs(pubConvs.Conversations),
})
if err != nil {
return res, err
}
// Search for conversations that match the topic name
for _, convLocal := range convsLocal {
if convLocal.Error != nil {
debugger.Debug(ctx, "FindConversations: skipping convID: %s localization failure: %s",
convLocal.GetConvID(), convLocal.Error.Message)
continue
}
if convLocal.Info.TopicName == topicName &&
convLocal.Info.TLFNameExpanded() == nameInfo.CanonicalName {
debugger.Debug(ctx, "FindConversations: found matching public conv: id: %s topicName: %s",
convLocal.GetConvID(), topicName)
res = append(res, convLocal)
}
}
}
}
return res, nil
}
attempts := make(map[chat1.ConversationMembersType]bool)
mt := membersTypeIn
L:
for {
var ierr error
attempts[mt] = true
res, ierr = findConvosWithMembersType(mt)
if ierr != nil || len(res) == 0 {
if ierr != nil {
debugger.Debug(ctx, "FindConversations: fail reason: %s mt: %v", ierr, mt)
} else {
debugger.Debug(ctx, "FindConversations: fail reason: no convs mt: %v", mt)
}
var newMT chat1.ConversationMembersType
switch mt {
case chat1.ConversationMembersType_TEAM:
err = ierr
debugger.Debug(ctx, "FindConversations: failed with team, aborting")
break L
case chat1.ConversationMembersType_IMPTEAMUPGRADE:
if !attempts[chat1.ConversationMembersType_IMPTEAMNATIVE] {
newMT = chat1.ConversationMembersType_IMPTEAMNATIVE
// Only set the error if the members type is the same as what was passed in
err = ierr
} else {
newMT = chat1.ConversationMembersType_KBFS
}
case chat1.ConversationMembersType_IMPTEAMNATIVE:
if !attempts[chat1.ConversationMembersType_IMPTEAMUPGRADE] {
newMT = chat1.ConversationMembersType_IMPTEAMUPGRADE
// Only set the error if the members type is the same as what was passed in
err = ierr
} else {
newMT = chat1.ConversationMembersType_KBFS
}
case chat1.ConversationMembersType_KBFS:
debugger.Debug(ctx, "FindConversations: failed with KBFS, aborting")
// We don't want to return random errors from KBFS if we are falling back to it,
// just return no conversations and call it a day
if membersTypeIn == chat1.ConversationMembersType_KBFS {
err = ierr
}
break L
}
debugger.Debug(ctx,
"FindConversations: failing to find anything for %v, trying again for %v", mt, newMT)
mt = newMT
} else {
debugger.Debug(ctx, "FindConversations: success with mt: %v", mt)
break L
}
}
return res, err
}
// Post a join or leave message. Must be called when the user is in the conv.
// Uses a blocking sender.
func postJoinLeave(ctx context.Context, g *globals.Context, ri func() chat1.RemoteInterface, uid gregor1.UID,
convID chat1.ConversationID, body chat1.MessageBody) (err error) {
typ, err := body.MessageType()
if err != nil {
return fmt.Errorf("message type for postJoinLeave: %v", err)
}
switch typ {
case chat1.MessageType_JOIN, chat1.MessageType_LEAVE:
// good
default:
return fmt.Errorf("invalid message type for postJoinLeave: %v", typ)
}
// Get the conversation from the inbox.
query := chat1.GetInboxLocalQuery{
ConvIDs: []chat1.ConversationID{convID},
}
ib, err := g.InboxSource.Read(ctx, uid, nil, true, &query, nil)
if err != nil {
return fmt.Errorf("inbox read error: %s", err)
}
if len(ib.Convs) != 1 {
return fmt.Errorf("post join/leave: found %d conversations", len(ib.Convs))
}
conv := ib.Convs[0]
plaintext := chat1.MessagePlaintext{
ClientHeader: chat1.MessageClientHeader{
Conv: conv.Info.Triple,
TlfName: conv.Info.TlfName,
TlfPublic: conv.Info.Visibility == keybase1.TLFVisibility_PUBLIC,
MessageType: typ,
Supersedes: chat1.MessageID(0),
Deletes: nil,
Prev: nil, // Filled by Sender
Sender: nil, // Filled by Sender
SenderDevice: nil, // Filled by Sender
MerkleRoot: nil, // Filled by Boxer
OutboxID: nil,
OutboxInfo: nil,
},
MessageBody: body,
}
// Send with a blocking sender
sender := NewBlockingSender(g, NewBoxer(g), ri)
_, _, err = sender.Send(ctx, convID, plaintext, 0, nil)
return err
}
func JoinConversation(ctx context.Context, g *globals.Context, debugger utils.DebugLabeler,
ri func() chat1.RemoteInterface, uid gregor1.UID, convID chat1.ConversationID) (err error) {
if err := g.ConvSource.AcquireConversationLock(ctx, uid, convID); err != nil {
return err
}
defer g.ConvSource.ReleaseConversationLock(ctx, uid, convID)
alreadyIn, err := g.InboxSource.IsMember(ctx, uid, convID)
if err != nil {
debugger.Debug(ctx, "JoinConversation: IsMember err: %s", err.Error())
// Assume we're not in.
alreadyIn = false
}
// Send the join command even if we're in.
_, err = ri().JoinConversation(ctx, convID)
if err != nil {
debugger.Debug(ctx, "JoinConversation: failed to join conversation: %s", err.Error())
return err
}
if _, err = g.InboxSource.MembershipUpdate(ctx, uid, 0, []chat1.ConversationMember{
chat1.ConversationMember{
Uid: uid,
ConvID: convID,
},
}, nil, nil, nil); err != nil {
debugger.Debug(ctx, "JoinConversation: failed to apply membership update: %s", err.Error())
}
if !alreadyIn {
// Send a message to the channel after joining.
joinMessageBody := chat1.NewMessageBodyWithJoin(chat1.MessageJoin{})
debugger.Debug(ctx, "JoinConversation: sending join message to: %s", convID)
err := postJoinLeave(ctx, g, ri, uid, convID, joinMessageBody)
if err != nil {
debugger.Debug(ctx, "JoinConversation: posting join-conv message failed: %v", err)
// ignore the error
}
}
return nil
}
func LeaveConversation(ctx context.Context, g *globals.Context, debugger utils.DebugLabeler,
ri func() chat1.RemoteInterface, uid gregor1.UID, convID chat1.ConversationID) (err error) {
alreadyIn, err := g.InboxSource.IsMember(ctx, uid, convID)
if err != nil {
debugger.Debug(ctx, "LeaveConversation: IsMember err: %s", err.Error())
// Pretend we're in.
alreadyIn = true
}
// Send a message to the channel to leave the conversation
if alreadyIn {
leaveMessageBody := chat1.NewMessageBodyWithLeave(chat1.MessageLeave{})
err := postJoinLeave(ctx, g, ri, uid, convID, leaveMessageBody)
if err != nil {
debugger.Debug(ctx, "LeaveConversation: posting leave-conv message failed: %v", err)
return err
}
} else {
_, err = ri().LeaveConversation(ctx, convID)
if err != nil {
debugger.Debug(ctx, "LeaveConversation: failed to leave conversation as a non-member: %s", err)
return err
}
}
return nil
}
func PreviewConversation(ctx context.Context, g *globals.Context, debugger utils.DebugLabeler,
ri func() chat1.RemoteInterface, uid gregor1.UID, convID chat1.ConversationID) (err error) {
alreadyIn, err := g.InboxSource.IsMember(ctx, uid, convID)
if err != nil {
debugger.Debug(ctx, "PreviewConversation: IsMember err: %s", err.Error())
// Assume we aren't in, server will reject us otherwise.
alreadyIn = false
}
if alreadyIn {
debugger.Debug(ctx, "PreviewConversation: already in the conversation, no need to preview")
return nil
}
_, err = ri().PreviewConversation(ctx, convID)
if err != nil {
debugger.Debug(ctx, "PreviewConversation: failed to preview conversation: %s", err.Error())
return err
}
return nil
}
func NewConversation(ctx context.Context, g *globals.Context, uid gregor1.UID, tlfName string,
topicName *string, topicType chat1.TopicType, membersType chat1.ConversationMembersType,
vis keybase1.TLFVisibility, ri func() chat1.RemoteInterface) (chat1.ConversationLocal, error) {
helper := newNewConversationHelper(g, uid, tlfName, topicName, topicType, membersType, vis, ri)
return helper.create(ctx)
}
type newConversationHelper struct {
globals.Contextified
utils.DebugLabeler
uid gregor1.UID
tlfName string
topicName *string
topicType chat1.TopicType
membersType chat1.ConversationMembersType
vis keybase1.TLFVisibility
ri func() chat1.RemoteInterface
}
func newNewConversationHelper(g *globals.Context, uid gregor1.UID, tlfName string, topicName *string,
topicType chat1.TopicType, membersType chat1.ConversationMembersType, vis keybase1.TLFVisibility,
ri func() chat1.RemoteInterface) *newConversationHelper {
return &newConversationHelper{
Contextified: globals.NewContextified(g),
DebugLabeler: utils.NewDebugLabeler(g.GetLog(), "newConversationHelper", false),
uid: uid,
tlfName: utils.AddUserToTLFName(g, tlfName, vis, membersType),
topicName: topicName,
topicType: topicType,
membersType: membersType,
vis: vis,
ri: ri,
}
}
func (n *newConversationHelper) findConversations(ctx context.Context,
membersType chat1.ConversationMembersType, topicName string, useLocalData bool) ([]chat1.ConversationLocal, error) {
onechatpertlf := true
return FindConversations(ctx, n.G(), n.DebugLabeler, useLocalData, n.ri, n.uid, n.tlfName, n.topicType,
membersType, n.vis, topicName, &onechatpertlf)
}
func (n *newConversationHelper) findExisting(ctx context.Context, topicName string, useLocalData bool) (res []chat1.ConversationLocal, err error) {
// proceed to findConversations for requested member type
return n.findConversations(ctx, n.membersType, topicName, useLocalData)
}
func (n *newConversationHelper) create(ctx context.Context) (res chat1.ConversationLocal, reserr error) {
defer n.Trace(ctx, func() error { return reserr }, "newConversationHelper")()
// Handle a nil topic name with default values for the members type specified
if n.topicName == nil {
// We never want a blank topic name in team chats, always default to the default team name
switch n.membersType {
case chat1.ConversationMembersType_TEAM:
n.topicName = &globals.DefaultTeamTopic
}
}
var findConvsTopicName string
if n.topicName != nil {
findConvsTopicName = *n.topicName
}
// Find any existing conversations that match this argument specifically. We need to do this check
// here in the client since we can't see the topic name on the server.
// NOTE: The CLI already does this. It is hard to move that code completely into the service, since
// there is a ton of logic in there to try and present a nice looking menu to help out the
// user and such. For the most part, the CLI just uses FindConversationsLocal though, so it
// should hopefully just result in a bunch of cache hits on the second invocation.
convs, err := n.findExisting(ctx, findConvsTopicName, true)
// If we find one conversation, then just return it as if we created it.
if len(convs) == 1 {
n.Debug(ctx, "found previous conversation that matches, returning")
return convs[0], err
}
if n.G().ExternalG().Env.GetChatMemberType() == "impteam" {
// if KBFS, return an error. Need to use IMPTEAM now.
if n.membersType == chat1.ConversationMembersType_KBFS {
// let it slide in devel for tests
if n.G().ExternalG().Env.GetRunMode() != libkb.DevelRunMode {
n.Debug(ctx, "KBFS conversations deprecated; switching membersType from KBFS to IMPTEAM")
n.membersType = chat1.ConversationMembersType_IMPTEAMNATIVE
}
}
}
n.Debug(ctx, "no matching previous conversation, proceeding to create new conv")
isPublic := n.vis == keybase1.TLFVisibility_PUBLIC
nis := CreateNameInfoSource(ctx, n.G(), n.membersType)
info, err := nis.LookupID(ctx, n.tlfName, isPublic)
if err != nil {
if n.membersType != chat1.ConversationMembersType_IMPTEAMNATIVE {
return res, err
}
if _, ok := err.(UnknownTLFNameError); !ok {
return res, err
}
// couldn't find implicit team, so make one
n.Debug(ctx, "making new implicit team %q", n.tlfName)
_, _, _, err = teams.LookupOrCreateImplicitTeam(ctx, n.G().ExternalG(), n.tlfName, isPublic)
if err != nil {
return res, err
}
info, err = nis.LookupID(ctx, n.tlfName, isPublic)
if err != nil {
return res, err
}
}
triple := chat1.ConversationIDTriple{
Tlfid: info.ID,
TopicType: n.topicType,
TopicID: make(chat1.TopicID, 16),
}
for i := 0; i < 5; i++ {
triple.TopicID, err = utils.NewChatTopicID()
if err != nil {
return res, fmt.Errorf("error creating topic ID: %s", err)
}
n.Debug(ctx, "attempt: %v [tlfID: %s topicType: %d topicID: %s name: %s public: %v mt: %v]",
i, triple.Tlfid, triple.TopicType, triple.TopicID, info.CanonicalName, isPublic,
n.membersType)
firstMessageBoxed, topicNameState, err := n.makeFirstMessage(ctx, triple, info.CanonicalName,
n.membersType, n.vis, n.topicName)
if err != nil {
// Check for DuplicateTopicNameError and run findExisting again to try and find it
switch err.(type) {
case DuplicateTopicNameError:
n.Debug(ctx, "duplicate topic name encountered, attempting to findExisting again")
var findErr error
convs, findErr = n.findExisting(ctx, findConvsTopicName, false)
if len(convs) == 1 {
n.Debug(ctx, "found previous conversation that matches, returning")
return convs[0], findErr
}
n.Debug(ctx, "failed to find previous conversation on second attempt: len(convs): %d err: %s",
len(convs), findErr)
}
return res, err
}
var ncrres chat1.NewConversationRemoteRes
ncrres, reserr = n.ri().NewConversationRemote2(ctx, chat1.NewConversationRemote2Arg{
IdTriple: triple,
TLFMessage: *firstMessageBoxed,
MembersType: n.membersType,
TopicNameState: topicNameState,
})
convID := ncrres.ConvID
if reserr != nil {
switch cerr := reserr.(type) {
case libkb.ChatStalePreviousStateError:
n.Debug(ctx, "stale topic name state, trying again")
continue
case libkb.ChatConvExistsError:
// This triple already exists.
n.Debug(ctx, "conv exists: %v", cerr.ConvID)
if triple.TopicType != chat1.TopicType_CHAT ||
n.membersType == chat1.ConversationMembersType_TEAM {
// Not a chat (or is a team) conversation. Multiples are fine. Just retry with a
// different topic ID.
continue
}
// A chat conversation already exists; just reuse it.
// Note that from this point on, TopicID is entirely the wrong value.
convID = cerr.ConvID
case libkb.ChatCollisionError:
// The triple did not exist, but a collision occurred on convID. Retry with a different topic ID.
n.Debug(ctx, "collision: %v", reserr)
continue
default:
return res, fmt.Errorf("error creating conversation: %s", reserr)
}
}
n.Debug(ctx, "established conv: %v", convID)
// create succeeded; grabbing the conversation and returning
ib, err := n.G().InboxSource.Read(ctx, n.uid, nil, false,
&chat1.GetInboxLocalQuery{
ConvIDs: []chat1.ConversationID{convID},
}, nil)
if err != nil {
return res, err