Re-organize everything to make it cleaner... hopefully
This commit is contained in:
100
chatOut.go
100
chatOut.go
@ -13,6 +13,7 @@ type chatOut struct { // not exported
|
||||
}
|
||||
type chatOutChannel struct {
|
||||
Name string `json:"name"`
|
||||
Public bool `json:"public"`
|
||||
MembersType string `json:"members_type"`
|
||||
TopicName string `json:"topic_name"`
|
||||
}
|
||||
@ -47,7 +48,7 @@ type chatOutResultChannel struct {
|
||||
TopicType string `json:"topic_type,omitempty"`
|
||||
TopicName string `json:"topic_name,omitempty"`
|
||||
}
|
||||
type chatOutResultConversations struct {
|
||||
type conversation struct {
|
||||
ID string `json:"id"`
|
||||
Channel chatOutResultChannel `json:"channel"`
|
||||
Unread bool `json:"unread"`
|
||||
@ -56,11 +57,11 @@ type chatOutResultConversations struct {
|
||||
MemberStatus string `json:"member_status"`
|
||||
}
|
||||
type ChatOut struct { // exported
|
||||
Message string `json:"message,omitempty"`
|
||||
ID int `json:"id,omitempty"`
|
||||
Ratelimits []chatOutResultRatelimits `json:"ratelimits,omitempty"`
|
||||
Conversations []chatOutResultConversations `json:"conversations,omitempty"`
|
||||
Offline bool `json:"offline,omitempty"`
|
||||
Message string `json:"message,omitempty"`
|
||||
ID int `json:"id,omitempty"`
|
||||
Ratelimits []chatOutResultRatelimits `json:"ratelimits,omitempty"`
|
||||
Conversations []conversation `json:"conversations,omitempty"`
|
||||
Offline bool `json:"offline,omitempty"`
|
||||
}
|
||||
|
||||
// ----
|
||||
@ -81,92 +82,51 @@ func chatAPIOut(keybasePath string, c chatOut) (chatOutResult, error) {
|
||||
return r, nil
|
||||
}
|
||||
|
||||
// ChatSendText() sends a chat message to a user.
|
||||
func (k Keybase) ChatSendText(user string, message ...string) (ChatOut, error) {
|
||||
// Send() sends a chat message
|
||||
func (c Chat) Send(message ...string) (ChatOut, error) {
|
||||
m := chatOut{}
|
||||
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, " ")
|
||||
|
||||
r, err := chatAPIOut(k.path, m)
|
||||
r, err := chatAPIOut(c.keybase.Path, m)
|
||||
if err != nil {
|
||||
return ChatOut{}, err
|
||||
}
|
||||
return r.Result, nil
|
||||
}
|
||||
|
||||
// ChatSendTextTeam() sends a chat message to a team.
|
||||
func (k Keybase) ChatSendTextTeam(team, channel string, message ...string) (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) {
|
||||
// React() sends a reaction to a message.
|
||||
func (c Chat) React(reaction string, messageId int) (ChatOut, error) {
|
||||
m := chatOut{}
|
||||
m.Method = "reaction"
|
||||
m.Params.Options.Channel.Name = user
|
||||
m.Params.Options.MessageID = messageId
|
||||
m.Params.Options.Channel.Name = c.Name
|
||||
m.Params.Options.Channel.MembersType = c.MembersType
|
||||
m.Params.Options.Channel.TopicName = c.TopicName
|
||||
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 {
|
||||
return ChatOut{}, err
|
||||
}
|
||||
return r.Result, nil
|
||||
}
|
||||
|
||||
// ChatReactTeam() sends a reaction to a message on a team.
|
||||
func (k Keybase) ChatReactTeam(team, channel, reaction string, 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) {
|
||||
// Delete() deletes a chat message
|
||||
func (c Chat) Delete(messageId int) (ChatOut, error) {
|
||||
m := chatOut{}
|
||||
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
|
||||
|
||||
r, err := chatAPIOut(k.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)
|
||||
r, err := chatAPIOut(c.keybase.Path, m)
|
||||
if err != nil {
|
||||
return ChatOut{}, err
|
||||
}
|
||||
@ -174,10 +134,10 @@ func (k Keybase) ChatDeleteMessageTeam(team, channel string, messageId int) (Cha
|
||||
}
|
||||
|
||||
// ChatList() returns a list of all conversations.
|
||||
func (k Keybase) ChatList() ([]chatOutResultConversations, error) {
|
||||
func (k Keybase) ChatList() ([]conversation, error) {
|
||||
m := chatOut{}
|
||||
m.Method = "list"
|
||||
|
||||
r, err := chatAPIOut(k.path, m)
|
||||
r, err := chatAPIOut(k.Path, m)
|
||||
return r.Result.Conversations, err
|
||||
}
|
||||
|
||||
116
keybase.go
116
keybase.go
@ -5,21 +5,51 @@ import (
|
||||
"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 {
|
||||
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 {
|
||||
ChatSendText(user string, message ...string) (ChatOut, error)
|
||||
ChatSendTextTeam(team, channel, message string) (ChatOut, error)
|
||||
ChatReact(user, reaction string, messageId int) (ChatOut, error)
|
||||
ChatReactTeam(team, channel, reaction string, messageId int) (ChatOut, error)
|
||||
ChatDeleteMessage(user string, messageId int) (ChatOut, error)
|
||||
ChatDeleteMessageTeam(team, channel string, messageId int) (ChatOut, error)
|
||||
ChatList() ([]chatOutResultConversations, error)
|
||||
LoggedIn() bool
|
||||
Username() string
|
||||
Version() string
|
||||
NewChat(channel map[string]interface{}) Chat
|
||||
ChatList() ([]conversation, error)
|
||||
loggedIn() bool
|
||||
username() string
|
||||
version() string
|
||||
}
|
||||
|
||||
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.
|
||||
func New(path ...string) Keybase {
|
||||
func NewKeybase(path ...string) Keybase {
|
||||
k := Keybase{}
|
||||
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.
|
||||
func (k Keybase) Username() string {
|
||||
cmd := exec.Command(k.path, "status", "-j")
|
||||
// Return a new Chat instance
|
||||
func (k Keybase) NewChat(channel map[string]interface{}) Chat {
|
||||
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()
|
||||
if err != nil {
|
||||
return ""
|
||||
@ -49,9 +119,9 @@ func (k Keybase) Username() string {
|
||||
return s.Username
|
||||
}
|
||||
|
||||
// LoggedIn() returns true if Keybase is currently logged in, otherwise returns false.
|
||||
func (k Keybase) LoggedIn() bool {
|
||||
cmd := exec.Command(k.path, "status", "-j")
|
||||
// loggedIn() returns true if Keybase is currently logged in, otherwise returns false.
|
||||
func (k Keybase) loggedIn() bool {
|
||||
cmd := exec.Command(k.Path, "status", "-j")
|
||||
cmdOut, err := cmd.Output()
|
||||
if err != nil {
|
||||
return false
|
||||
@ -63,9 +133,9 @@ func (k Keybase) LoggedIn() bool {
|
||||
return s.LoggedIn
|
||||
}
|
||||
|
||||
// Version() returns the version string of the client.
|
||||
func (k Keybase) Version() string {
|
||||
cmd := exec.Command(k.path, "version", "-S", "-f", "s")
|
||||
// version() returns the version string of the client.
|
||||
func (k Keybase) version() string {
|
||||
cmd := exec.Command(k.Path, "version", "-S", "-f", "s")
|
||||
cmdOut, err := cmd.Output()
|
||||
if err != nil {
|
||||
return ""
|
||||
|
||||
Reference in New Issue
Block a user