forked from Silverfish/proton-bridge
fix(GODT-2508): Handle Address Updated for none-existing address
If we receive an address update and the address does not exist, attempt to create it.
This commit is contained in:
@ -329,6 +329,24 @@ func TestBridge_User_AddressEvents_NoBadEvent(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestBridge_User_AddressEventUpdatedForAddressThatDoesNotExist_NoBadEvent(t *testing.T) {
|
||||
withEnv(t, func(ctx context.Context, s *server.Server, netCtl *proton.NetCtl, locator bridge.Locator, storeKey []byte) {
|
||||
// Create a user.
|
||||
userID, _, err := s.CreateUser("user", password)
|
||||
require.NoError(t, err)
|
||||
|
||||
withBridge(ctx, t, s.GetHostURL(), netCtl, locator, storeKey, func(bridge *bridge.Bridge, _ *bridge.Mocks) {
|
||||
userLoginAndSync(ctx, t, bridge, "user", password)
|
||||
})
|
||||
|
||||
withBridge(ctx, t, s.GetHostURL(), netCtl, locator, storeKey, func(bridge *bridge.Bridge, _ *bridge.Mocks) {
|
||||
_, err := s.CreateAddressAsUpdate(userID, "another@pm.me", password)
|
||||
require.NoError(t, err)
|
||||
userContinueEventProcess(ctx, t, s, bridge)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func TestBridge_User_Network_NoBadEvents(t *testing.T) {
|
||||
withEnv(t, func(ctx context.Context, s *server.Server, netCtl *proton.NetCtl, locator bridge.Locator, storeKey []byte) {
|
||||
retVal := int32(0)
|
||||
|
||||
@ -170,6 +170,16 @@ func (user *User) handleAddressEvents(ctx context.Context, addressEvents []proto
|
||||
|
||||
case proton.EventUpdate, proton.EventUpdateFlags:
|
||||
if err := user.handleUpdateAddressEvent(ctx, event); err != nil {
|
||||
if errors.Is(err, ErrAddressDoesNotExist) {
|
||||
logrus.Debugf("Address %v does not exist, will try create instead", event.Address.ID)
|
||||
if createErr := user.handleCreateAddressEvent(ctx, event); createErr != nil {
|
||||
user.reportError("Failed to apply address update event (with create)", createErr)
|
||||
return fmt.Errorf("failed to handle update address event (with create): %w", createErr)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
user.reportError("Failed to apply address update event", err)
|
||||
return fmt.Errorf("failed to handle update address event: %w", err)
|
||||
}
|
||||
@ -245,6 +255,8 @@ func (user *User) handleCreateAddressEvent(ctx context.Context, event proton.Add
|
||||
}, user.apiAddrsLock, user.apiLabelsLock, user.updateChLock)
|
||||
}
|
||||
|
||||
var ErrAddressDoesNotExist = errors.New("address does not exist")
|
||||
|
||||
func (user *User) handleUpdateAddressEvent(_ context.Context, event proton.AddressEvent) error { //nolint:unparam
|
||||
return safe.LockRet(func() error {
|
||||
user.log.WithFields(logrus.Fields{
|
||||
@ -254,8 +266,7 @@ func (user *User) handleUpdateAddressEvent(_ context.Context, event proton.Addre
|
||||
|
||||
oldAddr, ok := user.apiAddrs[event.Address.ID]
|
||||
if !ok {
|
||||
user.log.Debugf("Address %q does not exist", event.Address.ID)
|
||||
return nil
|
||||
return ErrAddressDoesNotExist
|
||||
}
|
||||
|
||||
user.apiAddrs[event.Address.ID] = event.Address
|
||||
|
||||
Reference in New Issue
Block a user