-
Notifications
You must be signed in to change notification settings - Fork 244
/
messenger_saved_address.go
126 lines (109 loc) · 4.19 KB
/
messenger_saved_address.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package protocol
import (
"context"
"time"
"github.com/golang/protobuf/proto"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/status-im/status-go/protocol/common"
"github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/services/wallet"
)
func (m *Messenger) UpsertSavedAddress(ctx context.Context, sa wallet.SavedAddress) error {
updatedClock, err := m.savedAddressesManager.UpdateMetadataAndUpsertSavedAddress(sa)
if err != nil {
return err
}
return m.syncNewSavedAddress(ctx, &sa, updatedClock, m.dispatchMessage)
}
func (m *Messenger) DeleteSavedAddress(ctx context.Context, address gethcommon.Address, ens string, isTest bool) error {
updateClock := uint64(time.Now().Unix())
_, err := m.savedAddressesManager.DeleteSavedAddress(address, ens, isTest, updateClock)
if err != nil {
return err
}
return m.syncDeletedSavedAddress(ctx, address, ens, isTest, updateClock, m.dispatchMessage)
}
func (m *Messenger) garbageCollectRemovedSavedAddresses() error {
return m.savedAddressesManager.DeleteSoftRemovedSavedAddresses(uint64(time.Now().AddDate(0, 0, -30).Unix()))
}
func (m *Messenger) dispatchSyncSavedAddress(ctx context.Context, syncMessage protobuf.SyncSavedAddress, rawMessageHandler RawMessageHandler) error {
if !m.hasPairedDevices() {
return nil
}
clock, chat := m.getLastClockWithRelatedChat()
encodedMessage, err := proto.Marshal(&syncMessage)
if err != nil {
return err
}
rawMessage := common.RawMessage{
LocalChatID: chat.ID,
Payload: encodedMessage,
MessageType: protobuf.ApplicationMetadataMessage_SYNC_SAVED_ADDRESS,
ResendAutomatically: true,
}
_, err = rawMessageHandler(ctx, rawMessage)
if err != nil {
return err
}
chat.LastClockValue = clock
return m.saveChat(chat)
}
func (m *Messenger) syncNewSavedAddress(ctx context.Context, savedAddress *wallet.SavedAddress, updateClock uint64, rawMessageHandler RawMessageHandler) error {
return m.dispatchSyncSavedAddress(ctx, protobuf.SyncSavedAddress{
Address: savedAddress.Address.Bytes(),
Name: savedAddress.Name,
Favourite: savedAddress.Favourite,
Removed: savedAddress.Removed,
UpdateClock: savedAddress.UpdateClock,
ChainShortNames: savedAddress.ChainShortNames,
Ens: savedAddress.ENSName,
IsTest: savedAddress.IsTest,
}, rawMessageHandler)
}
func (m *Messenger) syncDeletedSavedAddress(ctx context.Context, address gethcommon.Address, ens string, isTest bool, updateClock uint64, rawMessageHandler RawMessageHandler) error {
return m.dispatchSyncSavedAddress(ctx, protobuf.SyncSavedAddress{
Address: address.Bytes(),
UpdateClock: updateClock,
Removed: true,
IsTest: isTest,
Ens: ens,
}, rawMessageHandler)
}
func (m *Messenger) syncSavedAddress(ctx context.Context, savedAddress wallet.SavedAddress, rawMessageHandler RawMessageHandler) (err error) {
if savedAddress.Removed {
if err = m.syncDeletedSavedAddress(ctx, savedAddress.Address, savedAddress.ENSName, savedAddress.IsTest, savedAddress.UpdateClock, rawMessageHandler); err != nil {
return err
}
} else {
if err = m.syncNewSavedAddress(ctx, &savedAddress, savedAddress.UpdateClock, rawMessageHandler); err != nil {
return err
}
}
return
}
func (m *Messenger) handleSyncSavedAddress(state *ReceivedMessageState, syncMessage protobuf.SyncSavedAddress) (err error) {
address := gethcommon.BytesToAddress(syncMessage.Address)
if syncMessage.Removed {
_, err = m.savedAddressesManager.DeleteSavedAddress(
address, syncMessage.Ens, syncMessage.IsTest, syncMessage.UpdateClock)
if err != nil {
return err
}
state.Response.AddSavedAddress(&wallet.SavedAddress{Address: address, ENSName: syncMessage.Ens, IsTest: syncMessage.IsTest})
} else {
sa := wallet.SavedAddress{
Address: address,
Name: syncMessage.Name,
Favourite: syncMessage.Favourite,
ChainShortNames: syncMessage.ChainShortNames,
ENSName: syncMessage.Ens,
IsTest: syncMessage.IsTest,
}
_, err = m.savedAddressesManager.AddSavedAddressIfNewerUpdate(sa, syncMessage.UpdateClock)
if err != nil {
return err
}
state.Response.AddSavedAddress(&sa)
}
return
}