diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/RecipientTable.kt b/app/src/main/java/org/thoughtcrime/securesms/database/RecipientTable.kt index 73c278484b..03ff8123af 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/RecipientTable.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/database/RecipientTable.kt @@ -803,10 +803,11 @@ open class RecipientTable(context: Context, databaseHelper: SignalDatabase) : Da recipientId = RecipientId.from(id) } - if (insert.identityKey.isPresent && insert.aci.isPresent) { + if (insert.identityKey.isPresent && (insert.aci.isPresent || insert.pni.isPresent)) { try { + val serviceId: ServiceId = insert.aci.orNull() ?: insert.pni.get() val identityKey = IdentityKey(insert.identityKey.get(), 0) - identities.updateIdentityAfterSync(insert.aci.get().toString(), recipientId, identityKey, StorageSyncModels.remoteToLocalIdentityStatus(insert.identityState)) + identities.updateIdentityAfterSync(serviceId.toString(), recipientId, identityKey, StorageSyncModels.remoteToLocalIdentityStatus(insert.identityState)) } catch (e: InvalidKeyException) { Log.w(TAG, "Failed to process identity key during insert! Skipping.", e) } @@ -1071,7 +1072,7 @@ open class RecipientTable(context: Context, databaseHelper: SignalDatabase) : Da private fun getRecordForSync(query: String?, args: Array?): List { val table = """ - $TABLE_NAME LEFT OUTER JOIN ${IdentityTable.TABLE_NAME} ON $TABLE_NAME.$ACI_COLUMN = ${IdentityTable.TABLE_NAME}.${IdentityTable.ADDRESS} + $TABLE_NAME LEFT OUTER JOIN ${IdentityTable.TABLE_NAME} ON ($TABLE_NAME.$ACI_COLUMN = ${IdentityTable.TABLE_NAME}.${IdentityTable.ADDRESS} OR ($TABLE_NAME.$ACI_COLUMN IS NULL AND $TABLE_NAME.$PNI_COLUMN = ${IdentityTable.TABLE_NAME}.${IdentityTable.ADDRESS})) LEFT OUTER JOIN ${GroupTable.TABLE_NAME} ON $TABLE_NAME.$GROUP_ID = ${GroupTable.TABLE_NAME}.${GroupTable.GROUP_ID} LEFT OUTER JOIN ${ThreadTable.TABLE_NAME} ON $TABLE_NAME.$ID = ${ThreadTable.TABLE_NAME}.${ThreadTable.RECIPIENT_ID} """ @@ -1115,7 +1116,7 @@ open class RecipientTable(context: Context, databaseHelper: SignalDatabase) : Da .where( """ $STORAGE_SERVICE_ID NOT NULL AND ( - ($TYPE = ? AND $ACI_COLUMN NOT NULL AND $ID != ?) + ($TYPE = ? AND ($ACI_COLUMN NOT NULL OR $PNI_COLUMN NOT NULL) AND $ID != ?) OR $TYPE = ? OR @@ -2382,12 +2383,6 @@ open class RecipientTable(context: Context, databaseHelper: SignalDatabase) : Da } is PnpOperation.SetPni -> { - writableDatabase - .update(TABLE_NAME) - .values(ACI_COLUMN to operation.pni.toString()) - .where("$ID = ? AND ($ACI_COLUMN IS NULL OR $ACI_COLUMN = $PNI_COLUMN)", operation.recipientId) - .run() - writableDatabase .update(TABLE_NAME) .values( diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/helpers/SignalDatabaseMigrations.kt b/app/src/main/java/org/thoughtcrime/securesms/database/helpers/SignalDatabaseMigrations.kt index 5d268b8208..bb069c68f3 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/helpers/SignalDatabaseMigrations.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/database/helpers/SignalDatabaseMigrations.kt @@ -63,6 +63,7 @@ import org.thoughtcrime.securesms.database.helpers.migration.V204_GroupForeignKe import org.thoughtcrime.securesms.database.helpers.migration.V205_DropPushTable import org.thoughtcrime.securesms.database.helpers.migration.V206_AddConversationCountIndex import org.thoughtcrime.securesms.database.helpers.migration.V207_AddChunkSizeColumn +import org.thoughtcrime.securesms.database.helpers.migration.V208_ClearRecipientPniFromAciColumn /** * Contains all of the database migrations for [SignalDatabase]. Broken into a separate file for cleanliness. @@ -71,7 +72,7 @@ object SignalDatabaseMigrations { val TAG: String = Log.tag(SignalDatabaseMigrations.javaClass) - const val DATABASE_VERSION = 207 + const val DATABASE_VERSION = 208 @JvmStatic fun migrate(context: Application, db: SQLiteDatabase, oldVersion: Int, newVersion: Int) { @@ -310,6 +311,10 @@ object SignalDatabaseMigrations { if (oldVersion < 207) { V207_AddChunkSizeColumn.migrate(context, db, oldVersion, newVersion) } + + if (oldVersion < 208) { + V208_ClearRecipientPniFromAciColumn.migrate(context, db, oldVersion, newVersion) + } } @JvmStatic diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/helpers/migration/V208_ClearRecipientPniFromAciColumn.kt b/app/src/main/java/org/thoughtcrime/securesms/database/helpers/migration/V208_ClearRecipientPniFromAciColumn.kt new file mode 100644 index 0000000000..5dcadb75e7 --- /dev/null +++ b/app/src/main/java/org/thoughtcrime/securesms/database/helpers/migration/V208_ClearRecipientPniFromAciColumn.kt @@ -0,0 +1,19 @@ +/* + * Copyright 2023 Signal Messenger, LLC + * SPDX-License-Identifier: AGPL-3.0-only + */ + +package org.thoughtcrime.securesms.database.helpers.migration + +import android.app.Application +import net.zetetic.database.sqlcipher.SQLiteDatabase + +/** + * PNIs were incorrectly being set to ACI column, remove them if present. + */ +@Suppress("ClassName") +object V208_ClearRecipientPniFromAciColumn : SignalDatabaseMigration { + override fun migrate(context: Application, db: SQLiteDatabase, oldVersion: Int, newVersion: Int) { + db.execSQL("DELETE FROM recipient WHERE aci LIKE 'PNI:%'") + } +} diff --git a/app/src/main/java/org/thoughtcrime/securesms/storage/ContactRecordProcessor.java b/app/src/main/java/org/thoughtcrime/securesms/storage/ContactRecordProcessor.java index cd72af22a7..82d9d35eb5 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/storage/ContactRecordProcessor.java +++ b/app/src/main/java/org/thoughtcrime/securesms/storage/ContactRecordProcessor.java @@ -305,7 +305,7 @@ public class ContactRecordProcessor extends DefaultStorageRecordProcessor