Updated MembershipUpdateMessage to use interfaced ChatEntity

Replaces ChatMessage only field
This commit is contained in:
Samuel Hawksby-Robinson 2020-07-25 22:25:40 +01:00 committed by Andrea Maria Piana
parent b6d24e950c
commit 45c1e5ba1c
No known key found for this signature in database
GPG Key ID: AA6CCA6DE0E06424
3 changed files with 37 additions and 14 deletions

View File

@ -18,6 +18,7 @@ import (
"github.com/status-im/status-go/protocol/datasync" "github.com/status-im/status-go/protocol/datasync"
datasyncpeer "github.com/status-im/status-go/protocol/datasync/peer" datasyncpeer "github.com/status-im/status-go/protocol/datasync/peer"
"github.com/status-im/status-go/protocol/encryption" "github.com/status-im/status-go/protocol/encryption"
"github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/protocol/transport" "github.com/status-im/status-go/protocol/transport"
v1protocol "github.com/status-im/status-go/protocol/v1" v1protocol "github.com/status-im/status-go/protocol/v1"
) )
@ -262,13 +263,20 @@ func (p *MessageProcessor) SendPairInstallation(
// All the events in a group are encoded and added to the payload // All the events in a group are encoded and added to the payload
func (p *MessageProcessor) EncodeMembershipUpdate( func (p *MessageProcessor) EncodeMembershipUpdate(
group *v1protocol.Group, group *v1protocol.Group,
chatEntity proto.Message, chatEntity ChatEntity,
) ([]byte, error) { ) ([]byte, error) {
m := chatEntity.GetProtobuf().(*protobuf.ChatMessage)
e := chatEntity.GetProtobuf().(*protobuf.EmojiReaction)
if m == nil && e == nil {
return nil, errors.New("chat entity must be of type protobuf.ChatMessage or protobuf.EmojiReaction")
}
message := v1protocol.MembershipUpdateMessage{ message := v1protocol.MembershipUpdateMessage{
ChatID: group.ChatID(), ChatID: group.ChatID(),
Events: group.Events(), Events: group.Events(),
Message: chatEntity, Message: m,
EmojiReaction: e,
} }
encodedMessage, err := v1protocol.EncodeMembershipUpdateMessage(message) encodedMessage, err := v1protocol.EncodeMembershipUpdateMessage(message)
if err != nil { if err != nil {

View File

@ -3401,7 +3401,7 @@ func (m *Messenger) encodeChatEntity(chat *Chat, message ChatEntity) ([]byte, er
return nil, err return nil, err
} }
encodedMessage, err = m.processor.EncodeMembershipUpdate(group, message.GetProtobuf()) encodedMessage, err = m.processor.EncodeMembershipUpdate(group, message)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -20,9 +20,10 @@ import (
// about group membership changes. // about group membership changes.
// For more information, see https://github.com/status-im/specs/blob/master/status-group-chats-spec.md. // For more information, see https://github.com/status-im/specs/blob/master/status-group-chats-spec.md.
type MembershipUpdateMessage struct { type MembershipUpdateMessage struct {
ChatID string `json:"chatId"` // UUID concatenated with hex-encoded public key of the creator for the chat ChatID string `json:"chatId"` // UUID concatenated with hex-encoded public key of the creator for the chat
Events []MembershipUpdateEvent `json:"events"` Events []MembershipUpdateEvent `json:"events"`
Message proto.Message `json:"-"` Message *protobuf.ChatMessage `json:"-"`
EmojiReaction *protobuf.EmojiReaction `json:"-"`
} }
const signatureLength = 65 const signatureLength = 65
@ -59,7 +60,7 @@ func MembershipUpdateEventFromProtobuf(chatID string, raw []byte) (*MembershipUp
}, nil }, nil
} }
func (m *MembershipUpdateMessage) ToProtobuf() *protobuf.MembershipUpdateMessage { func (m *MembershipUpdateMessage) ToProtobuf() (*protobuf.MembershipUpdateMessage, error) {
var rawEvents [][]byte var rawEvents [][]byte
for _, e := range m.Events { for _, e := range m.Events {
var encodedEvent []byte var encodedEvent []byte
@ -67,13 +68,22 @@ func (m *MembershipUpdateMessage) ToProtobuf() *protobuf.MembershipUpdateMessage
encodedEvent = append(encodedEvent, e.RawPayload...) encodedEvent = append(encodedEvent, e.RawPayload...)
rawEvents = append(rawEvents, encodedEvent) rawEvents = append(rawEvents, encodedEvent)
} }
return &protobuf.MembershipUpdateMessage{
ChatId: m.ChatID,
Events: rawEvents,
// TODO handle this mUM := &protobuf.MembershipUpdateMessage{
Message: m.Message, ChatId: m.ChatID,
Events: rawEvents,
} }
switch {
case m.Message != nil:
mUM.ChatEntity = &protobuf.MembershipUpdateMessage_Message{Message: m.Message}
case m.EmojiReaction != nil:
mUM.ChatEntity = &protobuf.MembershipUpdateMessage_EmojiReaction{EmojiReaction: m.EmojiReaction}
default:
return nil, errors.New("neither Message or EmojiReaction is set")
}
return mUM, nil
} }
func MembershipUpdateMessageFromProtobuf(raw *protobuf.MembershipUpdateMessage) (*MembershipUpdateMessage, error) { func MembershipUpdateMessageFromProtobuf(raw *protobuf.MembershipUpdateMessage) (*MembershipUpdateMessage, error) {
@ -88,13 +98,18 @@ func MembershipUpdateMessageFromProtobuf(raw *protobuf.MembershipUpdateMessage)
return &MembershipUpdateMessage{ return &MembershipUpdateMessage{
ChatID: raw.ChatId, ChatID: raw.ChatId,
Events: events, Events: events,
Message: raw.Message, Message: raw.GetMessage(),
}, nil }, nil
} }
// EncodeMembershipUpdateMessage encodes a MembershipUpdateMessage using protobuf serialization. // EncodeMembershipUpdateMessage encodes a MembershipUpdateMessage using protobuf serialization.
func EncodeMembershipUpdateMessage(value MembershipUpdateMessage) ([]byte, error) { func EncodeMembershipUpdateMessage(value MembershipUpdateMessage) ([]byte, error) {
return proto.Marshal(value.ToProtobuf()) pb, err := value.ToProtobuf()
if err != nil {
return nil, err
}
return proto.Marshal(pb)
} }
// MembershipUpdateEvent contains an event information. // MembershipUpdateEvent contains an event information.