2022-03-23 18:47:00 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-03-28 14:55:05 +00:00
|
|
|
"encoding/json"
|
2022-03-23 18:47:00 +00:00
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/multiaccounts/errors"
|
|
|
|
"github.com/status-im/status-go/multiaccounts/settings"
|
2022-11-30 09:41:35 +00:00
|
|
|
"github.com/status-im/status-go/protocol/common"
|
2022-03-23 18:47:00 +00:00
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
|
|
|
)
|
|
|
|
|
|
|
|
// syncSettings syncs all settings that are syncable
|
2022-11-30 09:41:35 +00:00
|
|
|
func (m *Messenger) prepareSyncSettingsMessages(currentClock uint64) (resultRaw []*common.RawMessage, resultSync []*protobuf.SyncSetting, errors []error) {
|
2022-03-23 18:47:00 +00:00
|
|
|
s, err := m.settings.GetSettings()
|
|
|
|
if err != nil {
|
2022-11-30 09:41:35 +00:00
|
|
|
errors = append(errors, err)
|
|
|
|
return
|
2022-03-23 18:47:00 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 09:41:35 +00:00
|
|
|
logger := m.logger.Named("prepareSyncSettings")
|
2022-03-23 18:47:00 +00:00
|
|
|
// Do not use the network clock, use the db value
|
|
|
|
_, chat := m.getLastClockWithRelatedChat()
|
|
|
|
|
|
|
|
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))
|
2022-11-30 09:41:35 +00:00
|
|
|
errors = append(errors, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if clock == 0 {
|
|
|
|
clock = currentClock
|
2022-03-23 18:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build protobuf
|
2022-11-30 09:41:35 +00:00
|
|
|
rm, sm, err := sf.SyncProtobufFactory().FromStruct()(s, clock, chat.ID)
|
2022-03-23 18:47:00 +00:00
|
|
|
if err != nil {
|
|
|
|
// Collect errors to give other sync messages a chance to send
|
|
|
|
logger.Error("SyncProtobufFactory.Struct", zap.Error(err))
|
2022-11-30 09:41:35 +00:00
|
|
|
errors = append(errors, err)
|
2022-03-23 18:47:00 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 09:41:35 +00:00
|
|
|
resultRaw = append(resultRaw, rm)
|
|
|
|
resultSync = append(resultSync, sm)
|
2022-03-23 18:47:00 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-30 09:41:35 +00:00
|
|
|
return
|
|
|
|
}
|
2022-03-23 18:47:00 +00:00
|
|
|
|
2023-01-06 12:21:14 +00:00
|
|
|
func (m *Messenger) syncSettings(rawMessageHandler RawMessageHandler) error {
|
2022-11-30 09:41:35 +00:00
|
|
|
logger := m.logger.Named("syncSettings")
|
|
|
|
|
|
|
|
clock, _ := m.getLastClockWithRelatedChat()
|
|
|
|
rawMessages, _, errors := m.prepareSyncSettingsMessages(clock)
|
|
|
|
|
|
|
|
if len(errors) != 0 {
|
2022-03-23 18:47:00 +00:00
|
|
|
// return just the first error, the others have been logged
|
2022-11-30 09:41:35 +00:00
|
|
|
return errors[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, rm := range rawMessages {
|
2023-01-06 12:21:14 +00:00
|
|
|
_, err := rawMessageHandler(context.Background(), *rm)
|
2022-11-30 09:41:35 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Error("dispatchMessage", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logger.Debug("dispatchMessage success", zap.Any("rm", rm))
|
2022-03-23 18:47:00 +00:00
|
|
|
}
|
2022-11-30 09:41:35 +00:00
|
|
|
|
2022-03-23 18:47:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-30 09:41:35 +00:00
|
|
|
// extractSyncSetting parses incoming *protobuf.SyncSetting and stores the setting data if needed
|
|
|
|
func (m *Messenger) extractSyncSetting(syncSetting *protobuf.SyncSetting) (*settings.SyncSettingField, error) {
|
2022-03-23 18:47:00 +00:00
|
|
|
sf, err := settings.GetFieldFromProtobufType(syncSetting.Type)
|
|
|
|
if err != nil {
|
|
|
|
m.logger.Error(
|
2022-11-30 09:41:35 +00:00
|
|
|
"extractSyncSetting - settings.GetFieldFromProtobufType",
|
2022-03-23 18:47:00 +00:00
|
|
|
zap.Error(err),
|
|
|
|
zap.Any("syncSetting", syncSetting),
|
|
|
|
)
|
2022-11-30 09:41:35 +00:00
|
|
|
return nil, err
|
2022-03-23 18:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
spf := sf.SyncProtobufFactory()
|
|
|
|
if spf == nil {
|
2022-11-30 09:41:35 +00:00
|
|
|
m.logger.Warn("extractSyncSetting - received protobuf for setting with no SyncProtobufFactory", zap.Any("SettingField", sf))
|
|
|
|
return nil, nil
|
2022-03-23 18:47:00 +00:00
|
|
|
}
|
|
|
|
if spf.Inactive() {
|
2022-11-30 09:41:35 +00:00
|
|
|
m.logger.Warn("extractSyncSetting - received protobuf for inactive sync setting", zap.Any("SettingField", sf))
|
|
|
|
return nil, nil
|
2022-03-23 18:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
value := spf.ExtractValueFromProtobuf()(syncSetting)
|
|
|
|
|
|
|
|
err = m.settings.SaveSyncSetting(sf, value, syncSetting.Clock)
|
|
|
|
if err == errors.ErrNewClockOlderThanCurrent {
|
2022-11-30 09:41:35 +00:00
|
|
|
m.logger.Info("extractSyncSetting - SaveSyncSetting :", zap.Error(err))
|
|
|
|
return nil, nil
|
2022-03-23 18:47:00 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
2022-11-30 09:41:35 +00:00
|
|
|
return nil, err
|
2022-03-23 18:47:00 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 14:55:05 +00:00
|
|
|
if v, ok := value.([]byte); ok {
|
|
|
|
value = json.RawMessage(v)
|
|
|
|
}
|
|
|
|
|
2022-11-30 09:41:35 +00:00
|
|
|
return &settings.SyncSettingField{SettingField: sf, Value: value}, nil
|
2022-03-23 18:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2022-11-30 09:41:35 +00:00
|
|
|
rm, _, err := s.SyncProtobufFactory().FromInterface()(s.Value, clock, chat.ID)
|
2022-03-23 18:47:00 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|