/
convsource.go
887 lines (762 loc) · 26.6 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
package chat
import (
"errors"
"fmt"
"sort"
"sync"
"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"
context "golang.org/x/net/context"
)
type baseConversationSource struct {
globals.Contextified
utils.DebugLabeler
*sourceOfflinable
boxer *Boxer
ri func() chat1.RemoteInterface
}
func newBaseConversationSource(g *globals.Context, ri func() chat1.RemoteInterface, boxer *Boxer) *baseConversationSource {
labeler := utils.NewDebugLabeler(g.GetLog(), "baseConversationSource", false)
return &baseConversationSource{
Contextified: globals.NewContextified(g),
DebugLabeler: labeler,
ri: ri,
boxer: boxer,
sourceOfflinable: newSourceOfflinable(labeler),
}
}
func (s *baseConversationSource) SetRemoteInterface(ri func() chat1.RemoteInterface) {
s.ri = ri
}
func (s *baseConversationSource) postProcessThread(ctx context.Context, uid gregor1.UID,
conv chat1.Conversation, thread *chat1.ThreadView, q *chat1.GetThreadQuery,
superXform supersedesTransform, checkPrev bool) (err error) {
// 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 checkPrev {
_, err = CheckPrevPointersAndGetUnpreved(thread)
if err != nil {
return err
}
}
// Resolve supersedes
if q == nil || !q.DisableResolveSupersedes {
if superXform == nil {
superXform = newBasicSupersedesTransform(s.G())
}
if thread.Messages, err = superXform.Run(ctx, conv, uid, thread.Messages); err != nil {
return err
}
}
// Run type filter if it exists
thread.Messages = utils.FilterByType(thread.Messages, q, true)
// Fetch outbox and tack onto the result
outbox := storage.NewOutbox(s.G(), uid)
if err = outbox.SprinkleIntoThread(ctx, conv.GetConvID(), thread); err != nil {
if _, ok := err.(storage.MissError); !ok {
return err
}
}
return nil
}
func (s *baseConversationSource) TransformSupersedes(ctx context.Context, conv chat1.Conversation, uid gregor1.UID, msgs []chat1.MessageUnboxed) ([]chat1.MessageUnboxed, error) {
transform := newBasicSupersedesTransform(s.G())
return transform.Run(ctx, conv, uid, msgs)
}
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) 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) Pull(ctx context.Context, convID chat1.ConversationID,
uid gregor1.UID, query *chat1.GetThreadQuery, pagination *chat1.Pagination) (chat1.ThreadView, []*chat1.RateLimit, error) {
if convID.IsNil() {
return chat1.ThreadView{}, []*chat1.RateLimit{}, errors.New("RemoteConversationSource.Pull called with empty convID")
}
// Insta fail if we are offline
if s.IsOffline(ctx) {
return chat1.ThreadView{}, []*chat1.RateLimit{}, OfflineError{}
}
var rl []*chat1.RateLimit
// Get conversation metadata
conv, ratelim, err := GetUnverifiedConv(ctx, s.G(), uid, convID, true)
rl = append(rl, ratelim)
if err != nil {
return chat1.ThreadView{}, rl, err
}
// Fetch thread
rarg := chat1.GetThreadRemoteArg{
ConversationID: convID,
Query: query,
Pagination: pagination,
}
boxed, err := s.ri().GetThreadRemote(ctx, rarg)
rl = append(rl, boxed.RateLimit)
if err != nil {
return chat1.ThreadView{}, rl, err
}
thread, err := s.boxer.UnboxThread(ctx, boxed.Thread, conv)
if err != nil {
return chat1.ThreadView{}, rl, err
}
// Post process thread before returning
if err = s.postProcessThread(ctx, uid, conv, &thread, query, nil, true); err != nil {
return chat1.ThreadView{}, nil, err
}
return thread, rl, nil
}
func (s *RemoteConversationSource) PullLocalOnly(ctx context.Context, convID chat1.ConversationID,
uid gregor1.UID, query *chat1.GetThreadQuery, pagination *chat1.Pagination) (chat1.ThreadView, error) {
return chat1.ThreadView{}, storage.MissError{Msg: "PullLocalOnly is unimplemented for RemoteConversationSource"}
}
func (s *RemoteConversationSource) Clear(convID chat1.ConversationID, uid gregor1.UID) error {
return nil
}
func (s *RemoteConversationSource) GetMessages(ctx context.Context, conv chat1.Conversation,
uid gregor1.UID, msgIDs []chat1.MessageID) ([]chat1.MessageUnboxed, error) {
// Insta fail if we are offline
if s.IsOffline(ctx) {
return nil, OfflineError{}
}
rres, err := s.ri().GetMessagesRemote(ctx, chat1.GetMessagesRemoteArg{
ConversationID: conv.GetConvID(),
MessageIDs: msgIDs,
})
if err != nil {
return nil, err
}
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) {
if s.IsOffline(ctx) {
return nil, OfflineError{}
}
return s.boxer.UnboxMessages(ctx, msgs, conv)
}
type conversationLock struct {
refs, shares int
trace string
lock sync.Mutex
}
type conversationLockTab struct {
globals.Contextified
sync.Mutex
utils.DebugLabeler
convLocks map[string]*conversationLock
blockCb *chan struct{} // Testing
}
func newConversationLockTab(g *globals.Context) *conversationLockTab {
return &conversationLockTab{
Contextified: globals.NewContextified(g),
DebugLabeler: utils.NewDebugLabeler(g.GetLog(), "conversationLockTab", false),
convLocks: make(map[string]*conversationLock),
}
}
func (c *conversationLockTab) key(uid gregor1.UID, convID chat1.ConversationID) string {
return fmt.Sprintf("%s:%s", uid, convID)
}
// Acquire obtains a per user per conversation lock on a per trace basis. That is, the lock is a
// shared lock for the current chat trace, and serves to synchronize large chat operations. If there is
// no chat trace, this is a no-op.
func (c *conversationLockTab) Acquire(ctx context.Context, uid gregor1.UID, convID chat1.ConversationID) (blocked bool) {
key := c.key(uid, convID)
trace, ok := CtxTrace(ctx)
if !ok {
c.Debug(ctx, "Acquire: failed to find trace value, not using a lock: convID: %s", convID)
return false
}
c.Lock()
if lock, ok := c.convLocks[key]; ok {
if lock.trace == trace {
// Our request holds the lock on this conversation ID already, so just plow through it
lock.shares++
c.Unlock()
return
}
c.Debug(ctx, "Acquire: blocked by trace: %s on convID: %s", lock.trace, convID)
if c.blockCb != nil {
*c.blockCb <- struct{}{} // For testing
}
lock.refs++
c.Unlock() // Give up map lock while we are waiting for conv lock
lock.lock.Lock()
c.Lock()
lock.trace = trace
lock.shares = 1
c.Unlock()
return true
}
lock := &conversationLock{
shares: 1,
refs: 1,
trace: trace,
}
c.convLocks[key] = lock
lock.lock.Lock()
c.Unlock()
return false
}
func (c *conversationLockTab) Release(ctx context.Context, uid gregor1.UID, convID chat1.ConversationID) (released bool) {
c.Lock()
defer c.Unlock()
trace, ok := CtxTrace(ctx)
if !ok {
c.Debug(ctx, "Release: failed to find trace value, doing nothing: convID: %s", convID)
return false
}
key := c.key(uid, convID)
if lock, ok := c.convLocks[key]; ok {
if lock.trace != trace {
c.Debug(ctx, "Release: different trace trying to free lock? convID: %s lock.trace: %s trace: %s", convID, lock.trace, trace)
} else {
lock.shares--
if lock.shares == 0 {
lock.refs--
if lock.refs == 0 {
delete(c.convLocks, key)
}
lock.trace = ""
lock.lock.Unlock()
return true
}
}
}
return false
}
type HybridConversationSource struct {
globals.Contextified
utils.DebugLabeler
*baseConversationSource
storage *storage.Storage
lockTab *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.GetLog(), "HybridConversationSource", false),
baseConversationSource: newBaseConversationSource(g, ri, b),
storage: storage,
lockTab: newConversationLockTab(g),
}
}
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, func() error { return err }, "Push")()
s.lockTab.Acquire(ctx, uid, convID)
defer s.lockTab.Release(ctx, uid, convID)
// Grab conversation information before pushing
conv, _, err := GetUnverifiedConv(ctx, s.G(), uid, convID, true)
if err != nil {
return decmsg, continuousUpdate, err
}
// Check to see if we are "appending" this message to the current record.
maxMsgID, err := s.storage.GetMaxMsgID(ctx, convID, uid)
switch err.(type) {
case storage.MissError:
continuousUpdate = true
case nil:
continuousUpdate = maxMsgID >= msg.GetMessageID()-1
default:
return chat1.MessageUnboxed{}, continuousUpdate, err
}
decmsg, err = s.boxer.UnboxMessage(ctx, msg, conv)
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(),
})
}
if err = s.storage.Merge(ctx, convID, uid, []chat1.MessageUnboxed{decmsg}); err != nil {
return decmsg, continuousUpdate, err
}
return decmsg, continuousUpdate, nil
}
func (s *HybridConversationSource) identifyTLF(ctx context.Context, conv chat1.Conversation,
uid gregor1.UID, msgs []chat1.MessageUnboxed) error {
// If we are offline, then bail out of here with no error
if s.IsOffline(ctx) {
s.Debug(ctx, "identifyTLF: not performing identify because offline")
return nil
}
idMode, _, haveMode := IdentifyMode(ctx)
for _, msg := range msgs {
if msg.IsValid() {
// Early out if we are in GUI mode and don't have any breaks stored
idBroken := s.storage.IsTLFIdentifyBroken(ctx, msg.Valid().ClientHeader.Conv.Tlfid)
if haveMode && idMode == keybase1.TLFIdentifyBehavior_CHAT_GUI && !idBroken {
s.Debug(ctx, "identifyTLF: not performing identify because we stored a clean identify")
return nil
}
tlfName := msg.Valid().ClientHeader.TLFNameExpanded(conv.Metadata.FinalizeInfo)
switch conv.GetMembersType() {
case chat1.ConversationMembersType_TEAM:
// early out of team convs
return nil
case chat1.ConversationMembersType_IMPTEAM:
s.Debug(ctx, "identifyTLF: implicit team TLF, looking up display name for %s", tlfName)
tlfID := msg.Valid().ClientHeader.Conv.Tlfid
teamID, err := keybase1.TeamIDFromString(tlfID.String())
if err != nil {
return err
}
team, err := teams.Load(ctx, s.G().ExternalG(), keybase1.LoadTeamArg{ID: teamID})
if err != nil {
return err
}
display, err := team.ImplicitTeamDisplayName(ctx)
if err != nil {
return err
}
tlfName = display.String()
}
s.Debug(ctx, "identifyTLF: identifying from msg ID: %d name: %s convID: %s",
msg.GetMessageID(), tlfName, conv.GetConvID())
_, err := CtxKeyFinder(ctx, s.G()).Find(ctx, tlfName, conv.GetMembersType(),
msg.Valid().ClientHeader.TlfPublic)
if err != nil {
s.Debug(ctx, "identifyTLF: failure: name: %s convID: %s", tlfName, conv.GetConvID())
return err
}
return nil
}
}
s.Debug(ctx, "identifyTLF: no identify performed, no valid messages found")
return nil
}
func (s *HybridConversationSource) resolveHoles(ctx context.Context, uid gregor1.UID,
thread *chat1.ThreadView, conv chat1.Conversation) (err error) {
defer s.Trace(ctx, func() error { return err }, "resolveHoles")()
var msgIDs []chat1.MessageID
// Gather all placeholder messages so we can go fetch them
for index, msg := range thread.Messages {
state, err := msg.State()
if err != nil {
continue
}
if state == chat1.MessageUnboxedState_PLACEHOLDER {
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
}
if s.IsOffline(ctx) {
// Don't attempt if we are offline
return OfflineError{}
}
// Fetch all missing messages from server, and sub in the real ones into the placeholder slots
msgs, err := s.GetMessages(ctx, conv, uid, msgIDs)
if err != nil {
s.Debug(ctx, "resolveHoles: failed to get missing messages: %s", err.Error())
return err
}
msgLookup := make(map[chat1.MessageID]chat1.MessageUnboxed)
for _, msg := range msgs {
msgLookup[msg.GetMessageID()] = msg
}
for i, threadMsg := range thread.Messages {
state, err := threadMsg.State()
if err != nil {
continue
}
if state == chat1.MessageUnboxedState_PLACEHOLDER {
if msg, ok := msgLookup[threadMsg.GetMessageID()]; ok {
thread.Messages[i] = msg
} else {
s.Debug(ctx, "resolveHoles: did not fetch all placeholder messages, missing msgID: %d",
threadMsg.GetMessageID())
return fmt.Errorf("did not fetch all placeholder messages")
}
}
}
s.Debug(ctx, "resolveHoles: success: filled %d holes", len(msgs))
return nil
}
// 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 = 10
func (s *HybridConversationSource) Pull(ctx context.Context, convID chat1.ConversationID,
uid gregor1.UID, query *chat1.GetThreadQuery, pagination *chat1.Pagination) (thread chat1.ThreadView, rl []*chat1.RateLimit, err error) {
defer s.Trace(ctx, func() error { return err }, "Pull")()
if convID.IsNil() {
return chat1.ThreadView{}, rl, errors.New("HybridConversationSource.Pull called with empty convID")
}
s.lockTab.Acquire(ctx, uid, convID)
defer s.lockTab.Release(ctx, uid, convID)
// Get conversation metadata
conv, ratelim, err := GetUnverifiedConv(ctx, s.G(), uid, convID, true)
rl = append(rl, ratelim)
// Post process thread before returning
defer func() {
if err == nil {
err = s.postProcessThread(ctx, uid, conv, &thread, query, nil, true)
}
}()
var unboxConv unboxConversationInfo
if err == nil {
unboxConv = conv
// Try locally first
rc := storage.NewHoleyResultCollector(maxHolesForPull,
s.storage.ResultCollectorFromQuery(ctx, query, pagination))
thread, err = s.storage.Fetch(ctx, conv, uid, rc, 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: cache hit: convID: %s uid: %s holes: %d", convID, uid, rc.Holes())
err = s.resolveHoles(ctx, uid, &thread, conv)
}
if err == nil {
// Do online only things
if !s.IsOffline(ctx) {
// Identify this TLF by running crypt keys
if ierr := s.identifyTLF(ctx, conv, uid, thread.Messages); ierr != nil {
s.Debug(ctx, "Pull: identify failed: %s", ierr.Error())
return chat1.ThreadView{}, rl, ierr
}
// Before returning the stuff, update SenderDeviceRevokedAt on each message.
updatedMessages, err := s.updateMessages(ctx, thread.Messages)
if err != nil {
return chat1.ThreadView{}, rl, err
}
thread.Messages = updatedMessages
// 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()
res, err := s.ri().MarkAsRead(ctx, chat1.MarkAsReadArg{
ConversationID: convID,
MsgID: readMsgID,
})
if err != nil {
return chat1.ThreadView{}, nil, err
}
if _, err = s.G().InboxSource.ReadMessage(ctx, uid, 0, convID, readMsgID); err != nil {
return chat1.ThreadView{}, nil, err
}
rl = append(rl, res.RateLimit)
} else {
s.Debug(ctx, "Pull: skipping mark as read call")
}
}
return thread, rl, nil
}
s.Debug(ctx, "Pull: cache miss: err: %s", err.Error())
} else {
s.Debug(ctx, "Pull: error fetching conv metadata: convID: %s uid: %s err: %s", convID, uid,
err.Error())
// Assume this is a public convo for unbox purposes, since it is the only way any unboxing
// will succeed here since we don't know the members type.
unboxConv = newPublicUnboxConverstionInfo(convID)
}
// Insta fail if we are offline
if s.IsOffline(ctx) {
return chat1.ThreadView{}, rl, OfflineError{}
}
// Fetch the entire request on failure
rarg := chat1.GetThreadRemoteArg{
ConversationID: convID,
Query: query,
Pagination: pagination,
}
boxed, err := s.ri().GetThreadRemote(ctx, rarg)
rl = append(rl, boxed.RateLimit)
if err != nil {
return chat1.ThreadView{}, rl, err
}
// Unbox
thread, err = s.boxer.UnboxThread(ctx, boxed.Thread, unboxConv)
if err != nil {
return chat1.ThreadView{}, rl, err
}
// Store locally (just warn on error, don't abort the whole thing)
if err = s.storage.Merge(ctx, convID, uid, thread.Messages); err != nil {
s.Debug(ctx, "Pull: unable to commit thread locally: convID: %s uid: %s", convID, uid)
}
return thread, rl, nil
}
func (s *HybridConversationSource) updateMessages(ctx context.Context, messages []chat1.MessageUnboxed) ([]chat1.MessageUnboxed, error) {
updatedMessages := make([]chat1.MessageUnboxed, 0, len(messages))
for _, m := range messages {
m2, err := s.updateMessage(ctx, m)
if err != nil {
return updatedMessages, err
}
updatedMessages = append(updatedMessages, m2)
}
return updatedMessages, nil
}
func (s *HybridConversationSource) updateMessage(ctx context.Context, message chat1.MessageUnboxed) (chat1.MessageUnboxed, error) {
typ, err := message.State()
if err != nil {
return chat1.MessageUnboxed{}, err
}
switch typ {
case chat1.MessageUnboxedState_VALID:
m := message.Valid()
var verificationKey []byte
if m.HeaderSignature != nil {
verificationKey = m.HeaderSignature.K
}
if m.VerificationKey != nil {
verificationKey = *m.VerificationKey
}
if verificationKey == nil {
// Skip revocation check for messages cached before the sig/key was part of the cache.
s.Debug(ctx, "updateMessage skipping message (%v) with no cached HeaderSignature", m.ServerHeader.MessageID)
return message, nil
}
sender := m.ClientHeader.Sender
ctime := m.ServerHeader.Ctime
found, validAtCtime, revoked, err := s.boxer.ValidSenderKey(ctx, sender, verificationKey, ctime)
if err != nil {
return chat1.MessageUnboxed{}, err
}
if !found {
return chat1.MessageUnboxed{}, NewPermanentUnboxingError(libkb.NoKeyError{Msg: "sender key not found"})
}
if !validAtCtime {
return chat1.MessageUnboxed{}, NewPermanentUnboxingError(libkb.NoKeyError{Msg: "key invalid for sender at message ctime"})
}
m.SenderDeviceRevokedAt = revoked
updatedMessage := chat1.NewMessageUnboxedWithValid(m)
return updatedMessage, nil
default:
return message, nil
}
}
type pullLocalResultCollector struct {
*storage.SimpleResultCollector
num int
}
func (p *pullLocalResultCollector) Name() string {
return "pulllocal"
}
func (p *pullLocalResultCollector) String() string {
return fmt.Sprintf("[ %s: t: %d ]", p.Name(), p.num)
}
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 && len(p.Result()) > 0 {
return nil
}
return err
}
func newPullLocalResultCollector(num int) *pullLocalResultCollector {
return &pullLocalResultCollector{
num: num,
SimpleResultCollector: storage.NewSimpleResultCollector(num),
}
}
func (s *HybridConversationSource) PullLocalOnly(ctx context.Context, convID chat1.ConversationID,
uid gregor1.UID, query *chat1.GetThreadQuery, pagination *chat1.Pagination) (tv chat1.ThreadView, err error) {
defer s.Trace(ctx, func() error { return err }, "PullLocalOnly")()
s.lockTab.Acquire(ctx, uid, convID)
defer s.lockTab.Release(ctx, uid, convID)
// Post process thread before returning
defer func() {
if err == nil {
superXform := newBasicSupersedesTransform(s.G())
superXform.SetMessagesFunc(func(ctx context.Context, conv chat1.Conversation,
uid gregor1.UID, msgIDs []chat1.MessageID) (res []chat1.MessageUnboxed, err error) {
msgs, err := storage.New(s.G()).FetchMessages(ctx, conv.GetConvID(), uid, msgIDs)
if err != nil {
return nil, err
}
for _, msg := range msgs {
if msg != nil {
res = append(res, *msg)
}
}
return res, nil
})
// 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, conv, &tv, query, superXform, false)
}
}()
// 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
}
tv, err = s.storage.FetchUpToLocalMaxMsgID(ctx, convID, uid, newPullLocalResultCollector(num),
query, pagination)
if err != nil {
s.Debug(ctx, "PullLocalOnly: failed to fetch local messages: %s", err.Error())
return chat1.ThreadView{}, err
}
return tv, nil
}
func (s *HybridConversationSource) Clear(convID chat1.ConversationID, uid gregor1.UID) error {
return s.storage.MaybeNuke(true, nil, convID, uid)
}
type ByMsgID []chat1.MessageUnboxed
func (m ByMsgID) Len() int { return len(m) }
func (m ByMsgID) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
func (m ByMsgID) Less(i, j int) bool { return m[i].GetMessageID() > m[j].GetMessageID() }
func (s *HybridConversationSource) GetMessages(ctx context.Context, conv chat1.Conversation,
uid gregor1.UID, msgIDs []chat1.MessageID) ([]chat1.MessageUnboxed, error) {
convID := conv.GetConvID()
s.lockTab.Acquire(ctx, uid, convID)
defer s.lockTab.Release(ctx, uid, convID)
rmsgsTab := make(map[chat1.MessageID]chat1.MessageUnboxed)
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
s.Debug(ctx, "GetMessages: convID: %s uid: %s total msgs: %d remote: %d", convID, uid, len(msgIDs),
len(remoteMsgs))
if len(remoteMsgs) > 0 {
// Insta fail if we are offline
if s.IsOffline(ctx) {
return nil, OfflineError{}
}
rmsgs, err := s.ri().GetMessagesRemote(ctx, chat1.GetMessagesRemoteArg{
ConversationID: convID,
MessageIDs: remoteMsgs,
})
if err != nil {
return nil, err
}
// Unbox all the remote messages
rmsgsUnboxed, err := s.boxer.UnboxMessages(ctx, rmsgs.Msgs, conv)
if err != nil {
return nil, err
}
sort.Sort(ByMsgID(rmsgsUnboxed))
for _, rmsg := range rmsgsUnboxed {
rmsgsTab[rmsg.GetMessageID()] = rmsg
}
// Write out messages
if err := s.storage.Merge(ctx, convID, uid, rmsgsUnboxed); err != nil {
return nil, err
}
}
// Form final result
var res []chat1.MessageUnboxed
for index, msg := range msgs {
if msg != nil {
res = append(res, *msg)
} else {
res = append(res, rmsgsTab[msgIDs[index]])
}
}
// Identify this TLF by running crypt keys
if ierr := s.identifyTLF(ctx, conv, uid, res); ierr != nil {
s.Debug(ctx, "GetMessages: identify failed: %s", ierr.Error())
return nil, ierr
}
return res, nil
}
func (s *HybridConversationSource) GetMessagesWithRemotes(ctx context.Context,
conv chat1.Conversation, uid gregor1.UID, msgs []chat1.MessageBoxed) ([]chat1.MessageUnboxed, error) {
convID := conv.GetConvID()
s.lockTab.Acquire(ctx, uid, convID)
defer s.lockTab.Release(ctx, uid, convID)
var res []chat1.MessageUnboxed
var msgIDs []chat1.MessageID
for _, msg := range msgs {
msgIDs = append(msgIDs, msg.GetMessageID())
}
lmsgsTab := make(map[chat1.MessageID]chat1.MessageUnboxed)
lmsgs, err := s.storage.FetchMessages(ctx, convID, uid, msgIDs)
if err != nil {
return nil, err
}
for _, lmsg := range lmsgs {
if lmsg != nil {
lmsgsTab[lmsg.GetMessageID()] = *lmsg
}
}
s.Debug(ctx, "GetMessagesWithRemotes: convID: %s uid: %s total msgs: %d hits: %d", convID, uid,
len(msgs), len(lmsgsTab))
var merges []chat1.MessageUnboxed
for _, msg := range msgs {
if lmsg, ok := lmsgsTab[msg.GetMessageID()]; ok {
res = append(res, lmsg)
} else {
// Insta fail if we are offline
if s.IsOffline(ctx) {
return nil, OfflineError{}
}
unboxed, err := s.boxer.UnboxMessage(ctx, msg, conv)
if err != nil {
return res, err
}
merges = append(merges, unboxed)
res = append(res, unboxed)
}
}
if len(merges) > 0 {
sort.Sort(ByMsgID(merges))
if err = s.storage.Merge(ctx, convID, uid, merges); err != nil {
return res, err
}
}
// Identify this TLF by running crypt keys
if ierr := s.identifyTLF(ctx, conv, uid, res); ierr != nil {
s.Debug(ctx, "Pull: identify failed: %s", ierr.Error())
return res, ierr
}
sort.Sort(ByMsgID(res))
return res, nil
}
func NewConversationSource(g *globals.Context, typ string, boxer *Boxer, storage *storage.Storage,
ri func() chat1.RemoteInterface) types.ConversationSource {
if typ == "hybrid" {
return NewHybridConversationSource(g, boxer, storage, ri)
}
return NewRemoteConversationSource(g, boxer, ri)
}