Skip to content

Commit

Permalink
update account name when handle settings.preferred_name from backup m…
Browse files Browse the repository at this point in the history
…essage
  • Loading branch information
qfrank committed Sep 15, 2023
1 parent 308066e commit 0e9ccae
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
18 changes: 14 additions & 4 deletions protocol/messenger_backup_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,22 @@ func (m *Messenger) handleBackedUpSettings(message *protobuf.SyncSetting) error
return nil
}

if settingField != nil && m.config.messengerSignalsHandler != nil {
response := wakusync.WakuBackedUpDataResponse{
Setting: settingField,
if settingField != nil {
if message.GetType() == protobuf.SyncSetting_PREFERRED_NAME && message.GetValueString() != "" {
m.account.Name = message.GetValueString()
err = m.multiAccounts.SaveAccount(*m.account)
if err != nil {
m.logger.Warn("[handleBackedUpSettings] failed to save account", zap.Error(err))
return nil
}
}

m.config.messengerSignalsHandler.SendWakuBackedUpSettings(&response)
if m.config.messengerSignalsHandler != nil {
response := wakusync.WakuBackedUpDataResponse{
Setting: settingField,
}
m.config.messengerSignalsHandler.SendWakuBackedUpSettings(&response)
}
}

return nil
Expand Down
13 changes: 13 additions & 0 deletions protocol/messenger_backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ func (s *MessengerBackupSuite) TestBackupSettings() {
bob1Bio = "bio"
bob1Mnemonic = ""
bob1MnemonicRemoved = true
bob1PreferredName = "talent"
)

// Create bob1 and set fields which are supposed to be backed up to/fetched from waku
Expand All @@ -298,6 +299,8 @@ func (s *MessengerBackupSuite) TestBackupSettings() {
s.Require().NoError(err)
err = bob1.settings.SaveSettingField(settings.Mnemonic, bob1Mnemonic)
s.Require().NoError(err)
err = bob1.settings.SaveSettingField(settings.PreferredName, bob1PreferredName)
s.Require().NoError(err)

// Create bob2
bob2, err := newMessengerWithKey(s.shh, bob1.identity, s.logger, nil)
Expand Down Expand Up @@ -331,6 +334,9 @@ func (s *MessengerBackupSuite) TestBackupSettings() {
storedMnemonicRemoved, err := bob1.settings.MnemonicRemoved()
s.Require().NoError(err)
s.Require().Equal(bob1MnemonicRemoved, storedMnemonicRemoved)
storedPreferredName, err := bob1.settings.GetPreferredUsername()
s.NoError(err)
s.Equal(bob1PreferredName, storedPreferredName)

// Check bob2
storedBob2DisplayName, err := bob2.settings.DisplayName()
Expand All @@ -354,6 +360,9 @@ func (s *MessengerBackupSuite) TestBackupSettings() {
storedBob2MnemonicRemoved, err := bob2.settings.MnemonicRemoved()
s.Require().NoError(err)
s.Require().Equal(false, storedBob2MnemonicRemoved)
storedBob2PreferredName, err := bob2.settings.GetPreferredUsername()
s.NoError(err)
s.Equal("", storedBob2PreferredName)

// Backup
clock, err := bob1.BackupData(context.Background())
Expand Down Expand Up @@ -391,6 +400,10 @@ func (s *MessengerBackupSuite) TestBackupSettings() {
storedBob2MnemonicRemoved, err = bob2.settings.MnemonicRemoved()
s.Require().NoError(err)
s.Require().Equal(bob1MnemonicRemoved, storedBob2MnemonicRemoved)
storedBob2PreferredName, err = bob2.settings.GetPreferredUsername()
s.NoError(err)
s.Equal(bob1PreferredName, storedBob2PreferredName)
s.Equal(bob1PreferredName, bob2.account.Name)

lastBackup, err := bob1.lastBackup()
s.Require().NoError(err)
Expand Down
13 changes: 10 additions & 3 deletions protocol/messenger_identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,16 @@ func (m *Messenger) SaveSyncDisplayName(displayName string, clock uint64) error
if err != nil {
return err
}

m.account.Name = displayName
return m.multiAccounts.SaveAccount(*m.account)
preferredNameClock, err := m.settings.GetSettingLastSynced(settings.PreferredName)
if err != nil {
return err
}
// check clock of preferred name to avoid override account name
if preferredNameClock < clock {
m.account.Name = displayName
return m.multiAccounts.SaveAccount(*m.account)
}
return nil
}

func ValidateBio(bio *string) error {
Expand Down

0 comments on commit 0e9ccae

Please sign in to comment.