Implemented encodeChatEntity() into SendChatMessage and SendEmojiReaction

This commit is contained in:
Samuel Hawksby-Robinson 2020-07-26 23:06:58 +01:00 committed by Andrea Maria Piana
parent b70c362f2b
commit acfcac6748
No known key found for this signature in database
GPG Key ID: AA6CCA6DE0E06424
1 changed files with 16 additions and 50 deletions

View File

@ -1425,39 +1425,10 @@ func (m *Messenger) SendChatMessage(ctx context.Context, message *Message) (*Mes
return nil, err return nil, err
} }
var encodedMessage []byte encodedMessage, err := m.encodeChatEntity(chat, message)
switch chat.ChatType {
case ChatTypeOneToOne:
logger.Debug("sending private message")
message.MessageType = protobuf.MessageType_ONE_TO_ONE
encodedMessage, err = proto.Marshal(message)
if err != nil { if err != nil {
return nil, err return nil, err
} }
case ChatTypePublic:
logger.Debug("sending public message", zap.String("chatName", chat.Name))
message.MessageType = protobuf.MessageType_PUBLIC_GROUP
encodedMessage, err = proto.Marshal(message)
if err != nil {
return nil, err
}
case ChatTypePrivateGroupChat:
message.MessageType = 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)
if err != nil {
return nil, err
}
default:
return nil, errors.New("chat type not supported")
}
id, err := m.dispatchMessage(ctx, common.RawMessage{ id, err := m.dispatchMessage(ctx, common.RawMessage{
LocalChatID: chat.ID, LocalChatID: chat.ID,
@ -3268,12 +3239,17 @@ func (m *Messenger) SendEmojiReaction(ctx context.Context, chatID, messageID str
} }
clock, _ := chat.NextClockAndTimestamp(m.getTimesource()) clock, _ := chat.NextClockAndTimestamp(m.getTimesource())
emojiReaction := &protobuf.EmojiReaction{ emojiR := &EmojiReaction{
EmojiReaction: protobuf.EmojiReaction{
Clock: clock, Clock: clock,
MessageId: messageID, MessageId: messageID,
ChatId: chatID,
Type: protobuf.EmojiReaction_Type(emojiID), Type: protobuf.EmojiReaction_Type(emojiID),
},
From: types.EncodeHex(crypto.FromECDSAPub(&m.identity.PublicKey)),
Retracted: false,
} }
encodedMessage, err := proto.Marshal(emojiReaction) encodedMessage, err := m.encodeChatEntity(chat, emojiR)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -3288,17 +3264,7 @@ func (m *Messenger) SendEmojiReaction(ctx context.Context, chatID, messageID str
return nil, err return nil, err
} }
emojiR := &EmojiReaction{ emojiR.ID = types.EncodeHex(id)
EmojiReaction: protobuf.EmojiReaction{
Clock: clock,
MessageId: messageID,
ChatId: chatID,
Type: protobuf.EmojiReaction_Type(emojiID),
},
ID: types.EncodeHex(id),
From: types.EncodeHex(crypto.FromECDSAPub(&m.identity.PublicKey)),
Retracted: false,
}
response.EmojiReactions = []*EmojiReaction{emojiR} response.EmojiReactions = []*EmojiReaction{emojiR}
response.Chats = []*Chat{chat} response.Chats = []*Chat{chat}
@ -3381,14 +3347,14 @@ func (m *Messenger) encodeChatEntity(chat *Chat, message ChatEntity) ([]byte, er
case ChatTypeOneToOne: case ChatTypeOneToOne:
logger.Debug("sending private message") logger.Debug("sending private message")
message.SetMessageType(protobuf.MessageType_ONE_TO_ONE) message.SetMessageType(protobuf.MessageType_ONE_TO_ONE)
encodedMessage, err = proto.Marshal(message) encodedMessage, err = proto.Marshal(message.GetProtobuf())
if err != nil { if err != nil {
return nil, err return nil, err
} }
case ChatTypePublic: case ChatTypePublic:
logger.Debug("sending public message", zap.String("chatName", chat.Name)) logger.Debug("sending public message", zap.String("chatName", chat.Name))
message.SetMessageType(protobuf.MessageType_PUBLIC_GROUP) message.SetMessageType(protobuf.MessageType_PUBLIC_GROUP)
encodedMessage, err = proto.Marshal(message) encodedMessage, err = proto.Marshal(message.GetProtobuf())
if err != nil { if err != nil {
return nil, err return nil, err
} }