Re-organize everything to make it cleaner... hopefully

This commit is contained in:
Sam
2019-06-08 11:36:04 -04:00
parent f8cc1058af
commit 003ba3a4e0
2 changed files with 123 additions and 93 deletions

View File

@ -13,6 +13,7 @@ type chatOut struct { // not exported
} }
type chatOutChannel struct { type chatOutChannel struct {
Name string `json:"name"` Name string `json:"name"`
Public bool `json:"public"`
MembersType string `json:"members_type"` MembersType string `json:"members_type"`
TopicName string `json:"topic_name"` TopicName string `json:"topic_name"`
} }
@ -47,7 +48,7 @@ type chatOutResultChannel struct {
TopicType string `json:"topic_type,omitempty"` TopicType string `json:"topic_type,omitempty"`
TopicName string `json:"topic_name,omitempty"` TopicName string `json:"topic_name,omitempty"`
} }
type chatOutResultConversations struct { type conversation struct {
ID string `json:"id"` ID string `json:"id"`
Channel chatOutResultChannel `json:"channel"` Channel chatOutResultChannel `json:"channel"`
Unread bool `json:"unread"` Unread bool `json:"unread"`
@ -59,7 +60,7 @@ type ChatOut struct { // exported
Message string `json:"message,omitempty"` Message string `json:"message,omitempty"`
ID int `json:"id,omitempty"` ID int `json:"id,omitempty"`
Ratelimits []chatOutResultRatelimits `json:"ratelimits,omitempty"` Ratelimits []chatOutResultRatelimits `json:"ratelimits,omitempty"`
Conversations []chatOutResultConversations `json:"conversations,omitempty"` Conversations []conversation `json:"conversations,omitempty"`
Offline bool `json:"offline,omitempty"` Offline bool `json:"offline,omitempty"`
} }
@ -81,92 +82,51 @@ func chatAPIOut(keybasePath string, c chatOut) (chatOutResult, error) {
return r, nil return r, nil
} }
// ChatSendText() sends a chat message to a user. // Send() sends a chat message
func (k Keybase) ChatSendText(user string, message ...string) (ChatOut, error) { func (c Chat) Send(message ...string) (ChatOut, error) {
m := chatOut{} m := chatOut{}
m.Method = "send" m.Method = "send"
m.Params.Options.Channel.Name = user m.Params.Options.Channel.Name = c.Name
m.Params.Options.Channel.Public = c.Public
m.Params.Options.Channel.MembersType = c.MembersType
m.Params.Options.Channel.TopicName = c.TopicName
m.Params.Options.Message.Body = strings.Join(message, " ") m.Params.Options.Message.Body = strings.Join(message, " ")
r, err := chatAPIOut(k.path, m) r, err := chatAPIOut(c.keybase.Path, m)
if err != nil { if err != nil {
return ChatOut{}, err return ChatOut{}, err
} }
return r.Result, nil return r.Result, nil
} }
// ChatSendTextTeam() sends a chat message to a team. // React() sends a reaction to a message.
func (k Keybase) ChatSendTextTeam(team, channel string, message ...string) (ChatOut, error) { func (c Chat) React(reaction string, messageId int) (ChatOut, error) {
m := chatOut{}
m.Method = "send"
m.Params.Options.Channel.Name = team
m.Params.Options.Channel.MembersType = "team"
m.Params.Options.Channel.TopicName = channel
m.Params.Options.Message.Body = strings.Join(message, " ")
r, err := chatAPIOut(k.path, m)
if err != nil {
return ChatOut{}, err
}
return r.Result, nil
}
// ChatReact() sends a reaction to a user's message.
func (k Keybase) ChatReact(user, reaction string, messageId int) (ChatOut, error) {
m := chatOut{} m := chatOut{}
m.Method = "reaction" m.Method = "reaction"
m.Params.Options.Channel.Name = user m.Params.Options.Channel.Name = c.Name
m.Params.Options.MessageID = messageId m.Params.Options.Channel.MembersType = c.MembersType
m.Params.Options.Channel.TopicName = c.TopicName
m.Params.Options.Message.Body = reaction m.Params.Options.Message.Body = reaction
m.Params.Options.MessageID = messageId
r, err := chatAPIOut(k.path, m) r, err := chatAPIOut(c.keybase.Path, m)
if err != nil { if err != nil {
return ChatOut{}, err return ChatOut{}, err
} }
return r.Result, nil return r.Result, nil
} }
// ChatReactTeam() sends a reaction to a message on a team. // Delete() deletes a chat message
func (k Keybase) ChatReactTeam(team, channel, reaction string, messageId int) (ChatOut, error) { func (c Chat) Delete(messageId int) (ChatOut, error) {
m := chatOut{}
m.Method = "reaction"
m.Params.Options.Channel.Name = team
m.Params.Options.Channel.MembersType = "team"
m.Params.Options.Channel.TopicName = channel
m.Params.Options.MessageID = messageId
m.Params.Options.Message.Body = reaction
r, err := chatAPIOut(k.path, m)
if err != nil {
return ChatOut{}, err
}
return r.Result, nil
}
// ChatDeleteMessage() deletes a message from a one-on-one conversation.
func (k Keybase) ChatDeleteMessage(user string, messageId int) (ChatOut, error) {
m := chatOut{} m := chatOut{}
m.Method = "delete" m.Method = "delete"
m.Params.Options.Channel.Name = user m.Params.Options.Channel.Name = c.Name
m.Params.Options.Channel.Public = c.Public
m.Params.Options.Channel.MembersType = c.MembersType
m.Params.Options.Channel.TopicName = c.TopicName
m.Params.Options.MessageID = messageId m.Params.Options.MessageID = messageId
r, err := chatAPIOut(k.path, m) r, err := chatAPIOut(c.keybase.Path, m)
if err != nil {
return ChatOut{}, err
}
return r.Result, nil
}
// ChatDeleteMessageTeam() deletes a message from a team conversation.
func (k Keybase) ChatDeleteMessageTeam(team, channel string, messageId int) (ChatOut, error) {
m := chatOut{}
m.Method = "delete"
m.Params.Options.Channel.Name = team
m.Params.Options.Channel.MembersType = "team"
m.Params.Options.Channel.TopicName = channel
m.Params.Options.MessageID = messageId
r, err := chatAPIOut(k.path, m)
if err != nil { if err != nil {
return ChatOut{}, err return ChatOut{}, err
} }
@ -174,10 +134,10 @@ func (k Keybase) ChatDeleteMessageTeam(team, channel string, messageId int) (Cha
} }
// ChatList() returns a list of all conversations. // ChatList() returns a list of all conversations.
func (k Keybase) ChatList() ([]chatOutResultConversations, error) { func (k Keybase) ChatList() ([]conversation, error) {
m := chatOut{} m := chatOut{}
m.Method = "list" m.Method = "list"
r, err := chatAPIOut(k.path, m) r, err := chatAPIOut(k.Path, m)
return r.Result.Conversations, err return r.Result.Conversations, err
} }

View File

@ -5,21 +5,51 @@ import (
"os/exec" "os/exec"
) )
// Possible MemberTypes
const (
TEAM string = "team"
USER string = "impteamnative"
)
// Possible TopicTypes
const (
DEV string = "dev"
CHAT string = "chat"
)
// Keybase holds basic information about the local Keybase executable
type Keybase struct { type Keybase struct {
path string Path string
Username string
LoggedIn bool
Version string
}
// Channel is a map of options that can be passed to NewChat()
type Channel map[string]interface{}
// Chat holds basic information about a specific conversation
type Chat struct {
keybase Keybase
Name string
Public bool
MembersType string
TopicName string
TopicType string
}
type chat interface {
Send(message ...string) (ChatOut, error)
React(messageId int, reaction string) (ChatOut, error)
Delete(messageId int) (ChatOut, error)
} }
type keybase interface { type keybase interface {
ChatSendText(user string, message ...string) (ChatOut, error) NewChat(channel map[string]interface{}) Chat
ChatSendTextTeam(team, channel, message string) (ChatOut, error) ChatList() ([]conversation, error)
ChatReact(user, reaction string, messageId int) (ChatOut, error) loggedIn() bool
ChatReactTeam(team, channel, reaction string, messageId int) (ChatOut, error) username() string
ChatDeleteMessage(user string, messageId int) (ChatOut, error) version() string
ChatDeleteMessageTeam(team, channel string, messageId int) (ChatOut, error)
ChatList() ([]chatOutResultConversations, error)
LoggedIn() bool
Username() string
Version() string
} }
type status struct { type status struct {
@ -28,16 +58,56 @@ type status struct {
} }
// New() returns a new instance of Keybase object. Optionally, you can pass a string containing the path to the Keybase executable as the first argument. // New() returns a new instance of Keybase object. Optionally, you can pass a string containing the path to the Keybase executable as the first argument.
func New(path ...string) Keybase { func NewKeybase(path ...string) Keybase {
k := Keybase{}
if len(path) < 1 { if len(path) < 1 {
return Keybase{path: "/usr/bin/keybase"} k.Path = "keybase"
} else {
k.Path = path[0]
} }
return Keybase{path: path[0]} k.Version = k.version()
k.LoggedIn = k.loggedIn()
if k.LoggedIn == true {
k.Username = k.username()
}
return k
} }
// Username() returns the username of the currently logged-in Keybase user. // Return a new Chat instance
func (k Keybase) Username() string { func (k Keybase) NewChat(channel map[string]interface{}) Chat {
cmd := exec.Command(k.path, "status", "-j") var c Chat = Chat{}
c.keybase = k
if value, ok := channel["Name"].(string); ok == true {
c.Name = value
}
if value, ok := channel["Public"].(bool); ok == true {
c.Public = value
} else {
c.Public = false
}
if value, ok := channel["MembersType"].(string); ok == true {
c.MembersType = value
} else {
c.MembersType = USER
}
if value, ok := channel["TopicName"].(string); ok == true {
c.TopicName = value
} else {
if c.MembersType == TEAM {
c.TopicName = "general"
}
}
if value, ok := channel["TopicType"].(string); ok == true {
c.TopicType = value
} else {
c.TopicType = CHAT
}
return c
}
// username() returns the username of the currently logged-in Keybase user.
func (k Keybase) username() string {
cmd := exec.Command(k.Path, "status", "-j")
cmdOut, err := cmd.Output() cmdOut, err := cmd.Output()
if err != nil { if err != nil {
return "" return ""
@ -49,9 +119,9 @@ func (k Keybase) Username() string {
return s.Username return s.Username
} }
// LoggedIn() returns true if Keybase is currently logged in, otherwise returns false. // loggedIn() returns true if Keybase is currently logged in, otherwise returns false.
func (k Keybase) LoggedIn() bool { func (k Keybase) loggedIn() bool {
cmd := exec.Command(k.path, "status", "-j") cmd := exec.Command(k.Path, "status", "-j")
cmdOut, err := cmd.Output() cmdOut, err := cmd.Output()
if err != nil { if err != nil {
return false return false
@ -63,9 +133,9 @@ func (k Keybase) LoggedIn() bool {
return s.LoggedIn return s.LoggedIn
} }
// Version() returns the version string of the client. // version() returns the version string of the client.
func (k Keybase) Version() string { func (k Keybase) version() string {
cmd := exec.Command(k.path, "version", "-S", "-f", "s") cmd := exec.Command(k.Path, "version", "-S", "-f", "s")
cmdOut, err := cmd.Output() cmdOut, err := cmd.Output()
if err != nil { if err != nil {
return "" return ""