Skip to content

Commit c10dc59

Browse files
authored
refactor download progress and fix forward (keybase#24135)
1 parent 22f3f59 commit c10dc59

30 files changed

Lines changed: 386 additions & 394 deletions

‎go/chat/server.go‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,26 +1368,21 @@ func (h *Server) downloadAttachmentLocal(ctx context.Context, uid gregor1.UID, a
13681368

13691369
var identBreaks []keybase1.TLFIdentifyFailure
13701370
ctx = globals.ChatCtx(ctx, h.G(), arg.IdentifyBehavior, &identBreaks, h.identNotifier)
1371-
chatUI := h.getChatUI(arg.SessionID)
13721371
progress := func(bytesComplete, bytesTotal int64) {
1373-
parg := chat1.ChatAttachmentDownloadProgressArg{
1374-
SessionID: arg.SessionID,
1375-
BytesComplete: bytesComplete,
1376-
BytesTotal: bytesTotal,
1377-
}
1378-
_ = chatUI.ChatAttachmentDownloadProgress(ctx, parg)
1372+
h.G().NotifyRouter.HandleChatAttachmentDownloadProgress(ctx, keybase1.UID(uid.String()),
1373+
arg.ConversationID, arg.MessageID, bytesComplete, bytesTotal)
13791374
}
13801375

13811376
h.Debug(ctx, "downloadAttachmentLocal: fetching asset from attachment message: convID: %s messageID: %d",
13821377
arg.ConversationID, arg.MessageID)
13831378

1384-
_ = chatUI.ChatAttachmentDownloadStart(ctx)
13851379
err = attachments.Download(ctx, h.G(), uid, arg.ConversationID,
13861380
arg.MessageID, arg.Sink, arg.Preview, progress, h.remoteClient)
13871381
if err != nil {
13881382
return res, err
13891383
}
1390-
_ = chatUI.ChatAttachmentDownloadDone(ctx)
1384+
h.G().NotifyRouter.HandleChatAttachmentDownloadComplete(ctx, keybase1.UID(uid.String()),
1385+
arg.ConversationID, arg.MessageID)
13911386

13921387
return chat1.DownloadAttachmentLocalRes{
13931388
IdentifyFailures: identBreaks,

‎go/chat/utils/dummy_chat_ui.go‎

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,6 @@ type DummyChatUI struct{}
1111

1212
var _ chat1.ChatUiInterface = (*DummyChatUI)(nil)
1313

14-
func (r DummyChatUI) ChatAttachmentDownloadStart(ctx context.Context, sessionID int) error {
15-
return nil
16-
}
17-
18-
func (r DummyChatUI) ChatAttachmentDownloadProgress(ctx context.Context,
19-
arg chat1.ChatAttachmentDownloadProgressArg) error {
20-
return nil
21-
}
22-
23-
func (r DummyChatUI) ChatAttachmentDownloadDone(ctx context.Context, sessionID int) error {
24-
return nil
25-
}
26-
2714
func (r DummyChatUI) ChatInboxConversation(ctx context.Context, arg chat1.ChatInboxConversationArg) error {
2815
return nil
2916
}
@@ -217,6 +204,12 @@ func (d DummyChatNotifications) ChatAttachmentUploadStart(context.Context, chat1
217204
func (d DummyChatNotifications) ChatAttachmentUploadProgress(context.Context, chat1.ChatAttachmentUploadProgressArg) error {
218205
return nil
219206
}
207+
func (d DummyChatNotifications) ChatAttachmentDownloadProgress(context.Context, chat1.ChatAttachmentDownloadProgressArg) error {
208+
return nil
209+
}
210+
func (d DummyChatNotifications) ChatAttachmentDownloadComplete(context.Context, chat1.ChatAttachmentDownloadCompleteArg) error {
211+
return nil
212+
}
220213
func (d DummyChatNotifications) ChatPaymentInfo(context.Context, chat1.ChatPaymentInfoArg) error {
221214
return nil
222215
}

‎go/chat/utils/null_chat_ui.go‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@ type NullChatUI struct{}
1111

1212
var _ libkb.ChatUI = (*NullChatUI)(nil)
1313

14-
func (u NullChatUI) ChatAttachmentDownloadStart(context.Context) error { return nil }
15-
func (u NullChatUI) ChatAttachmentDownloadProgress(context.Context, chat1.ChatAttachmentDownloadProgressArg) error {
16-
return nil
17-
}
18-
func (u NullChatUI) ChatAttachmentDownloadDone(context.Context) error { return nil }
1914
func (u NullChatUI) ChatInboxUnverified(context.Context, chat1.ChatInboxUnverifiedArg) error {
2015
return nil
2116
}

‎go/client/chat_api_handler.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,8 @@ func (a *ChatAPI) DownloadV1(ctx context.Context, c Call, w io.Writer) error {
817817

818818
// opts are valid for download v1
819819

820-
return a.encodeReply(c, a.svcHandler.DownloadV1(ctx, opts, NewChatAPIUI()), w)
820+
return a.encodeReply(c, a.svcHandler.DownloadV1(ctx, opts, NewChatAPIUI(),
821+
utils.DummyChatNotifications{}), w)
821822
}
822823

823824
func (a *ChatAPI) SetStatusV1(ctx context.Context, c Call, w io.Writer) error {

‎go/client/chat_api_listen_display.go‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,14 @@ func (d *chatNotificationDisplay) ChatAttachmentUploadStart(context.Context, cha
257257
func (d *chatNotificationDisplay) ChatAttachmentUploadProgress(context.Context, chat1.ChatAttachmentUploadProgressArg) error {
258258
return nil
259259
}
260+
func (d *chatNotificationDisplay) ChatAttachmentDownloadProgress(context.Context,
261+
chat1.ChatAttachmentDownloadProgressArg) error {
262+
return nil
263+
}
264+
func (d *chatNotificationDisplay) ChatAttachmentDownloadComplete(context.Context,
265+
chat1.ChatAttachmentDownloadCompleteArg) error {
266+
return nil
267+
}
260268
func (d *chatNotificationDisplay) ChatPaymentInfo(context.Context, chat1.ChatPaymentInfoArg) error {
261269
return nil
262270
}

‎go/client/chat_api_test.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,8 @@ func (c *chatEcho) AttachV1(context.Context, attachOptionsV1, chat1.ChatUiInterf
266266
return Reply{Result: echoOK}
267267
}
268268

269-
func (c *chatEcho) DownloadV1(context.Context, downloadOptionsV1, chat1.ChatUiInterface) Reply {
269+
func (c *chatEcho) DownloadV1(context.Context, downloadOptionsV1, chat1.ChatUiInterface,
270+
chat1.NotifyChatInterface) Reply {
270271
return Reply{Result: echoOK}
271272
}
272273

‎go/client/chat_cli_ui.go‎

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,33 @@ func (n *ChatCLINotifications) ChatAttachmentUploadProgress(ctx context.Context,
5151
percent := int((100 * arg.BytesComplete) / arg.BytesTotal)
5252
if n.lastAttachmentPercent == 0 || percent == 100 || percent-n.lastAttachmentPercent >= 10 {
5353
w := n.terminal.ErrorWriter()
54-
fmt.Fprintf(w, "Attachment upload progress %d%% (%d of %d bytes uploaded)\n", percent, arg.BytesComplete, arg.BytesTotal)
54+
fmt.Fprintf(w, "Attachment upload progress %d%% (%d of %d bytes uploaded)\n", percent,
55+
arg.BytesComplete, arg.BytesTotal)
56+
n.lastAttachmentPercent = percent
57+
}
58+
return nil
59+
}
60+
61+
func (n *ChatCLINotifications) ChatAttachmentDownloadComplete(ctx context.Context,
62+
arg chat1.ChatAttachmentDownloadCompleteArg) error {
63+
if n.noOutput {
64+
return nil
65+
}
66+
w := n.terminal.ErrorWriter()
67+
fmt.Fprintf(w, "Attachment download "+ColorString(n.G(), "magenta", "finished")+"\n")
68+
return nil
69+
}
70+
71+
func (n *ChatCLINotifications) ChatAttachmentDownloadProgress(ctx context.Context,
72+
arg chat1.ChatAttachmentDownloadProgressArg) error {
73+
if n.noOutput {
74+
return nil
75+
}
76+
percent := int((100 * arg.BytesComplete) / arg.BytesTotal)
77+
if n.lastAttachmentPercent == 0 || percent == 100 || percent-n.lastAttachmentPercent >= 10 {
78+
w := n.terminal.ErrorWriter()
79+
fmt.Fprintf(w, "Attachment download progress %d%% (%d of %d bytes downloaded)\n", percent,
80+
arg.BytesComplete, arg.BytesTotal)
5581
n.lastAttachmentPercent = percent
5682
}
5783
return nil
@@ -78,37 +104,6 @@ func NewChatCLIUI(g *libkb.GlobalContext) *ChatCLIUI {
78104
}
79105
}
80106

81-
func (c *ChatCLIUI) ChatAttachmentDownloadStart(context.Context, int) error {
82-
if c.noOutput {
83-
return nil
84-
}
85-
w := c.terminal.ErrorWriter()
86-
fmt.Fprintf(w, "Attachment download "+ColorString(c.G(), "green", "starting")+"\n")
87-
return nil
88-
}
89-
90-
func (c *ChatCLIUI) ChatAttachmentDownloadProgress(ctx context.Context, arg chat1.ChatAttachmentDownloadProgressArg) error {
91-
if c.noOutput {
92-
return nil
93-
}
94-
percent := int((100 * arg.BytesComplete) / arg.BytesTotal)
95-
if c.lastAttachmentPercent == 0 || percent == 100 || percent-c.lastAttachmentPercent >= 10 {
96-
w := c.terminal.ErrorWriter()
97-
fmt.Fprintf(w, "Attachment download progress %d%% (%d of %d bytes downloaded)\n", percent, arg.BytesComplete, arg.BytesTotal)
98-
c.lastAttachmentPercent = percent
99-
}
100-
return nil
101-
}
102-
103-
func (c *ChatCLIUI) ChatAttachmentDownloadDone(context.Context, int) error {
104-
if c.noOutput {
105-
return nil
106-
}
107-
w := c.terminal.ErrorWriter()
108-
fmt.Fprintf(w, "Attachment download "+ColorString(c.G(), "magenta", "finished")+"\n")
109-
return nil
110-
}
111-
112107
func (c *ChatCLIUI) ChatInboxConversation(ctx context.Context, arg chat1.ChatInboxConversationArg) error {
113108
return nil
114109
}

‎go/client/chat_svc_handler.go‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type ChatServiceHandler interface {
2828
ReactionV1(context.Context, reactionOptionsV1) Reply
2929
DeleteV1(context.Context, deleteOptionsV1) Reply
3030
AttachV1(context.Context, attachOptionsV1, chat1.ChatUiInterface, chat1.NotifyChatInterface) Reply
31-
DownloadV1(context.Context, downloadOptionsV1, chat1.ChatUiInterface) Reply
31+
DownloadV1(context.Context, downloadOptionsV1, chat1.ChatUiInterface, chat1.NotifyChatInterface) Reply
3232
SetStatusV1(context.Context, setStatusOptionsV1) Reply
3333
MarkV1(context.Context, markOptionsV1) Reply
3434
SearchInboxV1(context.Context, searchInboxOptionsV1) Reply
@@ -892,9 +892,9 @@ func (c *chatServiceHandler) AttachV1(ctx context.Context, opts attachOptionsV1,
892892

893893
// DownloadV1 implements ChatServiceHandler.DownloadV1.
894894
func (c *chatServiceHandler) DownloadV1(ctx context.Context, opts downloadOptionsV1,
895-
chatUI chat1.ChatUiInterface) Reply {
895+
chatUI chat1.ChatUiInterface, notifyUI chat1.NotifyChatInterface) Reply {
896896
if opts.NoStream && opts.Output != "-" {
897-
return c.downloadV1NoStream(ctx, opts, chatUI)
897+
return c.downloadV1NoStream(ctx, opts, chatUI, notifyUI)
898898
}
899899
var fsink Sink
900900
if opts.Output == "-" {
@@ -914,10 +914,21 @@ func (c *chatServiceHandler) DownloadV1(ctx context.Context, opts downloadOption
914914
protocols := []rpc.Protocol{
915915
NewStreamUIProtocol(c.G()),
916916
chat1.ChatUiProtocol(c.chatUI),
917+
chat1.NotifyChatProtocol(notifyUI),
917918
}
918919
if err := RegisterProtocolsWithContext(protocols, c.G()); err != nil {
919920
return c.errReply(err)
920921
}
922+
cli, err := GetNotifyCtlClient(c.G())
923+
if err != nil {
924+
return c.errReply(err)
925+
}
926+
channels := keybase1.NotificationChannels{
927+
Chatattachments: true,
928+
}
929+
if err := cli.SetNotifications(context.TODO(), channels); err != nil {
930+
return c.errReply(err)
931+
}
921932

922933
convID, rlimits, err := c.resolveAPIConvID(ctx, opts.ConversationID, opts.Channel)
923934
if err != nil {
@@ -955,7 +966,7 @@ func (c *chatServiceHandler) DownloadV1(ctx context.Context, opts downloadOption
955966

956967
// downloadV1NoStream uses DownloadFileAttachmentLocal instead of DownloadAttachmentLocal.
957968
func (c *chatServiceHandler) downloadV1NoStream(ctx context.Context, opts downloadOptionsV1,
958-
chatUI chat1.ChatUiInterface) Reply {
969+
chatUI chat1.ChatUiInterface, notifyUI chat1.NotifyChatInterface) Reply {
959970
c.chatUI.RegisterChatUI(chatUI)
960971
defer c.chatUI.DeregisterChatUI(chatUI)
961972
client, err := GetChatLocalClient(c.G())
@@ -965,10 +976,21 @@ func (c *chatServiceHandler) downloadV1NoStream(ctx context.Context, opts downlo
965976
protocols := []rpc.Protocol{
966977
NewStreamUIProtocol(c.G()),
967978
chat1.ChatUiProtocol(c.chatUI),
979+
chat1.NotifyChatProtocol(notifyUI),
968980
}
969981
if err := RegisterProtocolsWithContext(protocols, c.G()); err != nil {
970982
return c.errReply(err)
971983
}
984+
cli, err := GetNotifyCtlClient(c.G())
985+
if err != nil {
986+
return c.errReply(err)
987+
}
988+
channels := keybase1.NotificationChannels{
989+
Chatattachments: true,
990+
}
991+
if err := cli.SetNotifications(context.TODO(), channels); err != nil {
992+
return c.errReply(err)
993+
}
972994

973995
convID, rlimits, err := c.resolveAPIConvID(ctx, opts.ConversationID, opts.Channel)
974996
if err != nil {

‎go/client/chat_ui_delegate.go‎

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -65,28 +65,6 @@ func (c *DelegateChatUI) getChatUI(sessionID int) *chat1.ChatUiInterface {
6565
return nil
6666
}
6767

68-
func (c *DelegateChatUI) ChatAttachmentDownloadStart(ctx context.Context, sessionID int) error {
69-
if chatUI := c.getChatUI(sessionID); chatUI != nil {
70-
return (*chatUI).ChatAttachmentDownloadStart(ctx, sessionID)
71-
}
72-
return nil
73-
}
74-
75-
func (c *DelegateChatUI) ChatAttachmentDownloadProgress(ctx context.Context,
76-
arg chat1.ChatAttachmentDownloadProgressArg) error {
77-
if chatUI := c.getChatUI(arg.SessionID); chatUI != nil {
78-
return (*chatUI).ChatAttachmentDownloadProgress(ctx, arg)
79-
}
80-
return nil
81-
}
82-
83-
func (c *DelegateChatUI) ChatAttachmentDownloadDone(ctx context.Context, sessionID int) error {
84-
if chatUI := c.getChatUI(sessionID); chatUI != nil {
85-
return (*chatUI).ChatAttachmentDownloadDone(ctx, sessionID)
86-
}
87-
return nil
88-
}
89-
9068
func (c *DelegateChatUI) ChatInboxConversation(ctx context.Context, arg chat1.ChatInboxConversationArg) error {
9169
if chatUI := c.getChatUI(arg.SessionID); chatUI != nil {
9270
return (*chatUI).ChatInboxConversation(ctx, arg)

‎go/client/cmd_chat_download.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func (c *CmdChatDownload) Run() (err error) {
115115
Preview: c.preview,
116116
}
117117
h := newChatServiceHandler(c.G())
118-
reply := h.DownloadV1(context.Background(), opts, NewChatCLIUI(c.G()))
118+
reply := h.DownloadV1(context.Background(), opts, NewChatCLIUI(c.G()), NewChatCLINotifications(c.G()))
119119
if reply.Error != nil {
120120
return errors.New(reply.Error.Message)
121121
}

0 commit comments

Comments
 (0)