fix(sync): no AC notification(NewInstallationReceived) when targetInstallationID not match

- Add targetInstallationID parameter to SendPairInstallation function
- Update protobuf SyncPairInstallation struct with TargetInstallationId field
- Modify method calls across multiple test files to include new parameter
- Update pairing.proto and pairing.pb.go with new field for local pairing
This commit is contained in:
frank 2024-09-26 12:39:40 +08:00
parent abfa2ebb79
commit 72c8fc5ade
No known key found for this signature in database
GPG Key ID: B56FA1FC264D28FD
23 changed files with 656 additions and 590 deletions

View File

@ -3150,6 +3150,8 @@ type ReceivedMessageState struct {
AllInstallations *installationMap
// List of communities modified
ModifiedInstallations *stringBoolMap
// List of installations targeted to this device modified
ModifiedInstallationsTargetedToThisDevice *stringBoolMap
// Map of existing messages
ExistingMessagesMap map[string]bool
// EmojiReactions is a list of emoji reactions for the current batch
@ -3322,14 +3324,15 @@ func (m *Messenger) buildMessageState() *ReceivedMessageState {
ModifiedContacts: new(stringBoolMap),
AllInstallations: m.allInstallations,
ModifiedInstallations: m.modifiedInstallations,
ExistingMessagesMap: make(map[string]bool),
EmojiReactions: make(map[string]*EmojiReaction),
GroupChatInvitations: make(map[string]*GroupChatInvitation),
Response: &MessengerResponse{},
Timesource: m.getTimesource(),
ResolvePrimaryName: m.ResolvePrimaryName,
AllBookmarks: make(map[string]*browsers.Bookmark),
AllTrustStatus: make(map[string]verification.TrustStatus),
ModifiedInstallationsTargetedToThisDevice: new(stringBoolMap),
ExistingMessagesMap: make(map[string]bool),
EmojiReactions: make(map[string]*EmojiReaction),
GroupChatInvitations: make(map[string]*GroupChatInvitation),
Response: &MessengerResponse{},
Timesource: m.getTimesource(),
ResolvePrimaryName: m.ResolvePrimaryName,
AllBookmarks: make(map[string]*browsers.Bookmark),
AllTrustStatus: make(map[string]verification.TrustStatus),
}
}
@ -3735,6 +3738,7 @@ func (m *Messenger) saveDataAndPrepareResponse(messageState *ReceivedMessageStat
}
}
targeted, _ := messageState.ModifiedInstallationsTargetedToThisDevice.Load(id)
if installation.Enabled {
// Delete AC notif since the installation is now enabled
err = m.deleteNotification(messageState.Response, id)
@ -3742,7 +3746,7 @@ func (m *Messenger) saveDataAndPrepareResponse(messageState *ReceivedMessageStat
m.logger.Error("error deleting notification", zap.Error(err))
return false
}
} else if id != m.installationID {
} else if id != m.installationID && targeted {
// Add activity center notification when we receive a new installation
notification := &ActivityCenterNotification{
ID: types.FromHex(id),

View File

@ -81,7 +81,7 @@ func (s *MessengerDeleteMessageForMeSuite) Pair() {
DeviceType: "alice2",
})
s.Require().NoError(err)
response, err := s.alice2.SendPairInstallation(context.Background(), nil)
response, err := s.alice2.SendPairInstallation(context.Background(), "", nil)
s.Require().NoError(err)
s.Require().NotNil(response)
s.Require().Len(response.Chats(), 1)

View File

@ -1321,6 +1321,9 @@ func (m *Messenger) HandleSyncPairInstallation(state *ReceivedMessageState, mess
// TODO(samyoul) remove storing of an updated reference pointer?
state.AllInstallations.Store(message.InstallationId, installation)
state.ModifiedInstallations.Store(message.InstallationId, true)
if message.TargetInstallationId == m.installationID {
state.ModifiedInstallationsTargetedToThisDevice.Store(message.InstallationId, true)
}
return nil
}

View File

@ -113,7 +113,7 @@ func (s *MessengerProfileDisplayNameHandlerSuite) TestDisplayNameSync() {
}
err = alicesOtherDevice.SetInstallationMetadata(alicesOtherDevice.installationID, im1)
s.Require().NoError(err)
response, err := alicesOtherDevice.SendPairInstallation(context.Background(), nil)
response, err := alicesOtherDevice.SendPairInstallation(context.Background(), "", nil)
s.Require().NoError(err)
s.Require().NotNil(response)
s.Require().Len(response.Chats(), 1)

View File

@ -43,7 +43,7 @@ func (s *MessengerInstallationSuite) TestReceiveInstallation() {
DeviceType: "their-device-type",
})
s.Require().NoError(err)
response, err := theirMessenger.SendPairInstallation(context.Background(), nil)
response, err := theirMessenger.SendPairInstallation(context.Background(), "", nil)
s.Require().NoError(err)
s.Require().NotNil(response)
s.Require().Len(response.Chats(), 1)
@ -242,7 +242,7 @@ func (s *MessengerInstallationSuite) TestSyncInstallation() {
DeviceType: "their-device-type",
})
s.Require().NoError(err)
response, err = theirMessenger.SendPairInstallation(context.Background(), nil)
response, err = theirMessenger.SendPairInstallation(context.Background(), "", nil)
s.Require().NoError(err)
s.Require().NotNil(response)
s.Require().Len(response.Chats(), 1)
@ -366,7 +366,7 @@ func (s *MessengerInstallationSuite) TestSyncInstallationNewMessages() {
DeviceType: "their-device-type",
})
s.Require().NoError(err)
response, err := bob2.SendPairInstallation(context.Background(), nil)
response, err := bob2.SendPairInstallation(context.Background(), "", nil)
s.Require().NoError(err)
s.Require().NotNil(response)
s.Require().Len(response.Chats(), 1)

