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
This commit is contained in:
Sale Djenic 2023-04-21 12:36:08 +02:00 committed by saledjenic
parent e4f23752a9
commit 1ae956ad3c
2 changed files with 22 additions and 20 deletions

View File

@ -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 {

View File

@ -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
}