forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convsource.go
1280 lines (1156 loc) · 45.3 KB
/
convsource.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 (
"errors"
"fmt"
"sort"
"time"
"github.com/keybase/client/go/chat/attachments"
"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"
context "golang.org/x/net/context"
)
type baseConversationSource struct {
globals.Contextified
utils.DebugLabeler
boxer *Boxer
ri func() chat1.RemoteInterface
blackoutPullForTesting bool
}
func newBaseConversationSource(g *globals.Context, ri func() chat1.RemoteInterface, boxer *Boxer) *baseConversationSource {
labeler := utils.NewDebugLabeler(g.ExternalG(), "baseConversationSource", false)
return &baseConversationSource{
Contextified: globals.NewContextified(g),
DebugLabeler: labeler,
ri: ri,
boxer: boxer,
}
}
func (s *baseConversationSource) SetRemoteInterface(ri func() chat1.RemoteInterface) {
s.ri = ri
}
// Sign implements github.com/keybase/go/chat/s3.Signer interface.
func (s *baseConversationSource) Sign(payload []byte) ([]byte, error) {
arg := chat1.S3SignArg{
Payload: payload,
Version: 1,
}
return s.ri().S3Sign(context.Background(), arg)
}
// DeleteAssets implements github.com/keybase/go/chat/storage/storage.AssetDeleter interface.
func (s *baseConversationSource) DeleteAssets(ctx context.Context, uid gregor1.UID,
convID chat1.ConversationID, assets []chat1.Asset) {
if len(assets) == 0 {
return
}
s.Debug(ctx, "DeleteAssets: deleting %d assets", len(assets))
// Fire off a background load of the thread with a post hook to delete the bodies cache
err := s.G().ConvLoader.Queue(ctx, types.NewConvLoaderJob(convID, &chat1.Pagination{Num: 0},
types.ConvLoaderPriorityHighest, types.ConvLoaderUnique,
func(ctx context.Context, tv chat1.ThreadView, job types.ConvLoaderJob) {
fetcher := s.G().AttachmentURLSrv.GetAttachmentFetcher()
if err := fetcher.DeleteAssets(ctx, convID, assets, s.ri, s); err != nil {
s.Debug(ctx, "DeleteAssets: Error purging ephemeral attachments %v", err)
}
}))
if err != nil {
s.Debug(ctx, "DeleteAssets: Error queuing conv job: %+v", err)
}
}
func (s *baseConversationSource) addPendingPreviews(ctx context.Context, thread *chat1.ThreadView) {
for index, m := range thread.Messages {
if !m.IsOutbox() {
continue
}
obr := m.Outbox()
if err := attachments.AddPendingPreview(ctx, s.G(), &obr); err != nil {
s.Debug(ctx, "addPendingPreviews: failed to get pending preview: outboxID: %s err: %s",
obr.OutboxID, err)
continue
}
thread.Messages[index] = chat1.NewMessageUnboxedWithOutbox(obr)
}
}
func (s *baseConversationSource) addConversationCards(ctx context.Context, uid gregor1.UID, reason chat1.GetThreadReason,
convID chat1.ConversationID, convOptional *chat1.ConversationLocal, thread *chat1.ThreadView) {
ctxShort, ctxShortCancel := context.WithTimeout(ctx, 2*time.Second)
defer ctxShortCancel()
if journeycardShouldNotRunOnReason[reason] {
s.Debug(ctx, "addConversationCards: skipping due to reason: %v", reason)
return
}
card, err := s.G().JourneyCardManager.PickCard(ctxShort, uid, convID, convOptional, thread)
ctxShortCancel()
if err != nil {
s.Debug(ctx, "addConversationCards: error getting next conversation card: %s", err)
return
}
if card == nil {
return
}
// Slot it in to the left of its prev.
addLeftOf := 0
for i := len(thread.Messages) - 1; i >= 0; i-- {
msgID := thread.Messages[i].GetMessageID()
if msgID != 0 && msgID >= card.PrevID {
addLeftOf = i
break
}
}
// Insert at index: https://github.com/golang/go/wiki/SliceTricks#insert
thread.Messages = append(thread.Messages, chat1.MessageUnboxed{})
copy(thread.Messages[addLeftOf+1:], thread.Messages[addLeftOf:])
thread.Messages[addLeftOf] = chat1.NewMessageUnboxedWithJourneycard(*card)
}
func (s *baseConversationSource) getRi(customRi func() chat1.RemoteInterface) chat1.RemoteInterface {
if customRi != nil {
return customRi()
}
return s.ri()
}
func (s *baseConversationSource) postProcessThread(ctx context.Context, uid gregor1.UID, reason chat1.GetThreadReason,
conv types.UnboxConversationInfo, thread *chat1.ThreadView, q *chat1.GetThreadQuery,
superXform types.SupersedesTransform, replyFiller types.ReplyFiller, checkPrev bool,
patchPagination bool, verifiedConv *chat1.ConversationLocal) (err error) {
if q != nil && q.DisablePostProcessThread {
return nil
}
s.Debug(ctx, "postProcessThread: thread messages starting out: %d", len(thread.Messages))
// Sanity check the prev pointers in this thread.
// TODO: We'll do this against what's in the cache once that's ready,
// rather than only checking the messages we just fetched against
// each other.
if s.blackoutPullForTesting {
thread.Messages = nil
return nil
}
if checkPrev {
_, _, err = CheckPrevPointersAndGetUnpreved(thread)
if err != nil {
return err
}
}
if patchPagination {
// Can mutate thread.Pagination.
s.patchPaginationLast(ctx, conv, uid, thread.Pagination, thread.Messages)
}
// Resolve supersedes & replies
deletedUpTo := conv.GetMaxDeletedUpTo()
if thread.Messages, err = s.TransformSupersedes(ctx, conv.GetConvID(), uid, thread.Messages, q, superXform,
replyFiller, &deletedUpTo); err != nil {
return err
}
s.Debug(ctx, "postProcessThread: thread messages after supersedes: %d", len(thread.Messages))
// Run type filter if it exists
thread.Messages = utils.FilterByType(thread.Messages, q, true)
s.Debug(ctx, "postProcessThread: thread messages after type filter: %d", len(thread.Messages))
// If we have exploded any messages while fetching them from cache, remove
// them now.
thread.Messages = utils.FilterExploded(conv, thread.Messages, s.boxer.clock.Now())
s.Debug(ctx, "postProcessThread: thread messages after explode filter: %d", len(thread.Messages))
// Add any conversation cards
s.addConversationCards(ctx, uid, reason, conv.GetConvID(), verifiedConv, thread)
// Fetch outbox and tack onto the result
outbox := storage.NewOutbox(s.G(), uid)
err = outbox.AppendToThread(ctx, conv.GetConvID(), thread)
switch err.(type) {
case nil, storage.MissError:
default:
return err
}
// Add attachment previews to pending messages
s.addPendingPreviews(ctx, thread)
return nil
}
func (s *baseConversationSource) TransformSupersedes(ctx context.Context,
convID chat1.ConversationID, uid gregor1.UID, msgs []chat1.MessageUnboxed,
q *chat1.GetThreadQuery, superXform types.SupersedesTransform, replyFiller types.ReplyFiller,
maxDeletedUpTo *chat1.MessageID) (res []chat1.MessageUnboxed, err error) {
defer s.Trace(ctx, &err, "TransformSupersedes")()
if q == nil || !q.DisableResolveSupersedes {
deletePlaceholders := q != nil && q.EnableDeletePlaceholders
if superXform == nil {
superXform = newBasicSupersedesTransform(s.G(), basicSupersedesTransformOpts{
UseDeletePlaceholders: deletePlaceholders,
})
}
if res, err = superXform.Run(ctx, convID, uid, msgs, maxDeletedUpTo); err != nil {
return nil, err
}
} else {
res = msgs
}
if replyFiller == nil {
replyFiller = NewReplyFiller(s.G())
}
return replyFiller.Fill(ctx, uid, convID, res)
}
// patchPaginationLast turns on page.Last if the messages are before InboxSource's view of Expunge.
func (s *baseConversationSource) patchPaginationLast(ctx context.Context, conv types.UnboxConversationInfo, uid gregor1.UID,
page *chat1.Pagination, msgs []chat1.MessageUnboxed) {
if page == nil || page.Last {
return
}
if len(msgs) == 0 {
s.Debug(ctx, "patchPaginationLast: true - no msgs")
page.Last = true
return
}
expunge := conv.GetExpunge()
if expunge == nil {
s.Debug(ctx, "patchPaginationLast: no expunge info")
return
}
end1 := msgs[0].GetMessageID()
end2 := msgs[len(msgs)-1].GetMessageID()
if end1.Min(end2) <= expunge.Upto {
s.Debug(ctx, "patchPaginationLast: true - hit upto")
// If any message is prior to the nukepoint, say this is the last page.
page.Last = true
}
}
func (s *baseConversationSource) GetMessage(ctx context.Context, convID chat1.ConversationID,
uid gregor1.UID, msgID chat1.MessageID, reason *chat1.GetThreadReason, ri func() chat1.RemoteInterface,
resolveSupersedes bool) (chat1.MessageUnboxed, error) {
msgs, err := s.G().ConvSource.GetMessages(ctx, convID, uid, []chat1.MessageID{msgID},
reason, s.ri, resolveSupersedes)
if err != nil {
return chat1.MessageUnboxed{}, err
}
if len(msgs) != 1 {
return chat1.MessageUnboxed{}, errors.New("message not found")
}
return msgs[0], nil
}
func (s *baseConversationSource) PullFull(ctx context.Context, convID chat1.ConversationID, uid gregor1.UID, reason chat1.GetThreadReason,
query *chat1.GetThreadQuery, maxPages *int) (res chat1.ThreadView, err error) {
ctx = libkb.WithLogTag(ctx, "PUL")
pagination := &chat1.Pagination{
Num: 300,
}
if maxPages == nil {
defaultMaxPages := 10000
maxPages = &defaultMaxPages
}
for i := 0; !pagination.Last && i < *maxPages; i++ {
thread, err := s.G().ConvSource.Pull(ctx, convID, uid, reason, nil, query, pagination)
if err != nil {
return res, err
}
res.Messages = append(res.Messages, thread.Messages...)
pagination.Next = thread.Pagination.Next
pagination.Last = thread.Pagination.Last
}
return res, nil
}
func (s *baseConversationSource) getUnreadlineRemote(ctx context.Context, convID chat1.ConversationID,
readMsgID chat1.MessageID) (*chat1.MessageID, error) {
res, err := s.ri().GetUnreadlineRemote(ctx, chat1.GetUnreadlineRemoteArg{
ConvID: convID,
ReadMsgID: readMsgID,
})
if err != nil {
return nil, err
}
return res.UnreadlineID, nil
}
type RemoteConversationSource struct {
globals.Contextified
*baseConversationSource
}
var _ types.ConversationSource = (*RemoteConversationSource)(nil)
func NewRemoteConversationSource(g *globals.Context, b *Boxer, ri func() chat1.RemoteInterface) *RemoteConversationSource {
return &RemoteConversationSource{
Contextified: globals.NewContextified(g),
baseConversationSource: newBaseConversationSource(g, ri, b),
}
}
func (s *RemoteConversationSource) AcquireConversationLock(ctx context.Context, uid gregor1.UID,
convID chat1.ConversationID) error {
return nil
}
func (s *RemoteConversationSource) ReleaseConversationLock(ctx context.Context, uid gregor1.UID,
convID chat1.ConversationID) {
}
func (s *RemoteConversationSource) Push(ctx context.Context, convID chat1.ConversationID,
uid gregor1.UID, msg chat1.MessageBoxed) (chat1.MessageUnboxed, bool, error) {
// Do nothing here, we don't care about pushed messages
// The bool param here is to indicate the update given is continuous to our current state,
// which for this source is not relevant, so we just return true
return chat1.MessageUnboxed{}, true, nil
}
func (s *RemoteConversationSource) PushUnboxed(ctx context.Context, conv types.UnboxConversationInfo,
uid gregor1.UID, msg []chat1.MessageUnboxed) error {
return nil
}
func (s *RemoteConversationSource) Pull(ctx context.Context, convID chat1.ConversationID,
uid gregor1.UID, reason chat1.GetThreadReason, customRi func() chat1.RemoteInterface,
query *chat1.GetThreadQuery, pagination *chat1.Pagination) (chat1.ThreadView, error) {
ctx = libkb.WithLogTag(ctx, "PUL")
if convID.IsNil() {
return chat1.ThreadView{}, errors.New("RemoteConversationSource.Pull called with empty convID")
}
// Get conversation metadata
conv, err := utils.GetVerifiedConv(ctx, s.G(), uid, convID, types.InboxSourceDataSourceAll)
if err != nil {
return chat1.ThreadView{}, err
}
// Fetch thread
rarg := chat1.GetThreadRemoteArg{
ConversationID: convID,
Query: query,
Pagination: pagination,
Reason: reason,
}
boxed, err := s.getRi(customRi).GetThreadRemote(ctx, rarg)
if err != nil {
return chat1.ThreadView{}, err
}
thread, err := s.boxer.UnboxThread(ctx, boxed.Thread, conv)
if err != nil {
return chat1.ThreadView{}, err
}
// Post process thread before returning
if err = s.postProcessThread(ctx, uid, reason, conv, &thread, query, nil, nil, true, false, &conv); err != nil {
return chat1.ThreadView{}, err
}
return thread, nil
}
func (s *RemoteConversationSource) PullLocalOnly(ctx context.Context, convID chat1.ConversationID,
uid gregor1.UID, reason chat1.GetThreadReason, query *chat1.GetThreadQuery, pagination *chat1.Pagination, maxPlaceholders int) (chat1.ThreadView, error) {
return chat1.ThreadView{}, storage.MissError{Msg: "PullLocalOnly is unimplemented for RemoteConversationSource"}
}
func (s *RemoteConversationSource) Clear(ctx context.Context, convID chat1.ConversationID, uid gregor1.UID, opts *types.ClearOpts) error {
return nil
}
func (s *RemoteConversationSource) GetMessages(ctx context.Context, convID chat1.ConversationID,
uid gregor1.UID, msgIDs []chat1.MessageID, threadReason *chat1.GetThreadReason,
customRi func() chat1.RemoteInterface, resolveSupersedes bool) (res []chat1.MessageUnboxed, err error) {
defer func() {
// unless arg says not to, transform the superseded messages
if !resolveSupersedes {
return
}
res, err = s.TransformSupersedes(ctx, convID, uid, res, nil, nil, nil, nil)
}()
rres, err := s.ri().GetMessagesRemote(ctx, chat1.GetMessagesRemoteArg{
ConversationID: convID,
MessageIDs: msgIDs,
ThreadReason: threadReason,
})
if err != nil {
return nil, err
}
conv := newBasicUnboxConversationInfo(convID, rres.MembersType, nil, rres.Visibility)
msgs, err := s.boxer.UnboxMessages(ctx, rres.Msgs, conv)
if err != nil {
return nil, err
}
return msgs, nil
}
func (s *RemoteConversationSource) GetMessagesWithRemotes(ctx context.Context,
conv chat1.Conversation, uid gregor1.UID, msgs []chat1.MessageBoxed) ([]chat1.MessageUnboxed, error) {
return s.boxer.UnboxMessages(ctx, msgs, conv)
}
func (s *RemoteConversationSource) GetUnreadline(ctx context.Context,
convID chat1.ConversationID, uid gregor1.UID, readMsgID chat1.MessageID) (*chat1.MessageID, error) {
return s.getUnreadlineRemote(ctx, convID, readMsgID)
}
func (s *RemoteConversationSource) Expunge(ctx context.Context,
conv types.UnboxConversationInfo, uid gregor1.UID, expunge chat1.Expunge) error {
return nil
}
func (s *RemoteConversationSource) EphemeralPurge(ctx context.Context, convID chat1.ConversationID,
uid gregor1.UID, purgeInfo *chat1.EphemeralPurgeInfo) (*chat1.EphemeralPurgeInfo, []chat1.MessageUnboxed, error) {
return nil, nil, nil
}
type HybridConversationSource struct {
globals.Contextified
utils.DebugLabeler
*baseConversationSource
numExpungeReload int
storage *storage.Storage
lockTab *utils.ConversationLockTab
}
var _ types.ConversationSource = (*HybridConversationSource)(nil)
func NewHybridConversationSource(g *globals.Context, b *Boxer, storage *storage.Storage,
ri func() chat1.RemoteInterface) *HybridConversationSource {
return &HybridConversationSource{
Contextified: globals.NewContextified(g),
DebugLabeler: utils.NewDebugLabeler(g.ExternalG(), "HybridConversationSource", false),
baseConversationSource: newBaseConversationSource(g, ri, b),
storage: storage,
lockTab: utils.NewConversationLockTab(g),
numExpungeReload: 50,
}
}
func (s *HybridConversationSource) AcquireConversationLock(ctx context.Context, uid gregor1.UID,
convID chat1.ConversationID) error {
_, err := s.lockTab.Acquire(ctx, uid, convID)
return err
}
func (s *HybridConversationSource) ReleaseConversationLock(ctx context.Context, uid gregor1.UID,
convID chat1.ConversationID) {
s.lockTab.Release(ctx, uid, convID)
}
func (s *HybridConversationSource) isContinuousPush(ctx context.Context, convID chat1.ConversationID,
uid gregor1.UID, msgID chat1.MessageID) (continuousUpdate bool, err error) {
maxMsgID, err := s.storage.GetMaxMsgID(ctx, convID, uid)
switch err.(type) {
case storage.MissError:
continuousUpdate = true
case nil:
continuousUpdate = maxMsgID >= msgID-1
default:
return false, err
}
return continuousUpdate, nil
}
// completeAttachmentUpload removes any attachment previews from pending preview storage
func (s *HybridConversationSource) completeAttachmentUpload(ctx context.Context, msg chat1.MessageUnboxed) {
if msg.GetMessageType() == chat1.MessageType_ATTACHMENT {
outboxID := msg.OutboxID()
if outboxID != nil {
s.G().AttachmentUploader.Complete(ctx, *outboxID)
}
}
}
func (s *HybridConversationSource) completeUnfurl(ctx context.Context, msg chat1.MessageUnboxed) {
if msg.GetMessageType() == chat1.MessageType_UNFURL {
outboxID := msg.OutboxID()
if outboxID != nil {
s.G().Unfurler.Complete(ctx, *outboxID)
}
}
}
func (s *HybridConversationSource) maybeNuke(ctx context.Context, convID chat1.ConversationID, uid gregor1.UID, err *error) {
if err != nil && utils.IsDeletedConvError(*err) {
s.Debug(ctx, "purging caches on: %v for convID: %v, uid: %v", *err, convID, uid)
if ierr := s.Clear(ctx, convID, uid, &types.ClearOpts{
SendLocalAdminNotification: true,
Reason: "Got unexpected conversation deleted error. Cleared conv and inbox cache",
}); ierr != nil {
s.Debug(ctx, "unable to Clear conv: %v", err)
}
if ierr := s.G().InboxSource.Clear(ctx, uid, nil); ierr != nil {
s.Debug(ctx, "unable to Clear inbox: %v", ierr)
}
s.G().UIInboxLoader.UpdateLayout(ctx, chat1.InboxLayoutReselectMode_DEFAULT, "ConvSource#maybeNuke")
*err = nil
}
}
func (s *HybridConversationSource) Push(ctx context.Context, convID chat1.ConversationID,
uid gregor1.UID, msg chat1.MessageBoxed) (decmsg chat1.MessageUnboxed, continuousUpdate bool, err error) {
defer s.Trace(ctx, &err, "Push")()
if _, err = s.lockTab.Acquire(ctx, uid, convID); err != nil {
return decmsg, continuousUpdate, err
}
defer s.lockTab.Release(ctx, uid, convID)
defer s.maybeNuke(ctx, convID, uid, &err)
// Grab conversation information before pushing
conv, err := utils.GetUnverifiedConv(ctx, s.G(), uid, convID, types.InboxSourceDataSourceAll)
if err != nil {
return decmsg, continuousUpdate, err
}
// Check to see if we are "appending" this message to the current record.
if continuousUpdate, err = s.isContinuousPush(ctx, convID, uid, msg.GetMessageID()); err != nil {
return decmsg, continuousUpdate, err
}
decmsg, err = s.boxer.UnboxMessage(ctx, msg, conv, nil)
if err != nil {
return decmsg, continuousUpdate, err
}
// Check conversation ID and change to error if it is wrong
if decmsg.IsValid() && !decmsg.Valid().ClientHeader.Conv.Derivable(convID) {
s.Debug(ctx, "invalid conversation ID detected, not derivable: %s", convID)
decmsg = chat1.NewMessageUnboxedWithError(chat1.MessageUnboxedError{
ErrMsg: "invalid conversation ID",
MessageID: msg.GetMessageID(),
MessageType: msg.GetMessageType(),
})
}
// Add to the local storage
if err = s.mergeMaybeNotify(ctx, conv, uid, []chat1.MessageUnboxed{decmsg}, chat1.GetThreadReason_GENERAL); err != nil {
return decmsg, continuousUpdate, err
}
if msg.ClientHeader.Sender.Eq(uid) && conv.GetMembersType() == chat1.ConversationMembersType_TEAM {
teamID, err := keybase1.TeamIDFromString(conv.Conv.Metadata.IdTriple.Tlfid.String())
if err != nil {
s.Debug(ctx, "Push: failed to get team ID: %v", err)
} else {
go s.G().JourneyCardManager.SentMessage(globals.BackgroundChatCtx(ctx, s.G()), uid, teamID, convID)
}
}
// Remove any pending previews from storage
s.completeAttachmentUpload(ctx, decmsg)
// complete any active unfurl
s.completeUnfurl(ctx, decmsg)
return decmsg, continuousUpdate, nil
}
func (s *HybridConversationSource) PushUnboxed(ctx context.Context, conv types.UnboxConversationInfo,
uid gregor1.UID, msgs []chat1.MessageUnboxed) (err error) {
defer s.Trace(ctx, &err, "PushUnboxed")()
convID := conv.GetConvID()
if _, err = s.lockTab.Acquire(ctx, uid, convID); err != nil {
return err
}
defer s.lockTab.Release(ctx, uid, convID)
defer s.maybeNuke(ctx, convID, uid, &err)
// sanity check against conv ID
for _, msg := range msgs {
if msg.IsValid() && !msg.Valid().ClientHeader.Conv.Derivable(convID) {
s.Debug(ctx, "PushUnboxed: pushing an unboxed message from wrong conv: correct: %s trip: %+v id: %d",
convID, msg.Valid().ClientHeader.Conv, msg.GetMessageID())
return errors.New("cannot push into a different conversation")
}
}
if err = s.mergeMaybeNotify(ctx, conv, uid, msgs, chat1.GetThreadReason_PUSH); err != nil {
return err
}
return nil
}
func (s *HybridConversationSource) resolveHoles(ctx context.Context, uid gregor1.UID,
thread *chat1.ThreadView, conv chat1.Conversation, reason chat1.GetThreadReason,
customRi func() chat1.RemoteInterface) (err error) {
defer s.Trace(ctx, &err, "resolveHoles")()
var msgIDs []chat1.MessageID
// Gather all placeholder messages so we can go fetch them
for index, msg := range thread.Messages {
if msg.IsPlaceholder() {
if index == len(thread.Messages)-1 {
// If the last message is a hole, we might not have fetched everything,
// so fail this case like a normal miss
return storage.MissError{}
}
msgIDs = append(msgIDs, msg.GetMessageID())
}
}
if len(msgIDs) == 0 {
// Nothing to do
return nil
}
// Fetch all missing messages from server, and sub in the real ones into the placeholder slots
msgs, err := s.GetMessages(ctx, conv.GetConvID(), uid, msgIDs, &reason, customRi, false)
if err != nil {
s.Debug(ctx, "resolveHoles: failed to get missing messages: %s", err.Error())
return err
}
s.Debug(ctx, "resolveHoles: success: filled %d holes", len(msgs))
msgMap := make(map[chat1.MessageID]chat1.MessageUnboxed)
for _, msg := range msgs {
msgMap[msg.GetMessageID()] = msg
}
for index, msg := range thread.Messages {
if msg.IsPlaceholder() {
newMsg, ok := msgMap[msg.GetMessageID()]
if !ok {
return fmt.Errorf("failed to find hole resolution: %v", msg.GetMessageID())
}
thread.Messages[index] = newMsg
}
}
return nil
}
func (s *HybridConversationSource) getConvForPull(ctx context.Context, uid gregor1.UID,
convID chat1.ConversationID) (res types.RemoteConversation, err error) {
rconv, err := utils.GetUnverifiedConv(ctx, s.G(), uid, convID, types.InboxSourceDataSourceAll)
if err != nil {
return res, err
}
if !rconv.Conv.HasMemberStatus(chat1.ConversationMemberStatus_NEVER_JOINED) {
return rconv, nil
}
s.Debug(ctx, "getConvForPull: in conversation with never joined, getting conv from remote")
return utils.GetUnverifiedConv(ctx, s.G(), uid, convID, types.InboxSourceDataSourceRemoteOnly)
}
// maxHolesForPull is the number of misses in the body storage cache we will tolerate missing. A good
// way to think about this number is the number of extra reads from the cache we need to do before
// formally declaring the request a failure.
var maxHolesForPull = 50
func (s *HybridConversationSource) Pull(ctx context.Context, convID chat1.ConversationID,
uid gregor1.UID, reason chat1.GetThreadReason, customRi func() chat1.RemoteInterface,
query *chat1.GetThreadQuery, pagination *chat1.Pagination) (thread chat1.ThreadView, err error) {
ctx = libkb.WithLogTag(ctx, "PUL")
defer s.Trace(ctx, &err, "Pull(%s)", convID)()
if convID.IsNil() {
return chat1.ThreadView{}, errors.New("HybridConversationSource.Pull called with empty convID")
}
if _, err = s.lockTab.Acquire(ctx, uid, convID); err != nil {
return thread, err
}
defer s.lockTab.Release(ctx, uid, convID)
defer s.maybeNuke(ctx, convID, uid, &err)
// Get conversation metadata
rconv, err := s.getConvForPull(ctx, uid, convID)
var unboxConv types.UnboxConversationInfo
if err == nil && !rconv.Conv.HasMemberStatus(chat1.ConversationMemberStatus_NEVER_JOINED) {
conv := rconv.Conv
unboxConv = conv
// Try locally first
rc := storage.NewHoleyResultCollector(maxHolesForPull,
s.storage.ResultCollectorFromQuery(ctx, query, pagination))
thread, err = s.fetchMaybeNotify(ctx, conv.GetConvID(), uid, rc, conv.ReaderInfo.MaxMsgid,
query, pagination)
if err == nil {
// Since we are using the "holey" collector, we need to resolve any placeholder
// messages that may have been fetched.
s.Debug(ctx, "Pull: (holey) cache hit: convID: %s uid: %s holes: %d msgs: %d",
unboxConv.GetConvID(), uid, rc.Holes(), len(thread.Messages))
err = s.resolveHoles(ctx, uid, &thread, conv, reason, customRi)
}
if err == nil {
// Before returning the stuff, send remote request to mark as read if
// requested.
if query != nil && query.MarkAsRead && len(thread.Messages) > 0 {
readMsgID := thread.Messages[0].GetMessageID()
if err = s.G().InboxSource.MarkAsRead(ctx, convID, uid, &readMsgID); err != nil {
return chat1.ThreadView{}, err
}
if _, err = s.G().InboxSource.ReadMessage(ctx, uid, 0, convID, readMsgID); err != nil {
return chat1.ThreadView{}, err
}
} else {
s.Debug(ctx, "Pull: skipping mark as read call")
}
// Run post process stuff
vconv, err := utils.GetVerifiedConv(ctx, s.G(), uid, convID, types.InboxSourceDataSourceAll)
if err == nil {
if err = s.postProcessThread(ctx, uid, reason, conv, &thread, query, nil, nil, true, true, &vconv); err != nil {
return thread, err
}
}
return thread, nil
}
s.Debug(ctx, "Pull: cache miss: err: %s", err)
} else {
s.Debug(ctx, "Pull: error fetching conv metadata: convID: %s uid: %s err: %s", convID, uid, err)
}
// Fetch the entire request on failure
rarg := chat1.GetThreadRemoteArg{
ConversationID: convID,
Query: query,
Pagination: pagination,
Reason: reason,
}
boxed, err := s.getRi(customRi).GetThreadRemote(ctx, rarg)
if err != nil {
return chat1.ThreadView{}, err
}
// Set up public inbox info if we don't have one with members type from remote call. Assume this is a
// public chat here, since it is the only chance we have to unbox it.
if unboxConv == nil {
unboxConv = newExtraInboxUnboxConverstionInfo(convID, boxed.MembersType, boxed.Visibility)
}
// Unbox
thread, err = s.boxer.UnboxThread(ctx, boxed.Thread, unboxConv)
if err != nil {
return chat1.ThreadView{}, err
}
// Store locally (just warn on error, don't abort the whole thing)
if err = s.mergeMaybeNotify(ctx, unboxConv, uid, thread.Messages, reason); err != nil {
s.Debug(ctx, "Pull: unable to commit thread locally: convID: %s uid: %s", convID, uid)
}
// Run post process stuff
if err = s.postProcessThread(ctx, uid, reason, unboxConv, &thread, query, nil, nil, true, true, nil); err != nil {
return thread, err
}
return thread, nil
}
type pullLocalResultCollector struct {
storage.ResultCollector
}
func (p *pullLocalResultCollector) Name() string {
return "pulllocal"
}
func (p *pullLocalResultCollector) String() string {
return fmt.Sprintf("[ %s: base: %s ]", p.Name(), p.ResultCollector)
}
func (p *pullLocalResultCollector) hasRealResults() bool {
for _, m := range p.Result() {
st, err := m.State()
if err != nil {
// count these
return true
}
switch st {
case chat1.MessageUnboxedState_PLACEHOLDER:
// don't count!
default:
return true
}
}
return false
}
func (p *pullLocalResultCollector) Error(err storage.Error) storage.Error {
// Swallow this error, we know we can miss if we get anything at all
if _, ok := err.(storage.MissError); ok && p.hasRealResults() {
return nil
}
return err
}
func newPullLocalResultCollector(baseRC storage.ResultCollector) *pullLocalResultCollector {
return &pullLocalResultCollector{
ResultCollector: baseRC,
}
}
func (s *HybridConversationSource) PullLocalOnly(ctx context.Context, convID chat1.ConversationID,
uid gregor1.UID, reason chat1.GetThreadReason, query *chat1.GetThreadQuery, pagination *chat1.Pagination, maxPlaceholders int) (tv chat1.ThreadView, err error) {
ctx = libkb.WithLogTag(ctx, "PUL")
defer s.Trace(ctx, &err, "PullLocalOnly")()
if _, err = s.lockTab.Acquire(ctx, uid, convID); err != nil {
return tv, err
}
defer s.lockTab.Release(ctx, uid, convID)
defer s.maybeNuke(ctx, convID, uid, &err)
// Post process thread before returning
defer func() {
if err == nil {
superXform := newBasicSupersedesTransform(s.G(), basicSupersedesTransformOpts{})
superXform.SetMessagesFunc(func(ctx context.Context, convID chat1.ConversationID,
uid gregor1.UID, msgIDs []chat1.MessageID,
_ *chat1.GetThreadReason, _ func() chat1.RemoteInterface, _ bool) (res []chat1.MessageUnboxed, err error) {
msgs, err := storage.New(s.G(), s).FetchMessages(ctx, convID, uid, msgIDs)
if err != nil {
return nil, err
}
for _, msg := range msgs {
if msg != nil {
res = append(res, *msg)
}
}
return res, nil
})
replyFiller := NewReplyFiller(s.G(), LocalOnlyReplyFill(true))
// Form a fake version of a conversation so we don't need to hit the network ever here
var conv chat1.Conversation
conv.Metadata.ConversationID = convID
err = s.postProcessThread(ctx, uid, reason, conv, &tv, query, superXform, replyFiller, false,
true, nil)
}
}()
// Fetch the inbox max message ID as well to compare against the local stored max messages
// if the caller is ok with receiving placeholders
var iboxMaxMsgID chat1.MessageID
if maxPlaceholders > 0 {
iboxRes, err := storage.NewInbox(s.G()).GetConversation(ctx, uid, convID)
if err != nil {
s.Debug(ctx, "PullLocalOnly: failed to read inbox for conv, not using: %s", err)
} else if iboxRes.Conv.ReaderInfo == nil {
s.Debug(ctx, "PullLocalOnly: no reader infoconv returned for conv, not using")
} else {
iboxMaxMsgID = iboxRes.Conv.ReaderInfo.MaxMsgid
s.Debug(ctx, "PullLocalOnly: found ibox max msgid: %d", iboxMaxMsgID)
}
}
// A number < 0 means it will fetch until it hits the end of the local copy. Our special
// result collector will suppress any miss errors
num := -1
if pagination != nil {
num = pagination.Num
}
baseRC := s.storage.ResultCollectorFromQuery(ctx, query, pagination)
baseRC.SetTarget(num)
rc := storage.NewHoleyResultCollector(maxPlaceholders, newPullLocalResultCollector(baseRC))
tv, err = s.fetchMaybeNotify(ctx, convID, uid, rc, iboxMaxMsgID, query, pagination)
if err != nil {
s.Debug(ctx, "PullLocalOnly: failed to fetch local messages with iboxMaxMsgID: %v: err %s, trying again with local max", iboxMaxMsgID, err)
tv, err = s.fetchMaybeNotify(ctx, convID, uid, rc, 0, query, pagination)
if err != nil {
s.Debug(ctx, "PullLocalOnly: failed to fetch local messages with local max: %s", err)
return chat1.ThreadView{}, err
}
}
return tv, nil
}
func (s *HybridConversationSource) Clear(ctx context.Context, convID chat1.ConversationID, uid gregor1.UID,
opts *types.ClearOpts) (err error) {
defer s.Trace(ctx, &err, "Clear(%v,%v)", uid, convID)()
defer s.PerfTrace(ctx, &err, "Clear(%v,%v)", uid, convID)()
start := time.Now()
defer func() {
var message string
if err == nil {
message = fmt.Sprintf("Clearing conv for %s", convID)
} else {
message = fmt.Sprintf("Failed to clear conv %s", convID)
}
s.G().RuntimeStats.PushPerfEvent(keybase1.PerfEvent{
EventType: keybase1.PerfEventType_CLEARCONV,
Message: message,
Ctime: keybase1.ToTime(start),
})
}()
kuid := keybase1.UID(uid.String())
if (s.G().Env.GetRunMode() == libkb.DevelRunMode || libkb.IsKeybaseAdmin(kuid)) &&
s.G().UIRouter != nil && opts != nil && opts.SendLocalAdminNotification {
ui, err := s.G().UIRouter.GetLogUI()
if err == nil && ui != nil {
ui.Critical("Clearing conv %s", opts.Reason)
}
}
epick := libkb.FirstErrorPicker{}
epick.Push(s.storage.ClearAll(ctx, convID, uid))
epick.Push(s.G().Indexer.Clear(ctx, uid, convID))
return epick.Error()
}
func (s *HybridConversationSource) GetMessages(ctx context.Context, convID chat1.ConversationID,
uid gregor1.UID, msgIDs []chat1.MessageID, threadReason *chat1.GetThreadReason,
customRi func() chat1.RemoteInterface, resolveSupersedes bool) (res []chat1.MessageUnboxed, err error) {
defer s.Trace(ctx, &err, "GetMessages: convID: %s msgIDs: %d",
convID, len(msgIDs))()
if _, err := s.lockTab.Acquire(ctx, uid, convID); err != nil {
return nil, err
}
defer s.lockTab.Release(ctx, uid, convID)
defer s.maybeNuke(ctx, convID, uid, &err)
defer func() {
// unless arg says not to, transform the superseded messages
if !resolveSupersedes {
return
}
res, err = s.TransformSupersedes(ctx, convID, uid, res, nil, nil, nil, nil)
}()
// Grab local messages
msgs, err := s.storage.FetchMessages(ctx, convID, uid, msgIDs)
if err != nil {
return nil, err
}
// Make a pass to determine which message IDs we need to grab remotely
var remoteMsgs []chat1.MessageID
for index, msg := range msgs {
if msg == nil {
remoteMsgs = append(remoteMsgs, msgIDs[index])
}
}
// Grab message from remote
rmsgsTab := make(map[chat1.MessageID]chat1.MessageUnboxed)
s.Debug(ctx, "GetMessages: convID: %s uid: %s total msgs: %d remote: %d", convID, uid, len(msgIDs),
len(remoteMsgs))
if len(remoteMsgs) > 0 {
rmsgs, err := s.getRi(customRi).GetMessagesRemote(ctx, chat1.GetMessagesRemoteArg{
ConversationID: convID,
MessageIDs: remoteMsgs,
ThreadReason: threadReason,
})
if err != nil {
return nil, err
}
// Unbox all the remote messages
conv := newBasicUnboxConversationInfo(convID, rmsgs.MembersType, nil, rmsgs.Visibility)
rmsgsUnboxed, err := s.boxer.UnboxMessages(ctx, rmsgs.Msgs, conv)
if err != nil {
return nil, err
}
sort.Sort(utils.ByMsgUnboxedMsgID(rmsgsUnboxed))
for _, rmsg := range rmsgsUnboxed {
rmsgsTab[rmsg.GetMessageID()] = rmsg
}
reason := chat1.GetThreadReason_GENERAL
if threadReason != nil {
reason = *threadReason
}
// Write out messages
if err := s.mergeMaybeNotify(ctx, conv, uid, rmsgsUnboxed, reason); err != nil {
return nil, err
}
// The localizer uses UnboxQuickMode for unboxing and storing messages. Because of this, if there
// is a message in the deep past used for something like a channel name, headline, or pin, then we
// will never actually cache it. Detect this case here and put a load of the messages onto the
// background loader so we can get these messages cached with the full checks on UnboxMessage.
if reason == chat1.GetThreadReason_LOCALIZE && globals.CtxUnboxMode(ctx) == types.UnboxModeQuick {
s.Debug(ctx, "GetMessages: convID: %s remoteMsgs: %d: cache miss on localizer mode with UnboxQuickMode, queuing job", convID, len(remoteMsgs))
// implement the load entirely in the post load hook since we only want to load those
// messages in remoteMsgs. We can do that by specifying a 0 length pagination object.
if err := s.G().ConvLoader.Queue(ctx, types.NewConvLoaderJob(convID, &chat1.Pagination{Num: 0},
types.ConvLoaderPriorityLowest, types.ConvLoaderUnique,
func(ctx context.Context, tv chat1.ThreadView, job types.ConvLoaderJob) {
reason := chat1.GetThreadReason_BACKGROUNDCONVLOAD
if _, err := s.G().ConvSource.GetMessages(ctx, convID, uid, remoteMsgs, &reason,
customRi, resolveSupersedes); err != nil {
s.Debug(ctx, "GetMessages: error loading UnboxQuickMode cache misses: ", err)
}
})); err != nil {
s.Debug(ctx, "GetMessages: error queuing conv loader job: %+v", err)
}
}
}
// Form final result
for index, msg := range msgs {
if msg != nil {
res = append(res, *msg)
} else {
res = append(res, rmsgsTab[msgIDs[index]])
}
}
return res, nil
}
func (s *HybridConversationSource) GetMessagesWithRemotes(ctx context.Context,
conv chat1.Conversation, uid gregor1.UID, msgs []chat1.MessageBoxed) (res []chat1.MessageUnboxed, err error) {
convID := conv.GetConvID()
if _, err := s.lockTab.Acquire(ctx, uid, convID); err != nil {
return nil, err
}