From 7e16f940de1c0a61956fc6857a11f567f0a7b8c3 Mon Sep 17 00:00:00 2001 From: Andrea Maria Piana Date: Thu, 9 Jul 2020 18:52:26 +0200 Subject: [PATCH] Handle query response --- protocol/common/message_processor.go | 4 + protocol/messenger.go | 32 +++- protocol/protobuf/push_notifications.pb.go | 119 ++++++------ protocol/protobuf/push_notifications.proto | 3 +- .../push_notification.go | 126 ++++++++---- .../push_notification_server.go | 17 +- protocol/push_notification_test.go | 179 +++++++++++++----- 7 files changed, 333 insertions(+), 147 deletions(-) diff --git a/protocol/common/message_processor.go b/protocol/common/message_processor.go index 59891f145..eaa6246e8 100644 --- a/protocol/common/message_processor.go +++ b/protocol/common/message_processor.go @@ -455,6 +455,10 @@ func (p *MessageProcessor) Subscribe() <-chan *SentMessage { return c } +func (p *MessageProcessor) JoinPublic(chatID string) error { + return p.transport.JoinPublic(chatID) +} + func MessageSpecToWhisper(spec *encryption.ProtocolMessageSpec) (*types.NewMessage, error) { var newMessage *types.NewMessage diff --git a/protocol/messenger.go b/protocol/messenger.go index 02c03791c..e44fbf928 100644 --- a/protocol/messenger.go +++ b/protocol/messenger.go @@ -1938,11 +1938,37 @@ func (m *Messenger) handleRetrievedMessages(chatWithMessages map[transport.Filte } logger.Debug("Handling PushNotificationQuery") // TODO: Compare DST with Identity - if err := m.pushNotificationServer.HandlePushNotificationQuery2(publicKey, msg.ParsedMessage.(protobuf.PushNotificationQuery)); err != nil { + if err := m.pushNotificationServer.HandlePushNotificationQuery2(publicKey, msg.ID, msg.ParsedMessage.(protobuf.PushNotificationQuery)); err != nil { logger.Warn("failed to handle PushNotificationQuery", zap.Error(err)) } // We continue in any case, no changes to messenger continue + case protobuf.PushNotificationRegistrationResponse: + logger.Debug("Received PushNotificationRegistrationResponse") + if m.pushNotificationClient == nil { + continue + } + logger.Debug("Handling PushNotificationRegistrationResponse") + // TODO: Compare DST with Identity + if err := m.pushNotificationClient.HandlePushNotificationRegistrationResponse(msg.ParsedMessage.(protobuf.PushNotificationRegistrationResponse)); err != nil { + logger.Warn("failed to handle PushNotificationRegistrationResponse", zap.Error(err)) + } + // We continue in any case, no changes to messenger + continue + + case protobuf.PushNotificationQueryResponse: + logger.Debug("Received PushNotificationQueryResponse") + if m.pushNotificationClient == nil { + continue + } + logger.Debug("Handling PushNotificationQueryResponse") + // TODO: Compare DST with Identity + if err := m.pushNotificationClient.HandlePushNotificationQueryResponse(msg.ParsedMessage.(protobuf.PushNotificationQueryResponse)); err != nil { + logger.Warn("failed to handle PushNotificationQueryResponse", zap.Error(err)) + } + // We continue in any case, no changes to messenger + continue + case protobuf.PushNotificationRequest: logger.Debug("Received PushNotificationRequest") if m.pushNotificationServer == nil { @@ -2995,9 +3021,9 @@ func (m *Messenger) AddPushNotificationServer(ctx context.Context, publicKey *ec } // RegisterForPushNotification register deviceToken with any push notification server enabled -func (m *Messenger) RegisterForPushNotifications(ctx context.Context, deviceToken string) error { +func (m *Messenger) RegisterForPushNotifications(ctx context.Context, deviceToken string) ([]string, error) { if m.pushNotificationClient == nil { - return errors.New("push notification client not enabled") + return nil, errors.New("push notification client not enabled") } var contactIDs []*ecdsa.PublicKey diff --git a/protocol/protobuf/push_notifications.pb.go b/protocol/protobuf/push_notifications.pb.go index 5b1784002..12dc55a40 100644 --- a/protocol/protobuf/push_notifications.pb.go +++ b/protocol/protobuf/push_notifications.pb.go @@ -469,7 +469,8 @@ func (m *PushNotificationQueryInfo) GetAllowedUserList() [][]byte { type PushNotificationQueryResponse struct { Info []*PushNotificationQueryInfo `protobuf:"bytes,1,rep,name=info,proto3" json:"info,omitempty"` - Success bool `protobuf:"varint,2,opt,name=success,proto3" json:"success,omitempty"` + MessageId []byte `protobuf:"bytes,2,opt,name=message_id,json=messageId,proto3" json:"message_id,omitempty"` + Success bool `protobuf:"varint,3,opt,name=success,proto3" json:"success,omitempty"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` XXX_sizecache int32 `json:"-"` @@ -507,6 +508,13 @@ func (m *PushNotificationQueryResponse) GetInfo() []*PushNotificationQueryInfo { return nil } +func (m *PushNotificationQueryResponse) GetMessageId() []byte { + if m != nil { + return m.MessageId + } + return nil +} + func (m *PushNotificationQueryResponse) GetSuccess() bool { if m != nil { return m.Success @@ -771,58 +779,59 @@ func init() { func init() { proto.RegisterFile("push_notifications.proto", fileDescriptor_200acd86044eaa5d) } var fileDescriptor_200acd86044eaa5d = []byte{ - // 847 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xac, 0x94, 0x51, 0x8f, 0xdb, 0x44, - 0x10, 0xc7, 0xb1, 0x93, 0xbb, 0xc4, 0x93, 0x70, 0x97, 0xae, 0xae, 0x57, 0x53, 0x51, 0x48, 0x0d, - 0x12, 0x51, 0x91, 0x22, 0x74, 0x48, 0x50, 0xf1, 0x44, 0xb8, 0xf3, 0x1d, 0xd1, 0x5d, 0xec, 0xb0, - 0xf1, 0x51, 0x21, 0x21, 0x59, 0x8e, 0xbd, 0xd7, 0x58, 0x71, 0x6d, 0xb3, 0xbb, 0x2e, 0xca, 0x03, - 0x12, 0x9f, 0x80, 0x17, 0xde, 0xf8, 0x14, 0xa8, 0xdf, 0x81, 0xef, 0x85, 0xbc, 0xb6, 0x53, 0xc7, - 0x71, 0x73, 0x79, 0xe8, 0x93, 0x3d, 0xb3, 0x33, 0xbb, 0x3b, 0xbf, 0xd9, 0xf9, 0x83, 0x1a, 0x27, - 0x6c, 0x61, 0x87, 0x11, 0xf7, 0xef, 0x7c, 0xd7, 0xe1, 0x7e, 0x14, 0xb2, 0x61, 0x4c, 0x23, 0x1e, - 0xa1, 0xb6, 0xf8, 0xcc, 0x93, 0x3b, 0xed, 0xbf, 0x06, 0x7c, 0x3c, 0x4d, 0xd8, 0xc2, 0x28, 0x45, - 0x61, 0xf2, 0xd2, 0x67, 0x9c, 0x8a, 0x7f, 0x64, 0x02, 0xf0, 0x68, 0x49, 0x42,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8a, 0x30, - 0xe6, 0xbc, 0x24, 0x42, 0x34, 0xbb, 0xb8, 0x30, 0xb5, 0xbf, 0x25, 0x78, 0xb4, 0xad, 0x00, 0x62, - 0x8c, 0xd1, 0x37, 0xd0, 0xce, 0x27, 0x9a, 0xe5, 0x94, 0x1e, 0xef, 0x90, 0x8d, 0x75, 0x6c, 0x7a, - 0xeb, 0x7c, 0xfb, 0xb7, 0x15, 0x29, 0xb9, 0xa7, 0x10, 0xf9, 0xa5, 0x9d, 0x86, 0xfb, 0x94, 0x14, - 0xd3, 0xd2, 0x71, 0xdc, 0x25, 0xce, 0x5d, 0xda, 0x3f, 0x32, 0x9c, 0x6e, 0xdf, 0x2a, 0x8e, 0x28, - 0xdf, 0x21, 0x71, 0xdf, 0x6f, 0x4a, 0xdc, 0xb3, 0x5d, 0x12, 0x97, 0x6e, 0x55, 0x2b, 0x6a, 0xef, - 0x03, 0xb7, 0xf6, 0xeb, 0x3e, 0xe2, 0x77, 0x0c, 0x9d, 0x17, 0xd8, 0x34, 0xae, 0xca, 0xca, 0x5f, - 0x11, 0x31, 0x39, 0xf5, 0x19, 0xa6, 0x65, 0x63, 0xfd, 0x6a, 0x3c, 0xb3, 0x74, 0xac, 0x5f, 0xf4, - 0x1a, 0x5a, 0x02, 0xea, 0x76, 0x41, 0xf9, 0xa3, 0xde, 0x44, 0x2f, 0x55, 0xd1, 0x7f, 0x07, 0x2d, - 0x2a, 0x6a, 0x4f, 0x9f, 0x6e, 0xda, 0xd0, 0xfe, 0x7d, 0x90, 0x70, 0x91, 0x30, 0x3f, 0x14, 0x91, - 0x5f, 0xff, 0x1f, 0x00, 0x00, 0xff, 0xff, 0xb5, 0x18, 0xec, 0xa3, 0x79, 0x08, 0x00, 0x00, + // 857 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xac, 0x95, 0x51, 0x6f, 0xe3, 0x44, + 0x10, 0xc7, 0x59, 0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 0x4d, 0xe7, 0x24, 0x93, + 0xf4, 0xbf, 0x15, 0x38, 0xdd, 0xbe, 0xd5, 0x32, 0x62, 0x62, 0xc7, 0x08, 0xfc, 0x76, 0x73, 0x04, + 0x3e, 0xdb, 0x35, 0x02, 0x93, 0xad, 0x2a, 0x87, 0xde, 0xbb, 0xc0, 0xad, 0xff, 0xbc, 0xcf, 0x70, + 0x3c, 0x86, 0xd6, 0x0b, 0x62, 0x5b, 0x57, 0xc5, 0x7f, 0x86, 0xd2, 0x90, 0x53, 0x12, 0xcd, 0xb2, + 0x1d, 0x97, 0x18, 0x57, 0xe6, 0xd8, 0x31, 0x88, 0x71, 0xd1, 0xa9, 0xe9, 0x31, 0x68, 0xdb, 0x09, + 0x65, 0x6f, 0x7e, 0x13, 0x3d, 0x2a, 0xa3, 0xff, 0x06, 0x1a, 0x4c, 0xe6, 0xce, 0x35, 0x45, 0x16, + 0xb4, 0x7b, 0x1f, 0x24, 0x92, 0x07, 0x4c, 0x0e, 0xa5, 0xe7, 0x97, 0xff, 0x05, 0x00, 0x00, 0xff, + 0xff, 0x8b, 0x53, 0x27, 0x32, 0x99, 0x08, 0x00, 0x00, } diff --git a/protocol/protobuf/push_notifications.proto b/protocol/protobuf/push_notifications.proto index e241cb062..fbcd74dc2 100644 --- a/protocol/protobuf/push_notifications.proto +++ b/protocol/protobuf/push_notifications.proto @@ -56,7 +56,8 @@ message PushNotificationQueryInfo { message PushNotificationQueryResponse { repeated PushNotificationQueryInfo info = 1; - bool success = 2; + bytes message_id = 2; + bool success = 3; } message PushNotification { diff --git a/protocol/push_notification_client/push_notification.go b/protocol/push_notification_client/push_notification.go index 13484d116..48b115e45 100644 --- a/protocol/push_notification_client/push_notification.go +++ b/protocol/push_notification_client/push_notification.go @@ -4,8 +4,11 @@ import ( "context" "crypto/aes" "crypto/cipher" + + "bytes" "crypto/ecdsa" "crypto/rand" + "encoding/hex" "errors" "io" "time" @@ -75,19 +78,21 @@ type Client struct { //messageProcessor is a message processor used to send and being notified of messages messageProcessor *common.MessageProcessor + //pushNotificationRegistrationResponses is a channel that listens to pushNotificationResponse + pushNotificationRegistrationResponses chan *protobuf.PushNotificationRegistrationResponse //pushNotificationQueryResponses is a channel that listens to pushNotificationResponse - pushNotificationQueryResponses chan *protobuf.PushNotificationQueryResponse } func New(persistence *Persistence, config *Config, processor *common.MessageProcessor) *Client { return &Client{ - quit: make(chan struct{}), - config: config, - pushNotificationQueryResponses: make(chan *protobuf.PushNotificationQueryResponse), - messageProcessor: processor, - persistence: persistence, - reader: rand.Reader} + quit: make(chan struct{}), + config: config, + pushNotificationRegistrationResponses: make(chan *protobuf.PushNotificationRegistrationResponse), + pushNotificationQueryResponses: make(chan *protobuf.PushNotificationQueryResponse), + messageProcessor: processor, + persistence: persistence, + reader: rand.Reader} } func (c *Client) Start() error { @@ -116,11 +121,6 @@ func (c *Client) Stop() error { return nil } -// This likely will return a channel as it's an asynchrous operation -func fetchNotificationInfoFor(publicKey *ecdsa.PublicKey) error { - return nil -} - // Sends an actual push notification, where do we get the chatID? func sendPushNotificationTo(publicKey *ecdsa.PublicKey, chatID string) error { return nil @@ -198,32 +198,32 @@ func (p *Client) buildPushNotificationRegistrationMessage(contactIDs []*ecdsa.Pu return options, nil } -func (c *Client) Register(deviceToken string, contactIDs []*ecdsa.PublicKey, mutedChatIDs []string) error { +func (c *Client) Register(deviceToken string, contactIDs []*ecdsa.PublicKey, mutedChatIDs []string) ([]string, error) { c.DeviceToken = deviceToken servers, err := c.persistence.GetServers() if err != nil { - return err + return nil, err } if len(servers) == 0 { - return errors.New("no servers to register with") + return nil, errors.New("no servers to register with") } registration, err := c.buildPushNotificationRegistrationMessage(contactIDs, mutedChatIDs) if err != nil { - return err + return nil, err } marshaledRegistration, err := proto.Marshal(registration) if err != nil { - return err + return nil, err } for _, server := range servers { encryptedRegistration, err := c.encryptRegistration(server.publicKey, marshaledRegistration) if err != nil { - return err + return nil, err } rawMessage := &common.RawMessage{ Payload: encryptedRegistration, @@ -235,20 +235,27 @@ func (c *Client) Register(deviceToken string, contactIDs []*ecdsa.PublicKey, mut // Send message and wait for reply } + // TODO: this needs to wait for all the registrations, probably best to poll the database for { select { case <-c.quit: - return nil + return nil, nil case <-time.After(5 * time.Second): - return errors.New("no query response received") - case <-c.pushNotificationQueryResponses: - return nil + return nil, errors.New("no registration response received") + case <-c.pushNotificationRegistrationResponses: + return nil, nil } } } // HandlePushNotificationRegistrationResponse should check whether the response was successful or not, retry if necessary otherwise store the result in the database -func (p *Client) HandlePushNotificationRegistrationResponse(response *protobuf.PushNotificationRegistrationResponse) error { +func (c *Client) HandlePushNotificationRegistrationResponse(response protobuf.PushNotificationRegistrationResponse) error { + c.config.Logger.Debug("received push notification registration response", zap.Any("response", response)) + select { + case c.pushNotificationRegistrationResponses <- &response: + default: + return errors.New("could not process push notification registration response") + } return nil } @@ -258,11 +265,11 @@ func (p *Client) HandlePushNotificationAdvertisement(info *protobuf.PushNotifica } // HandlePushNotificationQueryResponse should update the data in the database for a given user -func (c *Client) HandlePushNotificationQueryResponse(response *protobuf.PushNotificationQueryResponse) error { +func (c *Client) HandlePushNotificationQueryResponse(response protobuf.PushNotificationQueryResponse) error { c.config.Logger.Debug("received push notification query response", zap.Any("response", response)) select { - case c.pushNotificationQueryResponses <- response: + case c.pushNotificationQueryResponses <- &response: default: return errors.New("could not process push notification query response") } @@ -293,22 +300,65 @@ func (c *Client) AddPushNotificationServer(publicKey *ecdsa.PublicKey) error { } func (c *Client) RetrievePushNotificationInfo(publicKey *ecdsa.PublicKey) ([]*PushNotificationInfo, error) { - return nil, nil - /* - currentServers, err := c.persistence.GetServers() - if err != nil { - return err - } + hashedPublicKey := common.HashPublicKey(publicKey) + query := &protobuf.PushNotificationQuery{ + PublicKeys: [][]byte{hashedPublicKey}, + } + encodedMessage, err := proto.Marshal(query) + if err != nil { + return nil, err + } - for _, server := range currentServers { - if common.IsPubKeyEqual(server.publicKey, publicKey) { - return errors.New("push notification server already added") + rawMessage := &common.RawMessage{ + Payload: encodedMessage, + MessageType: protobuf.ApplicationMetadataMessage_PUSH_NOTIFICATION_QUERY, + } + + encodedPublicKey := hex.EncodeToString(hashedPublicKey) + c.config.Logger.Debug("sending query") + messageID, err := c.messageProcessor.SendPublic(context.Background(), encodedPublicKey, rawMessage) + + // TODO: this is probably best done by polling the database instead + for { + select { + case <-c.quit: + return nil, nil + case <-time.After(5 * time.Second): + return nil, errors.New("no registration query response received") + case response := <-c.pushNotificationQueryResponses: + if bytes.Compare(response.MessageId, messageID) != 0 { + // Not for us, queue back + c.pushNotificationQueryResponses <- response + // This is not accurate, we should then shrink the timeout + // Also we should handle multiple responses + continue } - } - return c.persistence.UpsertServer(&PushNotificationServer{ - publicKey: publicKey, - })*/ + if len(response.Info) == 0 { + return nil, errors.New("empty response from the server") + } + + var pushNotificationInfo []*PushNotificationInfo + for _, info := range response.Info { + if bytes.Compare(info.PublicKey, hashedPublicKey) != 0 { + continue + } + pushNotificationInfo = append(pushNotificationInfo, &PushNotificationInfo{ + PublicKey: publicKey, + AccessToken: info.AccessToken, + InstallationID: info.InstallationId, + }) + + } + + return pushNotificationInfo, nil + } + } +} + +func (s *Client) listenToPublicKeyQueryTopic(hashedPublicKey []byte) error { + encodedPublicKey := hex.EncodeToString(hashedPublicKey) + return s.messageProcessor.JoinPublic(encodedPublicKey) } func encryptAccessToken(plaintext []byte, key []byte, reader io.Reader) ([]byte, error) { diff --git a/protocol/push_notification_server/push_notification_server.go b/protocol/push_notification_server/push_notification_server.go index 0fdb8cdfb..be238fb13 100644 --- a/protocol/push_notification_server/push_notification_server.go +++ b/protocol/push_notification_server/push_notification_server.go @@ -3,6 +3,7 @@ package push_notification_server import ( "context" "crypto/ecdsa" + "encoding/hex" "errors" "github.com/golang/protobuf/proto" @@ -120,6 +121,8 @@ func (p *Server) ValidateRegistration(publicKey *ecdsa.PublicKey, payload []byte } func (p *Server) HandlePushNotificationQuery(query *protobuf.PushNotificationQuery) *protobuf.PushNotificationQueryResponse { + + p.config.Logger.Debug("handling push notification query") response := &protobuf.PushNotificationQueryResponse{} if query == nil || len(query.PublicKeys) == 0 { return response @@ -240,6 +243,12 @@ func (s *Server) HandlePushNotificationRegistration(publicKey *ecdsa.PublicKey, return response } + if err := s.listenToPublicKeyQueryTopic(common.HashPublicKey(publicKey)); err != nil { + response.Error = protobuf.PushNotificationRegistrationResponse_INTERNAL_ERROR + s.config.Logger.Error("failed to listen to topic", zap.Error(err)) + return response + + } response.Success = true s.config.Logger.Debug("handled push notification registration successfully") @@ -247,6 +256,11 @@ func (s *Server) HandlePushNotificationRegistration(publicKey *ecdsa.PublicKey, return response } +func (s *Server) listenToPublicKeyQueryTopic(hashedPublicKey []byte) error { + encodedPublicKey := hex.EncodeToString(hashedPublicKey) + return s.messageProcessor.JoinPublic(encodedPublicKey) +} + func (p *Server) HandlePushNotificationRegistration2(publicKey *ecdsa.PublicKey, payload []byte) error { response := p.HandlePushNotificationRegistration(publicKey, payload) if response == nil { @@ -266,11 +280,12 @@ func (p *Server) HandlePushNotificationRegistration2(publicKey *ecdsa.PublicKey, return err } -func (p *Server) HandlePushNotificationQuery2(publicKey *ecdsa.PublicKey, query protobuf.PushNotificationQuery) error { +func (p *Server) HandlePushNotificationQuery2(publicKey *ecdsa.PublicKey, messageID []byte, query protobuf.PushNotificationQuery) error { response := p.HandlePushNotificationQuery(&query) if response == nil { return nil } + response.MessageId = messageID encodedMessage, err := proto.Marshal(response) if err != nil { return err diff --git a/protocol/push_notification_test.go b/protocol/push_notification_test.go index bedcb87f5..37854d05f 100644 --- a/protocol/push_notification_test.go +++ b/protocol/push_notification_test.go @@ -15,6 +15,7 @@ import ( gethbridge "github.com/status-im/status-go/eth-node/bridge/geth" "github.com/status-im/status-go/eth-node/crypto" "github.com/status-im/status-go/eth-node/types" + "github.com/status-im/status-go/protocol/push_notification_client" "github.com/status-im/status-go/protocol/push_notification_server" "github.com/status-im/status-go/protocol/tt" "github.com/status-im/status-go/whisper/v6" @@ -24,6 +25,8 @@ func TestMessengerPushNotificationSuite(t *testing.T) { suite.Run(t, new(MessengerPushNotificationSuite)) } +// TODO: to test. Register -> stop server -> re-start -> is it loading the topics? + type MessengerPushNotificationSuite struct { suite.Suite m *Messenger // main instance of Messenger @@ -113,19 +116,26 @@ func (s *MessengerPushNotificationSuite) newPushNotificationServer(shh types.Whi func (s *MessengerPushNotificationSuite) TestReceivePushNotification() { errChan := make(chan error) - deviceToken := "token" + bob1DeviceToken := "token-1" + bob2DeviceToken := "token-2" + var bob1AccessTokens, bob2AccessTokens []string + bob1 := s.m + bob2 := s.newMessengerWithKey(s.shh, s.m.identity) server := s.newPushNotificationServer(s.shh) client2 := s.newMessenger(s.shh) - err := s.m.AddPushNotificationServer(context.Background(), &server.identity.PublicKey) + // Register bob1 + err := bob1.AddPushNotificationServer(context.Background(), &server.identity.PublicKey) s.Require().NoError(err) go func() { - err := s.m.RegisterForPushNotifications(context.Background(), deviceToken) + bob1AccessTokens, err = bob1.RegisterForPushNotifications(context.Background(), bob1DeviceToken) errChan <- err }() + // Receive message, reply + // TODO: find a better way to handle this waiting time.Sleep(500 * time.Millisecond) _, err = server.RetrieveAll() s.Require().NoError(err) @@ -138,58 +148,129 @@ func (s *MessengerPushNotificationSuite) TestReceivePushNotification() { _, err = server.RetrieveAll() s.Require().NoError(err) + // Check reply + // TODO: find a better way to handle this waiting + time.Sleep(500 * time.Millisecond) + _, err = bob1.RetrieveAll() + s.Require().NoError(err) + + time.Sleep(500 * time.Millisecond) + _, err = bob1.RetrieveAll() + s.Require().NoError(err) + + time.Sleep(500 * time.Millisecond) + _, err = bob1.RetrieveAll() + s.Require().NoError(err) + + // Make sure we receive it + err = <-errChan + s.Require().NoError(err) + s.Require().NotNil(bob1AccessTokens) + + // Register bob2 + err = bob2.AddPushNotificationServer(context.Background(), &server.identity.PublicKey) + s.Require().NoError(err) + + go func() { + bob2AccessTokens, err = bob2.RegisterForPushNotifications(context.Background(), bob2DeviceToken) + errChan <- err + }() + + // Receive message, reply + // TODO: find a better way to handle this waiting + time.Sleep(500 * time.Millisecond) + _, err = server.RetrieveAll() + s.Require().NoError(err) + + time.Sleep(500 * time.Millisecond) + _, err = server.RetrieveAll() + s.Require().NoError(err) + + time.Sleep(500 * time.Millisecond) + _, err = server.RetrieveAll() + s.Require().NoError(err) + + // Check reply + // TODO: find a better way to handle this waiting + time.Sleep(500 * time.Millisecond) + _, err = bob2.RetrieveAll() + s.Require().NoError(err) + + time.Sleep(500 * time.Millisecond) + _, err = bob2.RetrieveAll() + s.Require().NoError(err) + + time.Sleep(500 * time.Millisecond) + _, err = bob2.RetrieveAll() + s.Require().NoError(err) + + // Make sure we receive it + err = <-errChan + s.Require().NoError(err) + s.Require().NotNil(bob2AccessTokens) + + var info []*push_notification_client.PushNotificationInfo + go func() { + info, err = client2.pushNotificationClient.RetrievePushNotificationInfo(&bob2.identity.PublicKey) + errChan <- err + }() + + // Receive push notification query + // TODO: find a better way to handle this waiting + time.Sleep(500 * time.Millisecond) + _, err = server.RetrieveAll() + s.Require().NoError(err) + + time.Sleep(500 * time.Millisecond) + _, err = server.RetrieveAll() + s.Require().NoError(err) + + time.Sleep(500 * time.Millisecond) + _, err = server.RetrieveAll() + s.Require().NoError(err) + + // Receive push notification query response + // TODO: find a better way to handle this waiting + time.Sleep(500 * time.Millisecond) + _, err = client2.RetrieveAll() + s.Require().NoError(err) + + time.Sleep(500 * time.Millisecond) + _, err = client2.RetrieveAll() + s.Require().NoError(err) + + time.Sleep(500 * time.Millisecond) + _, err = client2.RetrieveAll() + s.Require().NoError(err) + err = <-errChan s.Require().NoError(err) - - info, err := client2.pushNotificationClient.RetrievePushNotificationInfo(&s.m.identity.PublicKey) - s.Require().NoError(err) s.Require().NotNil(info) + // Check we have replies for both bob1 and bob2 + s.Require().Len(info, 2) - /* - s.Require().Len(response.Contacts, 1) - contact := response.Contacts[0] - s.Require().True(contact.IsAdded()) + var bob1Info, bob2Info *push_notification_client.PushNotificationInfo - s.Require().Len(response.Chats, 1) - chat := response.Chats[0] - s.Require().False(chat.Active, "It does not create an active chat") + if info[0].AccessToken == bob1AccessTokens[0] { + bob1Info = info[0] + bob2Info = info[1] + } else { + bob2Info = info[0] + bob1Info = info[1] + } - // Wait for the message to reach its destination - response, err = WaitOnMessengerResponse( - s.m, - func(r *MessengerResponse) bool { return len(r.Contacts) > 0 }, - "contact request not received", - ) - s.Require().NoError(err) + s.Require().NotNil(bob1Info) + s.Require().Equal(bob1Info, &push_notification_client.PushNotificationInfo{ + InstallationID: bob1.installationID, + AccessToken: bob1DeviceToken, + PublicKey: &bob1.identity.PublicKey, + }) - receivedContact := response.Contacts[0] - s.Require().Equal(theirName, receivedContact.Name) - s.Require().Equal(theirPicture, receivedContact.Photo) - s.Require().False(receivedContact.ENSVerified) - s.Require().True(receivedContact.HasBeenAdded()) - s.Require().NotEmpty(receivedContact.LastUpdated) + s.Require().NotNil(bob2Info) + s.Require().Equal(bob2Info, &push_notification_client.PushNotificationInfo{ + InstallationID: bob2.installationID, + AccessToken: bob2DeviceToken, + PublicKey: &bob1.identity.PublicKey, + }) - newPicture := "new-picture" - err = theirMessenger.SendPushNotifications(context.Background(), newName, newPicture) - s.Require().NoError(err) - - // Wait for the message to reach its destination - response, err = WaitOnMessengerResponse( - s.m, - func(r *MessengerResponse) bool { - return len(r.Contacts) > 0 && response.Contacts[0].ID == theirContactID - }, - "contact request not received", - ) - - s.Require().NoError(err) - - receivedContact = response.Contacts[0] - s.Require().Equal(theirContactID, receivedContact.ID) - s.Require().Equal(newName, receivedContact.Name) - s.Require().Equal(newPicture, receivedContact.Photo) - s.Require().False(receivedContact.ENSVerified) - s.Require().True(receivedContact.HasBeenAdded()) - s.Require().NotEmpty(receivedContact.LastUpdated) - */ }