Skip to content

Commit 6dc7e5c

Browse files
authored
Add CLI chat retention-policy, read policy updates (keybase#10302)
* add CLI chat retention-policy, read policy updates * plurals
1 parent 66a9a8d commit 6dc7e5c

29 files changed

Lines changed: 1009 additions & 66 deletions

‎go/chat/boxer.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1540,7 +1540,7 @@ func (b *Boxer) compareHeadersMBV1(ctx context.Context, hServer chat1.MessageCli
15401540
return NewPermanentUnboxingError(NewHeaderMismatchError("MessageType"))
15411541
}
15421542

1543-
// Note: Supersedes, Deletes, and DeleteHistory are not checked because they are not
1543+
// Note: Supersedes, Deletes, and some other fields are not checked because they are not
15441544
// part of MessageClientHeaderVerified.
15451545

15461546
// Prev

‎go/chat/inboxsource.go‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,16 @@ func (s *RemoteInboxSource) MembershipUpdate(ctx context.Context, uid gregor1.UI
440440
return res, err
441441
}
442442

443+
func (s *RemoteInboxSource) SetConvRetention(ctx context.Context, uid gregor1.UID, vers chat1.InboxVers,
444+
convID chat1.ConversationID, policy chat1.RetentionPolicy) (res *chat1.ConversationLocal, err error) {
445+
return res, err
446+
}
447+
448+
func (s *RemoteInboxSource) SetTeamRetention(ctx context.Context, uid gregor1.UID, vers chat1.InboxVers,
449+
teamID keybase1.TeamID, policy chat1.RetentionPolicy) (res []chat1.ConversationLocal, err error) {
450+
return res, err
451+
}
452+
443453
type HybridInboxSource struct {
444454
globals.Contextified
445455
utils.DebugLabeler
@@ -643,6 +653,16 @@ func (s *HybridInboxSource) getConvLocal(ctx context.Context, uid gregor1.UID,
643653
return &ib.Convs[0], nil
644654
}
645655

656+
// Get convs. May return fewer or no conversations.
657+
func (s *HybridInboxSource) getConvsLocal(ctx context.Context, uid gregor1.UID,
658+
convIDs []chat1.ConversationID) ([]chat1.ConversationLocal, error) {
659+
// Read back affected conversation so we can send it to the frontend
660+
ib, _, err := s.Read(ctx, uid, nil, true, &chat1.GetInboxLocalQuery{
661+
ConvIDs: convIDs,
662+
}, nil)
663+
return ib.Convs, err
664+
}
665+
646666
func (s *HybridInboxSource) NewMessage(ctx context.Context, uid gregor1.UID, vers chat1.InboxVers,
647667
convID chat1.ConversationID, msg chat1.MessageBoxed, maxMsgs []chat1.MessageSummary) (conv *chat1.ConversationLocal, err error) {
648668
defer s.Trace(ctx, func() error { return err }, "NewMessage")()
@@ -808,6 +828,47 @@ func (s *HybridInboxSource) MembershipUpdate(ctx context.Context, uid gregor1.UI
808828
return res, nil
809829
}
810830

831+
func (s *HybridInboxSource) SetConvRetention(ctx context.Context, uid gregor1.UID, vers chat1.InboxVers,
832+
convID chat1.ConversationID, policy chat1.RetentionPolicy) (res *chat1.ConversationLocal, err error) {
833+
return s.modConversation(ctx, "SetConvRetention", uid, convID, func(ctx context.Context, ib *storage.Inbox) error {
834+
return ib.SetConvRetention(ctx, vers, convID, policy)
835+
})
836+
}
837+
838+
func (s *HybridInboxSource) SetTeamRetention(ctx context.Context, uid gregor1.UID, vers chat1.InboxVers,
839+
teamID keybase1.TeamID, policy chat1.RetentionPolicy) (convs []chat1.ConversationLocal, err error) {
840+
defer s.Trace(ctx, func() error { return err }, "SetTeamRetention")()
841+
ib := storage.NewInbox(s.G(), uid)
842+
convIDs, cerr := ib.SetTeamRetention(ctx, vers, teamID, policy)
843+
if cerr != nil {
844+
err = s.handleInboxError(ctx, cerr, uid)
845+
return nil, err
846+
}
847+
if convs, err = s.getConvsLocal(ctx, uid, convIDs); err != nil {
848+
s.Debug(ctx, "SetTeamRetention: unable to load conversations: convIDs: %v err: %s",
849+
convIDs, err.Error())
850+
return nil, nil
851+
}
852+
return convs, nil
853+
}
854+
855+
func (s *HybridInboxSource) modConversation(ctx context.Context, debugLabel string, uid gregor1.UID, convID chat1.ConversationID,
856+
mod func(context.Context, *storage.Inbox) error) (
857+
conv *chat1.ConversationLocal, err error) {
858+
defer s.Trace(ctx, func() error { return err }, debugLabel)()
859+
ib := storage.NewInbox(s.G(), uid)
860+
if cerr := mod(ctx, ib); cerr != nil {
861+
err = s.handleInboxError(ctx, cerr, uid)
862+
return nil, err
863+
}
864+
if conv, err = s.getConvLocal(ctx, uid, convID); err != nil {
865+
s.Debug(ctx, "%v: unable to load conversation: convID: %s err: %s",
866+
debugLabel, convID, err.Error())
867+
return nil, nil
868+
}
869+
return conv, nil
870+
}
871+
811872
func (s *localizerPipeline) localizeConversationsPipeline(ctx context.Context, uid gregor1.UID,
812873
convs []chat1.Conversation, maxUnbox *int, localizeCb *chan NonblockInboxResult) ([]chat1.ConversationLocal, error) {
813874

@@ -1016,6 +1077,9 @@ func (s *localizerPipeline) localizeConversation(ctx context.Context, uid gregor
10161077
}
10171078
}
10181079
}
1080+
conversationLocal.Expunge = conversationRemote.Expunge
1081+
conversationLocal.ConvRetention = conversationRemote.ConvRetention
1082+
conversationLocal.TeamRetention = conversationRemote.TeamRetention
10191083

10201084
if len(conversationRemote.MaxMsgSummaries) == 0 {
10211085
errMsg := "conversation has an empty MaxMsgSummaries field"

‎go/chat/keyfinder.go‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,3 @@ func (k *KeyFinderImpl) SetNameInfoSourceOverride(ni types.NameInfoSource) {
161161
func tlfIDToTeamdID(tlfID chat1.TLFID) (keybase1.TeamID, error) {
162162
return keybase1.TeamIDFromString(tlfID.String())
163163
}
164-
165-
func teamIDToTLFID(teamID keybase1.TeamID) (chat1.TLFID, error) {
166-
return chat1.MakeTLFID(teamID.String())
167-
}

‎go/chat/push.go‎

Lines changed: 126 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -495,10 +495,6 @@ func (g *PushHandler) Activity(ctx context.Context, m gregor.OutOfBandMessage) (
495495
}}
496496
g.G().Syncer.SendChatStaleNotifications(ctx, m.UID().Bytes(), supdate, true)
497497
}
498-
499-
if g.badger != nil && nm.UnreadUpdate != nil {
500-
g.badger.PushChatUpdate(*nm.UnreadUpdate, nm.InboxVers)
501-
}
502498
case types.ActionReadMessage:
503499
var nm chat1.ReadMessagePayload
504500
err = dec.Decode(&nm)
@@ -519,10 +515,6 @@ func (g *PushHandler) Activity(ctx context.Context, m gregor.OutOfBandMessage) (
519515
ConvID: nm.ConvID,
520516
Conv: g.presentUIItem(conv),
521517
})
522-
523-
if g.badger != nil && nm.UnreadUpdate != nil {
524-
g.badger.PushChatUpdate(*nm.UnreadUpdate, nm.InboxVers)
525-
}
526518
case types.ActionSetStatus:
527519
var nm chat1.SetStatusPayload
528520
err = dec.Decode(&nm)
@@ -542,10 +534,6 @@ func (g *PushHandler) Activity(ctx context.Context, m gregor.OutOfBandMessage) (
542534
Status: nm.Status,
543535
Conv: g.presentUIItem(conv),
544536
})
545-
546-
if g.badger != nil && nm.UnreadUpdate != nil {
547-
g.badger.PushChatUpdate(*nm.UnreadUpdate, nm.InboxVers)
548-
}
549537
case types.ActionSetAppNotificationSettings:
550538
var nm chat1.SetAppNotificationSettingsPayload
551539
err = dec.Decode(&nm)
@@ -598,11 +586,6 @@ func (g *PushHandler) Activity(ctx context.Context, m gregor.OutOfBandMessage) (
598586
activity = chat1.NewChatActivityWithNewConversation(chat1.NewConversationInfo{
599587
Conv: *g.presentUIItem(conv),
600588
})
601-
602-
if g.badger != nil && nm.UnreadUpdate != nil {
603-
g.badger.PushChatUpdate(*nm.UnreadUpdate, nm.InboxVers)
604-
}
605-
606589
case types.ActionTeamType:
607590
var nm chat1.TeamTypePayload
608591
err = dec.Decode(&nm)
@@ -621,13 +604,12 @@ func (g *PushHandler) Activity(ctx context.Context, m gregor.OutOfBandMessage) (
621604
TeamType: nm.TeamType,
622605
Conv: g.presentUIItem(conv),
623606
})
624-
625-
if g.badger != nil && nm.UnreadUpdate != nil {
626-
g.badger.PushChatUpdate(*nm.UnreadUpdate, nm.InboxVers)
627-
}
628607
default:
629608
g.Debug(ctx, "unhandled chat.activity action %q", action)
630609
}
610+
if g.badger != nil && gm.UnreadUpdate != nil {
611+
g.badger.PushChatUpdate(*gm.UnreadUpdate, gm.InboxVers)
612+
}
631613
g.notifyNewChatActivity(ctx, m.UID(), convID, conv, &activity)
632614
}(bctx)
633615
return nil
@@ -861,6 +843,125 @@ func (g *PushHandler) TeamChannels(ctx context.Context, m gregor.OutOfBandMessag
861843
return nil
862844
}
863845

