From b369b734ca7aebc69b1daddb6ee1ddbe62754901 Mon Sep 17 00:00:00 2001 From: Greyson Parrelli Date: Fri, 7 Aug 2020 17:31:06 -0400 Subject: [PATCH] Improve storage service insert recovery. --- .../securesms/database/RecipientDatabase.java | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 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 b30521f7b..17041a1cc 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/RecipientDatabase.java +++ b/app/src/main/java/org/thoughtcrime/securesms/database/RecipientDatabase.java @@ -715,10 +715,9 @@ public class RecipientDatabase extends Database { try { for (SignalContactRecord insert : contactInserts) { - ContentValues values = getValuesForStorageContact(insert, true); - long id = db.insertWithOnConflict(TABLE_NAME, null, values, SQLiteDatabase.CONFLICT_IGNORE); - - RecipientId recipientId; + ContentValues values = getValuesForStorageContact(insert, true); + long id = db.insertWithOnConflict(TABLE_NAME, null, values, SQLiteDatabase.CONFLICT_IGNORE); + RecipientId recipientId = null; if (id < 0) { values = getValuesForStorageContact(insert, false); @@ -727,17 +726,17 @@ public class RecipientDatabase extends Database { if (insert.getAddress().getNumber().isPresent()) { try { int count = db.update(TABLE_NAME, values, PHONE + " = ?", new String[] { insert.getAddress().getNumber().get() }); - recipientId = getByE164(insert.getAddress().getNumber().get()).get(); Log.w(TAG, "Updated " + count + " users by E164."); } catch (SQLiteConstraintException e) { Log.w(TAG, "[applyStorageSyncUpdates -- Insert] Failed to update the UUID on an existing E164 user. Possibly merging."); recipientId = getAndPossiblyMerge(insert.getAddress().getUuid().get(), insert.getAddress().getNumber().get(), true); Log.w(TAG, "[applyStorageSyncUpdates -- Insert] Resulting id: " + recipientId); } - } else { + } + + if (recipientId == null && insert.getAddress().getUuid().isPresent()) { try { int count = db.update(TABLE_NAME, values, UUID + " = ?", new String[] { insert.getAddress().getUuid().get().toString() }); - recipientId = getByUuid(insert.getAddress().getUuid().get()).get(); Log.w(TAG, "Updated " + count + " users by UUID."); } catch (SQLiteConstraintException e) { Log.w(TAG, "[applyStorageSyncUpdates -- Insert] Failed to update the E164 on an existing UUID user. Possibly merging."); @@ -745,6 +744,19 @@ public class RecipientDatabase extends Database { Log.w(TAG, "[applyStorageSyncUpdates -- Insert] Resulting id: " + recipientId); } } + + if (recipientId == null && insert.getAddress().getNumber().isPresent()) { + recipientId = getByE164(insert.getAddress().getNumber().get()).orNull(); + } + + if (recipientId == null && insert.getAddress().getUuid().isPresent()) { + recipientId = getByUuid(insert.getAddress().getUuid().get()).orNull(); + } + + if (recipientId == null) { + Log.w(TAG, "Failed to recover from a failed insert!"); + continue; + } } else { recipientId = RecipientId.from(id); }