Added general protobuf umarshaller function

This commit is contained in:
Samuel Hawksby-Robinson 2020-07-22 15:54:24 +01:00 committed by Andrea Maria Piana
parent 5823ebe446
commit eb562122b6
No known key found for this signature in database
GPG Key ID: AA6CCA6DE0E06424
3 changed files with 57 additions and 224 deletions

View File

@ -3259,7 +3259,7 @@ func (m *Messenger) SendEmojiReaction(ctx context.Context, chatID, messageID str
return nil, err
}
id, err := m.dispatchMessage(ctx, &RawMessage{
id, err := m.dispatchMessage(ctx, &common.RawMessage{
LocalChatID: chatID,
Payload: encodedMessage,
MessageType: protobuf.ApplicationMetadataMessage_EMOJI_REACTION,
@ -3303,8 +3303,8 @@ func (m *Messenger) SendEmojiReactionRetraction(ctx context.Context, emojiReacti
// Check that the sender is the key owner
pk := types.EncodeHex(crypto.FromECDSAPub(&m.identity.PublicKey))
if emojiReaction.From != pk {
return nil, errors.Errorf("identity mismatch, " +
"emoji reactions can only be retracted by the reaction sender, " +
return nil, errors.Errorf("identity mismatch, "+
"emoji reactions can only be retracted by the reaction sender, "+
"emoji reaction sent by '%s', current identity '%s'",
emojiReaction.From, pk,
)
@ -3328,7 +3328,7 @@ func (m *Messenger) SendEmojiReactionRetraction(ctx context.Context, emojiReacti
}
// Send the marshalled EmojiReactionRetraction protobuf
_, err = m.dispatchMessage(ctx, &RawMessage{
_, err = m.dispatchMessage(ctx, &common.RawMessage{
LocalChatID: emojiReaction.ChatID,
Payload: encodedMessage,
MessageType: protobuf.ApplicationMetadataMessage_EMOJI_REACTION_RETRACTION,

View File

@ -406,6 +406,7 @@ func _1594390919_create_emoji_reactions_tableUpSql() (*asset, error) {
return nil, err
}
<<<<<<< HEAD
<<<<<<< HEAD
info := bindataFileInfo{name: "1594390919_create_emoji_reactions_table.up.sql", size: 234, mode: os.FileMode(0644), modTime: time.Unix(1595864971, 0)}
=======
@ -415,8 +416,10 @@ func _1594390919_create_emoji_reactions_tableUpSql() (*asset, error) {
a := &asset{bytes: bytes, info: info, digest: [32]uint8{0x5f, 0xcb, 0xf0, 0xaf, 0xa8, 0x82, 0xec, 0x10, 0xd0, 0xae, 0x83, 0x78, 0xa7, 0x90, 0x84, 0x3d, 0xc0, 0xfe, 0x6, 0xe6, 0xc8, 0x8b, 0xda, 0xe0, 0x1a, 0x81, 0x86, 0x61, 0xe9, 0xb0, 0xa2, 0x31}}
=======
info := bindataFileInfo{name: "1594390919_create_emoji_reactions_table.up.sql", size: 263, mode: os.FileMode(0644), modTime: time.Unix(1595339114, 0)}
=======
info := bindataFileInfo{name: "1594390919_create_emoji_reactions_table.up.sql", size: 263, mode: os.FileMode(0644), modTime: time.Unix(1595840951, 0)}
>>>>>>> 546b68df9... Added general protobuf umarshaller function
a := &asset{bytes: bytes, info: info, digest: [32]uint8{0x60, 0xab, 0x81, 0xa6, 0x1a, 0x5a, 0x9, 0xee, 0x72, 0x18, 0x5d, 0xe7, 0xf1, 0x94, 0x66, 0x2d, 0x38, 0x29, 0x57, 0xf8, 0xec, 0x1c, 0x1b, 0x1f, 0xc6, 0x1d, 0xb5, 0x2e, 0xe8, 0x15, 0x8, 0x74}}
>>>>>>> c546f6146... make generate
return a, nil
}

View File

@ -4,6 +4,7 @@ import (
"crypto/ecdsa"
"encoding/json"
"log"
"reflect"
"github.com/golang/protobuf/proto"
"github.com/jinzhu/copier"
@ -175,261 +176,90 @@ func (m *StatusMessage) HandleApplicationMetadata() error {
func (m *StatusMessage) HandleApplication() error {
switch m.Type {
case protobuf.ApplicationMetadataMessage_CHAT_MESSAGE:
var message protobuf.ChatMessage
return m.unmarshalProtobufData(new(protobuf.ChatMessage))
err := proto.Unmarshal(m.DecryptedPayload, &message)
if err != nil {
m.ParsedMessage = nil
log.Printf("[message::DecodeMessage] could not decode ChatMessage: %#x, err: %v", m.Hash, err.Error())
} else {
m.ParsedMessage = message
return nil
}
case protobuf.ApplicationMetadataMessage_MEMBERSHIP_UPDATE_MESSAGE:
var message protobuf.MembershipUpdateMessage
err := proto.Unmarshal(m.DecryptedPayload, &message)
if err != nil {
m.ParsedMessage = nil
log.Printf("[message::DecodeMessage] could not decode MembershipUpdateMessage: %#x, err: %v", m.Hash, err.Error())
} else {
m.ParsedMessage = message
return m.unmarshalProtobufData(new(protobuf.MembershipUpdateMessage))
return nil
}
case protobuf.ApplicationMetadataMessage_ACCEPT_REQUEST_ADDRESS_FOR_TRANSACTION:
var message protobuf.AcceptRequestAddressForTransaction
err := proto.Unmarshal(m.DecryptedPayload, &message)
if err != nil {
m.ParsedMessage = nil
log.Printf("[message::DecodeMessage] could not decode AcceptRequestAddressForTransaction: %#x, err: %v", m.Hash, err.Error())
} else {
m.ParsedMessage = message
return m.unmarshalProtobufData(new(protobuf.AcceptRequestAddressForTransaction))
return nil
}
case protobuf.ApplicationMetadataMessage_SEND_TRANSACTION:
var message protobuf.SendTransaction
err := proto.Unmarshal(m.DecryptedPayload, &message)
if err != nil {
m.ParsedMessage = nil
log.Printf("[message::DecodeMessage] could not decode SendTransaction: %#x, err: %v", m.Hash, err.Error())
} else {
m.ParsedMessage = message
return nil
}
return m.unmarshalProtobufData(new(protobuf.SendTransaction))
case protobuf.ApplicationMetadataMessage_REQUEST_TRANSACTION:
var message protobuf.RequestTransaction
err := proto.Unmarshal(m.DecryptedPayload, &message)
if err != nil {
m.ParsedMessage = nil
log.Printf("[message::DecodeMessage] could not decode RequestTransaction: %#x, err: %v", m.Hash, err.Error())
} else {
m.ParsedMessage = message
return nil
}
return m.unmarshalProtobufData(new(protobuf.RequestTransaction))
case protobuf.ApplicationMetadataMessage_DECLINE_REQUEST_ADDRESS_FOR_TRANSACTION:
var message protobuf.DeclineRequestAddressForTransaction
err := proto.Unmarshal(m.DecryptedPayload, &message)
if err != nil {
m.ParsedMessage = nil
log.Printf("[message::DecodeMessage] could not decode DeclineRequestAddressForTransaction: %#x, err: %v", m.Hash, err.Error())
} else {
m.ParsedMessage = message
return m.unmarshalProtobufData(new(protobuf.DeclineRequestAddressForTransaction))
return nil
}
case protobuf.ApplicationMetadataMessage_DECLINE_REQUEST_TRANSACTION:
var message protobuf.DeclineRequestTransaction
err := proto.Unmarshal(m.DecryptedPayload, &message)
if err != nil {
m.ParsedMessage = nil
log.Printf("[message::DecodeMessage] could not decode DeclineRequestTransaction: %#x, err: %v", m.Hash, err.Error())
} else {
m.ParsedMessage = message
return nil
}
return m.unmarshalProtobufData(new(protobuf.DeclineRequestTransaction))
case protobuf.ApplicationMetadataMessage_REQUEST_ADDRESS_FOR_TRANSACTION:
var message protobuf.RequestAddressForTransaction
err := proto.Unmarshal(m.DecryptedPayload, &message)
if err != nil {
m.ParsedMessage = nil
log.Printf("[message::DecodeMessage] could not decode RequestAddressForTransaction: %#x, err: %v", m.Hash, err.Error())
} else {
m.ParsedMessage = message
return nil
}
return m.unmarshalProtobufData(new(protobuf.RequestAddressForTransaction))
case protobuf.ApplicationMetadataMessage_CONTACT_UPDATE:
var message protobuf.ContactUpdate
err := proto.Unmarshal(m.DecryptedPayload, &message)
if err != nil {
m.ParsedMessage = nil
log.Printf("[message::DecodeMessage] could not decode ContactUpdate: %#x, err: %v", m.Hash, err.Error())
} else {
m.ParsedMessage = message
return m.unmarshalProtobufData(new(protobuf.ContactUpdate))
return nil
}
case protobuf.ApplicationMetadataMessage_SYNC_INSTALLATION:
var message protobuf.SyncInstallation
err := proto.Unmarshal(m.DecryptedPayload, &message)
if err != nil {
m.ParsedMessage = nil
log.Printf("[message::DecodeMessage] could not decode SyncInstallation: %#x, err: %v", m.Hash, err.Error())
} else {
m.ParsedMessage = message
return m.unmarshalProtobufData(new(protobuf.SyncInstallation))
return nil
}
case protobuf.ApplicationMetadataMessage_SYNC_INSTALLATION_CONTACT:
var message protobuf.SyncInstallationContact
log.Printf("Sync installation contact")
err := proto.Unmarshal(m.DecryptedPayload, &message)
if err != nil {
m.ParsedMessage = nil
log.Printf("[message::DecodeMessage] could not decode SyncInstallationContact: %#x, err: %v", m.Hash, err.Error())
} else {
m.ParsedMessage = message
return m.unmarshalProtobufData(new(protobuf.SyncInstallationContact))
return nil
}
case protobuf.ApplicationMetadataMessage_SYNC_INSTALLATION_PUBLIC_CHAT:
var message protobuf.SyncInstallationPublicChat
err := proto.Unmarshal(m.DecryptedPayload, &message)
if err != nil {
m.ParsedMessage = nil
log.Printf("[message::DecodeMessage] could not decode SyncInstallationPublicChat: %#x, err: %v", m.Hash, err.Error())
} else {
m.ParsedMessage = message
return m.unmarshalProtobufData(new(protobuf.SyncInstallationPublicChat))
return nil
}
case protobuf.ApplicationMetadataMessage_SYNC_INSTALLATION_ACCOUNT:
var message protobuf.SyncInstallationAccount
err := proto.Unmarshal(m.DecryptedPayload, &message)
if err != nil {
m.ParsedMessage = nil
log.Printf("[message::DecodeMessage] could not decode SyncInstallationAccount: %#x, err: %v", m.Hash, err.Error())
} else {
m.ParsedMessage = message
return m.unmarshalProtobufData(new(protobuf.SyncInstallationAccount))
return nil
}
case protobuf.ApplicationMetadataMessage_PAIR_INSTALLATION:
var message protobuf.PairInstallation
err := proto.Unmarshal(m.DecryptedPayload, &message)
if err != nil {
m.ParsedMessage = nil
log.Printf("[message::DecodeMessage] could not decode PairInstallation: %#x, err: %v", m.Hash, err.Error())
} else {
m.ParsedMessage = message
return nil
}
case protobuf.ApplicationMetadataMessage_PUSH_NOTIFICATION_REGISTRATION:
// This message is a bit different as it's encrypted, so we pass it straight through
m.ParsedMessage = m.DecryptedPayload
return nil
return m.unmarshalProtobufData(new(protobuf.PairInstallation))
case protobuf.ApplicationMetadataMessage_CONTACT_CODE_ADVERTISEMENT:
var message protobuf.ContactCodeAdvertisement
err := proto.Unmarshal(m.DecryptedPayload, &message)
if err != nil {
m.ParsedMessage = nil
log.Printf("[message::DecodeMessage] could not decode ContactCodeAdvertisement: %#x, err: %v", m.Hash, err.Error())
} else {
m.ParsedMessage = message
return nil
}
return m.unmarshalProtobufData(new(protobuf.ContactCodeAdvertisement))
case protobuf.ApplicationMetadataMessage_PUSH_NOTIFICATION_REQUEST:
var message protobuf.PushNotificationRequest
err := proto.Unmarshal(m.DecryptedPayload, &message)
if err != nil {
m.ParsedMessage = nil
log.Printf("[message::DecodeMessage] could not decode PushNotificationRequest: %#x, err: %v", m.Hash, err.Error())
} else {
m.ParsedMessage = message
return nil
}
return m.unmarshalProtobufData(new(protobuf.PushNotificationRequest))
case protobuf.ApplicationMetadataMessage_PUSH_NOTIFICATION_REGISTRATION_RESPONSE:
var message protobuf.PushNotificationRegistrationResponse
err := proto.Unmarshal(m.DecryptedPayload, &message)
if err != nil {
m.ParsedMessage = nil
log.Printf("[message::DecodeMessage] could not decode PushNotificationRegistrationResponse: %#x, err: %v", m.Hash, err.Error())
} else {
m.ParsedMessage = message
return nil
}
return m.unmarshalProtobufData(new(protobuf.PushNotificationRegistrationResponse))
case protobuf.ApplicationMetadataMessage_PUSH_NOTIFICATION_QUERY:
var message protobuf.PushNotificationQuery
err := proto.Unmarshal(m.DecryptedPayload, &message)
if err != nil {
m.ParsedMessage = nil
log.Printf("[message::DecodeMessage] could not decode PushNotificationQuery: %#x, err: %v", m.Hash, err.Error())
} else {
m.ParsedMessage = message
return nil
}
return m.unmarshalProtobufData(new(protobuf.PushNotificationQuery))
case protobuf.ApplicationMetadataMessage_PUSH_NOTIFICATION_QUERY_RESPONSE:
var message protobuf.PushNotificationQueryResponse
err := proto.Unmarshal(m.DecryptedPayload, &message)
if err != nil {
m.ParsedMessage = nil
log.Printf("[message::DecodeMessage] could not decode PushNotificationQueryResponse: %#x, err: %v", m.Hash, err.Error())
} else {
m.ParsedMessage = message
return nil
}
return m.unmarshalProtobufData(new(protobuf.PushNotificationQueryResponse))
case protobuf.ApplicationMetadataMessage_PUSH_NOTIFICATION_RESPONSE:
var message protobuf.PushNotificationResponse
err := proto.Unmarshal(m.DecryptedPayload, &message)
if err != nil {
m.ParsedMessage = nil
log.Printf("[message::DecodeMessage] could not decode PushNotificationResponse: %#x, err: %v", m.Hash, err.Error())
} else {
m.ParsedMessage = message
return nil
}
return m.unmarshalProtobufData(new(protobuf.PushNotificationResponse))
case protobuf.ApplicationMetadataMessage_EMOJI_REACTION:
var message protobuf.EmojiReaction
err := proto.Unmarshal(m.DecryptedPayload, &message)
if err != nil {
m.ParsedMessage = nil
log.Printf("[message::DecodeMessage] could not decode EmojiReaction: %#x, err: %v", m.Hash, err.Error())
} else {
m.ParsedMessage = message
return nil
}
return m.unmarshalProtobufData(new(protobuf.EmojiReaction))
case protobuf.ApplicationMetadataMessage_EMOJI_REACTION_RETRACTION:
var message protobuf.EmojiReactionRetraction
err := proto.Unmarshal(m.DecryptedPayload, &message)
if err != nil {
m.ParsedMessage = nil
log.Printf("[message::DecodeMessage] could not decode EmojiReactionRetraction: %#x, err: %v", m.Hash, err.Error())
} else {
m.ParsedMessage = message
return nil
}
return m.unmarshalProtobufData(new(protobuf.EmojiReactionRetraction))
case protobuf.ApplicationMetadataMessage_PUSH_NOTIFICATION_REGISTRATION:
// This message is a bit different as it's encrypted, so we pass it straight through
m.ParsedMessage = m.DecryptedPayload
return nil
}
return nil
}
func (m *StatusMessage) unmarshalProtobufData(pb proto.Message) error {
var ptr proto.Message
rv := reflect.ValueOf(pb)
if rv.Kind() == reflect.Ptr {
ptr = pb
} else {
ptr = rv.Addr().Interface().(proto.Message)
}
err := proto.Unmarshal(m.DecryptedPayload, ptr)
if err != nil {
m.ParsedMessage = nil
log.Printf("[message::DecodeMessage] could not decode %T: %#x, err: %v", pb, m.Hash, err.Error())
} else {
rv = reflect.ValueOf(ptr)
m.ParsedMessage = rv.Elem()
return nil
}
return nil
}