Skip to content

Commit

Permalink
Use the same Recipient.self() instance in storage sync.
Browse files Browse the repository at this point in the history
  • Loading branch information
greyson-signal committed Apr 27, 2020
1 parent 7562555 commit 0ba1f66
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ protected void onRun() throws IOException, RetryLaterException {
.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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -145,7 +146,7 @@ private boolean performSync() throws IOException, RetryLaterException, InvalidKe
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()) {
Expand Down Expand Up @@ -198,12 +199,14 @@ private boolean performSync() throws IOException, RetryLaterException, InvalidKe

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,
Expand Down Expand Up @@ -254,15 +257,14 @@ private boolean performSync() throws IOException, RetryLaterException, InvalidKe
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);

Expand All @@ -281,7 +283,10 @@ private boolean performSync() throws IOException, RetryLaterException, InvalidKe
}
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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,23 +340,23 @@ public static boolean profileKeyChanged(RecordUpdate<SignalContactRecord> update
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())
Expand Down

0 comments on commit 0ba1f66

Please sign in to comment.