status-go/protocol/v1/encoder.go
Andrea Maria Piana baa0767c26
Handle membership update message
This commit does a few things:

1) Handle membership updates using protobuf and adds the relevant
endpoints.
2) Store in memory a map of chats + contacts for faster lookups, which
are then flushed to disk on each update
3) Validate incoming messages

Sorry for the large pr, but you know, v1 :)
2019-12-10 15:20:28 +01:00

49 lines
1.1 KiB
Go

package protocol
import (
"errors"
"io"
"reflect"
"github.com/russolsen/transit"
)
var (
pairMessageType = reflect.TypeOf(PairMessage{})
defaultMessageValueEncoder = &messageValueEncoder{}
)
// NewMessageEncoder returns a new Transit encoder
// that can encode Message values.
// More about Transit: https://github.com/cognitect/transit-format
func NewMessageEncoder(w io.Writer) *transit.Encoder {
encoder := transit.NewEncoder(w, false)
encoder.AddHandler(pairMessageType, defaultMessageValueEncoder)
return encoder
}
type messageValueEncoder struct{}
func (messageValueEncoder) IsStringable(reflect.Value) bool {
return false
}
func (messageValueEncoder) Encode(e transit.Encoder, value reflect.Value, asString bool) error {
switch message := value.Interface().(type) {
case PairMessage:
taggedValue := transit.TaggedValue{
Tag: pairMessageTag,
Value: []interface{}{
message.InstallationID,
message.DeviceType,
message.Name,
message.FCMToken,
},
}
return e.EncodeInterface(taggedValue, false)
}
return errors.New("unknown message type to encode")
}