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 return nil, err
} }
id, err := m.dispatchMessage(ctx, &RawMessage{ id, err := m.dispatchMessage(ctx, &common.RawMessage{
LocalChatID: chatID, LocalChatID: chatID,
Payload: encodedMessage, Payload: encodedMessage,
MessageType: protobuf.ApplicationMetadataMessage_EMOJI_REACTION, MessageType: protobuf.ApplicationMetadataMessage_EMOJI_REACTION,
@ -3328,7 +3328,7 @@ func (m *Messenger) SendEmojiReactionRetraction(ctx context.Context, emojiReacti
} }
// Send the marshalled EmojiReactionRetraction protobuf // Send the marshalled EmojiReactionRetraction protobuf
_, err = m.dispatchMessage(ctx, &RawMessage{ _, err = m.dispatchMessage(ctx, &common.RawMessage{
LocalChatID: emojiReaction.ChatID, LocalChatID: emojiReaction.ChatID,
Payload: encodedMessage, Payload: encodedMessage,
MessageType: protobuf.ApplicationMetadataMessage_EMOJI_REACTION_RETRACTION, MessageType: protobuf.ApplicationMetadataMessage_EMOJI_REACTION_RETRACTION,

View File

@ -406,6 +406,7 @@ func _1594390919_create_emoji_reactions_tableUpSql() (*asset, error) {
return nil, err return nil, err
} }
<<<<<<< HEAD
<<<<<<< HEAD <<<<<<< HEAD
info := bindataFileInfo{name: "1594390919_create_emoji_reactions_table.up.sql", size: 234, mode: os.FileMode(0644), modTime: time.Unix(1595864971, 0)} 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}} 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(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}} 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 return a, nil
} }

View File

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