From 93daa56db1e3fda2a97189d0faad7fd64a05ab36 Mon Sep 17 00:00:00 2001 From: Sam Date: Sun, 16 Feb 2020 23:49:58 -0500 Subject: [PATCH] Add GetConversations() --- chat.go | 20 +++++++++++++++----- types.go | 11 +++++++++++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/chat.go b/chat.go index da6c6b9..0545e8c 100644 --- a/chat.go +++ b/chat.go @@ -422,19 +422,29 @@ func (k *Keybase) DeleteByConvID(convID chat1.ConvIDStr, msgID chat1.MessageID) } // GetConversations returns a list of all conversations. Optionally, you can filter by unread -func (k *Keybase) GetConversations(unreadOnly bool) (SendResponse, error) { - var r SendResponse +func (k *Keybase) GetConversations(unreadOnly bool) ([]chat1.ConvSummary, error) { + var r Inbox opts := SendMessageOptions{ UnreadOnly: unreadOnly, } - r, err := k.SendMessage("list", opts) + arg := newSendMessageArg(opts) + arg.Method = "list" + + jsonBytes, _ := json.Marshal(arg) + + cmdOut, err := k.Exec("chat", "api", "-m", string(jsonBytes)) if err != nil { - return r, err + return []chat1.ConvSummary{}, err } - return r, nil + err = json.Unmarshal(cmdOut, &r) + if err != nil { + return []chat1.ConvSummary{}, err + } + + return r.Result.Convs, nil } // ChatList returns a list of all conversations. diff --git a/types.go b/types.go index 79d1879..aaa4dfc 100644 --- a/types.go +++ b/types.go @@ -76,6 +76,17 @@ func newSendMessageArg(options SendMessageOptions) sendMessageArg { } } +// Result holds the result porstion of a `list` method sent to the API +type Result struct { + Convs []chat1.ConvSummary `json:"conversations"` +} + +// Inbox holds the results of a `list`method sent to the API +type Inbox struct { + Result Result `json:"result"` + Error *Error `json:"error,omitempty"` +} + // SendResponse holds the data returned by the send method in the API type SendResponse struct { Result chat1.SendRes `json:"result"`