From 1fb3290038d92b3c0e5af8d8b5b7e97de2e44349 Mon Sep 17 00:00:00 2001 From: Greyson Parrelli Date: Tue, 11 May 2021 10:05:13 -0400 Subject: [PATCH] Be more direct with AccountRecord updates. --- .../securesms/database/RecipientDatabase.java | 19 ++++++------ .../securesms/jobs/StorageSyncJob.java | 2 ++ .../storage/AccountRecordProcessor.java | 2 +- .../securesms/storage/StorageSyncHelper.java | 29 +++++++++++-------- 4 files changed, 29 insertions(+), 23 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 e430da9eb..7b6dd563c 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/RecipientDatabase.java +++ b/app/src/main/java/org/thoughtcrime/securesms/database/RecipientDatabase.java @@ -928,13 +928,13 @@ public class RecipientDatabase extends Database { recipient.live().refresh(); } - public void applyStorageSyncAccountUpdate(@NonNull StorageId storageId, SignalAccountRecord update) { + public void applyStorageSyncAccountUpdate(@NonNull StorageRecordUpdate update) { SQLiteDatabase db = databaseHelper.getWritableDatabase(); ContentValues values = new ContentValues(); - ProfileName profileName = ProfileName.fromParts(update.getGivenName().orNull(), update.getFamilyName().orNull()); - Optional localKey = ProfileKeyUtil.profileKeyOptional(Recipient.self().getProfileKey()); - Optional remoteKey = ProfileKeyUtil.profileKeyOptional(update.getProfileKey().orNull()); + ProfileName profileName = ProfileName.fromParts(update.getNew().getGivenName().orNull(), update.getNew().getFamilyName().orNull()); + Optional localKey = ProfileKeyUtil.profileKeyOptional(update.getOld().getProfileKey().orNull()); + Optional remoteKey = ProfileKeyUtil.profileKeyOptional(update.getNew().getProfileKey().orNull()); String profileKey = remoteKey.or(localKey).transform(ProfileKey::serialize).transform(Base64::encodeBytes).orNull(); if (!remoteKey.isPresent()) { @@ -945,15 +945,15 @@ public class RecipientDatabase extends Database { values.put(PROFILE_FAMILY_NAME, profileName.getFamilyName()); values.put(PROFILE_JOINED_NAME, profileName.toString()); values.put(PROFILE_KEY, profileKey); - values.put(STORAGE_SERVICE_ID, Base64.encodeBytes(update.getId().getRaw())); + values.put(STORAGE_SERVICE_ID, Base64.encodeBytes(update.getNew().getId().getRaw())); - if (update.hasUnknownFields()) { - values.put(STORAGE_PROTO, Base64.encodeBytes(update.serializeUnknownFields())); + if (update.getNew().hasUnknownFields()) { + values.put(STORAGE_PROTO, Base64.encodeBytes(Objects.requireNonNull(update.getNew().serializeUnknownFields()))); } else { values.putNull(STORAGE_PROTO); } - int updateCount = db.update(TABLE_NAME, values, STORAGE_SERVICE_ID + " = ?", new String[]{Base64.encodeBytes(storageId.getRaw())}); + int updateCount = db.update(TABLE_NAME, values, STORAGE_SERVICE_ID + " = ?", new String[]{Base64.encodeBytes(update.getOld().getId().getRaw())}); if (updateCount < 1) { throw new AssertionError("Account update didn't match any rows!"); } @@ -962,7 +962,7 @@ public class RecipientDatabase extends Database { ApplicationDependencies.getJobManager().add(new RefreshAttributesJob()); } - DatabaseFactory.getThreadDatabase(context).applyStorageSyncUpdate(Recipient.self().getId(), update); + DatabaseFactory.getThreadDatabase(context).applyStorageSyncUpdate(Recipient.self().getId(), update.getNew()); Recipient.self().live().refresh(); } @@ -1460,7 +1460,6 @@ public class RecipientDatabase extends Database { ContentValues values = new ContentValues(1); values.put(UNIDENTIFIED_ACCESS_MODE, unidentifiedAccessMode.getMode()); if (update(id, values)) { - rotateStorageId(id); Recipient.live(id).refresh(); } } 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 8d8a636e5..84d9c01db 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/jobs/StorageSyncJob.java +++ b/app/src/main/java/org/thoughtcrime/securesms/jobs/StorageSyncJob.java @@ -276,6 +276,8 @@ public class StorageSyncJob extends BaseJob { db.beginTransaction(); try { + self = Recipient.self().fresh(); + new ContactRecordProcessor(context, self).process(remoteContacts, StorageSyncHelper.KEY_GENERATOR); new GroupV1RecordProcessor(context).process(remoteGv1, StorageSyncHelper.KEY_GENERATOR); new GroupV2RecordProcessor(context).process(remoteGv2, StorageSyncHelper.KEY_GENERATOR); diff --git a/app/src/main/java/org/thoughtcrime/securesms/storage/AccountRecordProcessor.java b/app/src/main/java/org/thoughtcrime/securesms/storage/AccountRecordProcessor.java index 63da9c8b0..b81907967 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/storage/AccountRecordProcessor.java +++ b/app/src/main/java/org/thoughtcrime/securesms/storage/AccountRecordProcessor.java @@ -127,7 +127,7 @@ public class AccountRecordProcessor extends DefaultStorageRecordProcessor update) { - StorageSyncHelper.applyAccountStorageSyncUpdates(context, self, update.getNew(), true); + StorageSyncHelper.applyAccountStorageSyncUpdates(context, self, update, true); } @Override 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 e0255a7b2..81fbae608 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/storage/StorageSyncHelper.java +++ b/app/src/main/java/org/thoughtcrime/securesms/storage/StorageSyncHelper.java @@ -139,20 +139,25 @@ public final class StorageSyncHelper { return SignalStorageRecord.forAccount(account); } - public static void applyAccountStorageSyncUpdates(@NonNull Context context, @NonNull Recipient self, @NonNull SignalAccountRecord update, boolean fetchProfile) { - DatabaseFactory.getRecipientDatabase(context).applyStorageSyncAccountUpdate(StorageId.forAccount(self.getStorageServiceId()), update); + public static void applyAccountStorageSyncUpdates(@NonNull Context context, @NonNull Recipient self, @NonNull SignalAccountRecord updatedRecord, boolean fetchProfile) { + SignalAccountRecord localRecord = buildAccountRecord(context, self).getAccount().get(); + applyAccountStorageSyncUpdates(context, self, new StorageRecordUpdate<>(localRecord, updatedRecord), fetchProfile); + } - TextSecurePreferences.setReadReceiptsEnabled(context, update.isReadReceiptsEnabled()); - TextSecurePreferences.setTypingIndicatorsEnabled(context, update.isTypingIndicatorsEnabled()); - TextSecurePreferences.setShowUnidentifiedDeliveryIndicatorsEnabled(context, update.isSealedSenderIndicatorsEnabled()); - SignalStore.settings().setLinkPreviewsEnabled(update.isLinkPreviewsEnabled()); - SignalStore.phoneNumberPrivacy().setPhoneNumberListingMode(update.isPhoneNumberUnlisted() ? PhoneNumberPrivacyValues.PhoneNumberListingMode.UNLISTED : PhoneNumberPrivacyValues.PhoneNumberListingMode.LISTED); - SignalStore.phoneNumberPrivacy().setPhoneNumberSharingMode(StorageSyncModels.remoteToLocalPhoneNumberSharingMode(update.getPhoneNumberSharingMode())); - SignalStore.settings().setPreferSystemContactPhotos(update.isPreferContactAvatars()); - SignalStore.paymentsValues().setEnabledAndEntropy(update.getPayments().isEnabled(), Entropy.fromBytes(update.getPayments().getEntropy().orNull())); + public static void applyAccountStorageSyncUpdates(@NonNull Context context, @NonNull Recipient self, @NonNull StorageRecordUpdate update, boolean fetchProfile) { + DatabaseFactory.getRecipientDatabase(context).applyStorageSyncAccountUpdate(update); - if (fetchProfile && update.getAvatarUrlPath().isPresent()) { - ApplicationDependencies.getJobManager().add(new RetrieveProfileAvatarJob(self, update.getAvatarUrlPath().get())); + TextSecurePreferences.setReadReceiptsEnabled(context, update.getNew().isReadReceiptsEnabled()); + TextSecurePreferences.setTypingIndicatorsEnabled(context, update.getNew().isTypingIndicatorsEnabled()); + TextSecurePreferences.setShowUnidentifiedDeliveryIndicatorsEnabled(context, update.getNew().isSealedSenderIndicatorsEnabled()); + SignalStore.settings().setLinkPreviewsEnabled(update.getNew().isLinkPreviewsEnabled()); + SignalStore.phoneNumberPrivacy().setPhoneNumberListingMode(update.getNew().isPhoneNumberUnlisted() ? PhoneNumberPrivacyValues.PhoneNumberListingMode.UNLISTED : PhoneNumberPrivacyValues.PhoneNumberListingMode.LISTED); + SignalStore.phoneNumberPrivacy().setPhoneNumberSharingMode(StorageSyncModels.remoteToLocalPhoneNumberSharingMode(update.getNew().getPhoneNumberSharingMode())); + SignalStore.settings().setPreferSystemContactPhotos(update.getNew().isPreferContactAvatars()); + SignalStore.paymentsValues().setEnabledAndEntropy(update.getNew().getPayments().isEnabled(), Entropy.fromBytes(update.getNew().getPayments().getEntropy().orNull())); + + if (fetchProfile && update.getNew().getAvatarUrlPath().isPresent()) { + ApplicationDependencies.getJobManager().add(new RetrieveProfileAvatarJob(self, update.getNew().getAvatarUrlPath().get())); } }