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"
|
|
|
|
|
2024-09-26 22:37:32 +00:00
|
|
|
gocommon "github.com/status-im/status-go/common"
|
2022-03-23 18:47:00 +00:00
|
|
|
"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
|
2023-03-30 10:18:13 +00:00
|
|
|
func (m *Messenger) prepareSyncSettingsMessages(currentClock uint64, prepareForBackup bool) (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) {
|
2023-03-30 10:18:13 +00:00
|
|
|
// DisplayName is backed up via `protobuf.BackedUpProfile` message.
|
|
|
|
if prepareForBackup && sf.SyncProtobufFactory().SyncSettingProtobufType() == protobuf.SyncSetting_DISPLAY_NAME {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-03-23 18:47:00 +00:00
|
|
|
// 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()
|
2023-03-30 10:18:13 +00:00
|
|
|
rawMessages, _, errors := m.prepareSyncSettingsMessages(clock, false)
|
2022-11-30 09:41:35 +00:00
|
|
|
|
|
|
|
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
|
2023-02-14 16:49:57 +00:00
|
|
|
func (m *Messenger) extractAndSaveSyncSetting(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() {
|
2024-09-26 22:37:32 +00:00
|
|
|
defer gocommon.LogOnPanic()
|
2022-03-23 18:47:00 +00:00
|
|
|
logger := m.logger.Named("SyncSettingsLoop")
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
2023-12-21 18:05:30 +00:00
|
|
|
case s := <-m.settings.GetSyncQueue():
|
2022-03-23 18:47:00 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2023-10-24 10:15:32 +00:00
|
|
|
|
|
|
|
func (m *Messenger) startSettingsChangesLoop() {
|
|
|
|
channel := m.settings.SubscribeToChanges()
|
|
|
|
go func() {
|
2024-09-26 22:37:32 +00:00
|
|
|
defer gocommon.LogOnPanic()
|
2023-10-24 10:15:32 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case s := <-channel:
|
|
|
|
switch s.GetReactName() {
|
|
|
|
case settings.DisplayName.GetReactName():
|
|
|
|
m.selfContact.DisplayName = s.Value.(string)
|
2023-11-13 20:07:35 +00:00
|
|
|
m.publishSelfContactSubscriptions(&SelfContactChangeEvent{DisplayNameChanged: true})
|
2023-10-24 10:15:32 +00:00
|
|
|
case settings.PreferredName.GetReactName():
|
|
|
|
m.selfContact.EnsName = s.Value.(string)
|
2023-11-13 20:07:35 +00:00
|
|
|
m.publishSelfContactSubscriptions(&SelfContactChangeEvent{PreferredNameChanged: true})
|
2023-10-24 10:15:32 +00:00
|
|
|
case settings.Bio.GetReactName():
|
|
|
|
m.selfContact.Bio = s.Value.(string)
|
2023-11-13 20:07:35 +00:00
|
|
|
m.publishSelfContactSubscriptions(&SelfContactChangeEvent{BioChanged: true})
|
2023-10-24 10:15:32 +00:00
|
|
|
}
|
|
|
|
case <-m.quit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|