From 370fce9406d960990f21b71654dca1435cd0e01a Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 25 Jul 2019 12:20:14 -0400 Subject: [PATCH] Combine chatIn.go and chatOut.go into just chat.go --- chatIn.go => chat.go | 94 ++++++++++++++++++++++++++++++++++++++++ chatOut.go | 100 ------------------------------------------- 2 files changed, 94 insertions(+), 100 deletions(-) rename chatIn.go => chat.go (69%) delete mode 100644 chatOut.go diff --git a/chatIn.go b/chat.go similarity index 69% rename from chatIn.go rename to chat.go index 2746c6a..b9cb197 100644 --- a/chatIn.go +++ b/chat.go @@ -4,6 +4,7 @@ import ( "bufio" "encoding/json" "os/exec" + "strings" "time" ) @@ -99,6 +100,99 @@ func heartbeat(c chan<- ChatAPI, freq time.Duration) { } } +// chatAPIOut sends JSON requests to the chat API and returns its response. +func chatAPIOut(keybasePath string, c ChatAPI) (ChatAPI, error) { + jsonBytes, _ := json.Marshal(c) + + cmd := exec.Command(keybasePath, "chat", "api", "-m", string(jsonBytes)) + cmdOut, err := cmd.Output() + if err != nil { + return ChatAPI{}, err + } + + var r ChatAPI + if err := json.Unmarshal(cmdOut, &r); err != nil { + return ChatAPI{}, err + } + + return r, nil +} + +// Send sends a chat message +func (c Chat) Send(message ...string) (ChatAPI, error) { + m := ChatAPI{ + Params: ¶ms{}, + } + m.Method = "send" + m.Params.Options.Channel = c.Channel + m.Params.Options.Message.Body = strings.Join(message, " ") + + r, err := chatAPIOut(c.keybase.Path, m) + if err != nil { + return ChatAPI{}, err + } + return r, nil +} + +// Edit edits a previously sent chat message +func (c Chat) Edit(messageId int, message ...string) (ChatAPI, error) { + m := ChatAPI{ + Params: ¶ms{}, + } + m.Method = "edit" + m.Params.Options.Channel = c.Channel + m.Params.Options.Message.Body = strings.Join(message, " ") + m.Params.Options.MessageID = messageId + + r, err := chatAPIOut(c.keybase.Path, m) + if err != nil { + return ChatAPI{}, err + } + return r, nil +} + +// React sends a reaction to a message. +func (c Chat) React(messageId int, reaction string) (ChatAPI, error) { + m := ChatAPI{ + Params: ¶ms{}, + } + m.Method = "reaction" + m.Params.Options.Channel = c.Channel + m.Params.Options.Message.Body = reaction + m.Params.Options.MessageID = messageId + + r, err := chatAPIOut(c.keybase.Path, m) + if err != nil { + return ChatAPI{}, err + } + return r, nil +} + +// Delete deletes a chat message +func (c Chat) Delete(messageId int) (ChatAPI, error) { + m := ChatAPI{ + Params: ¶ms{}, + } + m.Method = "delete" + m.Params.Options.Channel = c.Channel + m.Params.Options.MessageID = messageId + + r, err := chatAPIOut(c.keybase.Path, m) + if err != nil { + return ChatAPI{}, err + } + return r, nil +} + +// ChatList returns a list of all conversations. +func (k *Keybase) ChatList() (ChatAPI, error) { + m := ChatAPI{} + m.Method = "list" + + r, err := chatAPIOut(k.Path, m) + return r, err +} + // Read fetches chat messages from a conversation. By default, 10 messages will // be fetched at a time. However, if count is passed, then that is the number of // messages that will be fetched. diff --git a/chatOut.go b/chatOut.go deleted file mode 100644 index 68b131e..0000000 --- a/chatOut.go +++ /dev/null @@ -1,100 +0,0 @@ -package keybase - -import ( - "encoding/json" - "os/exec" - "strings" -) - -// chatAPIOut sends JSON requests to the chat API and returns its response. -func chatAPIOut(keybasePath string, c ChatAPI) (ChatAPI, error) { - jsonBytes, _ := json.Marshal(c) - - cmd := exec.Command(keybasePath, "chat", "api", "-m", string(jsonBytes)) - cmdOut, err := cmd.Output() - if err != nil { - return ChatAPI{}, err - } - - var r ChatAPI - if err := json.Unmarshal(cmdOut, &r); err != nil { - return ChatAPI{}, err - } - - return r, nil -} - -// Send sends a chat message -func (c Chat) Send(message ...string) (ChatAPI, error) { - m := ChatAPI{ - Params: ¶ms{}, - } - m.Method = "send" - m.Params.Options.Channel = c.Channel - m.Params.Options.Message.Body = strings.Join(message, " ") - - r, err := chatAPIOut(c.keybase.Path, m) - if err != nil { - return ChatAPI{}, err - } - return r, nil -} - -// Edit edits a previously sent chat message -func (c Chat) Edit(messageId int, message ...string) (ChatAPI, error) { - m := ChatAPI{ - Params: ¶ms{}, - } - m.Method = "edit" - m.Params.Options.Channel = c.Channel - m.Params.Options.Message.Body = strings.Join(message, " ") - m.Params.Options.MessageID = messageId - - r, err := chatAPIOut(c.keybase.Path, m) - if err != nil { - return ChatAPI{}, err - } - return r, nil -} - -// React sends a reaction to a message. -func (c Chat) React(messageId int, reaction string) (ChatAPI, error) { - m := ChatAPI{ - Params: ¶ms{}, - } - m.Method = "reaction" - m.Params.Options.Channel = c.Channel - m.Params.Options.Message.Body = reaction - m.Params.Options.MessageID = messageId - - r, err := chatAPIOut(c.keybase.Path, m) - if err != nil { - return ChatAPI{}, err - } - return r, nil -} - -// Delete deletes a chat message -func (c Chat) Delete(messageId int) (ChatAPI, error) { - m := ChatAPI{ - Params: ¶ms{}, - } - m.Method = "delete" - m.Params.Options.Channel = c.Channel - m.Params.Options.MessageID = messageId - - r, err := chatAPIOut(c.keybase.Path, m) - if err != nil { - return ChatAPI{}, err - } - return r, nil -} - -// ChatList returns a list of all conversations. -func (k *Keybase) ChatList() (ChatAPI, error) { - m := ChatAPI{} - m.Method = "list" - - r, err := chatAPIOut(k.Path, m) - return r, err -}