From f7197ca1de0fedf1de6b3c470d922f252c6d5818 Mon Sep 17 00:00:00 2001 From: Mikunj Date: Fri, 17 Jan 2020 12:51:37 +1100 Subject: [PATCH] Update how swarms are stored in the database. --- .../securesms/loki/LokiAPIDatabase.kt | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/org/thoughtcrime/securesms/loki/LokiAPIDatabase.kt b/src/org/thoughtcrime/securesms/loki/LokiAPIDatabase.kt index b65597dec7..5b2c3d6f1c 100644 --- a/src/org/thoughtcrime/securesms/loki/LokiAPIDatabase.kt +++ b/src/org/thoughtcrime/securesms/loki/LokiAPIDatabase.kt @@ -66,9 +66,13 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database( val database = databaseHelper.readableDatabase return database.get(swarmCache, "${Companion.hexEncodedPublicKey} = ?", wrap(hexEncodedPublicKey)) { cursor -> val swarmAsString = cursor.getString(cursor.getColumnIndexOrThrow(swarm)) - swarmAsString.split(", ").map { targetAsString -> - val components = targetAsString.split("?port=") - LokiAPITarget(components[0], components[1].toInt()) + swarmAsString.split(", ").mapNotNull { targetAsString -> + val components = targetAsString.split("-") + val address = components[0] + val port = components.getOrNull(1) ?: return@mapNotNull null + val identificationKey = components.getOrNull(2) ?: return@mapNotNull null + val encryptionKey = components.getOrNull(3)?: return@mapNotNull null + LokiAPITarget(address, port.toInt(), LokiAPITarget.Keys(identificationKey, encryptionKey)) } }?.toSet() } @@ -76,7 +80,12 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database( override fun setSwarmCache(hexEncodedPublicKey: String, newValue: Set) { val database = databaseHelper.writableDatabase val swarmAsString = newValue.joinToString(", ") { target -> - "${target.address}?port=${target.port}" + var string = "${target.address}-${target.port}" + val keys = target.publicKeys + if (keys != null) { + string += "-${keys.identification}-${keys.encryption}" + } + string } val row = wrap(mapOf( Companion.hexEncodedPublicKey to hexEncodedPublicKey, swarm to swarmAsString )) database.insertOrUpdate(swarmCache, row, "${Companion.hexEncodedPublicKey} = ?", wrap(hexEncodedPublicKey))