View File

@ -30,7 +30,7 @@ func (m *Messenger) EnableInstallationAndSync(request *requests.EnableInstallati
response := &MessengerResponse{}
response.AddInstallation(installation)
pairResponse, err := m.SendPairInstallation(context.Background(), nil)
pairResponse, err := m.SendPairInstallation(context.Background(), request.InstallationID, nil)
if err != nil {
return nil, err
}
@ -76,7 +76,7 @@ func (m *Messenger) EnableInstallationAndPair(request *requests.EnableInstallati
i.Enabled = true
}
m.allInstallations.Store(request.InstallationID, i)
response, err := m.SendPairInstallation(context.Background(), nil)
response, err := m.SendPairInstallation(context.Background(), request.GetInstallationId(), nil)
if err != nil {
return nil, err
}
@ -99,7 +99,7 @@ func (m *Messenger) EnableInstallationAndPair(request *requests.EnableInstallati
}
// SendPairInstallation sends a pair installation message
func (m *Messenger) SendPairInstallation(ctx context.Context, rawMessageHandler RawMessageHandler) (*MessengerResponse, error) {
func (m *Messenger) SendPairInstallation(ctx context.Context, targetInstallationID string, rawMessageHandler RawMessageHandler) (*MessengerResponse, error) {
var err error
var response MessengerResponse
@ -115,11 +115,13 @@ func (m *Messenger) SendPairInstallation(ctx context.Context, rawMessageHandler
clock, chat := m.getLastClockWithRelatedChat()
pairMessage := &protobuf.SyncPairInstallation{
Clock: clock,
Name: installation.InstallationMetadata.Name,
InstallationId: installation.ID,
DeviceType: installation.InstallationMetadata.DeviceType,
Version: installation.Version}
Clock: clock,
Name: installation.InstallationMetadata.Name,
InstallationId: installation.ID,
DeviceType: installation.InstallationMetadata.DeviceType,
Version: installation.Version,
TargetInstallationId: targetInstallationID,
}
encodedMessage, err := proto.Marshal(pairMessage)
if err != nil {
return nil, err

View File

@ -58,12 +58,41 @@ func (s *MessengerPairingSuite) TestEnableNonExistingInstallation() {
}
// TestMessengerPairAfterSeedPhrase tests the scenario where alice2 wants to sync with alice1
func (s *MessengerPairingSuite) TestWrongTargetInstallationID() {
alice1 := s.m
alice2, err := newMessengerWithKey(s.shh, s.privateKey, s.logger, nil)
s.Require().NoError(err)
defer TearDownMessenger(&s.Suite, alice2)
wrongTargetInstallationID := uuid.New().String()
mockRequest := requests.NewMockEnableInstallationAndPair(alice1.installationID, func() string {
return wrongTargetInstallationID
})
_, err = alice2.EnableInstallationAndPair(mockRequest)
s.Require().NoError(err)
_, err = WaitOnMessengerResponse(
alice1,
func(r *MessengerResponse) bool {
for _, i := range r.Installations() {
// We expect the installation to be added but no activity center notification
if i.ID == alice2.installationID && len(r.ActivityCenterNotifications()) == 0 {
return true
}
}
return false
},
"no messages",
)
s.Require().NoError(err)
}
// TestMessengerSyncFallback tests the scenario where alice2 wants to sync with alice1
// alice1 generated the connection string for bootstraping alice2
// alice2 failed to connect to alice1 and restored from seed phrase
// alice2 get the installationID1 from alice1 via parsing the connection string
// alice2 should get the display name from alice1 after pairing
func (s *MessengerPairingSuite) TestMessengerPairAfterSeedPhrase() {
func (s *MessengerPairingSuite) TestMessengerSyncFallback() {
alice1 := s.m
alice2, err := newMessengerWithKey(s.shh, s.privateKey, s.logger, nil)
s.Require().NoError(err)
@ -92,7 +121,7 @@ func (s *MessengerPairingSuite) TestMessengerPairAfterSeedPhrase() {
alice1,
func(r *MessengerResponse) bool {
for _, i := range r.Installations() {
if i.ID == installationID2 {
if i.ID == installationID2 && len(r.ActivityCenterNotifications()) == 1 && r.ActivityCenterNotifications()[0].Type == ActivityCenterNotificationTypeNewInstallationReceived {
return true
}
}

View File

@ -40,7 +40,7 @@ func (s *MessengerSyncBookmarkSuite) TestSyncBookmark() {
DeviceType: "their-device-type",
})
s.Require().NoError(err)
response, err := theirMessenger.SendPairInstallation(context.Background(), nil)
response, err := theirMessenger.SendPairInstallation(context.Background(), "", nil)
s.Require().NoError(err)
s.Require().NotNil(response)
s.Require().Len(response.Chats(), 1)

View File

@ -82,7 +82,7 @@ func (s *MessengerSyncChatSuite) Pair() {
DeviceType: "alice2",
})
s.Require().NoError(err)
response, err := s.alice2.SendPairInstallation(context.Background(), nil)
response, err := s.alice2.SendPairInstallation(context.Background(), "", nil)
s.Require().NoError(err)
s.Require().NotNil(response)
s.Require().Len(response.Chats(), 1)

View File

@ -31,7 +31,7 @@ func (s *MessengerSyncClearHistory) pair() *Messenger {
DeviceType: "their-device-type",
})
s.Require().NoError(err)
response, err := theirMessenger.SendPairInstallation(context.Background(), nil)
response, err := theirMessenger.SendPairInstallation(context.Background(), "", nil)
s.Require().NoError(err)
s.Require().NotNil(response)

View File

@ -58,7 +58,7 @@ func (s *MessengerSyncKeycardChangeSuite) SetupTest() {
}
err = s.other.SetInstallationMetadata(s.other.installationID, imOther)
s.Require().NoError(err)
response, err := s.other.SendPairInstallation(context.Background(), nil)
response, err := s.other.SendPairInstallation(context.Background(), "", nil)
s.Require().NoError(err)
s.Require().NotNil(response)

View File

@ -58,7 +58,7 @@ func (s *MessengerSyncKeycardsStateSuite) SetupTest() {
}
err = s.other.SetInstallationMetadata(s.other.installationID, imOther)
s.Require().NoError(err)
response, err := s.other.SendPairInstallation(context.Background(), nil)
response, err := s.other.SendPairInstallation(context.Background(), "", nil)
s.Require().NoError(err)
s.Require().NotNil(response)

View File

@ -35,7 +35,7 @@ func (s *MessengerSyncProfilePictureSuite) TestSyncProfilePicture() {
DeviceType: "their-device-type",
})
s.Require().NoError(err)
response, err := theirMessenger.SendPairInstallation(context.Background(), nil)
response, err := theirMessenger.SendPairInstallation(context.Background(), "", nil)
s.Require().NoError(err)
s.Require().NotNil(response)
s.Require().Len(response.Chats(), 1)

View File

@ -62,7 +62,7 @@ func (s *MessengerSyncSavedAddressesSuite) SetupTest() {
}
err = s.other.SetInstallationMetadata(s.other.installationID, imOther)
s.Require().NoError(err)
response, err := s.other.SendPairInstallation(context.Background(), nil)
response, err := s.other.SendPairInstallation(context.Background(), "", nil)
s.Require().NoError(err)
s.Require().NotNil(response)

View File

@ -43,7 +43,7 @@ func (s *MessengerSyncVerificationRequests) TestSyncVerificationRequests() {
DeviceType: "their-device-type",
})
s.Require().NoError(err)
response, err := theirMessenger.SendPairInstallation(context.Background(), nil)
response, err := theirMessenger.SendPairInstallation(context.Background(), "", nil)
s.Require().NoError(err)
s.Require().NotNil(response)
s.Require().Len(response.Chats(), 1)
@ -107,7 +107,7 @@ func (s *MessengerSyncVerificationRequests) TestSyncTrust() {
DeviceType: "their-device-type",
})
s.Require().NoError(err)
response, err := theirMessenger.SendPairInstallation(context.Background(), nil)
response, err := theirMessenger.SendPairInstallation(context.Background(), "", nil)
s.Require().NoError(err)
s.Require().NotNil(response)
s.Require().Len(response.Chats(), 1)

View File

@ -101,7 +101,7 @@ func (s *MessengerSyncWalletSuite) TestSyncWallets() {
}
err = alicesOtherDevice.SetInstallationMetadata(alicesOtherDevice.installationID, im1)
s.Require().NoError(err)
response, err := alicesOtherDevice.SendPairInstallation(context.Background(), nil)
response, err := alicesOtherDevice.SendPairInstallation(context.Background(), "", nil)
s.Require().NoError(err)
s.Require().NotNil(response)
s.Require().Len(response.Chats(), 1)
@ -321,7 +321,7 @@ func (s *MessengerSyncWalletSuite) TestSyncWalletAccountsReorder() {
}
err = alicesOtherDevice.SetInstallationMetadata(alicesOtherDevice.installationID, im1)
s.Require().NoError(err)
response, err := alicesOtherDevice.SendPairInstallation(context.Background(), nil)
response, err := alicesOtherDevice.SendPairInstallation(context.Background(), "", nil)
s.Require().NoError(err)
s.Require().NotNil(response)
s.Require().Len(response.Chats(), 1)
@ -511,7 +511,7 @@ func (s *MessengerSyncWalletSuite) TestSyncWalletAccountOrderAfterDeletion() {
}
err = alicesOtherDevice.SetInstallationMetadata(alicesOtherDevice.installationID, im1)
s.Require().NoError(err)
response, err := alicesOtherDevice.SendPairInstallation(context.Background(), nil)
response, err := alicesOtherDevice.SendPairInstallation(context.Background(), "", nil)
s.Require().NoError(err)
s.Require().NotNil(response)
s.Require().Len(response.Chats(), 1)

View File

@ -277,7 +277,7 @@ func FindFirstByContentType(messages []*common.Message, contentType protobuf.Cha
func PairDevices(s *suite.Suite, device1, device2 *Messenger) {
// Send pairing data
response, err := device1.SendPairInstallation(context.Background(), nil)
response, err := device1.SendPairInstallation(context.Background(), "", nil)
s.Require().NoError(err)
s.Require().NotNil(response)
s.Len(response.Chats(), 1)

File diff suppressed because it is too large Load Diff

View File

@ -91,8 +91,9 @@ message SyncPairInstallation {
string installation_id = 2;
string device_type = 3;
string name = 4;
// following fields used for local pairing
// used for local pairing
uint32 version = 5;
string target_installation_id = 6;
}
message SyncInstallationContactV2 {

View File

@ -7,7 +7,8 @@ import (
var ErrEnableInstallationAndPairInvalidID = errors.New("enable installation and pair: invalid installation id")
type EnableInstallationAndPair struct {
InstallationID string `json:"installationId"`
InstallationID string `json:"installationId"`
getInstallationID func() string
}
func (j *EnableInstallationAndPair) Validate() error {
@ -17,3 +18,17 @@ func (j *EnableInstallationAndPair) Validate() error {
return nil
}
func (j *EnableInstallationAndPair) GetInstallationId() string {
if j.getInstallationID != nil {
return j.getInstallationID()
}
return j.InstallationID
}
func NewMockEnableInstallationAndPair(installationID string, mockGetInstallationID func() string) *EnableInstallationAndPair {
return &EnableInstallationAndPair{
InstallationID: installationID,
getInstallationID: mockGetInstallationID,
}
}

View File

@ -36,7 +36,7 @@ func (s *SyncRawMessageHandler) CollectInstallationData(rawMessageCollector *Raw
if err != nil {
return err
}
_, err = messenger.SendPairInstallation(context.TODO(), rawMessageCollector.dispatchMessage)
_, err = messenger.SendPairInstallation(context.TODO(), "", rawMessageCollector.dispatchMessage)
return err
}

View File

@ -907,7 +907,7 @@ func (s *SyncDeviceSuite) TestTransferringKeystoreFilesAfterStopUisngKeycard() {
s.Require().NoError(err)
err = clientMessenger.SetInstallationMetadata(settings.InstallationID, im1)
s.Require().NoError(err)
response, err := clientMessenger.SendPairInstallation(context.Background(), nil)
response, err := clientMessenger.SendPairInstallation(context.Background(), "", nil)
s.Require().NoError(err)
s.Require().NotNil(response)
s.Require().Len(response.Chats(), 1)

View File

@ -1057,7 +1057,7 @@ func (api *PublicAPI) VerifiedUntrustworthy(ctx context.Context, request *reques
}
func (api *PublicAPI) SendPairInstallation(ctx context.Context) (*protocol.MessengerResponse, error) {
return api.service.messenger.SendPairInstallation(ctx, nil)
return api.service.messenger.SendPairInstallation(ctx, "", nil)
}
func (api *PublicAPI) SyncDevices(ctx context.Context, name, picture string) error {