Skip to content

Commit

Permalink
Improve storage service insert recovery.
Browse files Browse the repository at this point in the history
  • Loading branch information
greyson-signal committed Aug 7, 2020
1 parent 57150a2 commit b369b73
Showing 1 changed file with 19 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -715,10 +715,9 @@ public void applyStorageSyncUpdates(@NonNull Collection<SignalContactRecord>

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);
Expand All @@ -727,24 +726,37 @@ public void applyStorageSyncUpdates(@NonNull Collection<SignalContactRecord>
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.");
recipientId = getAndPossiblyMerge(insert.getAddress().getUuid().get(), insert.getAddress().getNumber().get(), true);
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);
}
Expand Down

0 comments on commit b369b73

Please sign in to comment.