Use the same Recipient.self() instance in storage sync.

This commit is contained in:
Greyson Parrelli 2020-04-27 13:05:22 -04:00
parent 7562555687
commit 0ba1f66136
3 changed files with 23 additions and 18 deletions

View File

@ -90,7 +90,7 @@ public class StorageForcePushJob extends BaseJob {
.map(s -> StorageSyncModels.localToRemoteRecord(s, Objects.requireNonNull(newContactStorageIds.get(s.getId())).getRaw(), archivedRecipients)) .map(s -> StorageSyncModels.localToRemoteRecord(s, Objects.requireNonNull(newContactStorageIds.get(s.getId())).getRaw(), archivedRecipients))
.toList(); .toList();
SignalStorageRecord accountRecord = StorageSyncHelper.buildAccountRecord(context, StorageId.forAccount(Recipient.self().fresh().getStorageServiceId())); SignalStorageRecord accountRecord = StorageSyncHelper.buildAccountRecord(context, Recipient.self().fresh());
List<StorageId> allNewStorageIds = new ArrayList<>(newContactStorageIds.values()); List<StorageId> allNewStorageIds = new ArrayList<>(newContactStorageIds.values());
inserts.add(accountRecord); inserts.add(accountRecord);

View File

@ -43,6 +43,7 @@ import org.whispersystems.signalservice.internal.storage.protos.ManifestRecord;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@ -145,7 +146,7 @@ public class StorageSyncJob extends BaseJob {
if (remoteManifest.isPresent() && remoteManifestVersion > localManifestVersion) { if (remoteManifest.isPresent() && remoteManifestVersion > localManifestVersion) {
Log.i(TAG, "[Remote Newer] Newer manifest version found!"); Log.i(TAG, "[Remote Newer] Newer manifest version found!");
List<StorageId> allLocalStorageKeys = getAllLocalStorageIds(context); List<StorageId> allLocalStorageKeys = getAllLocalStorageIds(context, Recipient.self().fresh());
KeyDifferenceResult keyDifference = StorageSyncHelper.findKeyDifference(remoteManifest.get().getStorageIds(), allLocalStorageKeys); KeyDifferenceResult keyDifference = StorageSyncHelper.findKeyDifference(remoteManifest.get().getStorageIds(), allLocalStorageKeys);
if (!keyDifference.isEmpty()) { if (!keyDifference.isEmpty()) {
@ -198,12 +199,14 @@ public class StorageSyncJob extends BaseJob {
localManifestVersion = TextSecurePreferences.getStorageManifestVersion(context); localManifestVersion = TextSecurePreferences.getStorageManifestVersion(context);
List<StorageId> allLocalStorageKeys = getAllLocalStorageIds(context); Recipient self = Recipient.self().fresh();
List<StorageId> allLocalStorageKeys = getAllLocalStorageIds(context, self);
List<RecipientSettings> pendingUpdates = recipientDatabase.getPendingRecipientSyncUpdates(); List<RecipientSettings> pendingUpdates = recipientDatabase.getPendingRecipientSyncUpdates();
List<RecipientSettings> pendingInsertions = recipientDatabase.getPendingRecipientSyncInsertions(); List<RecipientSettings> pendingInsertions = recipientDatabase.getPendingRecipientSyncInsertions();
List<RecipientSettings> pendingDeletions = recipientDatabase.getPendingRecipientSyncDeletions(); List<RecipientSettings> pendingDeletions = recipientDatabase.getPendingRecipientSyncDeletions();
Optional<SignalAccountRecord> pendingAccountInsert = StorageSyncHelper.getPendingAccountSyncInsert(context); Optional<SignalAccountRecord> pendingAccountInsert = StorageSyncHelper.getPendingAccountSyncInsert(context, self);
Optional<SignalAccountRecord> pendingAccountUpdate = StorageSyncHelper.getPendingAccountSyncUpdate(context); Optional<SignalAccountRecord> pendingAccountUpdate = StorageSyncHelper.getPendingAccountSyncUpdate(context, self);
Set<RecipientId> archivedRecipients = DatabaseFactory.getThreadDatabase(context).getArchivedRecipients(); Set<RecipientId> archivedRecipients = DatabaseFactory.getThreadDatabase(context).getArchivedRecipients();
Optional<LocalWriteResult> localWriteResult = StorageSyncHelper.buildStorageUpdatesForLocal(localManifestVersion, Optional<LocalWriteResult> localWriteResult = StorageSyncHelper.buildStorageUpdatesForLocal(localManifestVersion,
allLocalStorageKeys, allLocalStorageKeys,
@ -254,15 +257,14 @@ public class StorageSyncJob extends BaseJob {
return needsMultiDeviceSync; return needsMultiDeviceSync;
} }
private static @NonNull List<StorageId> getAllLocalStorageIds(@NonNull Context context) { private static @NonNull List<StorageId> getAllLocalStorageIds(@NonNull Context context, @NonNull Recipient self) {
Recipient self = Recipient.self().fresh();
return Util.concatenatedList(DatabaseFactory.getRecipientDatabase(context).getContactStorageSyncIds(), return Util.concatenatedList(DatabaseFactory.getRecipientDatabase(context).getContactStorageSyncIds(),
Collections.singletonList(StorageId.forAccount(self.getStorageServiceId())), Collections.singletonList(StorageId.forAccount(self.getStorageServiceId())),
DatabaseFactory.getStorageKeyDatabase(context).getAllKeys()); DatabaseFactory.getStorageKeyDatabase(context).getAllKeys());
} }
private static @NonNull List<SignalStorageRecord> buildLocalStorageRecords(@NonNull Context context, @NonNull List<StorageId> ids, @NonNull Set<RecipientId> archivedRecipients) { private static @NonNull List<SignalStorageRecord> buildLocalStorageRecords(@NonNull Context context, @NonNull List<StorageId> ids, @NonNull Set<RecipientId> archivedRecipients) {
Recipient self = Recipient.self().fresh();
RecipientDatabase recipientDatabase = DatabaseFactory.getRecipientDatabase(context); RecipientDatabase recipientDatabase = DatabaseFactory.getRecipientDatabase(context);
StorageKeyDatabase storageKeyDatabase = DatabaseFactory.getStorageKeyDatabase(context); StorageKeyDatabase storageKeyDatabase = DatabaseFactory.getStorageKeyDatabase(context);
@ -281,7 +283,10 @@ public class StorageSyncJob extends BaseJob {
} }
break; break;
case ManifestRecord.Identifier.Type.ACCOUNT_VALUE: case ManifestRecord.Identifier.Type.ACCOUNT_VALUE:
records.add(StorageSyncHelper.buildAccountRecord(context, id)); if (!Arrays.equals(self.getStorageServiceId(), id.getRaw())) {
throw new AssertionError("Local storage ID doesn't match self!");
}
records.add(StorageSyncHelper.buildAccountRecord(context, self));
break; break;
default: default:
SignalStorageRecord unknown = storageKeyDatabase.getById(id.getRaw()); SignalStorageRecord unknown = storageKeyDatabase.getById(id.getRaw());

View File

@ -340,23 +340,23 @@ public final class StorageSyncHelper {
return !OptionalUtil.byteArrayEquals(update.getOld().getProfileKey(), update.getNew().getProfileKey()); return !OptionalUtil.byteArrayEquals(update.getOld().getProfileKey(), update.getNew().getProfileKey());
} }
public static Optional<SignalAccountRecord> getPendingAccountSyncUpdate(@NonNull Context context) { public static Optional<SignalAccountRecord> getPendingAccountSyncUpdate(@NonNull Context context, @NonNull Recipient self) {
if (DatabaseFactory.getRecipientDatabase(context).getDirtyState(Recipient.self().getId()) != RecipientDatabase.DirtyState.UPDATE) { if (DatabaseFactory.getRecipientDatabase(context).getDirtyState(self.getId()) != RecipientDatabase.DirtyState.UPDATE) {
return Optional.absent(); return Optional.absent();
} }
return Optional.of(buildAccountRecord(context, null).getAccount().get()); return Optional.of(buildAccountRecord(context, self).getAccount().get());
} }
public static Optional<SignalAccountRecord> getPendingAccountSyncInsert(@NonNull Context context) { public static Optional<SignalAccountRecord> getPendingAccountSyncInsert(@NonNull Context context, @NonNull Recipient self) {
if (DatabaseFactory.getRecipientDatabase(context).getDirtyState(Recipient.self().getId()) != RecipientDatabase.DirtyState.INSERT) { if (DatabaseFactory.getRecipientDatabase(context).getDirtyState(self.getId()) != RecipientDatabase.DirtyState.INSERT) {
return Optional.absent(); return Optional.absent();
} }
return Optional.of(buildAccountRecord(context, null).getAccount().get()); return Optional.of(buildAccountRecord(context, self).getAccount().get());
} }
public static SignalStorageRecord buildAccountRecord(@NonNull Context context, @Nullable StorageId id) { public static SignalStorageRecord buildAccountRecord(@NonNull Context context, @NonNull Recipient self) {
Recipient self = Recipient.self().fresh();
SignalAccountRecord account = new SignalAccountRecord.Builder(id != null ? id.getRaw() : self.getStorageServiceId()) SignalAccountRecord account = new SignalAccountRecord.Builder(self.getStorageServiceId())
.setProfileKey(self.getProfileKey()) .setProfileKey(self.getProfileKey())
.setGivenName(self.getProfileName().getGivenName()) .setGivenName(self.getProfileName().getGivenName())
.setFamilyName(self.getProfileName().getFamilyName()) .setFamilyName(self.getProfileName().getFamilyName())