dont use a pointer for public methods

This commit is contained in:
Andrea Maria Piana 2020-07-28 15:22:22 +02:00
parent 4574ab4c22
commit 0fb5ed2207
5 changed files with 92 additions and 41 deletions

View File

@ -117,7 +117,7 @@ func (p *MessageProcessor) Stop() {
func (p *MessageProcessor) SendPrivate(
ctx context.Context,
recipient *ecdsa.PublicKey,
rawMessage *RawMessage,
rawMessage RawMessage,
) ([]byte, error) {
p.logger.Debug(
"sending a private message",
@ -136,7 +136,7 @@ func (p *MessageProcessor) SendPrivate(
rawMessage.Sender = p.identity
}
return p.sendPrivate(ctx, recipient, rawMessage)
return p.sendPrivate(ctx, recipient, &rawMessage)
}
// SendGroup takes encoded data, encrypts it and sends through the wire,
@ -144,7 +144,7 @@ func (p *MessageProcessor) SendPrivate(
func (p *MessageProcessor) SendGroup(
ctx context.Context,
recipients []*ecdsa.PublicKey,
rawMessage *RawMessage,
rawMessage RawMessage,
) ([]byte, error) {
p.logger.Debug(
"sending a private group message",
@ -156,7 +156,7 @@ func (p *MessageProcessor) SendGroup(
}
// Calculate messageID first and set on raw message
wrappedMessage, err := p.wrapMessageV1(rawMessage)
wrappedMessage, err := p.wrapMessageV1(&rawMessage)
if err != nil {
return nil, errors.Wrap(err, "failed to wrap message")
}
@ -165,7 +165,7 @@ func (p *MessageProcessor) SendGroup(
// Send to each recipients
for _, recipient := range recipients {
_, err = p.sendPrivate(ctx, recipient, rawMessage)
_, err = p.sendPrivate(ctx, recipient, &rawMessage)
if err != nil {
return nil, errors.Wrap(err, "failed to send message")
}
@ -232,11 +232,11 @@ func (p *MessageProcessor) sendPrivate(
func (p *MessageProcessor) SendPairInstallation(
ctx context.Context,
recipient *ecdsa.PublicKey,
rawMessage *RawMessage,
rawMessage RawMessage,
) ([]byte, error) {
p.logger.Debug("sending private message", zap.String("recipient", types.EncodeHex(crypto.FromECDSAPub(recipient))))
wrappedMessage, err := p.wrapMessageV1(rawMessage)
wrappedMessage, err := p.wrapMessageV1(&rawMessage)
if err != nil {
return nil, errors.Wrap(err, "failed to wrap message")
}
@ -283,14 +283,14 @@ func (p *MessageProcessor) EncodeMembershipUpdate(
func (p *MessageProcessor) SendPublic(
ctx context.Context,
chatName string,
rawMessage *RawMessage,
rawMessage RawMessage,
) ([]byte, error) {
// Set sender
if rawMessage.Sender == nil {
rawMessage.Sender = p.identity
}
wrappedMessage, err := p.wrapMessageV1(rawMessage)
wrappedMessage, err := p.wrapMessageV1(&rawMessage)
if err != nil {
return nil, errors.Wrap(err, "failed to wrap message")
}
@ -306,7 +306,7 @@ func (p *MessageProcessor) SendPublic(
rawMessage.ID = types.EncodeHex(messageID)
// notify before dispatching
p.notifyOnScheduledMessage(rawMessage)
p.notifyOnScheduledMessage(&rawMessage)
hash, err := p.transport.SendPublic(ctx, newMessage, chatName)
if err != nil {

View File

@ -626,7 +626,7 @@ func (m *Messenger) CreateGroupChatWithMembers(ctx context.Context, name string,
}
m.allChats[chat.ID] = &chat
_, err = m.dispatchMessage(ctx, &common.RawMessage{
_, err = m.dispatchMessage(ctx, common.RawMessage{
LocalChatID: chat.ID,
Payload: encodedMessage,
MessageType: protobuf.ApplicationMetadataMessage_MEMBERSHIP_UPDATE_MESSAGE,
@ -692,7 +692,7 @@ func (m *Messenger) RemoveMemberFromGroupChat(ctx context.Context, chatID string
if err != nil {
return nil, err
}
_, err = m.dispatchMessage(ctx, &common.RawMessage{
_, err = m.dispatchMessage(ctx, common.RawMessage{
LocalChatID: chat.ID,
Payload: encodedMessage,
MessageType: protobuf.ApplicationMetadataMessage_MEMBERSHIP_UPDATE_MESSAGE,
@ -755,7 +755,7 @@ func (m *Messenger) AddMembersToGroupChat(ctx context.Context, chatID string, me
if err != nil {
return nil, err
}
_, err = m.dispatchMessage(ctx, &common.RawMessage{
_, err = m.dispatchMessage(ctx, common.RawMessage{
LocalChatID: chat.ID,
Payload: encodedMessage,
MessageType: protobuf.ApplicationMetadataMessage_MEMBERSHIP_UPDATE_MESSAGE,
@ -820,7 +820,7 @@ func (m *Messenger) ChangeGroupChatName(ctx context.Context, chatID string, name
if err != nil {
return nil, err
}
_, err = m.dispatchMessage(ctx, &common.RawMessage{
_, err = m.dispatchMessage(ctx, common.RawMessage{
LocalChatID: chat.ID,
Payload: encodedMessage,
MessageType: protobuf.ApplicationMetadataMessage_MEMBERSHIP_UPDATE_MESSAGE,
@ -886,7 +886,7 @@ func (m *Messenger) AddAdminsToGroupChat(ctx context.Context, chatID string, mem
if err != nil {
return nil, err
}
_, err = m.dispatchMessage(ctx, &common.RawMessage{
_, err = m.dispatchMessage(ctx, common.RawMessage{
LocalChatID: chat.ID,
Payload: encodedMessage,
MessageType: protobuf.ApplicationMetadataMessage_MEMBERSHIP_UPDATE_MESSAGE,
@ -954,7 +954,7 @@ func (m *Messenger) ConfirmJoiningGroup(ctx context.Context, chatID string) (*Me
if err != nil {
return nil, err
}
_, err = m.dispatchMessage(ctx, &common.RawMessage{
_, err = m.dispatchMessage(ctx, common.RawMessage{
LocalChatID: chat.ID,
Payload: encodedMessage,
MessageType: protobuf.ApplicationMetadataMessage_MEMBERSHIP_UPDATE_MESSAGE,
@ -1022,7 +1022,7 @@ func (m *Messenger) LeaveGroupChat(ctx context.Context, chatID string, remove bo
if err != nil {
return nil, err
}
_, err = m.dispatchMessage(ctx, &common.RawMessage{
_, err = m.dispatchMessage(ctx, common.RawMessage{
LocalChatID: chat.ID,
Payload: encodedMessage,
MessageType: protobuf.ApplicationMetadataMessage_MEMBERSHIP_UPDATE_MESSAGE,
@ -1229,7 +1229,7 @@ func (m *Messenger) ReSendChatMessage(ctx context.Context, messageID string) err
return errors.New("chat not found")
}
_, err = m.dispatchMessage(ctx, &common.RawMessage{
_, err = m.dispatchMessage(ctx, common.RawMessage{
LocalChatID: chat.ID,
Payload: message.Payload,
MessageType: message.MessageType,
@ -1250,7 +1250,7 @@ func (m *Messenger) hasPairedDevices() bool {
}
// sendToPairedDevices will check if we have any paired devices and send to them if necessary
func (m *Messenger) sendToPairedDevices(ctx context.Context, spec *common.RawMessage) error {
func (m *Messenger) sendToPairedDevices(ctx context.Context, spec common.RawMessage) error {
hasPairedDevices := m.hasPairedDevices()
// We send a message to any paired device
if hasPairedDevices {
@ -1262,7 +1262,7 @@ func (m *Messenger) sendToPairedDevices(ctx context.Context, spec *common.RawMes
return nil
}
func (m *Messenger) dispatchPairInstallationMessage(ctx context.Context, spec *common.RawMessage) ([]byte, error) {
func (m *Messenger) dispatchPairInstallationMessage(ctx context.Context, spec common.RawMessage) ([]byte, error) {
var err error
var id []byte
@ -1273,7 +1273,7 @@ func (m *Messenger) dispatchPairInstallationMessage(ctx context.Context, spec *c
}
spec.ID = types.EncodeHex(id)
spec.SendCount++
err = m.persistence.SaveRawMessage(spec)
err = m.persistence.SaveRawMessage(&spec)
if err != nil {
return nil, err
}
@ -1281,7 +1281,7 @@ func (m *Messenger) dispatchPairInstallationMessage(ctx context.Context, spec *c
return id, nil
}
func (m *Messenger) dispatchMessage(ctx context.Context, spec *common.RawMessage) ([]byte, error) {
func (m *Messenger) dispatchMessage(ctx context.Context, spec common.RawMessage) ([]byte, error) {
var err error
var id []byte
logger := m.logger.With(zap.String("site", "dispatchMessage"), zap.String("chatID", spec.LocalChatID))
@ -1351,7 +1351,7 @@ func (m *Messenger) dispatchMessage(ctx context.Context, spec *common.RawMessage
}
spec.ID = types.EncodeHex(id)
spec.SendCount++
err = m.persistence.SaveRawMessage(spec)
err = m.persistence.SaveRawMessage(&spec)
if err != nil {
return nil, err
}
@ -1457,7 +1457,7 @@ func (m *Messenger) SendChatMessage(ctx context.Context, message *Message) (*Mes
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,
SendPushNotification: m.featureFlags.PushNotifications && !chat.Public(),
Payload: encodedMessage,
@ -1566,7 +1566,7 @@ func (m *Messenger) sendContactUpdate(ctx context.Context, chatID, ensName, prof
return nil, err
}
_, err = m.dispatchMessage(ctx, &common.RawMessage{
_, err = m.dispatchMessage(ctx, common.RawMessage{
LocalChatID: chatID,
Payload: encodedMessage,
MessageType: protobuf.ApplicationMetadataMessage_CONTACT_UPDATE,
@ -1660,7 +1660,7 @@ func (m *Messenger) SendPairInstallation(ctx context.Context) (*MessengerRespons
return nil, err
}
_, err = m.dispatchPairInstallationMessage(ctx, &common.RawMessage{
_, err = m.dispatchPairInstallationMessage(ctx, common.RawMessage{
LocalChatID: chatID,
Payload: encodedMessage,
MessageType: protobuf.ApplicationMetadataMessage_PAIR_INSTALLATION,
@ -1707,7 +1707,7 @@ func (m *Messenger) syncPublicChat(ctx context.Context, publicChat *Chat) error
return err
}
_, err = m.dispatchMessage(ctx, &common.RawMessage{
_, err = m.dispatchMessage(ctx, common.RawMessage{
LocalChatID: chatID,
Payload: encodedMessage,
MessageType: protobuf.ApplicationMetadataMessage_SYNC_INSTALLATION_PUBLIC_CHAT,
@ -1750,7 +1750,7 @@ func (m *Messenger) syncContact(ctx context.Context, contact *Contact) error {
return err
}
_, err = m.dispatchMessage(ctx, &common.RawMessage{
_, err = m.dispatchMessage(ctx, common.RawMessage{
LocalChatID: chatID,
Payload: encodedMessage,
MessageType: protobuf.ApplicationMetadataMessage_SYNC_INSTALLATION_CONTACT,
@ -2395,7 +2395,7 @@ func (m *Messenger) RequestTransaction(ctx context.Context, chatID, value, contr
if err != nil {
return nil, err
}
id, err := m.dispatchMessage(ctx, &common.RawMessage{
id, err := m.dispatchMessage(ctx, common.RawMessage{
LocalChatID: chat.ID,
Payload: encodedMessage,
MessageType: protobuf.ApplicationMetadataMessage_REQUEST_TRANSACTION,
@ -2471,7 +2471,7 @@ func (m *Messenger) RequestAddressForTransaction(ctx context.Context, chatID, fr
if err != nil {
return nil, err
}
id, err := m.dispatchMessage(ctx, &common.RawMessage{
id, err := m.dispatchMessage(ctx, common.RawMessage{
LocalChatID: chat.ID,
Payload: encodedMessage,
MessageType: protobuf.ApplicationMetadataMessage_REQUEST_ADDRESS_FOR_TRANSACTION,
@ -2573,7 +2573,7 @@ func (m *Messenger) AcceptRequestAddressForTransaction(ctx context.Context, mess
return nil, err
}
newMessageID, err := m.dispatchMessage(ctx, &common.RawMessage{
newMessageID, err := m.dispatchMessage(ctx, common.RawMessage{
LocalChatID: chat.ID,
Payload: encodedMessage,
MessageType: protobuf.ApplicationMetadataMessage_ACCEPT_REQUEST_ADDRESS_FOR_TRANSACTION,
@ -2656,7 +2656,7 @@ func (m *Messenger) DeclineRequestTransaction(ctx context.Context, messageID str
return nil, err
}
newMessageID, err := m.dispatchMessage(ctx, &common.RawMessage{
newMessageID, err := m.dispatchMessage(ctx, common.RawMessage{
LocalChatID: chat.ID,
Payload: encodedMessage,
MessageType: protobuf.ApplicationMetadataMessage_DECLINE_REQUEST_TRANSACTION,
@ -2738,7 +2738,7 @@ func (m *Messenger) DeclineRequestAddressForTransaction(ctx context.Context, mes
return nil, err
}
newMessageID, err := m.dispatchMessage(ctx, &common.RawMessage{
newMessageID, err := m.dispatchMessage(ctx, common.RawMessage{
LocalChatID: chat.ID,
Payload: encodedMessage,
MessageType: protobuf.ApplicationMetadataMessage_DECLINE_REQUEST_ADDRESS_FOR_TRANSACTION,
@ -2835,7 +2835,7 @@ func (m *Messenger) AcceptRequestTransaction(ctx context.Context, transactionHas
return nil, err
}
newMessageID, err := m.dispatchMessage(ctx, &common.RawMessage{
newMessageID, err := m.dispatchMessage(ctx, common.RawMessage{
LocalChatID: chat.ID,
Payload: encodedMessage,
MessageType: protobuf.ApplicationMetadataMessage_SEND_TRANSACTION,
@ -2912,7 +2912,7 @@ func (m *Messenger) SendTransaction(ctx context.Context, chatID, value, contract
return nil, err
}
newMessageID, err := m.dispatchMessage(ctx, &common.RawMessage{
newMessageID, err := m.dispatchMessage(ctx, common.RawMessage{
LocalChatID: chat.ID,
Payload: encodedMessage,
MessageType: protobuf.ApplicationMetadataMessage_SEND_TRANSACTION,

View File

@ -241,3 +241,54 @@ func (s *MessengerInstallationSuite) TestSyncInstallation() {
s.Require().True(actualContact.IsAdded())
}
func (s *MessengerInstallationSuite) TestSyncInstallationNewMessages() {
bob1 := s.m
// pair
bob2 := s.newMessengerWithKey(s.shh, s.privateKey)
alice := s.newMessenger(s.shh)
err := bob2.SetInstallationMetadata(bob2.installationID, &multidevice.InstallationMetadata{
Name: "their-name",
DeviceType: "their-device-type",
})
s.Require().NoError(err)
response, err := bob2.SendPairInstallation(context.Background())
s.Require().NoError(err)
s.Require().NotNil(response)
s.Require().Len(response.Chats, 1)
s.Require().False(response.Chats[0].Active)
// Wait for the message to reach its destination
response, err = WaitOnMessengerResponse(
bob1,
func(r *MessengerResponse) bool { return len(r.Installations) > 0 },
"installation not received",
)
s.Require().NoError(err)
actualInstallation := response.Installations[0]
s.Require().Equal(bob2.installationID, actualInstallation.ID)
err = bob1.EnableInstallation(bob2.installationID)
s.Require().NoError(err)
// send a message from bob1 to alice, it should be received on both bob1 and bob2
alicePkString := types.EncodeHex(crypto.FromECDSAPub(&alice.identity.PublicKey))
chat := CreateOneToOneChat(alicePkString, &alice.identity.PublicKey, bob1.transport)
s.Require().NoError(bob1.SaveChat(&chat))
inputMessage := buildTestMessage(chat)
_, err = s.m.SendChatMessage(context.Background(), inputMessage)
s.Require().NoError(err)
// Wait for the message to reach its destination
_, err = WaitOnMessengerResponse(
bob2,
func(r *MessengerResponse) bool { return len(r.Messages) > 0 },
"message not received",
)
s.Require().NoError(err)
}

View File

@ -945,7 +945,7 @@ func (c *Client) registerWithServer(registration *protobuf.PushNotificationRegis
if err != nil {
return err
}
rawMessage := &common.RawMessage{
rawMessage := common.RawMessage{
Payload: encryptedRegistration,
MessageType: protobuf.ApplicationMetadataMessage_PUSH_NOTIFICATION_REGISTRATION,
}
@ -1034,7 +1034,7 @@ func (c *Client) sendNotification(publicKey *ecdsa.PublicKey, installationIDs []
return nil, err
}
rawMessage := &common.RawMessage{
rawMessage := common.RawMessage{
Payload: payload,
Sender: ephemeralKey,
// we skip encryption as we don't want to save any key material
@ -1262,7 +1262,7 @@ func (c *Client) queryPushNotificationInfo(publicKey *ecdsa.PublicKey) error {
return err
}
rawMessage := &common.RawMessage{
rawMessage := common.RawMessage{
Payload: encodedMessage,
Sender: ephemeralKey,
// we don't want to wrap in an encryption layer message

View File

@ -91,7 +91,7 @@ func (s *Server) HandlePushNotificationRegistration(publicKey *ecdsa.PublicKey,
return err
}
rawMessage := &common.RawMessage{
rawMessage := common.RawMessage{
Payload: encodedMessage,
MessageType: protobuf.ApplicationMetadataMessage_PUSH_NOTIFICATION_REGISTRATION_RESPONSE,
// we skip encryption as might be sent from an ephemeral key
@ -114,7 +114,7 @@ func (s *Server) HandlePushNotificationQuery(publicKey *ecdsa.PublicKey, message
return err
}
rawMessage := &common.RawMessage{
rawMessage := common.RawMessage{
Payload: encodedMessage,
MessageType: protobuf.ApplicationMetadataMessage_PUSH_NOTIFICATION_QUERY_RESPONSE,
// we skip encryption as sent from an ephemeral key
@ -138,7 +138,7 @@ func (s *Server) HandlePushNotificationRequest(publicKey *ecdsa.PublicKey,
return err
}
rawMessage := &common.RawMessage{
rawMessage := common.RawMessage{
Payload: encodedMessage,
MessageType: protobuf.ApplicationMetadataMessage_PUSH_NOTIFICATION_RESPONSE,
// We skip encryption here as the message has been sent from an ephemeral key