forked from keybase/client
/
chat_cli_fetchers.go
88 lines (72 loc) · 2.71 KB
/
chat_cli_fetchers.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
// Copyright 2016 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package client
import (
"fmt"
"os"
"golang.org/x/net/context"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/protocol/chat1"
"github.com/keybase/client/go/protocol/keybase1"
isatty "github.com/mattn/go-isatty"
)
type chatCLIConversationFetcher struct {
query chat1.GetConversationForCLILocalQuery
resolvingRequest chatConversationResolvingRequest
}
func (f chatCLIConversationFetcher) fetch(ctx context.Context, g *libkb.GlobalContext) (conversations chat1.ConversationLocal, messages []chat1.MessageUnboxed, err error) {
chatClient, err := GetChatLocalClient(g)
if err != nil {
return chat1.ConversationLocal{}, nil, fmt.Errorf("Getting chat service client error: %s", err)
}
resolver := &chatConversationResolver{G: g, ChatClient: chatClient}
resolver.TlfClient, err = GetTlfClient(g)
if err != nil {
return chat1.ConversationLocal{}, nil, err
}
hasTTY := isatty.IsTerminal(os.Stdout.Fd())
conversation, _, err := resolver.Resolve(ctx, f.resolvingRequest, chatConversationResolvingBehavior{
CreateIfNotExists: false,
MustNotExist: false,
Interactive: hasTTY,
IdentifyBehavior: keybase1.TLFIdentifyBehavior_CHAT_CLI,
})
if err != nil {
// Resolver errors should already by human readable.
return chat1.ConversationLocal{}, nil, err
}
if conversation == nil {
return chat1.ConversationLocal{}, nil, nil
}
f.query.Conv = *conversation
if conversation.Info.Id == nil || len(conversation.Info.Id) == 0 {
return chat1.ConversationLocal{}, nil, fmt.Errorf("empty conversationInfo.Id: %+v", conversation.Info)
}
gcfclres, err := chatClient.GetConversationForCLILocal(ctx, f.query)
if err != nil {
return chat1.ConversationLocal{}, nil, fmt.Errorf("GetConversationForCLILocal error: %s", err)
}
if gcfclres.Offline {
g.UI.GetTerminalUI().Printf(ColorString("yellow", "WARNING: conversation results obtained in OFFLINE mode\n"))
}
return gcfclres.Conversation, gcfclres.Messages, nil
}
type chatCLIInboxFetcher struct {
query chat1.GetInboxSummaryForCLILocalQuery
}
func (f chatCLIInboxFetcher) fetch(ctx context.Context, g *libkb.GlobalContext) (conversations []chat1.ConversationLocal, err error) {
chatClient, err := GetChatLocalClient(g)
if err != nil {
return nil, fmt.Errorf("Getting chat service client error: %s", err)
}
var convs []chat1.ConversationLocal
res, err := chatClient.GetInboxSummaryForCLILocal(ctx, f.query)
if err != nil {
return nil, err
}
convs = res.Conversations
if res.Offline {
g.UI.GetTerminalUI().Printf(ColorString("yellow", "WARNING: inbox results obtained in OFFLINE mode\n"))
}
return convs, nil
}