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))
.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());
inserts.add(accountRecord);

View File

@ -43,6 +43,7 @@ import org.whispersystems.signalservice.internal.storage.protos.ManifestRecord;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
@ -145,7 +146,7 @@ public class StorageSyncJob extends BaseJob {
if (remoteManifest.isPresent() && remoteManifestVersion > localManifestVersion) {
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);
if (!keyDifference.isEmpty()) {
@ -198,12 +199,14 @@ public class StorageSyncJob extends BaseJob {
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> pendingInsertions = recipientDatabase.getPendingRecipientSyncInsertions();
List<RecipientSettings> pendingDeletions = recipientDatabase.getPendingRecipientSyncDeletions();
Optional<SignalAccountRecord> pendingAccountInsert = StorageSyncHelper.getPendingAccountSyncInsert(context);
Optional<SignalAccountRecord> pendingAccountUpdate = StorageSyncHelper.getPendingAccountSyncUpdate(context);
Optional<SignalAccountRecord> pendingAccountInsert = StorageSyncHelper.getPendingAccountSyncInsert(context, self);
Optional<SignalAccountRecord> pendingAccountUpdate = StorageSyncHelper.getPendingAccountSyncUpdate(context, self);
Set<RecipientId> archivedRecipients = DatabaseFactory.getThreadDatabase(context).getArchivedRecipients();
Optional<LocalWriteResult> localWriteResult = StorageSyncHelper.buildStorageUpdatesForLocal(localManifestVersion,
allLocalStorageKeys,
@ -254,15 +257,14 @@ public class StorageSyncJob extends BaseJob {
return needsMultiDeviceSync;
}
private static @NonNull List<StorageId> getAllLocalStorageIds(@NonNull Context context) {
Recipient self = Recipient.self().fresh();
private static @NonNull List<StorageId> getAllLocalStorageIds(@NonNull Context context, @NonNull Recipient self) {
return Util.concatenatedList(DatabaseFactory.getRecipientDatabase(context).getContactStorageSyncIds(),
Collections.singletonList(StorageId.forAccount(self.getStorageServiceId())),
DatabaseFactory.getStorageKeyDatabase(context).getAllKeys());
}
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);
StorageKeyDatabase storageKeyDatabase = DatabaseFactory.getStorageKeyDatabase(context);
@ -281,7 +283,10 @@ public class StorageSyncJob extends BaseJob {
}
break;
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;
default:
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());
}
public static Optional<SignalAccountRecord> getPendingAccountSyncUpdate(@NonNull Context context) {
if (DatabaseFactory.getRecipientDatabase(context).getDirtyState(Recipient.self().getId()) != RecipientDatabase.DirtyState.UPDATE) {
public static Optional<SignalAccountRecord> getPendingAccountSyncUpdate(@NonNull Context context, @NonNull Recipient self) {
if (DatabaseFactory.getRecipientDatabase(context).getDirtyState(self.getId()) != RecipientDatabase.DirtyState.UPDATE) {
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) {
if (DatabaseFactory.getRecipientDatabase(context).getDirtyState(Recipient.self().getId()) != RecipientDatabase.DirtyState.INSERT) {
public static Optional<SignalAccountRecord> getPendingAccountSyncInsert(@NonNull Context context, @NonNull Recipient self) {
if (DatabaseFactory.getRecipientDatabase(context).getDirtyState(self.getId()) != RecipientDatabase.DirtyState.INSERT) {
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) {
Recipient self = Recipient.self().fresh();
SignalAccountRecord account = new SignalAccountRecord.Builder(id != null ? id.getRaw() : self.getStorageServiceId())
public static SignalStorageRecord buildAccountRecord(@NonNull Context context, @NonNull Recipient self) {
SignalAccountRecord account = new SignalAccountRecord.Builder(self.getStorageServiceId())
.setProfileKey(self.getProfileKey())
.setGivenName(self.getProfileName().getGivenName())
.setFamilyName(self.getProfileName().getFamilyName())