Change Runner() to Run() and add ability to pass options
This commit is contained in:
65
chatIn.go
65
chatIn.go
@ -3,7 +3,6 @@ package keybase
|
|||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"os/exec"
|
"os/exec"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -118,19 +117,41 @@ type chatInMsg struct {
|
|||||||
ChannelMention string `json:"channel_mention"`
|
ChannelMention string `json:"channel_mention"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Creates a string of json-encoded channels to pass to keybase chat api-listen --filter-channels
|
// RunOptions holds a set of options to be passed to Run
|
||||||
func createFilterString(channelFilters ...Channel) string {
|
type RunOptions struct {
|
||||||
if len(channelFilters) == 0 {
|
Local bool // Subscribe to local messages
|
||||||
return "[]"
|
HideExploding bool // Ignore exploding messages
|
||||||
|
Dev bool // Subscribe to dev channel messages
|
||||||
|
Wallet bool // Subscribe to wallet events
|
||||||
|
FilterChannel Channel // Only subscribe to messages from specified channel
|
||||||
|
FilterChannels []Channel // Only subscribe to messages from specified channels
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonBytes, _ := json.Marshal(channelFilters)
|
// Creates a string of a json-encoded channel to pass to keybase chat api-listen --filter-channel
|
||||||
return fmt.Sprintf("%s", string(jsonBytes))
|
func createFilterString(channel Channel) string {
|
||||||
|
if channel.Name == "" {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
jsonBytes, _ := json.Marshal(channel)
|
||||||
|
return string(jsonBytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Creates a string of json-encoded channels to pass to keybase chat api-listen --filter-channels
|
||||||
|
func createFiltersString(channels []Channel) string {
|
||||||
|
if len(channels) == 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
jsonBytes, _ := json.Marshal(channels)
|
||||||
|
return string(jsonBytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get new messages coming into keybase and send them into the channel
|
// Get new messages coming into keybase and send them into the channel
|
||||||
func getNewMessages(k Keybase, c chan<- ChatIn, filterString string) {
|
func getNewMessages(k Keybase, c chan<- ChatIn, execOptions []string) {
|
||||||
keybaseListen := exec.Command(k.Path, "chat", "api-listen", "--filter-channels", filterString)
|
execCommand := []string{"chat", "api-listen"}
|
||||||
|
if len(execOptions) > 0 {
|
||||||
|
execCommand = append(execCommand, execOptions...)
|
||||||
|
}
|
||||||
|
keybaseListen := exec.Command(k.Path, execCommand...)
|
||||||
keybaseOutput, _ := keybaseListen.StdoutPipe()
|
keybaseOutput, _ := keybaseListen.StdoutPipe()
|
||||||
keybaseListen.Start()
|
keybaseListen.Start()
|
||||||
scanner := bufio.NewScanner(keybaseOutput)
|
scanner := bufio.NewScanner(keybaseOutput)
|
||||||
@ -142,11 +163,31 @@ func getNewMessages(k Keybase, c chan<- ChatIn, filterString string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Runner() runs keybase chat api-listen, and passes incoming messages to the message handler func
|
// Run() runs keybase chat api-listen, and passes incoming messages to the message handler func
|
||||||
func (k Keybase) Runner(handler func(ChatIn), channelFilters ...Channel) {
|
func (k Keybase) Run(handler func(ChatIn), options ...RunOptions) {
|
||||||
|
runOptions := make([]string, 0)
|
||||||
|
if len(options) > 0 {
|
||||||
|
if options[0].Local {
|
||||||
|
runOptions = append(runOptions, "--local")
|
||||||
|
}
|
||||||
|
if options[0].HideExploding {
|
||||||
|
runOptions = append(runOptions, "--hide-exploding")
|
||||||
|
}
|
||||||
|
if options[0].Dev {
|
||||||
|
runOptions = append(runOptions, "--dev")
|
||||||
|
}
|
||||||
|
if len(options[0].FilterChannels) > 0 {
|
||||||
|
runOptions = append(runOptions, "--filter-channels")
|
||||||
|
runOptions = append(runOptions, createFiltersString(options[0].FilterChannels))
|
||||||
|
}
|
||||||
|
if options[0].FilterChannel.Name != "" {
|
||||||
|
runOptions = append(runOptions, "--filter-channel")
|
||||||
|
runOptions = append(runOptions, createFilterString(options[0].FilterChannel))
|
||||||
|
}
|
||||||
|
}
|
||||||
c := make(chan ChatIn, 50)
|
c := make(chan ChatIn, 50)
|
||||||
defer close(c)
|
defer close(c)
|
||||||
go getNewMessages(k, c, createFilterString(channelFilters...))
|
go getNewMessages(k, c, runOptions)
|
||||||
for {
|
for {
|
||||||
go handler(<-c)
|
go handler(<-c)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -40,7 +40,7 @@ type chat interface {
|
|||||||
|
|
||||||
type keybase interface {
|
type keybase interface {
|
||||||
NewChat(channel Channel) Chat
|
NewChat(channel Channel) Chat
|
||||||
Runner(handler func(ChatIn), channelFilters ...Channel)
|
Run(handler func(ChatIn), options ...RunOptions)
|
||||||
ChatList() ([]conversation, error)
|
ChatList() ([]conversation, error)
|
||||||
loggedIn() bool
|
loggedIn() bool
|
||||||
username() string
|
username() string
|
||||||
|
|||||||
Reference in New Issue
Block a user