From 23e55ac5f7950171c0601ef01f5839ab0b2b286b Mon Sep 17 00:00:00 2001 From: Alan Evans Date: Wed, 12 Feb 2020 17:30:20 -0400 Subject: [PATCH] Clear unidentified access mode when profile key changes. --- .../securesms/database/RecipientDatabase.java | 13 +++++++++---- .../securesms/jobs/PushProcessMessageJob.java | 11 ++++++----- 2 files changed, 15 insertions(+), 9 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 b7403a389..cf0cb78e1 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/RecipientDatabase.java +++ b/app/src/main/java/org/thoughtcrime/securesms/database/RecipientDatabase.java @@ -799,25 +799,30 @@ public class RecipientDatabase extends Database { /** * Updates the profile key. *

- * If it changes, it clears out the profile key credential. + * If it changes, it clears out the profile key credential and resets the unidentified access mode. + * @return true iff changed. */ - public void setProfileKey(@NonNull RecipientId id, @Nullable ProfileKey profileKey) { + public boolean setProfileKey(@NonNull RecipientId id, @NonNull ProfileKey profileKey) { String selection = ID + " = ?"; String[] args = new String[]{id.serialize()}; ContentValues valuesToCompare = new ContentValues(1); - ContentValues valuesToSet = new ContentValues(2); - String encodedProfileKey = profileKey == null ? null : Base64.encodeBytes(profileKey.serialize()); + ContentValues valuesToSet = new ContentValues(3); + String encodedProfileKey = Base64.encodeBytes(profileKey.serialize()); valuesToCompare.put(PROFILE_KEY, encodedProfileKey); valuesToSet.put(PROFILE_KEY, encodedProfileKey); valuesToSet.putNull(PROFILE_KEY_CREDENTIAL); + valuesToSet.put(UNIDENTIFIED_ACCESS_MODE, UnidentifiedAccessMode.UNKNOWN.getMode()); SqlUtil.UpdateQuery updateQuery = SqlUtil.buildTrueUpdateQuery(selection, args, valuesToCompare); if (update(updateQuery, valuesToSet)) { markDirty(id, DirtyState.UPDATE); Recipient.live(id).refresh(); + return true; + } else { + return false; } } diff --git a/app/src/main/java/org/thoughtcrime/securesms/jobs/PushProcessMessageJob.java b/app/src/main/java/org/thoughtcrime/securesms/jobs/PushProcessMessageJob.java index 137fd724d..3a4257977 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/jobs/PushProcessMessageJob.java +++ b/app/src/main/java/org/thoughtcrime/securesms/jobs/PushProcessMessageJob.java @@ -1180,13 +1180,14 @@ public final class PushProcessMessageJob extends BaseJob { { RecipientDatabase database = DatabaseFactory.getRecipientDatabase(context); Recipient recipient = Recipient.externalPush(context, content.getSender()); - ProfileKey currentProfileKey = ProfileKeyUtil.profileKeyOrNull(recipient.getProfileKey()); ProfileKey messageProfileKey = ProfileKeyUtil.profileKeyOrNull(messageProfileKeyBytes); - if (messageProfileKey != null && !messageProfileKey.equals(currentProfileKey)) { - database.setProfileKey(recipient.getId(), messageProfileKey); - database.setUnidentifiedAccessMode(recipient.getId(), RecipientDatabase.UnidentifiedAccessMode.UNKNOWN); - ApplicationDependencies.getJobManager().add(new RetrieveProfileJob(recipient)); + if (messageProfileKey != null) { + if (database.setProfileKey(recipient.getId(), messageProfileKey)) { + ApplicationDependencies.getJobManager().add(new RetrieveProfileJob(recipient)); + } + } else { + Log.w(TAG, "Ignored invalid profile key seen in message"); } }