From 158182baa005e74f56e8fe79f9dde17ee6325fa2 Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Tue, 4 Jun 2019 11:23:44 +1000 Subject: [PATCH] Make swarm cache port specific Last message hash value cache is intentionally not port specific so that if a snode switches to a different port the information about which messages it has stored isn't lost --- src/org/thoughtcrime/securesms/loki/LokiAPIDatabase.kt | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/org/thoughtcrime/securesms/loki/LokiAPIDatabase.kt b/src/org/thoughtcrime/securesms/loki/LokiAPIDatabase.kt index 72d77160c7..9c847adc16 100644 --- a/src/org/thoughtcrime/securesms/loki/LokiAPIDatabase.kt +++ b/src/org/thoughtcrime/securesms/loki/LokiAPIDatabase.kt @@ -7,7 +7,6 @@ import org.thoughtcrime.securesms.database.Database import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper import org.whispersystems.signalservice.loki.api.LokiAPIDatabaseProtocol import org.whispersystems.signalservice.loki.api.LokiAPITarget -import org.whispersystems.signalservice.loki.api.LokiSwarmAPI class LokiAPIDatabase(private val userPublicKey: String, context: Context, helper: SQLCipherOpenHelper) : Database(context, helper), LokiAPIDatabaseProtocol { @@ -32,13 +31,18 @@ class LokiAPIDatabase(private val userPublicKey: String, context: Context, helpe override fun getSwarmCache(hexEncodedPublicKey: String): List? { return get(swarmCache, "${Companion.hexEncodedPublicKey} = ?", wrap(hexEncodedPublicKey)) { cursor -> val swarmAsString = cursor.getString(cursor.getColumnIndexOrThrow(swarm)) - swarmAsString.split(",").map { LokiAPITarget(it, LokiSwarmAPI.defaultSnodePort) } + swarmAsString.split(",").map { targetAsString -> + val components = targetAsString.split("?port=") + LokiAPITarget(components[0], components[1].toInt()) + } } } override fun setSwarmCache(hexEncodedPublicKey: String, newValue: List) { val database = databaseHelper.writableDatabase - val swarmAsString = newValue.joinToString(",") { it.address } + val swarmAsString = newValue.joinToString(",") { target -> + "${target.address}?port=${target.port}" + } database.update(swarmCache, wrap(mapOf( swarm to swarmAsString )), "${Companion.hexEncodedPublicKey} = ?", wrap(hexEncodedPublicKey)) }