2022-11-30 09:41:35 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
2022-12-12 10:20:32 +00:00
|
|
|
"database/sql"
|
2022-11-30 09:41:35 +00:00
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/images"
|
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
|
|
|
"github.com/status-im/status-go/protocol/wakusync"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
SyncWakuSectionKeyProfile = "profile"
|
|
|
|
SyncWakuSectionKeyContacts = "contacts"
|
|
|
|
SyncWakuSectionKeyCommunities = "communities"
|
|
|
|
SyncWakuSectionKeySettings = "settings"
|
2023-02-27 10:19:18 +00:00
|
|
|
SyncWakuSectionKeyKeycards = "keycards"
|
2022-11-30 09:41:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (m *Messenger) HandleBackup(state *ReceivedMessageState, message protobuf.Backup) []error {
|
|
|
|
var errors []error
|
|
|
|
|
|
|
|
err := m.handleBackedUpProfile(message.Profile, message.Clock)
|
|
|
|
if err != nil {
|
|
|
|
errors = append(errors, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, contact := range message.Contacts {
|
|
|
|
err = m.HandleSyncInstallationContact(state, *contact)
|
|
|
|
if err != nil {
|
|
|
|
errors = append(errors, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, community := range message.Communities {
|
|
|
|
err = m.handleSyncCommunity(state, *community)
|
|
|
|
if err != nil {
|
|
|
|
errors = append(errors, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = m.handleBackedUpSettings(message.Setting)
|
|
|
|
if err != nil {
|
|
|
|
errors = append(errors, err)
|
|
|
|
}
|
|
|
|
|
2023-02-27 10:19:18 +00:00
|
|
|
err = m.handleBackedUpKeycards(message.Keycards)
|
|
|
|
if err != nil {
|
|
|
|
errors = append(errors, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send signal about applied backup progress
|
2022-11-30 09:41:35 +00:00
|
|
|
if m.config.messengerSignalsHandler != nil {
|
|
|
|
response := wakusync.WakuBackedUpDataResponse{}
|
|
|
|
|
|
|
|
response.AddFetchingBackedUpDataDetails(SyncWakuSectionKeyProfile, message.ProfileDetails)
|
|
|
|
response.AddFetchingBackedUpDataDetails(SyncWakuSectionKeyContacts, message.ContactsDetails)
|
|
|
|
response.AddFetchingBackedUpDataDetails(SyncWakuSectionKeyCommunities, message.CommunitiesDetails)
|
|
|
|
response.AddFetchingBackedUpDataDetails(SyncWakuSectionKeySettings, message.SettingsDetails)
|
2023-02-27 10:19:18 +00:00
|
|
|
response.AddFetchingBackedUpDataDetails(SyncWakuSectionKeyKeycards, message.KeycardsDetails)
|
2022-11-30 09:41:35 +00:00
|
|
|
|
|
|
|
m.config.messengerSignalsHandler.SendWakuFetchingBackupProgress(&response)
|
|
|
|
}
|
|
|
|
|
2023-02-27 10:19:18 +00:00
|
|
|
state.Response.BackupHandled = true
|
|
|
|
|
2022-11-30 09:41:35 +00:00
|
|
|
return errors
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) handleBackedUpProfile(message *protobuf.BackedUpProfile, backupTime uint64) error {
|
|
|
|
if message == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-12 10:20:32 +00:00
|
|
|
contentSet := false
|
2022-11-30 09:41:35 +00:00
|
|
|
response := wakusync.WakuBackedUpDataResponse{
|
|
|
|
Profile: &wakusync.BackedUpProfile{},
|
|
|
|
}
|
|
|
|
|
2023-03-30 10:18:13 +00:00
|
|
|
err := m.SaveSyncDisplayName(message.DisplayName, message.DisplayNameClock)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-11-30 09:41:35 +00:00
|
|
|
}
|
|
|
|
|
2023-03-30 10:18:13 +00:00
|
|
|
contentSet = true
|
|
|
|
response.AddDisplayName(message.DisplayName)
|
|
|
|
|
2022-11-30 09:41:35 +00:00
|
|
|
syncWithBackedUpImages := false
|
|
|
|
dbImages, err := m.multiAccounts.GetIdentityImages(message.KeyUid)
|
|
|
|
if err != nil {
|
2022-12-12 10:20:32 +00:00
|
|
|
if err != sql.ErrNoRows {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// if images are deleted and no images were backed up, then we need to delete them on other devices,
|
|
|
|
// that's why we don't return in case of `sql.ErrNoRows`
|
2022-11-30 09:41:35 +00:00
|
|
|
syncWithBackedUpImages = true
|
|
|
|
}
|
|
|
|
if len(dbImages) == 0 {
|
|
|
|
if len(message.Pictures) > 0 {
|
|
|
|
syncWithBackedUpImages = true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// since both images (large and thumbnail) are always stored in the same time, we're free to use either of those two clocks for comparison
|
|
|
|
lastImageStoredClock := dbImages[0].Clock
|
|
|
|
syncWithBackedUpImages = lastImageStoredClock < backupTime
|
|
|
|
}
|
|
|
|
|
|
|
|
if syncWithBackedUpImages {
|
|
|
|
if len(message.Pictures) == 0 {
|
|
|
|
err = m.multiAccounts.DeleteIdentityImage(message.KeyUid)
|
2022-12-12 10:20:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
contentSet = true
|
|
|
|
response.AddImages(nil)
|
2022-11-30 09:41:35 +00:00
|
|
|
} else {
|
|
|
|
idImages := make([]images.IdentityImage, len(message.Pictures))
|
|
|
|
for i, pic := range message.Pictures {
|
|
|
|
img := images.IdentityImage{
|
|
|
|
Name: pic.Name,
|
|
|
|
Payload: pic.Payload,
|
|
|
|
Width: int(pic.Width),
|
|
|
|
Height: int(pic.Height),
|
|
|
|
FileSize: int(pic.FileSize),
|
|
|
|
ResizeTarget: int(pic.ResizeTarget),
|
|
|
|
Clock: pic.Clock,
|
|
|
|
}
|
|
|
|
idImages[i] = img
|
|
|
|
}
|
|
|
|
err = m.multiAccounts.StoreIdentityImages(message.KeyUid, idImages, false)
|
2022-12-12 10:20:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
contentSet = true
|
|
|
|
response.AddImages(idImages)
|
2022-11-30 09:41:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-12 10:20:32 +00:00
|
|
|
if m.config.messengerSignalsHandler != nil && contentSet {
|
2022-11-30 09:41:35 +00:00
|
|
|
m.config.messengerSignalsHandler.SendWakuBackedUpProfile(&response)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) handleBackedUpSettings(message *protobuf.SyncSetting) error {
|
|
|
|
if message == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-30 10:18:13 +00:00
|
|
|
// DisplayName is recovered via `protobuf.BackedUpProfile` message
|
|
|
|
if message.GetType() == protobuf.SyncSetting_DISPLAY_NAME {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-14 16:49:57 +00:00
|
|
|
settingField, err := m.extractAndSaveSyncSetting(message)
|
2022-11-30 09:41:35 +00:00
|
|
|
if err != nil {
|
|
|
|
m.logger.Warn("failed to handle SyncSetting from backed up message", zap.Error(err))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-27 10:19:18 +00:00
|
|
|
if settingField != nil && m.config.messengerSignalsHandler != nil {
|
2022-12-12 10:20:32 +00:00
|
|
|
response := wakusync.WakuBackedUpDataResponse{
|
|
|
|
Setting: settingField,
|
|
|
|
}
|
2022-11-30 09:41:35 +00:00
|
|
|
|
2023-02-27 10:19:18 +00:00
|
|
|
m.config.messengerSignalsHandler.SendWakuBackedUpSettings(&response)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) handleBackedUpKeycards(message *protobuf.SyncAllKeycards) error {
|
|
|
|
if message == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
allKeycards, err := m.syncReceivedKeycards(*message)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.config.messengerSignalsHandler != nil {
|
|
|
|
response := wakusync.WakuBackedUpDataResponse{
|
|
|
|
Keycards: allKeycards,
|
2022-12-12 10:20:32 +00:00
|
|
|
}
|
2023-02-27 10:19:18 +00:00
|
|
|
|
|
|
|
m.config.messengerSignalsHandler.SendWakuBackedUpSettings(&response)
|
2022-11-30 09:41:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|