Enable parsing of messages

This commit is contained in:
Andrea Maria Piana 2019-11-05 10:25:40 +01:00
parent ac9bf97fb3
commit 03adc878d8
No known key found for this signature in database
GPG Key ID: AA6CCA6DE0E06424
6 changed files with 38 additions and 5 deletions

View File

@ -128,6 +128,7 @@ func (s *MessageProcessorSuite) TestHandleDecodedMessagesSingle() {
s.Require().Equal(&privateKey.PublicKey, decodedMessages[0].SigPubKey())
s.Require().Equal(protocol.MessageID(&privateKey.PublicKey, encodedPayload), decodedMessages[0].ID)
s.Require().Equal(s.testMessage, decodedMessages[0].ParsedMessage)
s.Require().Equal(protocol.MessageT, decodedMessages[0].MessageType)
}
func (s *MessageProcessorSuite) TestHandleDecodedMessagesRaw() {
@ -176,6 +177,7 @@ func (s *MessageProcessorSuite) TestHandleDecodedMessagesWrapped() {
s.Require().Equal(protocol.MessageID(&authorKey.PublicKey, wrappedPayload), decodedMessages[0].ID)
s.Require().Equal(encodedPayload, decodedMessages[0].DecryptedPayload)
s.Require().Equal(s.testMessage, decodedMessages[0].ParsedMessage)
s.Require().Equal(protocol.MessageT, decodedMessages[0].MessageType)
}
func (s *MessageProcessorSuite) TestHandleDecodedMessagesDatasync() {
@ -212,11 +214,13 @@ func (s *MessageProcessorSuite) TestHandleDecodedMessagesDatasync() {
s.Require().Equal(protocol.MessageID(&relayerKey.PublicKey, encodedPayload), decodedMessages[0].ID)
s.Require().Equal(encodedPayload, decodedMessages[0].DecryptedPayload)
s.Require().Equal(s.testMessage, decodedMessages[0].ParsedMessage)
s.Require().Equal(protocol.MessageT, decodedMessages[0].MessageType)
s.Require().Equal(&authorKey.PublicKey, decodedMessages[1].SigPubKey())
s.Require().Equal(protocol.MessageID(&authorKey.PublicKey, wrappedPayload), decodedMessages[1].ID)
s.Require().Equal(encodedPayload, decodedMessages[1].DecryptedPayload)
s.Require().Equal(s.testMessage, decodedMessages[1].ParsedMessage)
s.Require().Equal(protocol.MessageT, decodedMessages[1].MessageType)
}
func (s *MessageProcessorSuite) TestHandleDecodedMessagesDatasyncEncrypted() {
@ -277,9 +281,11 @@ func (s *MessageProcessorSuite) TestHandleDecodedMessagesDatasyncEncrypted() {
s.Require().Equal(protocol.MessageID(&relayerKey.PublicKey, encodedPayload), decodedMessages[0].ID)
s.Require().Equal(encodedPayload, decodedMessages[0].DecryptedPayload)
s.Require().Equal(s.testMessage, decodedMessages[0].ParsedMessage)
s.Require().Equal(protocol.MessageT, decodedMessages[0].MessageType)
s.Require().Equal(&authorKey.PublicKey, decodedMessages[1].SigPubKey())
s.Require().Equal(protocol.MessageID(&authorKey.PublicKey, wrappedPayload), decodedMessages[1].ID)
s.Require().Equal(encodedPayload, decodedMessages[1].DecryptedPayload)
s.Require().Equal(s.testMessage, decodedMessages[1].ParsedMessage)
s.Require().Equal(protocol.MessageT, decodedMessages[1].MessageType)
}

View File

@ -805,7 +805,7 @@ func (m *Messenger) RetrieveRawAll() (map[transport.Filter][]*protocol.StatusMes
for chat, messages := range chatWithMessages {
for _, shhMessage := range messages {
// TODO: fix this to use an exported method.
statusMessages, err := m.processor.handleMessages(shhMessage, false)
statusMessages, err := m.processor.handleMessages(shhMessage, true)
if err != nil {
logger.Info("failed to decode messages", zap.Error(err))
continue

View File

@ -76,6 +76,8 @@ func statusMessageHandler(d transit.Decoder, value interface{}) (interface{}, er
switch keyKeyword {
case transit.Keyword("text"):
sm.Content.Text, ok = contentVal.(string)
case transit.Keyword("response-to"):
sm.Content.ResponseTo, ok = contentVal.(string)
case transit.Keyword("chat-id"):
sm.Content.ChatID, ok = contentVal.(string)
}

View File

@ -34,8 +34,9 @@ var (
// Content contains the chat ID and the actual text of a message.
type Content struct {
ChatID string `json:"chat_id"`
Text string `json:"text"`
ChatID string `json:"chat_id"`
Text string `json:"text"`
ResponseTo string `json:"response-to"`
}
// TimestampInMs is a timestamp in milliseconds.

View File

@ -10,14 +10,14 @@ import (
)
var (
testMessageBytes = []byte(`["~#c4",["abc123","text/plain","~:public-group-user-message",154593077368201,1545930773682,["^ ","~:chat-id","testing-adamb","~:text","abc123"]]]`)
testMessageBytes = []byte(`["~#c4",["abc123","text/plain","~:public-group-user-message",154593077368201,1545930773682,["^ ","~:chat-id","testing-adamb","~:response-to", "id","~:text","abc123"]]]`)
testMessageStruct = Message{
Text: "abc123",
ContentT: "text/plain",
MessageT: "public-group-user-message",
Clock: 154593077368201,
Timestamp: 1545930773682,
Content: Content{"testing-adamb", "abc123"},
Content: Content{"testing-adamb", "abc123", "id"},
}
)

View File

@ -15,10 +15,20 @@ import (
statusproto "github.com/status-im/status-protocol-go/types"
)
type StatusMessageT int
const (
MessageT StatusMessageT = iota + 1
MembershipUpdateMessageT
PairMessageT
)
// StatusMessage is any Status Protocol message.
type StatusMessage struct {
// TransportMessage is the parsed message received from the transport layer, i.e the input
TransportMessage *whispertypes.Message
// MessageType is the type of application message contained
MessageType StatusMessageT
// ParsedMessage is the parsed message by the application layer, i.e the output
ParsedMessage interface{}
@ -144,6 +154,20 @@ func (m *StatusMessage) HandleApplication() error {
return err
}
m.ParsedMessage = value
switch m.ParsedMessage.(type) {
case Message:
m.MessageType = MessageT
case MembershipUpdateMessage:
m.MessageType = MembershipUpdateMessageT
case PairMessage:
m.MessageType = PairMessageT
// By default we null the parsed message field, as
// otherwise is populated with the raw transit and we are
// unable to marshal in case it contains maps
// as they have type map[interface{}]interface{}
default:
m.ParsedMessage = nil
}
return nil
}