From eedbcdd564ae2411c729adf590d14296319ec20d Mon Sep 17 00:00:00 2001 From: Greyson Parrelli Date: Thu, 23 Apr 2020 13:33:48 -0400 Subject: [PATCH] Fix issue with group storage sync. --- .../securesms/database/RecipientDatabase.java | 4 +- .../securesms/jobs/StorageSyncJob.java | 2 +- .../securesms/storage/StorageSyncHelper.java | 41 ++++++++++++------- .../securesms/storage/StorageSyncModels.java | 1 + 4 files changed, 32 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/RecipientDatabase.java b/app/src/main/java/org/thoughtcrime/securesms/database/RecipientDatabase.java index 07242fdef..ae96caab7 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/RecipientDatabase.java +++ b/app/src/main/java/org/thoughtcrime/securesms/database/RecipientDatabase.java @@ -1458,10 +1458,12 @@ public class RecipientDatabase extends Database { } void markDirty(@NonNull RecipientId recipientId, @NonNull DirtyState dirtyState) { + Log.d(TAG, "Attempting to mark " + recipientId + " with dirty state " + dirtyState, new Throwable()); + ContentValues contentValues = new ContentValues(1); contentValues.put(DIRTY, dirtyState.getId()); - String query = ID + " = ? AND (" + UUID + " NOT NULL OR " + PHONE + " NOT NULL) AND "; + String query = ID + " = ? AND (" + UUID + " NOT NULL OR " + PHONE + " NOT NULL OR " + GROUP_ID + " NOT NULL) AND "; String[] args = new String[] { recipientId.serialize(), String.valueOf(dirtyState.id) }; switch (dirtyState) { diff --git a/app/src/main/java/org/thoughtcrime/securesms/jobs/StorageSyncJob.java b/app/src/main/java/org/thoughtcrime/securesms/jobs/StorageSyncJob.java index e1b356c96..b67591b00 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/jobs/StorageSyncJob.java +++ b/app/src/main/java/org/thoughtcrime/securesms/jobs/StorageSyncJob.java @@ -275,7 +275,7 @@ public class StorageSyncJob extends BaseJob { case ManifestRecord.Identifier.Type.GROUPV2_VALUE: RecipientSettings settings = recipientDatabase.getByStorageId(id.getRaw()); if (settings != null) { - records.add(StorageSyncModels.localToRemoteRecord(settings, archivedRecipients)); + } else { Log.w(TAG, "Missing local recipient model! Type: " + id.getType()); } diff --git a/app/src/main/java/org/thoughtcrime/securesms/storage/StorageSyncHelper.java b/app/src/main/java/org/thoughtcrime/securesms/storage/StorageSyncHelper.java index 124d980b6..51878f00b 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/storage/StorageSyncHelper.java +++ b/app/src/main/java/org/thoughtcrime/securesms/storage/StorageSyncHelper.java @@ -133,25 +133,38 @@ public final class StorageSyncHelper { } for (RecipientSettings update : updates) { - byte[] oldKey = update.getStorageId(); - byte[] newKey = generateKey(); + StorageId oldId; + StorageId newId; - storageInserts.add(StorageSyncModels.localToRemoteRecord(update, newKey, archivedRecipients)); - storageDeletes.add(ByteBuffer.wrap(oldKey)); - completeIds.remove(StorageId.forContact(oldKey)); - completeIds.add(StorageId.forContact(newKey)); - storageKeyUpdates.put(update.getId(), newKey); + switch (update.getGroupType()) { + case NONE: + oldId = StorageId.forContact(update.getStorageId()); + newId = StorageId.forContact(generateKey()); + break; + case SIGNAL_V1: + oldId = StorageId.forGroupV1(update.getStorageId()); + newId = StorageId.forGroupV1(generateKey()); + break; + default: + throw new AssertionError("Unsupported type!"); + } + + storageInserts.add(StorageSyncModels.localToRemoteRecord(update, newId.getRaw(), archivedRecipients)); + storageDeletes.add(ByteBuffer.wrap(oldId.getRaw())); + completeIds.remove(oldId); + completeIds.add(newId); + storageKeyUpdates.put(update.getId(), newId.getRaw()); } if (accountUpdate.isPresent()) { - byte[] oldKey = accountUpdate.get().getId().getRaw(); - byte[] newKey = generateKey(); + StorageId oldId = accountUpdate.get().getId(); + StorageId newId = StorageId.forAccount(generateKey()); - storageInserts.add(SignalStorageRecord.forAccount(StorageId.forAccount(newKey), accountUpdate.get())); - storageDeletes.add(ByteBuffer.wrap(oldKey)); - completeIds.remove(StorageId.forAccount(oldKey)); - completeIds.add(StorageId.forAccount(newKey)); - storageKeyUpdates.put(Recipient.self().getId(), newKey); + storageInserts.add(SignalStorageRecord.forAccount(newId, accountUpdate.get())); + storageDeletes.add(ByteBuffer.wrap(oldId.getRaw())); + completeIds.remove(oldId); + completeIds.add(newId); + storageKeyUpdates.put(Recipient.self().getId(), newId.getRaw()); } if (storageInserts.isEmpty() && storageDeletes.isEmpty()) { diff --git a/app/src/main/java/org/thoughtcrime/securesms/storage/StorageSyncModels.java b/app/src/main/java/org/thoughtcrime/securesms/storage/StorageSyncModels.java index 4ea3a416a..5faec1345 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/storage/StorageSyncModels.java +++ b/app/src/main/java/org/thoughtcrime/securesms/storage/StorageSyncModels.java @@ -9,6 +9,7 @@ import org.whispersystems.signalservice.api.push.SignalServiceAddress; import org.whispersystems.signalservice.api.storage.SignalContactRecord; import org.whispersystems.signalservice.api.storage.SignalGroupV1Record; import org.whispersystems.signalservice.api.storage.SignalStorageRecord; +import org.whispersystems.signalservice.api.storage.StorageId; import org.whispersystems.signalservice.internal.storage.protos.ContactRecord.IdentityState; import java.util.Set;