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,38 +1425,9 @@ func (m *Messenger) SendChatMessage(ctx context.Context, message *Message) (*Mes
return nil, err
}
var encodedMessage []byte
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 {
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")
encodedMessage, err := m.encodeChatEntity(chat, message)
if err != nil {
return nil, err
}
id, err := m.dispatchMessage(ctx, common.RawMessage{
@ -3268,12 +3239,17 @@ func (m *Messenger) SendEmojiReaction(ctx context.Context, chatID, messageID str
}
clock, _ := chat.NextClockAndTimestamp(m.getTimesource())
emojiReaction := &protobuf.EmojiReaction{
Clock: clock,
MessageId: messageID,
Type: protobuf.EmojiReaction_Type(emojiID),
emojiR := &EmojiReaction{
EmojiReaction: protobuf.EmojiReaction{
Clock: clock,
MessageId: messageID,
ChatId: chatID,
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 {
return nil, err
}
@ -3288,17 +3264,7 @@ func (m *Messenger) SendEmojiReaction(ctx context.Context, chatID, messageID str
return nil, err
}
emojiR := &EmojiReaction{
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,
}
emojiR.ID = types.EncodeHex(id)
response.EmojiReactions = []*EmojiReaction{emojiR}
response.Chats = []*Chat{chat}
@ -3381,14 +3347,14 @@ func (m *Messenger) encodeChatEntity(chat *Chat, message ChatEntity) ([]byte, er
case ChatTypeOneToOne:
logger.Debug("sending private message")
message.SetMessageType(protobuf.MessageType_ONE_TO_ONE)
encodedMessage, err = proto.Marshal(message)
encodedMessage, err = proto.Marshal(message.GetProtobuf())
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)
encodedMessage, err = proto.Marshal(message.GetProtobuf())
if err != nil {
return nil, err
}