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 94a877b762..e430da9ebb 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/RecipientDatabase.java +++ b/app/src/main/java/org/thoughtcrime/securesms/database/RecipientDatabase.java @@ -2662,12 +2662,17 @@ public class RecipientDatabase extends Database { /** * Does not trigger any recipient refreshes -- it is assumed the caller handles this. + * Will *not* give storageIds to those that shouldn't get them (e.g. MMS groups, unregistered + * users). */ void rotateStorageId(@NonNull RecipientId recipientId) { ContentValues values = new ContentValues(1); values.put(STORAGE_SERVICE_ID, Base64.encodeBytes(StorageSyncHelper.generateKey())); - databaseHelper.getWritableDatabase().update(TABLE_NAME, values, ID_WHERE, SqlUtil.buildArgs(recipientId)); + String query = ID + " = ? AND (" + GROUP_TYPE + " IN (?, ?) OR " + REGISTERED + " = ?)"; + String[] args = SqlUtil.buildArgs(recipientId, GroupType.SIGNAL_V1.getId(), GroupType.SIGNAL_V2.getId(), RegisteredState.REGISTERED.getId()); + + databaseHelper.getWritableDatabase().update(TABLE_NAME, values, query, args); } /** diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java b/app/src/main/java/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java index 16b078b812..ab9bda104b 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java +++ b/app/src/main/java/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java @@ -179,8 +179,9 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper implements SignalDatab private static final int CLEAN_STORAGE_IDS_WITHOUT_INFO = 95; private static final int CLEAN_REACTION_NOTIFICATIONS = 96; private static final int STORAGE_SERVICE_REFACTOR = 97; + private static final int CLEAR_MMS_STORAGE_IDS = 98; - private static final int DATABASE_VERSION = 97; + private static final int DATABASE_VERSION = 98; private static final String DATABASE_NAME = "signal.db"; private final Context context; @@ -1438,6 +1439,15 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper implements SignalDatab Log.d(TAG, String.format(Locale.US, "For storage service refactor migration, there were %d inserts, %d updated, and %d deletes. Cleared the dirty status on %d rows.", insertCount, updateCount, deleteCount, dirtyCount)); } + if (oldVersion < CLEAR_MMS_STORAGE_IDS) { + ContentValues deleteValues = new ContentValues(); + deleteValues.putNull("storage_service_key"); + + int deleteCount = db.update("recipient", deleteValues, "storage_service_key NOT NULL AND (group_type = 1 OR (group_type = 0 AND phone IS NULL AND uuid IS NULL))", null); + + Log.d(TAG, "Cleared storageIds from " + deleteCount + " rows. They were either MMS groups or empty contacts."); + } + db.setTransactionSuccessful(); } finally { db.endTransaction();