-
Notifications
You must be signed in to change notification settings - Fork 244
/
messenger_sync_settings.go
139 lines (117 loc) · 3.85 KB
/
messenger_sync_settings.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
127
128
129
130
131
132
133
134
135
136
137
138
139
package protocol
import (
"context"
"encoding/json"
"go.uber.org/zap"
"github.com/status-im/status-go/multiaccounts/errors"
"github.com/status-im/status-go/multiaccounts/settings"
"github.com/status-im/status-go/protocol/protobuf"
)
// syncSettings syncs all settings that are syncable
func (m *Messenger) syncSettings() error {
logger := m.logger.Named("syncSettings")
s, err := m.settings.GetSettings()
if err != nil {
return err
}
// Do not use the network clock, use the db value
_, chat := m.getLastClockWithRelatedChat()
var errs []error
for _, sf := range settings.SettingFieldRegister {
if sf.CanSync(settings.FromStruct) {
// Pull clock from the db
clock, err := m.settings.GetSettingLastSynced(sf)
if err != nil {
logger.Error("m.settings.GetSettingLastSynced", zap.Error(err), zap.Any("SettingField", sf))
return err
}
// Build protobuf
rm, err := sf.SyncProtobufFactory().FromStruct()(s, clock, chat.ID)
if err != nil {
// Collect errors to give other sync messages a chance to send
logger.Error("SyncProtobufFactory.Struct", zap.Error(err))
errs = append(errs, err)
}
_, err = m.dispatchMessage(context.Background(), *rm)
if err != nil {
logger.Error("dispatchMessage", zap.Error(err))
return err
}
logger.Debug("dispatchMessage success", zap.Any("rm", rm))
}
}
if len(errs) != 0 {
// return just the first error, the others have been logged
return errs[0]
}
return nil
}
// handleSyncSetting parses incoming *protobuf.SyncSetting and stores the setting data if needed
func (m *Messenger) handleSyncSetting(response *MessengerResponse, syncSetting *protobuf.SyncSetting) error {
sf, err := settings.GetFieldFromProtobufType(syncSetting.Type)
if err != nil {
m.logger.Error(
"handleSyncSetting - settings.GetFieldFromProtobufType",
zap.Error(err),
zap.Any("syncSetting", syncSetting),
)
return err
}
spf := sf.SyncProtobufFactory()
if spf == nil {
m.logger.Warn("handleSyncSetting - received protobuf for setting with no SyncProtobufFactory", zap.Any("SettingField", sf))
return nil
}
if spf.Inactive() {
m.logger.Warn("handleSyncSetting - received protobuf for inactive sync setting", zap.Any("SettingField", sf))
return nil
}
value := spf.ExtractValueFromProtobuf()(syncSetting)
err = m.settings.SaveSyncSetting(sf, value, syncSetting.Clock)
if err == errors.ErrNewClockOlderThanCurrent {
m.logger.Info("handleSyncSetting - SaveSyncSetting :", zap.Error(err))
return nil
}
if err != nil {
return err
}
if v, ok := value.([]byte); ok {
value = json.RawMessage(v)
}
response.Settings = append(response.Settings, &settings.SyncSettingField{SettingField: sf, Value: value})
return nil
}
// startSyncSettingsLoop watches the m.settings.SyncQueue and sends a sync message in response to a settings update
func (m *Messenger) startSyncSettingsLoop() {
go func() {
logger := m.logger.Named("SyncSettingsLoop")
for {
select {
case s := <-m.settings.SyncQueue:
if s.CanSync(settings.FromInterface) {
logger.Debug("setting for sync received from settings.SyncQueue")
clock, chat := m.getLastClockWithRelatedChat()
// Only the messenger has access to the clock, so set the settings sync clock here.
err := m.settings.SetSettingLastSynced(s.SettingField, clock)
if err != nil {
logger.Error("m.settings.SetSettingLastSynced", zap.Error(err))
break
}
rm, err := s.SyncProtobufFactory().FromInterface()(s.Value, clock, chat.ID)
if err != nil {
logger.Error("SyncProtobufFactory().FromInterface", zap.Error(err), zap.Any("SyncSettingField", s))
break
}
_, err = m.dispatchMessage(context.Background(), *rm)
if err != nil {
logger.Error("dispatchMessage", zap.Error(err))
break
}
logger.Debug("message dispatched")
}
case <-m.quit:
return
}
}
}()
}