Browse Source

Previous commit didn't work and broke other things. This is a different implementation to fix the same problems.

main
Sam 5 years ago
parent
commit
207ad74bdd
  1. 45
      chat.go
  2. 34
      types.go

45
chat.go

@ -74,6 +74,10 @@ func getNewMessages(k *Keybase, c chan<- ChatAPI, execOptions []string) {
for scanner.Scan() { for scanner.Scan() {
var jsonData ChatAPI var jsonData ChatAPI
json.Unmarshal([]byte(scanner.Text()), &jsonData) json.Unmarshal([]byte(scanner.Text()), &jsonData)
if len([]byte(jsonData.ErrorRaw)) > 0 {
var errorListen = string(jsonData.ErrorRaw)
jsonData.ErrorListen = &errorListen
}
c <- jsonData c <- jsonData
} }
}(scanner, c) }(scanner, c)
@ -151,8 +155,11 @@ func chatAPIOut(k *Keybase, c ChatAPI) (ChatAPI, error) {
if err := json.Unmarshal(cmdOut, &r); err != nil { if err := json.Unmarshal(cmdOut, &r); err != nil {
return ChatAPI{}, err return ChatAPI{}, err
} }
if r.Error != nil { if len([]byte(r.ErrorRaw)) > 0 {
return ChatAPI{}, errors.New(r.Error.Message) var errorRead Error
json.Unmarshal([]byte(r.ErrorRaw), &errorRead)
r.ErrorRead = &errorRead
return r, errors.New(r.ErrorRead.Message)
} }
return r, nil return r, nil
@ -173,7 +180,7 @@ func (c Chat) Send(message ...string) (ChatAPI, error) {
r, err := chatAPIOut(c.keybase, m) r, err := chatAPIOut(c.keybase, m)
if err != nil { if err != nil {
return ChatAPI{}, err return r, err
} }
return r, nil return r, nil
} }
@ -194,7 +201,7 @@ func (c Chat) Reply(replyTo int, message ...string) (ChatAPI, error) {
r, err := chatAPIOut(c.keybase, m) r, err := chatAPIOut(c.keybase, m)
if err != nil { if err != nil {
return ChatAPI{}, err return r, err
} }
return r, nil return r, nil
} }
@ -214,7 +221,7 @@ func (c Chat) Edit(messageID int, message ...string) (ChatAPI, error) {
r, err := chatAPIOut(c.keybase, m) r, err := chatAPIOut(c.keybase, m)
if err != nil { if err != nil {
return ChatAPI{}, err return r, err
} }
return r, nil return r, nil
} }
@ -234,7 +241,7 @@ func (c Chat) React(messageID int, reaction string) (ChatAPI, error) {
r, err := chatAPIOut(c.keybase, m) r, err := chatAPIOut(c.keybase, m)
if err != nil { if err != nil {
return ChatAPI{}, err return r, err
} }
return r, nil return r, nil
} }
@ -250,7 +257,7 @@ func (c Chat) Delete(messageID int) (ChatAPI, error) {
r, err := chatAPIOut(c.keybase, m) r, err := chatAPIOut(c.keybase, m)
if err != nil { if err != nil {
return ChatAPI{}, err return r, err
} }
return r, nil return r, nil
} }
@ -293,7 +300,7 @@ func (c Chat) ReadMessage(messageID int) (*ChatAPI, error) {
r, err := chatAPIOut(c.keybase, m) r, err := chatAPIOut(c.keybase, m)
if err != nil { if err != nil {
return &ChatAPI{}, err return &r, err
} }
r.keybase = *c.keybase r.keybase = *c.keybase
return &r, nil return &r, nil
@ -320,7 +327,7 @@ func (c Chat) Read(count ...int) (*ChatAPI, error) {
r, err := chatAPIOut(c.keybase, m) r, err := chatAPIOut(c.keybase, m)
if err != nil { if err != nil {
return &ChatAPI{}, err return &r, err
} }
r.keybase = *c.keybase r.keybase = *c.keybase
return &r, nil return &r, nil
@ -349,7 +356,7 @@ func (c *ChatAPI) Next(count ...int) (*ChatAPI, error) {
result, err := chatAPIOut(&c.keybase, m) result, err := chatAPIOut(&c.keybase, m)
if err != nil { if err != nil {
return &ChatAPI{}, err return &result, err
} }
k := c.keybase k := c.keybase
*c = result *c = result
@ -380,7 +387,7 @@ func (c *ChatAPI) Previous(count ...int) (*ChatAPI, error) {
result, err := chatAPIOut(&c.keybase, m) result, err := chatAPIOut(&c.keybase, m)
if err != nil { if err != nil {
return &ChatAPI{}, err return &result, err
} }
k := c.keybase k := c.keybase
*c = result *c = result
@ -401,7 +408,7 @@ func (c Chat) Upload(title string, filepath string) (ChatAPI, error) {
r, err := chatAPIOut(c.keybase, m) r, err := chatAPIOut(c.keybase, m)
if err != nil { if err != nil {
return ChatAPI{}, err return r, err
} }
return r, nil return r, nil
} }
@ -418,7 +425,7 @@ func (c Chat) Download(messageID int, filepath string) (ChatAPI, error) {
r, err := chatAPIOut(c.keybase, m) r, err := chatAPIOut(c.keybase, m)
if err != nil { if err != nil {
return ChatAPI{}, err return r, err
} }
return r, nil return r, nil
} }
@ -438,7 +445,7 @@ func (c Chat) LoadFlip(messageID int, conversationID string, flipConversationID
r, err := chatAPIOut(c.keybase, m) r, err := chatAPIOut(c.keybase, m)
if err != nil { if err != nil {
return ChatAPI{}, err return r, err
} }
return r, nil return r, nil
} }
@ -454,7 +461,7 @@ func (c Chat) Pin(messageID int) (ChatAPI, error) {
r, err := chatAPIOut(c.keybase, m) r, err := chatAPIOut(c.keybase, m)
if err != nil { if err != nil {
return ChatAPI{}, err return r, err
} }
return r, nil return r, nil
} }
@ -469,7 +476,7 @@ func (c Chat) Unpin() (ChatAPI, error) {
r, err := chatAPIOut(c.keybase, m) r, err := chatAPIOut(c.keybase, m)
if err != nil { if err != nil {
return ChatAPI{}, err return r, err
} }
return r, nil return r, nil
} }
@ -485,7 +492,7 @@ func (c Chat) Mark(messageID int) (ChatAPI, error) {
r, err := chatAPIOut(c.keybase, m) r, err := chatAPIOut(c.keybase, m)
if err != nil { if err != nil {
return ChatAPI{}, err return r, err
} }
return r, nil return r, nil
} }
@ -497,7 +504,7 @@ func (k *Keybase) ClearCommands() (ChatAPI, error) {
r, err := chatAPIOut(k, m) r, err := chatAPIOut(k, m)
if err != nil { if err != nil {
return ChatAPI{}, err return r, err
} }
return r, nil return r, nil
} }
@ -514,7 +521,7 @@ func (k *Keybase) AdvertiseCommands(advertisements []BotAdvertisement) (ChatAPI,
r, err := chatAPIOut(k, m) r, err := chatAPIOut(k, m)
if err != nil { if err != nil {
return ChatAPI{}, err return r, err
} }
return r, nil return r, nil
} }