846+
func (g *PushHandler) SetConvRetention(ctx context.Context, m gregor.OutOfBandMessage) (err error) {
847+
var identBreaks []keybase1.TLFIdentifyFailure
848+
ctx = Context(ctx, g.G(), keybase1.TLFIdentifyBehavior_CHAT_GUI, &identBreaks,
849+
g.identNotifier)
850+
defer g.Trace(ctx, func() error { return err }, "SetConvRetention")()
851+
if m.Body() == nil {
852+
return errors.New("gregor handler for SetConvRetention update: nil message body")
853+
}
854+
855+
var update chat1.SetConvRetentionUpdate
856+
reader := bytes.NewReader(m.Body().Bytes())
857+
dec := codec.NewDecoder(reader, &codec.MsgpackHandle{WriteExt: true})
858+
err = dec.Decode(&update)
859+
if err != nil {
860+
return err
861+
}
862+
uid := gregor1.UID(m.UID().Bytes())
863+
864+
// Order updates based on inbox version of the update from the server
865+
cb := g.orderer.WaitForTurn(ctx, uid, update.InboxVers)
866+
bctx := BackgroundContext(ctx, g.G())
867+
go func(ctx context.Context) {
868+
defer g.Trace(ctx, func() error { return err }, "SetConvRetention(goroutine)")()
869+
<-cb
870+
g.Lock()
871+
defer g.Unlock()
872+
defer g.orderer.CompleteTurn(ctx, uid, update.InboxVers)
873+
874+
// Update inbox
875+
conv, err := g.G().InboxSource.SetConvRetention(ctx, m.UID().Bytes(), update.InboxVers,
876+
update.ConvID, update.Policy)
877+
if err != nil {
878+
g.Debug(ctx, "SetConvRetention: unable to update inbox: %s", err.Error())
879+
return
880+
}
881+
if conv == nil {
882+
return
883+
}
884+
// Send notify for each conversation ID
885+
if conv.GetTopicType() == chat1.TopicType_CHAT {
886+
if g.shouldSendNotifications() {
887+
g.G().NotifyRouter.HandleChatSetConvRetention(ctx, keybase1.UID(uid.String()),
888+
conv.GetConvID(), g.presentUIItem(conv))
889+
} else {
890+
supdate := []chat1.ConversationStaleUpdate{chat1.ConversationStaleUpdate{
891+
ConvID: conv.GetConvID(),
892+
UpdateType: chat1.StaleUpdateType_CLEAR,
893+
}}
894+
g.G().Syncer.SendChatStaleNotifications(ctx, uid, supdate, false)
895+
}
896+
}
897+
}(bctx)
898+
899+
return nil
900+
}
901+
902+
func (g *PushHandler) SetTeamRetention(ctx context.Context, m gregor.OutOfBandMessage) (err error) {
903+
var identBreaks []keybase1.TLFIdentifyFailure
904+
ctx = Context(ctx, g.G(), keybase1.TLFIdentifyBehavior_CHAT_GUI, &identBreaks,
905+
g.identNotifier)
906+
defer g.Trace(ctx, func() error { return err }, "SetTeamRetention")()
907+
if m.Body() == nil {
908+
return errors.New("gregor handler for SetTeamRetention update: nil message body")
909+
}
910+
911+
var update chat1.SetTeamRetentionUpdate
912+
reader := bytes.NewReader(m.Body().Bytes())
913+
dec := codec.NewDecoder(reader, &codec.MsgpackHandle{WriteExt: true})
914+
err = dec.Decode(&update)
915+
if err != nil {
916+
return err
917+
}
918+
uid := gregor1.UID(m.UID().Bytes())
919+
920+
// Order updates based on inbox version of the update from the server
921+
cb := g.orderer.WaitForTurn(ctx, uid, update.InboxVers)
922+
bctx := BackgroundContext(ctx, g.G())
923+
go func(ctx context.Context) {
924+
defer g.Trace(ctx, func() error { return err }, "SetTeamRetention(goroutine)")()
925+
<-cb
926+
g.Lock()
927+
defer g.Unlock()
928+
defer g.orderer.CompleteTurn(ctx, uid, update.InboxVers)
929+
930+
// Update inbox
931+
var convs []chat1.ConversationLocal
932+
if convs, err = g.G().InboxSource.SetTeamRetention(ctx, m.UID().Bytes(), update.InboxVers,
933+
update.TeamID, update.Policy); err != nil {
934+
g.Debug(ctx, "SetTeamRetention: unable to update inbox: %s", err.Error())
935+
return
936+
}
937+
if len(convs) == 0 {
938+
g.Debug(ctx, "SetTeamRetention: no local convs affected")
939+
return
940+
}
941+
// Send notify for each conversation ID
942+
var convUIItems []chat1.InboxUIItem
943+
var staleUpdates []chat1.ConversationStaleUpdate
944+
for _, conv := range convs {
945+
if conv.GetTopicType() == chat1.TopicType_CHAT {
946+
convUIItems = append(convUIItems, *g.presentUIItem(&conv))
947+
staleUpdates = append(staleUpdates, chat1.ConversationStaleUpdate{
948+
ConvID: conv.GetConvID(),
949+
UpdateType: chat1.StaleUpdateType_CLEAR,
950+
})
951+
}
952+
}
953+
954+
if g.shouldSendNotifications() {
955+
g.G().NotifyRouter.HandleChatSetTeamRetention(ctx, keybase1.UID(uid.String()), update.TeamID, convUIItems)
956+
} else {
957+
g.G().Syncer.SendChatStaleNotifications(ctx, uid, staleUpdates, false)
958+
}
959+
960+
}(bctx)
961+
962+
return nil
963+
}
964+
864965
func (g *PushHandler) HandleOobm(ctx context.Context, obm gregor.OutOfBandMessage) (bool, error) {
865966
if obm.System() == nil {
866967
return false, errors.New("nil system in out of band message")
@@ -879,6 +980,10 @@ func (g *PushHandler) HandleOobm(ctx context.Context, obm gregor.OutOfBandMessag
879980
return true, g.MembershipUpdate(ctx, obm)
880981
case types.PushTeamChannels:
881982
return true, g.TeamChannels(ctx, obm)
983+
case types.PushConvRetention:
984+
return true, g.SetConvRetention(ctx, obm)
985+
case types.PushTeamRetention:
986+
return true, g.SetTeamRetention(ctx, obm)
882987
}
883988

884989
return false, nil

‎go/chat/server_test.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1727,6 +1727,8 @@ type serverChatListener struct {
17271727
inboxSynced chan chat1.ChatSyncResult
17281728
}
17291729

1730+
var _ libkb.NotifyListener = (*serverChatListener)(nil)
1731+
17301732
func (n *serverChatListener) ChatIdentifyUpdate(update keybase1.CanonicalTLFNameAndIDWithBreaks) {
17311733
n.identifyUpdate <- update
17321734
}

0 commit comments

Comments
 (0)