From 1ae956ad3c37a83ce787656d019571d8f3cf073b Mon Sep 17 00:00:00 2001 From: Sale Djenic Date: Fri, 21 Apr 2023 12:36:08 +0200 Subject: [PATCH] feat: accounts improvement - setting clock when adding/updating accounts - syncing/backing up accounts with the time they actually were updated instead the time when dispatching is done as it was before --- protocol/messenger.go | 36 +++++++++++++++++++----------------- protocol/messenger_backup.go | 6 +++--- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/protocol/messenger.go b/protocol/messenger.go index 8c0c947d4..8f5ec4c33 100644 --- a/protocol/messenger.go +++ b/protocol/messenger.go @@ -2480,6 +2480,10 @@ func (m *Messenger) SyncDevices(ctx context.Context, ensName, photoPath string, } func (m *Messenger) SaveAccounts(accs []*accounts.Account) error { + clock, _ := m.getLastClockWithRelatedChat() + for _, acc := range accs { + acc.Clock = clock + } err := m.settings.SaveAccounts(accs) if err != nil { return err @@ -2498,27 +2502,30 @@ func (m *Messenger) DeleteAccount(address types.Address) error { if err != nil { return err } + + clock, chat := m.getLastClockWithRelatedChat() + acc.Clock = clock acc.Removed = true accs := []*accounts.Account{acc} - return m.syncWallets(accs, m.dispatchMessage) + err = m.syncWallets(accs, m.dispatchMessage) + if err != nil { + return err + } + + chat.LastClockValue = clock + return m.saveChat(chat) } -func (m *Messenger) prepareSyncWalletAccountsMessage(accs []*accounts.Account, clock uint64) *protobuf.SyncWalletAccounts { +func (m *Messenger) prepareSyncWalletAccountsMessage(accs []*accounts.Account) *protobuf.SyncWalletAccounts { accountMessages := make([]*protobuf.SyncWalletAccount, 0) for _, acc := range accs { if acc.Chat { continue } - var accountClock uint64 - if acc.Clock == 0 { - accountClock = clock - } else { - accountClock = acc.Clock - } syncMessage := &protobuf.SyncWalletAccount{ - Clock: accountClock, + Clock: acc.Clock, Address: acc.Address.Bytes(), Wallet: acc.Wallet, Chat: acc.Chat, @@ -2554,9 +2561,9 @@ func (m *Messenger) syncWallets(accs []*accounts.Account, rawMessageHandler RawM ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() - clock, chat := m.getLastClockWithRelatedChat() + _, chat := m.getLastClockWithRelatedChat() - message := m.prepareSyncWalletAccountsMessage(accs, clock) + message := m.prepareSyncWalletAccountsMessage(accs) encodedMessage, err := proto.Marshal(message) if err != nil { @@ -2571,12 +2578,7 @@ func (m *Messenger) syncWallets(accs []*accounts.Account, rawMessageHandler RawM } _, err = rawMessageHandler(ctx, rawMessage) - if err != nil { - return err - } - - chat.LastClockValue = clock - return m.saveChat(chat) + return err } func (m *Messenger) syncContactRequestDecision(ctx context.Context, requestID string, accepted bool, rawMessageHandler RawMessageHandler) error { diff --git a/protocol/messenger_backup.go b/protocol/messenger_backup.go index f4ed10740..97633b72c 100644 --- a/protocol/messenger_backup.go +++ b/protocol/messenger_backup.go @@ -95,7 +95,7 @@ func (m *Messenger) BackupData(ctx context.Context) (uint64, error) { return 0, errors[0] } - syncWalletAccounts, err := m.backupWalletAccounts(clock) + syncWalletAccounts, err := m.backupWalletAccounts() if err != nil { return 0, err } @@ -400,11 +400,11 @@ func (m *Messenger) backupProfile(ctx context.Context, clock uint64) ([]*protobu return backupMessages, nil } -func (m *Messenger) backupWalletAccounts(clock uint64) (*protobuf.SyncWalletAccounts, error) { +func (m *Messenger) backupWalletAccounts() (*protobuf.SyncWalletAccounts, error) { accounts, err := m.settings.GetAccounts() if err != nil { return nil, err } - return m.prepareSyncWalletAccountsMessage(accounts, clock), nil + return m.prepareSyncWalletAccountsMessage(accounts), nil }