From e11282c51960ff57d57ccbd60a7195a0a7906905 Mon Sep 17 00:00:00 2001 From: David Haukeness Date: Thu, 26 Mar 2020 21:37:12 +0000 Subject: [PATCH 1/7] added kvstore team name to args --- args.go | 7 +++++++ main.go | 1 + 2 files changed, 8 insertions(+) diff --git a/args.go b/args.go index baccdfe..6f94f74 100644 --- a/args.go +++ b/args.go @@ -20,6 +20,7 @@ func (b *bot) parseArgs(args []string) error { flags.StringVar(&cliConfig.LogConvIDStr, "log-convid", "", "sets the keybase chat1.ConvIDStr to log debugging to keybase chat.") flags.StringVar(&cliConfig.FeedbackConvIDStr, "feedback-convid", "", "sets the keybase chat1.ConvIDStr to send feedback to.") flags.StringVar(&cliConfig.FeedbackTeamAdvert, "feedback-team-advert", "", "sets the keybase team/channel to advertise feedback. @team#channel") + flags.StringVar(&cliConfig.KVStoreTeam, "kvstore-team", "", "sets the keybase team where kvstore values are stored") if err := flags.Parse(args[1:]); err != nil { return err } @@ -38,6 +39,9 @@ func (b *bot) parseArgs(args []string) error { if cliConfig.FeedbackTeamAdvert != "" { b.config.FeedbackTeamAdvert = cliConfig.FeedbackTeamAdvert } + if cliConfig.KVStoreTeam != "" { + b.config.KVStoreTeam = cliConfig.KVStoreTeam + } } // then print the running options @@ -48,6 +52,9 @@ func (b *bot) parseArgs(args []string) error { if b.config.FeedbackConvIDStr != "" { b.debug("Feedback enabled to %s and advertising %s", b.config.FeedbackConvIDStr, b.config.FeedbackTeamAdvert) } + if b.config.KVStoreTeam != "" { + b.debug("keybase kvstore enabled in @%s", b.config.KVStoreTeam) + } return nil } diff --git a/main.go b/main.go index d7c7b6f..34494e8 100644 --- a/main.go +++ b/main.go @@ -29,6 +29,7 @@ type botConfig struct { LogConvIDStr string `env:"BOT_LOG_CONVID" envDefault:""` FeedbackConvIDStr string `env:"BOT_FEEDBACK_CONVID" envDefault:""` FeedbackTeamAdvert string `env:"BOT_FEEDBACK_TEAM_ADVERT" envDefault:""` + KVStoreTeam string `env:"BOT_KVSTORE_TEAM" envDefault:""` } // hold reply information when needed From 8b2311a51b1de6fca7b7fd882ff8dae1bf2f389b Mon Sep 17 00:00:00 2001 From: David Haukeness Date: Thu, 26 Mar 2020 23:00:00 +0000 Subject: [PATCH 2/7] updated deps --- go.mod | 1 + go.sum | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/go.mod b/go.mod index add3dc6..456b3a6 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/caarlos0/env v3.5.0+incompatible github.com/sethvargo/go-diceware v0.2.0 github.com/stretchr/testify v1.5.1 // indirect + github.com/ugorji/go v1.1.7 // indirect gopkg.in/yaml.v2 v2.2.4 // indirect samhofi.us/x/keybase v0.0.0-20200312153536-07f5168a6a29 ) diff --git a/go.sum b/go.sum index 57f6907..28f4362 100644 --- a/go.sum +++ b/go.sum @@ -13,6 +13,10 @@ github.com/sethvargo/go-diceware v0.2.0/go.mod h1:II+37A5sTGAtg3zd/JqyVQ8qqAjSm/ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= +github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= +github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs= +github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= From 473873be6c64f9fd94a8a89e584b90dde172e0ec Mon Sep 17 00:00:00 2001 From: David Haukeness Date: Thu, 26 Mar 2020 23:00:09 +0000 Subject: [PATCH 3/7] inital kvstore get/put --- kvstore.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 kvstore.go diff --git a/kvstore.go b/kvstore.go new file mode 100644 index 0000000..9500a06 --- /dev/null +++ b/kvstore.go @@ -0,0 +1,16 @@ +package main + +// writeKV is an internal func that ensures KVStore values get written consistently +func (b *bot) writeKV(key string, value string) error { + _, err := b.k.KVPut(&b.config.KVStoreTeam, b.k.Username, key, value) + if err != nil { + return err + } + return nil +} + +// getGV is an internal function that ensures KVStore values are retreived consistently +func (b *bot) getKV(key string) (value string, revision int, err error) { + res, err := b.k.KVGet(&b.config.KVStoreTeam, b.k.Username, key) + return res.EntryValue, res.Revision, err +} From 13c971c63545312444f7cdc26da952c21c05482f Mon Sep 17 00:00:00 2001 From: David Haukeness Date: Mon, 30 Mar 2020 22:21:25 +0000 Subject: [PATCH 4/7] initial kvstore functions --- commands.go | 54 +++++++++++++++++++++++++++++++++++++++++++++ go.mod | 6 ++++- go.sum | 15 +++++++++---- handlers.go | 12 ++++++++-- kvstore.go | 63 +++++++++++++++++++++++++++++++++++++++++++++++------ main.go | 16 ++++++++++++++ types.go | 8 +++++++ utils.go | 33 ++++++++++++++++++++++++++++ 8 files changed, 193 insertions(+), 14 deletions(-) create mode 100644 types.go diff --git a/commands.go b/commands.go index c7b49fa..c99a73d 100644 --- a/commands.go +++ b/commands.go @@ -3,6 +3,7 @@ package main import ( "fmt" "log" + "net/url" "strings" "samhofi.us/x/keybase/types/chat1" @@ -40,3 +41,56 @@ func (b *bot) sendFeedback(convid chat1.ConvIDStr, mesgID chat1.MessageID, sende func (b *bot) sendWelcome(convid chat1.ConvIDStr) { b.k.SendMessageByConvID(convid, "Hello there!! I'm the Jitsi meeting bot, made by @haukened\nI can start Jitsi meetings right here in this chat!\nI can be activated in 2 ways:\n 1. `@jitsibot`\n 2.`!jitsi`\nYou can provide feedback to my humans using:\n 1. `@jitsibot feedback `\n 2. `!jitsibot feedback `\nYou can also join @jitsi_meet to talk about features, enhancements, or talk to live humans! Everyone is welcome!\nI also accept donations to offset hosting costs, just send some XLM to my wallet if you feel like it by typing `+5XLM@jitsibot`\nIf you ever need to see this message again, ask me for help or say hello to me!") } + +func (b *bot) setKValue(convid chat1.ConvIDStr, msgID chat1.MessageID, args []string) { + if args[0] != "set" { + return + } + switch len(args) { + case 3: + if args[1] == "url" { + // first validate the URL + u, err := url.ParseRequestURI(args[2]) + if err != nil { + b.k.ReplyByConvID(convid, msgID, "ERROR - `%s`", err) + return + } + // then make sure its HTTPS + if u.Scheme != "https" { + b.k.ReplyByConvID(convid, msgID, "ERROR - HTTPS Required") + return + } + // then get the current options + var opts ConvOptions + err = b.KVStoreGetStruct(convid, &opts) + if err != nil { + eid := b.logError(err) + b.k.ReactByConvID(convid, msgID, "Error %s", eid) + return + } + // then update the struct using only the scheme and hostname:port + if u.Port() != "" { + opts.CustomURL = fmt.Sprintf("%s://%s:%s/", u.Scheme, u.Hostname(), u.Port()) + } else { + opts.CustomURL = fmt.Sprintf("%s://%s/", u.Scheme, u.Hostname()) + } + // then write that back to kvstore, with revision + err = b.KVStorePutStruct(convid, opts) + if err != nil { + eid := b.logError(err) + b.k.ReactByConvID(convid, msgID, "ERROR %s", eid) + return + } + b.k.ReactByConvID(convid, msgID, "OK!") + return + } + default: + return + } +} + +func (b *bot) listKValue(convid chat1.ConvIDStr, msgID chat1.MessageID, args []string) { + if args[0] != "list" { + return + } +} diff --git a/go.mod b/go.mod index 456b3a6..0e1b834 100644 --- a/go.mod +++ b/go.mod @@ -4,9 +4,13 @@ go 1.13 require ( github.com/caarlos0/env v3.5.0+incompatible + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/kr/pretty v0.1.0 // indirect github.com/sethvargo/go-diceware v0.2.0 github.com/stretchr/testify v1.5.1 // indirect - github.com/ugorji/go v1.1.7 // indirect + github.com/teris-io/shortid v0.0.0-20171029131806-771a37caa5cf + github.com/ugorji/go/codec v1.1.7 + gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect gopkg.in/yaml.v2 v2.2.4 // indirect samhofi.us/x/keybase v0.0.0-20200312153536-07f5168a6a29 ) diff --git a/go.sum b/go.sum index 28f4362..8519edd 100644 --- a/go.sum +++ b/go.sum @@ -2,10 +2,13 @@ github.com/caarlos0/env v3.5.0+incompatible h1:Yy0UN8o9Wtr/jGHZDpCBLpNrzcFLLM2yi github.com/caarlos0/env v3.5.0+incompatible/go.mod h1:tdCsowwCzMLdkqRYDlHpZCp2UooDD3MspDBjZ2AD02Y= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= -github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/lithammer/shortuuid/v3 v3.0.4 h1:uj4xhotfY92Y1Oa6n6HUiFn87CdoEHYUlTy0+IgbLrs= -github.com/lithammer/shortuuid/v3 v3.0.4/go.mod h1:RviRjexKqIzx/7r1peoAITm6m7gnif/h+0zmolKJjzw= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/sethvargo/go-diceware v0.2.0 h1:3QzXGqUe0UR9y1XYSz1dxGS+fKtXOxRqqKjy+cG1yTI= @@ -13,12 +16,16 @@ github.com/sethvargo/go-diceware v0.2.0/go.mod h1:II+37A5sTGAtg3zd/JqyVQ8qqAjSm/ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +github.com/teris-io/shortid v0.0.0-20171029131806-771a37caa5cf h1:Z2X3Os7oRzpdJ75iPqWZc0HeJWFYNCvKsfpQwFpRNTA= +github.com/teris-io/shortid v0.0.0-20171029131806-771a37caa5cf/go.mod h1:M8agBzgqHIhgj7wEn9/0hJUZcrvt9VY+Ln+S1I5Mha0= github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs= github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/handlers.go b/handlers.go index 303cf91..7d4405c 100644 --- a/handlers.go +++ b/handlers.go @@ -49,7 +49,8 @@ func (b *bot) chatHandler(m chat1.MsgSummary) { // Determine first if this is a command if strings.HasPrefix(m.Content.Text.Body, "!") || strings.HasPrefix(m.Content.Text.Body, "@") { // determine the root command - words := strings.Fields(m.Content.Text.Body) + body := strings.ToLower(m.Content.Text.Body) + words := strings.Fields(body) command := strings.Replace(words[0], "@", "", 1) command = strings.Replace(command, "!", "", 1) command = strings.ToLower(command) @@ -58,7 +59,14 @@ func (b *bot) chatHandler(m chat1.MsgSummary) { nargs := len(args) switch command { case b.k.Username: - fallthrough + if nargs > 0 { + switch args[0] { + case "set": + b.setKValue(m.ConvID, m.Id, args) + case "list": + b.listKValue(m.ConvID, m.Id, args) + } + } case "jitsi": if nargs == 0 { b.setupMeeting(m.ConvID, m.Sender.Username, args, m.Channel.MembersType) diff --git a/kvstore.go b/kvstore.go index 9500a06..2c35940 100644 --- a/kvstore.go +++ b/kvstore.go @@ -1,16 +1,65 @@ package main -// writeKV is an internal func that ensures KVStore values get written consistently -func (b *bot) writeKV(key string, value string) error { - _, err := b.k.KVPut(&b.config.KVStoreTeam, b.k.Username, key, value) +import ( + "reflect" + + "samhofi.us/x/keybase/types/chat1" +) + +// mashals an interface to JSON and sends to kvstore +func (b *bot) KVStorePutStruct(convIDstr chat1.ConvIDStr, v interface{}) error { + // marshal the struct to JSON + kvstoreDataString, err := encodeStructToJSONString(v) + if err != nil { + return err + } + // put the string in kvstore + err = b.KVStorePut(string(convIDstr), getTypeName(v), kvstoreDataString) + if err != nil { + return err + } + return nil +} + +func (b *bot) KVStoreGetStruct(convIDstr chat1.ConvIDStr, v interface{}) error { + // get the string from kvstore + result, err := b.KVStoreGet(string(convIDstr), getTypeName(v)) + if err != nil { + return err + } + // if there was no result just return and the struct is unmodified + if result == "" { + return nil + } + // unmarshal the string into JSON + err = decodeJSONStringToStruct(v, result) if err != nil { return err } return nil } -// getGV is an internal function that ensures KVStore values are retreived consistently -func (b *bot) getKV(key string) (value string, revision int, err error) { - res, err := b.k.KVGet(&b.config.KVStoreTeam, b.k.Username, key) - return res.EntryValue, res.Revision, err +func (b *bot) KVStorePut(namespace string, key string, value string) error { + _, err := b.k.KVPut(&b.config.KVStoreTeam, namespace, key, value) + if err != nil { + return err + } + return nil +} + +func (b *bot) KVStoreGet(namespace string, key string) (string, error) { + kvResult, err := b.k.KVGet(&b.config.KVStoreTeam, namespace, key) + if err != nil { + return "", err + } + return kvResult.EntryValue, nil +} + +// getTypeName returns the name of a type, regardless of if its a pointer or not +func getTypeName(v interface{}) string { + t := reflect.TypeOf(v) + if t.Kind() == reflect.Ptr { + return t.Elem().Name() + } + return t.Name() } diff --git a/main.go b/main.go index 34494e8..7582da4 100644 --- a/main.go +++ b/main.go @@ -111,6 +111,22 @@ func (b *bot) run(args []string) error { b.k.ClearCommands() b.registerCommands() + // this is just for testing, and doesn't work yet + if err := b.KVStorePutStruct("test", &ConvOptions{ConvID: "test", CustomURL: "https://te.st:888"}); err != nil { + log.Printf("KV: %+v", err) + } + var vRes1 ConvOptions + if err := b.KVStoreGetStruct("test", &vRes1); err != nil { + log.Printf("KV: %+v", err) + } else { + fmt.Printf("VR: %+v\n", vRes1) + } + var vRes2 ConvOptions + if err := b.KVStoreGetStruct("test1", &vRes2); err != nil { + log.Printf("KV: %+v", err) + } else { + fmt.Printf("VR: %+v\n", vRes2) + } log.Println("Starting...") b.k.Run(b.handlers, &b.opts) return nil diff --git a/types.go b/types.go new file mode 100644 index 0000000..f14b6d4 --- /dev/null +++ b/types.go @@ -0,0 +1,8 @@ +package main + +// ConvOptions stores team specific options like custom servers +type ConvOptions struct { + ConvID string `json:"converation_id,omitempty"` + NotificationsEnabled bool `json:"notifications_enabled,omitempty"` + CustomURL string `json:"custom_url,omitempty"` +} diff --git a/utils.go b/utils.go index 8f3c80e..ef8ba5e 100644 --- a/utils.go +++ b/utils.go @@ -4,6 +4,8 @@ import ( "encoding/json" "fmt" + "github.com/teris-io/shortid" + "github.com/ugorji/go/codec" "samhofi.us/x/keybase/types/chat1" ) @@ -27,3 +29,34 @@ func getFeedbackExtendedDescription(bc botConfig) *chat1.UserBotExtendedDescript MobileBody: "Please note: Your feedback will be public!", } } + +func encodeStructToJSONString(v interface{}) (string, error) { + jh := codecHandle() + var bytes []byte + err := codec.NewEncoderBytes(&bytes, jh).Encode(v) + if err != nil { + return "", err + } + result := string(bytes) + return result, nil +} + +func decodeJSONStringToStruct(v interface{}, src string) error { + bytes := []byte(src) + jh := codecHandle() + return codec.NewDecoderBytes(bytes, jh).Decode(v) +} + +func codecHandle() *codec.JsonHandle { + var jh codec.JsonHandle + return &jh +} + +func (b *bot) logError(err error) string { + // generate the error id + eid := shortid.MustGenerate() + // send the error to the log + b.debug("`%s` - %s", eid, err) + // then return the error id for use + return eid +} From dd8552e733ac8aff7b136eade69979f4492d11ea Mon Sep 17 00:00:00 2001 From: David Haukeness Date: Mon, 30 Mar 2020 22:28:00 +0000 Subject: [PATCH 5/7] updated deps --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 0e1b834..4185378 100644 --- a/go.mod +++ b/go.mod @@ -12,5 +12,5 @@ require ( github.com/ugorji/go/codec v1.1.7 gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect gopkg.in/yaml.v2 v2.2.4 // indirect - samhofi.us/x/keybase v0.0.0-20200312153536-07f5168a6a29 + samhofi.us/x/keybase v0.0.0-20200315012740-74fb4a152b35 ) diff --git a/go.sum b/go.sum index 8519edd..e9ad379 100644 --- a/go.sum +++ b/go.sum @@ -29,5 +29,5 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -samhofi.us/x/keybase v0.0.0-20200312153536-07f5168a6a29 h1:+yj8+O6C56QM8WGZYtDwBFxkdPT4UgVU+O9W+6N85kk= -samhofi.us/x/keybase v0.0.0-20200312153536-07f5168a6a29/go.mod h1:fcva80IUFyWcHtV4bBSzgKg07K6Rvuvi3GtGCLNGkyE= +samhofi.us/x/keybase v0.0.0-20200315012740-74fb4a152b35 h1:pnpx+34wna1ML9JxHEChf3lDSBOOEh91B5IDuNFUZYk= +samhofi.us/x/keybase v0.0.0-20200315012740-74fb4a152b35/go.mod h1:fcva80IUFyWcHtV4bBSzgKg07K6Rvuvi3GtGCLNGkyE= From 86217a5d6363aa797a6faed72024cee8d25861a4 Mon Sep 17 00:00:00 2001 From: David Haukeness Date: Mon, 30 Mar 2020 22:30:27 +0000 Subject: [PATCH 6/7] moved JSON to kvstore --- kvstore.go | 23 +++++++++++++++++++++++ utils.go | 23 ----------------------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/kvstore.go b/kvstore.go index 2c35940..4eafe7c 100644 --- a/kvstore.go +++ b/kvstore.go @@ -3,6 +3,7 @@ package main import ( "reflect" + "github.com/ugorji/go/codec" "samhofi.us/x/keybase/types/chat1" ) @@ -63,3 +64,25 @@ func getTypeName(v interface{}) string { } return t.Name() } + +func encodeStructToJSONString(v interface{}) (string, error) { + jh := codecHandle() + var bytes []byte + err := codec.NewEncoderBytes(&bytes, jh).Encode(v) + if err != nil { + return "", err + } + result := string(bytes) + return result, nil +} + +func decodeJSONStringToStruct(v interface{}, src string) error { + bytes := []byte(src) + jh := codecHandle() + return codec.NewDecoderBytes(bytes, jh).Decode(v) +} + +func codecHandle() *codec.JsonHandle { + var jh codec.JsonHandle + return &jh +} diff --git a/utils.go b/utils.go index ef8ba5e..b401642 100644 --- a/utils.go +++ b/utils.go @@ -5,7 +5,6 @@ import ( "fmt" "github.com/teris-io/shortid" - "github.com/ugorji/go/codec" "samhofi.us/x/keybase/types/chat1" ) @@ -30,28 +29,6 @@ func getFeedbackExtendedDescription(bc botConfig) *chat1.UserBotExtendedDescript } } -func encodeStructToJSONString(v interface{}) (string, error) { - jh := codecHandle() - var bytes []byte - err := codec.NewEncoderBytes(&bytes, jh).Encode(v) - if err != nil { - return "", err - } - result := string(bytes) - return result, nil -} - -func decodeJSONStringToStruct(v interface{}, src string) error { - bytes := []byte(src) - jh := codecHandle() - return codec.NewDecoderBytes(bytes, jh).Decode(v) -} - -func codecHandle() *codec.JsonHandle { - var jh codec.JsonHandle - return &jh -} - func (b *bot) logError(err error) string { // generate the error id eid := shortid.MustGenerate() From 631d5f5c0ec3b8c495e1013cb65cec18ec89d11a Mon Sep 17 00:00:00 2001 From: David Haukeness Date: Mon, 30 Mar 2020 22:32:32 +0000 Subject: [PATCH 7/7] documenation --- kvstore.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/kvstore.go b/kvstore.go index 4eafe7c..d4d3569 100644 --- a/kvstore.go +++ b/kvstore.go @@ -7,7 +7,7 @@ import ( "samhofi.us/x/keybase/types/chat1" ) -// mashals an interface to JSON and sends to kvstore +// KvStorePutStruct marshals an interface to JSON and sends to kvstore func (b *bot) KVStorePutStruct(convIDstr chat1.ConvIDStr, v interface{}) error { // marshal the struct to JSON kvstoreDataString, err := encodeStructToJSONString(v) @@ -22,6 +22,7 @@ func (b *bot) KVStorePutStruct(convIDstr chat1.ConvIDStr, v interface{}) error { return nil } +// KVStoreGetStruct gets a string from kvstore and unmarshals the JSON to a struct func (b *bot) KVStoreGetStruct(convIDstr chat1.ConvIDStr, v interface{}) error { // get the string from kvstore result, err := b.KVStoreGet(string(convIDstr), getTypeName(v)) @@ -40,6 +41,7 @@ func (b *bot) KVStoreGetStruct(convIDstr chat1.ConvIDStr, v interface{}) error { return nil } +// KVStorePut puts a string into kvstore given a key and namespace func (b *bot) KVStorePut(namespace string, key string, value string) error { _, err := b.k.KVPut(&b.config.KVStoreTeam, namespace, key, value) if err != nil { @@ -48,6 +50,7 @@ func (b *bot) KVStorePut(namespace string, key string, value string) error { return nil } +// KVStoreGet gets a string from kvstore given a key and namespace func (b *bot) KVStoreGet(namespace string, key string) (string, error) { kvResult, err := b.k.KVGet(&b.config.KVStoreTeam, namespace, key) if err != nil {