8 changed files with 193 additions and 14 deletions
@ -1,16 +1,65 @@ |
|||||||
package main |
package main |
||||||
|
|
||||||
// writeKV is an internal func that ensures KVStore values get written consistently
|
import ( |
||||||
func (b *bot) writeKV(key string, value string) error { |
"reflect" |
||||||
_, err := b.k.KVPut(&b.config.KVStoreTeam, b.k.Username, key, value) |
|
||||||
|
"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 { |
if err != nil { |
||||||
return err |
return err |
||||||
} |
} |
||||||
return nil |
return nil |
||||||
} |
} |
||||||
|
|
||||||
// getGV is an internal function that ensures KVStore values are retreived consistently
|
func (b *bot) KVStorePut(namespace string, key string, value string) error { |
||||||
func (b *bot) getKV(key string) (value string, revision int, err error) { |
_, err := b.k.KVPut(&b.config.KVStoreTeam, namespace, key, value) |
||||||
res, err := b.k.KVGet(&b.config.KVStoreTeam, b.k.Username, key) |
if err != nil { |
||||||
return res.EntryValue, res.Revision, err |
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() |
||||||
} |
} |
||||||
|
@ -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"` |
||||||
|
} |
Loading…
Reference in new issue