Created ChatEntity encoding per chat type

This commit is contained in:
Samuel Hawksby-Robinson 2020-07-25 17:13:08 +01:00 committed by Andrea Maria Piana
parent 6ffe67deec
commit 3e857203ac
No known key found for this signature in database
GPG Key ID: AA6CCA6DE0E06424
8 changed files with 100 additions and 21 deletions

View File

@ -3,11 +3,18 @@ package protocol
import (
"crypto/ecdsa"
"github.com/golang/protobuf/proto"
"github.com/status-im/status-go/protocol/protobuf"
)
type ChatEntity interface {
proto.Message
GetChatId() string
GetMessageType() protobuf.MessageType
GetSigPubKey() *ecdsa.PublicKey
GetProtobuf() proto.Message
SetMessageType(messageType protobuf.MessageType)
}

View File

@ -18,7 +18,6 @@ import (
"github.com/status-im/status-go/protocol/datasync"
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/protobuf"
"github.com/status-im/status-go/protocol/transport"
v1protocol "github.com/status-im/status-go/protocol/v1"
)
@ -263,13 +262,13 @@ func (p *MessageProcessor) SendPairInstallation(
// All the events in a group are encoded and added to the payload
func (p *MessageProcessor) EncodeMembershipUpdate(
group *v1protocol.Group,
chatMessage *protobuf.ChatMessage,
chatEntity proto.Message,
) ([]byte, error) {
message := v1protocol.MembershipUpdateMessage{
ChatID: group.ChatID(),
Events: group.Events(),
Message: chatMessage,
Message: chatEntity,
}
encodedMessage, err := v1protocol.EncodeMembershipUpdateMessage(message)
if err != nil {

View File

@ -3,6 +3,8 @@ package protocol
import (
"crypto/ecdsa"
"github.com/golang/protobuf/proto"
"github.com/status-im/status-go/protocol/protobuf"
)
@ -25,7 +27,19 @@ type EmojiReaction struct {
}
// GetSigPubKey returns an ecdsa encoded public key
// this function is also required to implement the ChatEntity interface
// this function is required to implement the ChatEntity interface
func (e EmojiReaction) GetSigPubKey() *ecdsa.PublicKey {
return e.SigPubKey
}
// GetProtoBuf returns the struct's embedded protobuf struct
// this function is required to implement the ChatEntity interface
func (e EmojiReaction) GetProtobuf() proto.Message {
return &e.EmojiReaction
}
// SetMessageType a setter for the MessageType field
// this function is required to implement the ChatEntity interface
func (e *EmojiReaction) SetMessageType(messageType protobuf.MessageType) {
e.MessageType = messageType
}

View File

@ -10,8 +10,9 @@ import (
"unicode"
"unicode/utf8"
"github.com/status-im/markdown"
"github.com/golang/protobuf/proto"
"github.com/status-im/markdown"
"github.com/status-im/status-go/protocol/protobuf"
)
@ -334,7 +335,19 @@ func getAudioMessageMIME(i *protobuf.AudioMessage) (string, error) {
}
// GetSigPubKey returns an ecdsa encoded public key
// this function is also required to implement the ChatEntity interface
// this function is required to implement the ChatEntity interface
func (m Message) GetSigPubKey() *ecdsa.PublicKey {
return m.SigPubKey
}
// GetProtoBuf returns the struct's embedded protobuf struct
// this function is required to implement the ChatEntity interface
func (m *Message) GetProtobuf() proto.Message {
return &m.ChatMessage
}
// SetMessageType a setter for the MessageType field
// this function is required to implement the ChatEntity interface
func (m *Message) SetMessageType(messageType protobuf.MessageType) {
m.MessageType = messageType
}

View File

@ -107,8 +107,9 @@ func (m *MessageHandler) HandleMembershipUpdate(messageState *ReceivedMessageSta
// Set in the map
messageState.ModifiedChats[chat.ID] = true
if message.Message != nil {
messageState.CurrentMessageState.Message = *message.Message
msg, ok := message.Message.(*protobuf.ChatMessage)
if msg != nil && ok {
messageState.CurrentMessageState.Message = *msg
return m.HandleChatMessage(messageState)
}

View File

@ -732,9 +732,9 @@ func (db sqlitePersistence) SaveEmojiReaction(emojiReaction *EmojiReaction) (err
emojiReaction.ID,
emojiReaction.Clock,
emojiReaction.From,
emojiReaction.EmojiID,
emojiReaction.MessageID,
emojiReaction.ChatID,
emojiReaction.Type,
emojiReaction.MessageId,
emojiReaction.ChatId,
emojiReaction.Retracted,
}
@ -788,9 +788,9 @@ func (db sqlitePersistence) EmojiReactionByID(id string) (*EmojiReaction, error)
&emojiReaction.ID,
&emojiReaction.Clock,
&emojiReaction.From,
&emojiReaction.EmojiID,
&emojiReaction.MessageID,
&emojiReaction.ChatID,
&emojiReaction.Type,
&emojiReaction.MessageId,
&emojiReaction.ChatId,
&emojiReaction.Retracted,
}
err = row.Scan(args...)

View File

@ -4,6 +4,7 @@ import (
"context"
"crypto/ecdsa"
"database/sql"
"github.com/duo-labs/webauthn.io/logger"
"io/ioutil"
"math/rand"
"os"
@ -3288,11 +3289,13 @@ func (m *Messenger) SendEmojiReaction(ctx context.Context, chatID, messageID str
}
emojiR := &EmojiReaction{
EmojiReaction: protobuf.EmojiReaction{
Clock: clock,
MessageId: messageID,
ChatId: chatID,
Type: protobuf.EmojiReaction_Type(emojiID),
},
ID: types.EncodeHex(id),
Clock: clock,
MessageID: messageID,
ChatID: chatID,
EmojiID: protobuf.EmojiReaction_Type(emojiID),
From: types.EncodeHex(crypto.FromECDSAPub(&m.identity.PublicKey)),
Retracted: false,
}
@ -3328,7 +3331,7 @@ func (m *Messenger) SendEmojiReactionRetraction(ctx context.Context, emojiReacti
}
// Get chat and clock
chat, ok := m.allChats[emojiReaction.ChatID]
chat, ok := m.allChats[emojiReaction.GetChatId()]
if !ok {
return nil, ErrChatNotFound
}
@ -3346,7 +3349,7 @@ func (m *Messenger) SendEmojiReactionRetraction(ctx context.Context, emojiReacti
// Send the marshalled EmojiReactionRetraction protobuf
_, err = m.dispatchMessage(ctx, &common.RawMessage{
LocalChatID: emojiReaction.ChatID,
LocalChatID: emojiReaction.GetChatId(),
Payload: encodedMessage,
MessageType: protobuf.ApplicationMetadataMessage_EMOJI_REACTION_RETRACTION,
ResendAutomatically: true,
@ -3369,3 +3372,43 @@ func (m *Messenger) SendEmojiReactionRetraction(ctx context.Context, emojiReacti
return &response, nil
}
func (m *Messenger) encodeChatEntity(chat *Chat, message ChatEntity) ([]byte, error) {
var encodedMessage []byte
var err error
switch chat.ChatType {
case ChatTypeOneToOne:
logger.Debug("sending private message")
message.SetMessageType(protobuf.MessageType_ONE_TO_ONE)
encodedMessage, err = proto.Marshal(message)
if err != nil {
return nil, err
}
case ChatTypePublic:
logger.Debug("sending public message", zap.String("chatName", chat.Name))
message.SetMessageType(protobuf.MessageType_PUBLIC_GROUP)
encodedMessage, err = proto.Marshal(message)
if err != nil {
return nil, err
}
case ChatTypePrivateGroupChat:
message.SetMessageType(protobuf.MessageType_PRIVATE_GROUP)
logger.Debug("sending group message", zap.String("chatName", chat.Name))
group, err := newProtocolGroupFromChat(chat)
if err != nil {
return nil, err
}
encodedMessage, err = m.processor.EncodeMembershipUpdate(group, message.GetProtobuf())
if err != nil {
return nil, err
}
default:
return nil, errors.New("chat type not supported")
}
return encodedMessage, nil
}

View File

@ -22,7 +22,7 @@ import (
type MembershipUpdateMessage struct {
ChatID string `json:"chatId"` // UUID concatenated with hex-encoded public key of the creator for the chat
Events []MembershipUpdateEvent `json:"events"`
Message *protobuf.ChatMessage `json:"-"`
Message proto.Message `json:"-"`
}
const signatureLength = 65
@ -70,6 +70,8 @@ func (m *MembershipUpdateMessage) ToProtobuf() *protobuf.MembershipUpdateMessage
return &protobuf.MembershipUpdateMessage{
ChatId: m.ChatID,
Events: rawEvents,
// TODO handle this
Message: m.Message,
}
}