34
types.go

@ -1,6 +1,9 @@
package keybase package keybase
import "time" import (
"encoding/json"
"time"
)
// RunOptions holds a set of options to be passed to Run // RunOptions holds a set of options to be passed to Run
type RunOptions struct { type RunOptions struct {
@ -16,20 +19,21 @@ type RunOptions struct {
// ChatAPI holds information about a message received by the `keybase chat api-listen` command // ChatAPI holds information about a message received by the `keybase chat api-listen` command
type ChatAPI struct { type ChatAPI struct {
Type string `json:"type,omitempty"` Type string `json:"type,omitempty"`
Source string `json:"source,omitempty"` Source string `json:"source,omitempty"`
Msg *msg `json:"msg,omitempty"` Msg *msg `json:"msg,omitempty"`
Method string `json:"method,omitempty"` Method string `json:"method,omitempty"`
Params *params `json:"params,omitempty"` Params *params `json:"params,omitempty"`
Message string `json:"message,omitempty"` Message string `json:"message,omitempty"`
ID int `json:"id,omitempty"` ID int `json:"id,omitempty"`
Ratelimits []rateLimits `json:"ratelimits,omitempty"` Ratelimits []rateLimits `json:"ratelimits,omitempty"`
Notification *notification `json:"notification,omitempty"` Notification *notification `json:"notification,omitempty"`
Result *result `json:"result,omitempty"` Result *result `json:"result,omitempty"`
Pagination *pagination `json:"pagination,omitempty"` Pagination *pagination `json:"pagination,omitempty"`
Error *Error `json:"error,omitempty"` ErrorRaw json.RawMessage `json:"error,omitempty"` // Raw JSON string containit any errors returned
ErrorListen *string `json:"error,omitempty"` // Keybase's api-listen command has error messages in this format instead of their normal error output ErrorRead *Error `json:"-"` // Errors returned by any outgoing chat functions such as Read(), Edit(), etc
keybase Keybase // Some methods will need this, so I'm passing it but keeping it unexported ErrorListen *string `json:"-"` // Errors returned by the api-listen command (used in the Run() function)
keybase Keybase // Some methods will need this, so I'm passing it but keeping it unexported
} }
type sender struct { type sender struct {

Loading…
Cancel
Save