Adding Transactions messages to the localnotifications (#2142)

* Revert "Revert "Expand Local Notifications to support multiple Notification types (#2100)""

This reverts commit 5887337b88.

* Revert "Revert "fix protocol.MessageNotificationBody marshalling""

This reverts commit cf0a16dff1.

* Bump version to 0.70.0

* Added localnotifications for Transaction messages

* Fixed bug where Message.SigPubKey was presumed to be set

* Added lookup for contact existing in Messenger.allContacts

Additionally added functionality to add a contact to the messenger store if it isn't present

* Get chat directly from Messenger.allChats store

Co-authored-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
This commit is contained in:
Samuel Hawksby-Robinson 2021-02-23 07:37:08 +00:00 committed by GitHub
parent 363ab0a2ab
commit 730f540a0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 92 additions and 0 deletions

View File

@ -3,6 +3,7 @@ package common
import (
"crypto/ecdsa"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
@ -15,6 +16,7 @@ import (
"github.com/status-im/markdown"
"github.com/status-im/markdown/ast"
"github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/images"
"github.com/status-im/status-go/protocol/protobuf"
)
@ -397,3 +399,33 @@ func (m *Message) SetMessageType(messageType protobuf.MessageType) {
func (m *Message) WrapGroupMessage() bool {
return true
}
// GetPublicKey attempts to return or recreate the *ecdsa.PublicKey of the Message sender.
// If the m.SigPubKey is set this will be returned
// If the m.From is present the string is decoded and unmarshalled into a *ecdsa.PublicKey, the m.SigPubKey is set and returned
// Else an error is thrown
// This function differs from GetSigPubKey() as this function may return an error
func (m *Message) GetSenderPubKey() (*ecdsa.PublicKey, error) {
// TODO requires tests
if m.SigPubKey != nil {
return m.SigPubKey, nil
}
if len(m.From) > 0 {
fromB, err := hex.DecodeString(m.From[2:])
if err != nil {
return nil, err
}
senderPubKey, err := crypto.UnmarshalPubkey(fromB)
if err != nil {
return nil, err
}
m.SigPubKey = senderPubKey
return senderPubKey, nil
}
return nil, errors.New("no Message.SigPubKey or Message.From set unable to get public key")
}

View File

@ -2753,6 +2753,31 @@ type ReceivedMessageState struct {
Timesource common.TimeSource
}
// addNewMessageNotification takes a common.Message and generates a new MessageNotificationBody and appends it to the
// []Response.Notifications if the message is m.New
func (r *ReceivedMessageState) addNewMessageNotification(m *common.Message) error {
if !m.New {
return nil
}
pubKey, err := m.GetSenderPubKey()
if err != nil {
return err
}
contactID := contactIDFromPublicKey(pubKey)
r.Response.Notifications = append(
r.Response.Notifications,
MessageNotificationBody{
Message: m,
Contact: r.AllContacts[contactID],
Chat: r.AllChats[m.ChatId],
},
)
return nil
}
func (m *Messenger) handleRetrievedMessages(chatWithMessages map[transport.Filter][]*types.Message) (*MessengerResponse, error) {
m.mutex.Lock()
defer m.mutex.Unlock()
@ -3234,6 +3259,11 @@ func (m *Messenger) handleRetrievedMessages(chatWithMessages map[transport.Filte
for _, message := range messageState.Response.Messages {
if _, ok := newMessagesIds[message.ID]; ok {
message.New = true
// Create notification body to be eventually passed to `localnotifications.SendMessageNotifications()`
if err = messageState.addNewMessageNotification(message); err != nil {
return nil, err
}
}
}
@ -4197,6 +4227,16 @@ func (m *Messenger) ValidateTransactions(ctx context.Context, addresses []types.
m.allChats[chat.ID] = chat
modifiedChats[chat.ID] = true
contact, err := m.getOrBuildContactFromMessage(message)
if err != nil {
return nil, err
}
response.Notifications = append(response.Notifications, MessageNotificationBody{
Message: message,
Contact: contact,
Chat: m.allChats[message.ChatId],
})
}
for id := range modifiedChats {
response.Chats = append(response.Chats, m.allChats[id])
@ -4607,3 +4647,22 @@ func (m *Messenger) encodeChatEntity(chat *Chat, message common.ChatEntity) ([]b
return encodedMessage, nil
}
func (m *Messenger) getOrBuildContactFromMessage(msg *common.Message) (*Contact, error) {
if c, ok := m.allContacts[msg.From]; ok {
return c, nil
}
senderPubKey, err := msg.GetSenderPubKey()
if err != nil {
return nil, err
}
senderID := contactIDFromPublicKey(senderPubKey)
c, err := buildContact(senderID, senderPubKey)
if err != nil {
return nil, err
}
m.allContacts[msg.From] = c
return c, nil
}

View File

@ -331,6 +331,7 @@ func (s *Service) verifyTransactionLoop(tick time.Duration, cancel <-chan struct
}
if !response.IsEmpty() {
PublisherSignalHandler{}.NewMessages(response)
localnotifications.SendMessageNotifications(response.Notifications)
}
case <-cancel:
cancelVerifyTransaction()