/
chat_svc_handler.go
1767 lines (1622 loc) · 50.8 KB
/
chat_svc_handler.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 client
import (
"errors"
"fmt"
"strings"
"time"
"github.com/araddon/dateparse"
"github.com/adamwalz/keybase-client/go/chat"
"github.com/adamwalz/keybase-client/go/chat/attachments"
"github.com/adamwalz/keybase-client/go/chat/utils"
"github.com/adamwalz/keybase-client/go/libkb"
"github.com/adamwalz/keybase-client/go/protocol/chat1"
gregor1 "github.com/adamwalz/keybase-client/go/protocol/gregor1"
"github.com/adamwalz/keybase-client/go/protocol/keybase1"
"github.com/keybase/go-framed-msgpack-rpc/rpc"
"golang.org/x/net/context"
)
// ChatServiceHandler can call the service.
type ChatServiceHandler interface {
ListV1(context.Context, listOptionsV1) Reply
ReadV1(context.Context, readOptionsV1) Reply
GetV1(context.Context, getOptionsV1) Reply
SendV1(context.Context, sendOptionsV1, chat1.ChatUiInterface) Reply
EditV1(context.Context, editOptionsV1) Reply
ReactionV1(context.Context, reactionOptionsV1) Reply
DeleteV1(context.Context, deleteOptionsV1) Reply
AttachV1(context.Context, attachOptionsV1, chat1.ChatUiInterface, chat1.NotifyChatInterface) Reply
DownloadV1(context.Context, downloadOptionsV1, chat1.ChatUiInterface, chat1.NotifyChatInterface) Reply
SetStatusV1(context.Context, setStatusOptionsV1) Reply
MarkV1(context.Context, markOptionsV1) Reply
SearchInboxV1(context.Context, searchInboxOptionsV1) Reply
SearchRegexpV1(context.Context, searchRegexpOptionsV1) Reply
NewConvV1(context.Context, newConvOptionsV1) Reply
ListConvsOnNameV1(context.Context, listConvsOnNameOptionsV1) Reply
JoinV1(context.Context, joinOptionsV1) Reply
LeaveV1(context.Context, leaveOptionsV1) Reply
AddToChannelV1(context.Context, addToChannelOptionsV1) Reply
RemoveFromChannelV1(context.Context, removeFromChannelOptionsV1) Reply
LoadFlipV1(context.Context, loadFlipOptionsV1) Reply
GetUnfurlSettingsV1(context.Context) Reply
SetUnfurlSettingsV1(context.Context, setUnfurlSettingsOptionsV1) Reply
AdvertiseCommandsV1(context.Context, advertiseCommandsOptionsV1) Reply
ClearCommandsV1(context.Context, clearCommandsOptionsV1) Reply
ListCommandsV1(context.Context, listCommandsOptionsV1) Reply
PinV1(context.Context, pinOptionsV1) Reply
UnpinV1(context.Context, unpinOptionsV1) Reply
GetResetConvMembersV1(context.Context) Reply
AddResetConvMemberV1(context.Context, addResetConvMemberOptionsV1) Reply
GetDeviceInfoV1(context.Context, getDeviceInfoOptionsV1) Reply
ListMembersV1(context.Context, listMembersOptionsV1) Reply
EmojiAddV1(context.Context, emojiAddOptionsV1) Reply
EmojiAddAliasV1(context.Context, emojiAddAliasOptionsV1) Reply
EmojiRemoveV1(context.Context, emojiRemoveOptionsV1) Reply
EmojiListV1(context.Context) Reply
}
// chatServiceHandler implements ChatServiceHandler.
type chatServiceHandler struct {
libkb.Contextified
chatUI *DelegateChatUI
}
func newChatServiceHandler(g *libkb.GlobalContext) *chatServiceHandler {
return &chatServiceHandler{
Contextified: libkb.NewContextified(g),
chatUI: newDelegateChatUI(),
}
}
// ListV1 implements ChatServiceHandler.ListV1.
func (c *chatServiceHandler) ListV1(ctx context.Context, opts listOptionsV1) Reply {
var cl chat1.ChatList
var rlimits []chat1.RateLimit
client, err := GetChatLocalClient(c.G())
if err != nil {
return c.errReply(err)
}
topicType, err := TopicTypeFromStrDefault(opts.TopicType)
if err != nil {
return c.errReply(err)
}
var convIDs []chat1.ConversationID
if opts.ConversationID != "" {
convID, err := chat1.MakeConvID(opts.ConversationID.String())
if err != nil {
return c.errReply(err)
}
convIDs = append(convIDs, convID)
}
res, err := client.GetInboxAndUnboxUILocal(ctx, chat1.GetInboxAndUnboxUILocalArg{
Query: &chat1.GetInboxLocalQuery{
ConvIDs: convIDs,
Status: utils.VisibleChatConversationStatuses(),
TopicType: &topicType,
UnreadOnly: opts.UnreadOnly,
OneChatTypePerTLF: new(bool),
},
IdentifyBehavior: keybase1.TLFIdentifyBehavior_CHAT_CLI,
})
if err != nil {
return c.errReply(err)
}
rlimits = utils.AggRateLimits(res.RateLimits)
if opts.FailOffline && res.Offline {
return c.errReply(chat.OfflineError{})
}
cl = chat1.ChatList{
Offline: res.Offline,
IdentifyFailures: res.IdentifyFailures,
}
for _, conv := range res.Conversations {
cl.Conversations = append(cl.Conversations, utils.ExportToSummary(conv))
}
cl.RateLimits = c.aggRateLimits(rlimits)
return Reply{Result: cl}
}
func (c *chatServiceHandler) ListConvsOnNameV1(ctx context.Context, opts listConvsOnNameOptionsV1) Reply {
client, err := GetChatLocalClient(c.G())
if err != nil {
return c.errReply(err)
}
topicType, err := TopicTypeFromStrDefault(opts.TopicType)
if err != nil {
return c.errReply(err)
}
mt := MembersTypeFromStrDefault(opts.MembersType, c.G().GetEnv())
listRes, err := client.GetTLFConversationsLocal(ctx, chat1.GetTLFConversationsLocalArg{
TlfName: opts.Name,
TopicType: topicType,
MembersType: mt,
})
if err != nil {
return c.errReply(err)
}
var cl chat1.ChatList
cl.RateLimits = c.aggRateLimits(listRes.RateLimits)
for _, conv := range listRes.Convs {
cl.Conversations = append(cl.Conversations, utils.ExportToSummary(conv))
}
return Reply{Result: cl}
}
func (c *chatServiceHandler) JoinV1(ctx context.Context, opts joinOptionsV1) Reply {
client, err := GetChatLocalClient(c.G())
if err != nil {
return c.errReply(err)
}
convID, rl, err := c.resolveAPIConvID(ctx, opts.ConversationID, opts.Channel)
if err != nil {
return c.errReply(err)
}
res, err := client.JoinConversationByIDLocal(ctx, convID)
if err != nil {
return c.errReply(err)
}
rl = append(rl, res.RateLimits...)
cres := chat1.EmptyRes{
RateLimits: c.aggRateLimits(rl),
}
return Reply{Result: cres}
}
func (c *chatServiceHandler) LeaveV1(ctx context.Context, opts leaveOptionsV1) Reply {
client, err := GetChatLocalClient(c.G())
if err != nil {
return c.errReply(err)
}
convID, rl, err := c.resolveAPIConvID(ctx, opts.ConversationID, opts.Channel)
if err != nil {
return c.errReply(err)
}
res, err := client.LeaveConversationLocal(ctx, convID)
if err != nil {
return c.errReply(err)
}
rl = append(rl, res.RateLimits...)
cres := chat1.EmptyRes{
RateLimits: c.aggRateLimits(rl),
}
return Reply{Result: cres}
}
func (c *chatServiceHandler) AddToChannelV1(ctx context.Context, opts addToChannelOptionsV1) Reply {
client, err := GetChatLocalClient(c.G())
if err != nil {
return c.errReply(err)
}
convID, _, err := c.resolveAPIConvID(ctx, opts.ConversationID, opts.Channel)
if err != nil {
return c.errReply(err)
}
err = client.BulkAddToConv(ctx, chat1.BulkAddToConvArg{
Usernames: opts.Usernames,
ConvID: convID,
})
if err != nil {
return c.errReply(err)
}
return Reply{Result: true}
}
func (c *chatServiceHandler) RemoveFromChannelV1(ctx context.Context, opts removeFromChannelOptionsV1) Reply {
client, err := GetChatLocalClient(c.G())
if err != nil {
return c.errReply(err)
}
convID, rl, err := c.resolveAPIConvID(ctx, opts.ConversationID, opts.Channel)
if err != nil {
return c.errReply(err)
}
res, err := client.RemoveFromConversationLocal(ctx, chat1.RemoveFromConversationLocalArg{
ConvID: convID,
Usernames: opts.Usernames,
})
if err != nil {
return c.errReply(err)
}
rl = append(rl, res.RateLimits...)
cres := chat1.EmptyRes{
RateLimits: c.aggRateLimits(rl),
}
return Reply{Result: cres}
}
func (c *chatServiceHandler) LoadFlipV1(ctx context.Context, opts loadFlipOptionsV1) Reply {
client, err := GetChatLocalClient(c.G())
if err != nil {
return c.errReply(err)
}
hostConvID, err := chat1.MakeConvID(opts.ConversationID.String())
if err != nil {
return c.errReply(err)
}
flipConvID, err := chat1.MakeConvID(opts.FlipConversationID.String())
if err != nil {
return c.errReply(err)
}
gameID, err := chat1.MakeFlipGameID(opts.GameID)
if err != nil {
return c.errReply(err)
}
res, err := client.LoadFlip(ctx, chat1.LoadFlipArg{
HostConvID: hostConvID,
HostMsgID: opts.MsgID,
FlipConvID: flipConvID,
GameID: gameID,
})
if err != nil {
return c.errReply(err)
}
return Reply{Result: res}
}
func (c *chatServiceHandler) GetUnfurlSettingsV1(ctx context.Context) Reply {
client, err := GetChatLocalClient(c.G())
if err != nil {
return c.errReply(err)
}
res, err := client.GetUnfurlSettings(ctx)
if err != nil {
return c.errReply(err)
}
return Reply{
Result: map[string]interface{}{
"mode": strings.ToLower(chat1.UnfurlModeRevMap[res.Mode]),
"whitelist": res.Whitelist,
},
}
}
func (c *chatServiceHandler) SetUnfurlSettingsV1(ctx context.Context, opts setUnfurlSettingsOptionsV1) Reply {
client, err := GetChatLocalClient(c.G())
if err != nil {
return c.errReply(err)
}
if err := client.SaveUnfurlSettings(ctx, chat1.SaveUnfurlSettingsArg{
Mode: opts.intMode,
Whitelist: opts.Whitelist,
}); err != nil {
return c.errReply(err)
}
return Reply{Result: true}
}
func (c *chatServiceHandler) GetDeviceInfoV1(ctx context.Context, opts getDeviceInfoOptionsV1) Reply {
client, err := GetUserClient(c.G())
if err != nil {
return c.errReply(err)
}
user, err := client.LoadUserByName(ctx, keybase1.LoadUserByNameArg{Username: opts.Username})
if err != nil {
return c.errReply(err)
}
arg := keybase1.LoadUserPlusKeysV2Arg{
Uid: user.Uid,
}
them, err := client.LoadUserPlusKeysV2(ctx, arg)
if err != nil {
return c.errReply(err)
}
var res chat1.GetDeviceInfoRes
for _, m := range them.Current.DeviceKeys {
dev := chat1.DeviceInfo{
DeviceID: m.DeviceID,
DeviceDescription: m.DeviceDescription,
DeviceType: m.DeviceType,
DeviceCtime: m.Base.CTime.UnixSeconds(),
}
if string(m.DeviceID) != "" && m.Base.Revocation == nil {
res.Devices = append(res.Devices, dev)
}
}
return Reply{Result: res}
}
func (c *chatServiceHandler) AdvertiseCommandsV1(ctx context.Context, opts advertiseCommandsOptionsV1) Reply {
client, err := GetChatLocalClient(c.G())
if err != nil {
return c.errReply(err)
}
var alias *string
if opts.Alias != "" {
alias = new(string)
*alias = opts.Alias
}
var ads []chat1.AdvertiseCommandsParam
for _, ad := range opts.Advertisements {
typ, err := chat1.GetAdvertTyp(ad.Typ)
if err != nil {
return c.errReply(err)
}
var teamName *string
if ad.TeamName != "" {
adTeamName := ad.TeamName
teamName = &adTeamName
}
var convID *chat1.ConversationID
if ad.ConvID != "" {
adConvID, err := chat1.MakeConvID(ad.ConvID.String())
if err != nil {
return c.errReply(err)
}
convID = &adConvID
}
ads = append(ads, chat1.AdvertiseCommandsParam{
Typ: typ,
Commands: ad.Commands,
TeamName: teamName,
ConvID: convID,
})
}
res, err := client.AdvertiseBotCommandsLocal(ctx, chat1.AdvertiseBotCommandsLocalArg{
Alias: alias,
Advertisements: ads,
})
if err != nil {
return c.errReply(err)
}
return Reply{Result: res}
}
func (c *chatServiceHandler) ClearCommandsV1(ctx context.Context, opts clearCommandsOptionsV1) Reply {
client, err := GetChatLocalClient(c.G())
if err != nil {
return c.errReply(err)
}
var filter *chat1.ClearBotCommandsFilter
if opts.Filter != nil {
typ, err := chat1.GetAdvertTyp(opts.Filter.Typ)
if err != nil {
return c.errReply(err)
}
var teamName *string
if opts.Filter.TeamName != "" {
filterTeamName := opts.Filter.TeamName
teamName = &filterTeamName
}
var convID *chat1.ConversationID
if opts.Filter.ConvID != "" {
filterConvID, err := chat1.MakeConvID(opts.Filter.ConvID.String())
if err != nil {
return c.errReply(err)
}
convID = &filterConvID
}
filter = &chat1.ClearBotCommandsFilter{
Typ: typ,
TeamName: teamName,
ConvID: convID,
}
}
res, err := client.ClearBotCommandsLocal(ctx, filter)
if err != nil {
return c.errReply(err)
}
return Reply{Result: res}
}
func (c *chatServiceHandler) ListCommandsV1(ctx context.Context, opts listCommandsOptionsV1) Reply {
client, err := GetChatLocalClient(c.G())
if err != nil {
return c.errReply(err)
}
convID, rl, err := c.resolveAPIConvID(ctx, opts.ConversationID, opts.Channel)
if err != nil {
return c.errReply(err)
}
lres, err := client.ListBotCommandsLocal(ctx, convID)
if err != nil {
return c.errReply(err)
}
res := chat1.ListCommandsRes{
Commands: lres.Commands,
}
res.RateLimits = c.aggRateLimits(append(rl, lres.RateLimits...))
return Reply{Result: res}
}
func (c *chatServiceHandler) PinV1(ctx context.Context, opts pinOptionsV1) Reply {
client, err := GetChatLocalClient(c.G())
if err != nil {
return c.errReply(err)
}
convID, rl, err := c.resolveAPIConvID(ctx, opts.ConversationID, opts.Channel)
if err != nil {
return c.errReply(err)
}
lres, err := client.PinMessage(ctx, chat1.PinMessageArg{
ConvID: convID,
MsgID: opts.MessageID,
})
if err != nil {
return c.errReply(err)
}
rl = append(rl, lres.RateLimits...)
res := chat1.EmptyRes{
RateLimits: c.aggRateLimits(rl),
}
return Reply{Result: res}
}
func (c *chatServiceHandler) UnpinV1(ctx context.Context, opts unpinOptionsV1) Reply {
client, err := GetChatLocalClient(c.G())
if err != nil {
return c.errReply(err)
}
convID, rl, err := c.resolveAPIConvID(ctx, opts.ConversationID, opts.Channel)
if err != nil {
return c.errReply(err)
}
lres, err := client.UnpinMessage(ctx, convID)
if err != nil {
return c.errReply(err)
}
rl = append(rl, lres.RateLimits...)
res := chat1.EmptyRes{
RateLimits: c.aggRateLimits(rl),
}
return Reply{Result: res}
}
func (c *chatServiceHandler) GetResetConvMembersV1(ctx context.Context) Reply {
client, err := GetChatLocalClient(c.G())
if err != nil {
return c.errReply(err)
}
lres, err := client.GetAllResetConvMembers(ctx)
if err != nil {
return c.errReply(err)
}
var res chat1.GetResetConvMembersRes
for _, m := range lres.Members {
res.Members = append(res.Members, chat1.ResetConvMemberAPI{
ConversationID: m.Conv.ConvIDStr(),
Username: m.Username,
})
}
res.RateLimits = c.aggRateLimits(lres.RateLimits)
return Reply{Result: res}
}
func (c *chatServiceHandler) AddResetConvMemberV1(ctx context.Context, opts addResetConvMemberOptionsV1) Reply {
client, err := GetChatLocalClient(c.G())
if err != nil {
return c.errReply(err)
}
convID, err := chat1.MakeConvID(opts.ConversationID.String())
if err != nil {
return c.errReply(err)
}
if err := client.AddTeamMemberAfterReset(ctx, chat1.AddTeamMemberAfterResetArg{
Username: opts.Username,
ConvID: convID,
}); err != nil {
return c.errReply(err)
}
return Reply{Result: chat1.EmptyRes{}}
}
func (c *chatServiceHandler) reactionMapToUI(reactions chat1.ReactionMap) (res chat1.UIReactionMap) {
res.Reactions = make(map[string]chat1.UIReactionDesc)
for emoji, users := range reactions.Reactions {
res.Reactions[emoji] = chat1.UIReactionDesc{
Users: make(map[string]chat1.Reaction),
}
for username, reaction := range users {
res.Reactions[emoji].Users[username] = reaction
}
}
return res
}
func (c *chatServiceHandler) formatMessages(ctx context.Context, messages []chat1.MessageUnboxed,
conv chat1.ConversationLocal, selfUID keybase1.UID, readMsgID chat1.MessageID, unreadOnly bool) (ret []chat1.Message, err error) {
for _, m := range messages {
st, err := m.State()
if err != nil {
return nil, errors.New("invalid message: unknown state")
}
if st == chat1.MessageUnboxedState_ERROR {
em := m.Error().ErrMsg
ret = append(ret, chat1.Message{
Error: &em,
})
continue
}
// skip any PLACEHOLDER or OUTBOX messages
if st != chat1.MessageUnboxedState_VALID {
continue
}
mv := m.Valid()
if mv.ClientHeader.MessageType == chat1.MessageType_TLFNAME {
// skip TLFNAME messages
continue
}
unread := mv.ServerHeader.MessageID > readMsgID
if unreadOnly && !unread {
continue
}
if !selfUID.IsNil() {
fromSelf := (mv.ClientHeader.Sender.String() == selfUID.String())
unread = unread && (!fromSelf)
if unreadOnly && fromSelf {
continue
}
}
prev := mv.ClientHeader.Prev
// Avoid having null show up in the output JSON.
if prev == nil {
prev = []chat1.MessagePreviousPointer{}
}
msg := chat1.MsgSummary{
Id: mv.ServerHeader.MessageID,
ConvID: conv.GetConvID().ConvIDStr(),
Channel: chat1.ChatChannel{
Name: conv.Info.TlfName,
Public: mv.ClientHeader.TlfPublic,
TopicType: strings.ToLower(mv.ClientHeader.Conv.TopicType.String()),
MembersType: strings.ToLower(conv.GetMembersType().String()),
TopicName: conv.Info.TopicName,
},
Sender: chat1.MsgSender{
Uid: keybase1.UID(mv.ClientHeader.Sender.String()),
DeviceID: keybase1.DeviceID(mv.ClientHeader.SenderDevice.String()),
Username: mv.SenderUsername,
DeviceName: mv.SenderDeviceName,
},
SentAt: mv.ServerHeader.Ctime.UnixSeconds(),
SentAtMs: mv.ServerHeader.Ctime.UnixMilliseconds(),
Prev: prev,
Unread: unread,
RevokedDevice: mv.SenderDeviceRevokedAt != nil,
KbfsEncrypted: mv.ClientHeader.KbfsCryptKeysUsed == nil || *mv.ClientHeader.KbfsCryptKeysUsed,
IsEphemeral: mv.IsEphemeral(),
IsEphemeralExpired: mv.IsEphemeralExpired(time.Now()),
ETime: mv.Etime(),
Content: c.convertMsgBody(mv.MessageBody),
HasPairwiseMacs: mv.HasPairwiseMacs(),
AtMentionUsernames: mv.AtMentionUsernames,
ChannelMention: strings.ToLower(mv.ChannelMention.String()),
ChannelNameMentions: utils.PresentChannelNameMentions(ctx, mv.ChannelNameMentions),
}
if mv.ClientHeader.BotUID != nil {
botUID := keybase1.UID(mv.ClientHeader.BotUID.String())
msg.BotInfo = &chat1.MsgBotInfo{
BotUID: botUID,
BotUsername: mv.BotUsername,
}
}
if mv.Reactions.Reactions != nil {
uireact := c.reactionMapToUI(mv.Reactions)
msg.Reactions = &uireact
}
ret = append(ret, chat1.Message{
Msg: &msg,
})
}
if ret == nil {
// Avoid having null show up in the output JSON.
ret = []chat1.Message{}
}
return ret, nil
}
// ReadV1 implements ChatServiceHandler.ReadV1.
func (c *chatServiceHandler) ReadV1(ctx context.Context, opts readOptionsV1) Reply {
var rlimits []chat1.RateLimit
client, err := GetChatLocalClient(c.G())
if err != nil {
return c.errReply(err)
}
conv, rlimits, err := c.findConversation(ctx, opts.ConversationID, opts.Channel)
if err != nil {
return c.errReply(err)
}
arg := chat1.GetThreadLocalArg{
ConversationID: conv.Info.Id,
Pagination: opts.Pagination,
Query: &chat1.GetThreadQuery{
MarkAsRead: !opts.Peek,
},
IdentifyBehavior: keybase1.TLFIdentifyBehavior_CHAT_CLI,
}
threadView, err := client.GetThreadLocal(ctx, arg)
if err != nil {
return c.errReply(err)
}
rlimits = append(rlimits, threadView.RateLimits...)
// Check to see if this was fetched offline and we should fail
if opts.FailOffline && threadView.Offline {
return c.errReply(chat.OfflineError{})
}
// This could be lower than the truth if any messages were
// posted between the last two gregor rpcs.
readMsgID := conv.ReaderInfo.ReadMsgid
selfUID := c.G().Env.GetUID()
if selfUID.IsNil() {
c.G().Log.Warning("Could not get self UID for api")
}
messages, err := c.formatMessages(ctx, threadView.Thread.Messages, conv, selfUID, readMsgID, opts.UnreadOnly)
if err != nil {
return c.errReply(err)
}
thread := chat1.Thread{
Offline: threadView.Offline,
IdentifyFailures: threadView.IdentifyFailures,
Pagination: threadView.Thread.Pagination,
Messages: messages,
}
thread.RateLimits = c.aggRateLimits(rlimits)
return Reply{Result: thread}
}
// GetV1 implements ChatServiceHandler.GetV1.
func (c *chatServiceHandler) GetV1(ctx context.Context, opts getOptionsV1) Reply {
var rlimits []chat1.RateLimit
client, err := GetChatLocalClient(c.G())
if err != nil {
return c.errReply(err)
}
conv, rlimits, err := c.findConversation(ctx, opts.ConversationID, opts.Channel)
if err != nil {
return c.errReply(err)
}
arg := chat1.GetMessagesLocalArg{
ConversationID: conv.Info.Id,
MessageIDs: opts.MessageIDs,
IdentifyBehavior: keybase1.TLFIdentifyBehavior_CHAT_CLI,
}
res, err := client.GetMessagesLocal(ctx, arg)
if err != nil {
return c.errReply(err)
}
// Check to see if this was fetched offline and we should fail
if opts.FailOffline && res.Offline {
return c.errReply(chat.OfflineError{})
}
selfUID := c.G().Env.GetUID()
if selfUID.IsNil() {
c.G().Log.Warning("Could not get self UID for api")
}
messages, err := c.formatMessages(ctx, res.Messages, conv, selfUID, 0 /* readMsgID */, false /* unreadOnly */)
if err != nil {
return c.errReply(err)
}
thread := chat1.Thread{
Offline: res.Offline,
IdentifyFailures: res.IdentifyFailures,
Messages: messages,
}
thread.RateLimits = c.aggRateLimits(rlimits)
return Reply{Result: thread}
}
// SendV1 implements ChatServiceHandler.SendV1.
func (c *chatServiceHandler) SendV1(ctx context.Context, opts sendOptionsV1, chatUI chat1.ChatUiInterface) Reply {
convID, err := chat1.MakeConvID(opts.ConversationID.String())
if err != nil {
return c.errReply(fmt.Errorf("invalid conv ID: %s", opts.ConversationID))
}
arg := sendArgV1{
conversationID: convID,
channel: opts.Channel,
body: chat1.NewMessageBodyWithText(chat1.MessageText{Body: opts.Message.Body}),
mtype: chat1.MessageType_TEXT,
response: "message sent",
nonblock: opts.Nonblock,
ephemeralLifetime: opts.EphemeralLifetime,
replyTo: opts.ReplyTo,
}
return c.sendV1(ctx, arg, chatUI)
}
// DeleteV1 implements ChatServiceHandler.DeleteV1.
func (c *chatServiceHandler) DeleteV1(ctx context.Context, opts deleteOptionsV1) Reply {
convID, _, err := c.resolveAPIConvID(ctx, opts.ConversationID, opts.Channel)
if err != nil {
return c.errReply(err)
}
messages := []chat1.MessageID{opts.MessageID}
arg := sendArgV1{
conversationID: convID,
channel: opts.Channel,
mtype: chat1.MessageType_DELETE,
supersedes: opts.MessageID,
deletes: messages,
response: "message deleted",
// NOTE: The service will fill in the IDs of edit messages that also need to be deleted.
body: chat1.NewMessageBodyWithDelete(chat1.MessageDelete{MessageIDs: messages}),
}
return c.sendV1(ctx, arg, utils.DummyChatUI{})
}
// EditV1 implements ChatServiceHandler.EditV1.
func (c *chatServiceHandler) EditV1(ctx context.Context, opts editOptionsV1) Reply {
convID, err := chat1.MakeConvID(opts.ConversationID.String())
if err != nil {
return c.errReply(fmt.Errorf("invalid conv ID: %s", opts.ConversationID))
}
arg := sendArgV1{
conversationID: convID,
channel: opts.Channel,
body: chat1.NewMessageBodyWithEdit(chat1.MessageEdit{MessageID: opts.MessageID, Body: opts.Message.Body}),
mtype: chat1.MessageType_EDIT,
supersedes: opts.MessageID,
response: "message edited",
}
return c.sendV1(ctx, arg, utils.DummyChatUI{})
}
// ReactionV1 implements ChatServiceHandler.ReactionV1.
func (c *chatServiceHandler) ReactionV1(ctx context.Context, opts reactionOptionsV1) Reply {
convID, err := chat1.MakeConvID(opts.ConversationID.String())
if err != nil {
return c.errReply(fmt.Errorf("invalid conv ID: %s", opts.ConversationID))
}
arg := sendArgV1{
conversationID: convID,
channel: opts.Channel,
body: chat1.NewMessageBodyWithReaction(chat1.MessageReaction{MessageID: opts.MessageID, Body: opts.Message.Body}),
mtype: chat1.MessageType_REACTION,
supersedes: opts.MessageID,
response: "message reacted to",
}
return c.sendV1(ctx, arg, utils.DummyChatUI{})
}
// AttachV1 implements ChatServiceHandler.AttachV1.
func (c *chatServiceHandler) AttachV1(ctx context.Context, opts attachOptionsV1,
chatUI chat1.ChatUiInterface, notifyUI chat1.NotifyChatInterface) Reply {
var rl []chat1.RateLimit
convID, err := chat1.MakeConvID(opts.ConversationID.String())
if err != nil {
return c.errReply(fmt.Errorf("invalid conv ID: %s", opts.ConversationID))
}
sarg := sendArgV1{
conversationID: convID,
channel: opts.Channel,
mtype: chat1.MessageType_ATTACHMENT,
ephemeralLifetime: opts.EphemeralLifetime,
}
existing, existingRl, err := c.getExistingConvs(ctx, sarg.conversationID, sarg.channel)
if err != nil {
return c.errReply(err)
}
rl = append(rl, existingRl...)
header, err := c.makePostHeader(ctx, sarg, existing)
if err != nil {
return c.errReply(err)
}
rl = append(rl, header.rateLimits...)
vis := keybase1.TLFVisibility_PRIVATE
if header.clientHeader.TlfPublic {
vis = keybase1.TLFVisibility_PUBLIC
}
var ephemeralLifetime *gregor1.DurationSec
if header.clientHeader.EphemeralMetadata != nil {
ephemeralLifetime = &header.clientHeader.EphemeralMetadata.Lifetime
}
arg := chat1.PostFileAttachmentArg{
ConversationID: header.conversationID,
TlfName: header.clientHeader.TlfName,
Visibility: vis,
Filename: opts.Filename,
Title: opts.Title,
EphemeralLifetime: ephemeralLifetime,
}
// check for preview
if len(opts.Preview) > 0 {
loc := chat1.NewPreviewLocationWithFile(opts.Preview)
arg.CallerPreview = &chat1.MakePreviewRes{
Location: &loc,
}
}
c.chatUI.RegisterChatUI(chatUI)
defer c.chatUI.DeregisterChatUI(chatUI)
client, err := GetChatLocalClient(c.G())
if err != nil {
return c.errReply(err)
}
protocols := []rpc.Protocol{
NewStreamUIProtocol(c.G()),
chat1.ChatUiProtocol(c.chatUI),
chat1.NotifyChatProtocol(notifyUI),
}
if err := RegisterProtocolsWithContext(protocols, c.G()); err != nil {
return c.errReply(err)
}
cli, err := GetNotifyCtlClient(c.G())
if err != nil {
return c.errReply(err)
}
channels := keybase1.NotificationChannels{
Chatattachments: true,
}
if err := cli.SetNotifications(context.TODO(), channels); err != nil {
return c.errReply(err)
}
var pres chat1.PostLocalRes
pres, err = client.PostFileAttachmentLocal(ctx, chat1.PostFileAttachmentLocalArg{
SessionID: getSessionID(chatUI),
Arg: arg,
})
rl = append(rl, pres.RateLimits...)
if err != nil {
return c.errReply(err)
}
res := chat1.SendRes{
Message: "attachment sent",
MessageID: &pres.MessageID,
RateLimits: c.aggRateLimits(rl),
}
return Reply{Result: res}
}
// DownloadV1 implements ChatServiceHandler.DownloadV1.
func (c *chatServiceHandler) DownloadV1(ctx context.Context, opts downloadOptionsV1,
chatUI chat1.ChatUiInterface, notifyUI chat1.NotifyChatInterface) Reply {
if opts.NoStream && opts.Output != "-" {
return c.downloadV1NoStream(ctx, opts, chatUI, notifyUI)
}
var fsink Sink
if opts.Output == "-" {
fsink = &StdoutSink{}
} else {
fsink = NewFileSink(c.G(), opts.Output)
}
defer fsink.Close()
sink := c.G().XStreams.ExportWriter(fsink)
c.chatUI.RegisterChatUI(chatUI)
defer c.chatUI.DeregisterChatUI(chatUI)
client, err := GetChatLocalClient(c.G())
if err != nil {
return c.errReply(err)
}
protocols := []rpc.Protocol{
NewStreamUIProtocol(c.G()),
chat1.ChatUiProtocol(c.chatUI),
chat1.NotifyChatProtocol(notifyUI),
}
if err := RegisterProtocolsWithContext(protocols, c.G()); err != nil {
return c.errReply(err)
}
cli, err := GetNotifyCtlClient(c.G())
if err != nil {
return c.errReply(err)
}
channels := keybase1.NotificationChannels{
Chatattachments: true,
}
if err := cli.SetNotifications(context.TODO(), channels); err != nil {
return c.errReply(err)
}
convID, rlimits, err := c.resolveAPIConvID(ctx, opts.ConversationID, opts.Channel)
if err != nil {
return c.errReply(err)
}
arg := chat1.DownloadAttachmentLocalArg{
SessionID: getSessionID(chatUI),
ConversationID: convID,
MessageID: opts.MessageID,
Sink: sink,
Preview: opts.Preview,
IdentifyBehavior: keybase1.TLFIdentifyBehavior_CHAT_CLI,
}
dres, err := client.DownloadAttachmentLocal(ctx, arg)
if err != nil {
return c.errReply(err)
}
rlimits = append(rlimits, dres.RateLimits...)
if opts.Output != "-" {
if err := attachments.Quarantine(ctx, opts.Output); err != nil {
c.G().Log.Warning("failed to quarantine attachment download: %s", err)
}
}
res := chat1.SendRes{
Message: fmt.Sprintf("attachment downloaded to %s", opts.Output),
RateLimits: c.aggRateLimits(rlimits),
IdentifyFailures: dres.IdentifyFailures,
}
return Reply{Result: res}
}
// downloadV1NoStream uses DownloadFileAttachmentLocal instead of DownloadAttachmentLocal.
func (c *chatServiceHandler) downloadV1NoStream(ctx context.Context, opts downloadOptionsV1,
chatUI chat1.ChatUiInterface, notifyUI chat1.NotifyChatInterface) Reply {
c.chatUI.RegisterChatUI(chatUI)
defer c.chatUI.DeregisterChatUI(chatUI)
client, err := GetChatLocalClient(c.G())
if err != nil {
return c.errReply(err)
}
protocols := []rpc.Protocol{
NewStreamUIProtocol(c.G()),
chat1.ChatUiProtocol(c.chatUI),
chat1.NotifyChatProtocol(notifyUI),
}
if err := RegisterProtocolsWithContext(protocols, c.G()); err != nil {
return c.errReply(err)
}
cli, err := GetNotifyCtlClient(c.G())
if err != nil {
return c.errReply(err)
}
channels := keybase1.NotificationChannels{
Chatattachments: true,
}
if err := cli.SetNotifications(context.TODO(), channels); err != nil {
return c.errReply(err)
}
convID, rlimits, err := c.resolveAPIConvID(ctx, opts.ConversationID, opts.Channel)
if err != nil {
return c.errReply(err)
}
arg := chat1.DownloadFileAttachmentLocalArg{