Update for core changes

This commit is contained in:
nielsandriesse 2020-06-01 10:33:47 +10:00
parent 6c4b3b0fff
commit cae6afec52
3 changed files with 46 additions and 46 deletions

View File

@ -133,16 +133,16 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper {
db.execSQL(StickerDatabase.CREATE_TABLE); db.execSQL(StickerDatabase.CREATE_TABLE);
db.execSQL(LokiAPIDatabase.getCreateSnodePoolCacheCommand()); db.execSQL(LokiAPIDatabase.getCreateSnodePoolCacheCommand());
db.execSQL(LokiAPIDatabase.getCreatePathCacheCommand()); db.execSQL(LokiAPIDatabase.getCreateOnionRequestPathCacheCommand());
db.execSQL(LokiAPIDatabase.getCreateSwarmCacheCommand()); db.execSQL(LokiAPIDatabase.getCreateSwarmCacheCommand());
db.execSQL(LokiAPIDatabase.getCreateLastMessageHashValueTableCommand()); db.execSQL(LokiAPIDatabase.getCreateLastMessageHashValueCacheCommand());
db.execSQL(LokiAPIDatabase.getCreateReceivedMessageHashValuesTableCommand()); db.execSQL(LokiAPIDatabase.getCreateReceivedMessageHashValuesCacheCommand());
db.execSQL(LokiAPIDatabase.getCreateGroupChatAuthTokenTableCommand()); db.execSQL(LokiAPIDatabase.getCreateOpenGroupAuthTokenCacheCommand());
db.execSQL(LokiAPIDatabase.getCreateLastMessageServerIDTableCommand()); db.execSQL(LokiAPIDatabase.getCreateLastMessageServerIDCacheCommand());
db.execSQL(LokiAPIDatabase.getCreateLastDeletionServerIDTableCommand()); db.execSQL(LokiAPIDatabase.getCreateLastDeletionServerIDCacheCommand());
db.execSQL(LokiAPIDatabase.getCreateDeviceLinkTableCommand()); db.execSQL(LokiAPIDatabase.getCreateDeviceLinkCacheCommand());
db.execSQL(LokiAPIDatabase.getCreateUserCountTableCommand()); db.execSQL(LokiAPIDatabase.getCreateUserCountCacheCommand());
db.execSQL(LokiAPIDatabase.getCreateSessionRequestTimestampTableCommand()); db.execSQL(LokiAPIDatabase.getCreateSessionRequestTimestampCacheCommand());
db.execSQL(LokiPreKeyBundleDatabase.getCreateTableCommand()); db.execSQL(LokiPreKeyBundleDatabase.getCreateTableCommand());
db.execSQL(LokiPreKeyRecordDatabase.getCreateTableCommand()); db.execSQL(LokiPreKeyRecordDatabase.getCreateTableCommand());
db.execSQL(LokiMessageDatabase.getCreateMessageFriendRequestTableCommand()); db.execSQL(LokiMessageDatabase.getCreateMessageFriendRequestTableCommand());
@ -515,9 +515,9 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper {
} }
if (oldVersion < lokiV1) { if (oldVersion < lokiV1) {
db.execSQL(LokiAPIDatabase.getCreateGroupChatAuthTokenTableCommand()); db.execSQL(LokiAPIDatabase.getCreateOpenGroupAuthTokenCacheCommand());
db.execSQL(LokiAPIDatabase.getCreateLastMessageServerIDTableCommand()); db.execSQL(LokiAPIDatabase.getCreateLastMessageServerIDCacheCommand());
db.execSQL(LokiAPIDatabase.getCreateLastDeletionServerIDTableCommand()); db.execSQL(LokiAPIDatabase.getCreateLastDeletionServerIDCacheCommand());
} }
if (oldVersion < lokiV2) { if (oldVersion < lokiV2) {
@ -525,7 +525,7 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper {
} }
if (oldVersion < lokiV3) { if (oldVersion < lokiV3) {
db.execSQL(LokiAPIDatabase.getCreateDeviceLinkTableCommand()); db.execSQL(LokiAPIDatabase.getCreateDeviceLinkCacheCommand());
db.execSQL(LokiThreadDatabase.getCreatePublicChatTableCommand()); db.execSQL(LokiThreadDatabase.getCreatePublicChatTableCommand());
db.execSQL("ALTER TABLE groups ADD COLUMN avatar_url TEXT"); db.execSQL("ALTER TABLE groups ADD COLUMN avatar_url TEXT");
@ -537,7 +537,7 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper {
} }
if (oldVersion < lokiV5) { if (oldVersion < lokiV5) {
db.execSQL(LokiAPIDatabase.getCreateUserCountTableCommand()); db.execSQL(LokiAPIDatabase.getCreateUserCountCacheCommand());
} }
if (oldVersion < lokiV6) { if (oldVersion < lokiV6) {
@ -582,12 +582,12 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper {
} }
if (oldVersion < lokiV8) { if (oldVersion < lokiV8) {
db.execSQL(LokiAPIDatabase.getCreateSessionRequestTimestampTableCommand()); db.execSQL(LokiAPIDatabase.getCreateSessionRequestTimestampCacheCommand());
} }
if (oldVersion < lokiV9) { if (oldVersion < lokiV9) {
db.execSQL(LokiAPIDatabase.getCreateSnodePoolCacheCommand()); db.execSQL(LokiAPIDatabase.getCreateSnodePoolCacheCommand());
db.execSQL(LokiAPIDatabase.getCreatePathCacheCommand()); db.execSQL(LokiAPIDatabase.getCreateOnionRequestPathCacheCommand());
} }
db.setTransactionSuccessful(); db.setTransactionSuccessful();

View File

@ -27,7 +27,7 @@ import org.thoughtcrime.securesms.loki.utilities.fadeIn
import org.thoughtcrime.securesms.loki.utilities.fadeOut import org.thoughtcrime.securesms.loki.utilities.fadeOut
import org.thoughtcrime.securesms.loki.utilities.getColorWithID import org.thoughtcrime.securesms.loki.utilities.getColorWithID
import org.whispersystems.signalservice.loki.api.onionrequests.OnionRequestAPI import org.whispersystems.signalservice.loki.api.onionrequests.OnionRequestAPI
import org.whispersystems.signalservice.loki.api.onionrequests.Snode import org.whispersystems.signalservice.loki.api.Snode
class PathActivity : PassphraseRequiredActionBarActivity() { class PathActivity : PassphraseRequiredActionBarActivity() {
private val broadcastReceivers = mutableListOf<BroadcastReceiver>() private val broadcastReceivers = mutableListOf<BroadcastReceiver>()
@ -168,7 +168,7 @@ class PathActivity : PassphraseRequiredActionBarActivity() {
} }
private fun rebuildPath() { private fun rebuildPath() {
DatabaseFactory.getLokiAPIDatabase(this).clearPaths() DatabaseFactory.getLokiAPIDatabase(this).clearOnionRequestPaths()
OnionRequestAPI.guardSnodes = setOf() OnionRequestAPI.guardSnodes = setOf()
OnionRequestAPI.paths = listOf() OnionRequestAPI.paths = listOf()
OnionRequestAPI.buildPaths() OnionRequestAPI.buildPaths()

View File

@ -20,13 +20,13 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
// Snode pool cache // Snode pool cache
private val snodePoolCache = "loki_snode_pool_cache" private val snodePoolCache = "loki_snode_pool_cache"
private val dummyKey = "dummy_key" private val dummyKey = "dummy_key"
private val snodePoolKey = "snode_pool_key" private val snodePool = "snode_pool_key"
@JvmStatic val createSnodePoolCacheCommand = "CREATE TABLE $snodePoolCache ($dummyKey TEXT PRIMARY KEY, $snodePoolKey TEXT);" @JvmStatic val createSnodePoolCacheCommand = "CREATE TABLE $snodePoolCache ($dummyKey TEXT PRIMARY KEY, $snodePool TEXT);"
// Path cache // Onion request path cache
private val pathCache = "loki_path_cache" private val onionRequestPathCache = "loki_path_cache"
private val indexPath = "index_path" private val indexPath = "index_path"
private val snode = "snode" private val snode = "snode"
@JvmStatic val createPathCacheCommand = "CREATE TABLE $pathCache ($indexPath TEXT PRIMARY KEY, $snode TEXT);" @JvmStatic val createOnionRequestPathCacheCommand = "CREATE TABLE $onionRequestPathCache ($indexPath TEXT PRIMARY KEY, $snode TEXT);"
// Swarm cache // Swarm cache
private val swarmCache = "loki_api_swarm_cache" private val swarmCache = "loki_api_swarm_cache"
private val hexEncodedPublicKey = "hex_encoded_public_key" private val hexEncodedPublicKey = "hex_encoded_public_key"
@ -36,51 +36,51 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
private val lastMessageHashValueCache = "loki_api_last_message_hash_value_cache" private val lastMessageHashValueCache = "loki_api_last_message_hash_value_cache"
private val target = "target" private val target = "target"
private val lastMessageHashValue = "last_message_hash_value" private val lastMessageHashValue = "last_message_hash_value"
@JvmStatic val createLastMessageHashValueTableCommand = "CREATE TABLE $lastMessageHashValueCache ($target TEXT PRIMARY KEY, $lastMessageHashValue TEXT);" @JvmStatic val createLastMessageHashValueCacheCommand = "CREATE TABLE $lastMessageHashValueCache ($target TEXT PRIMARY KEY, $lastMessageHashValue TEXT);"
// Received message hash values cache // Received message hash values cache
private val receivedMessageHashValuesCache = "loki_api_received_message_hash_values_cache" private val receivedMessageHashValuesCache = "loki_api_received_message_hash_values_cache"
private val userID = "user_id" private val userID = "user_id"
private val receivedMessageHashValues = "received_message_hash_values" private val receivedMessageHashValues = "received_message_hash_values"
@JvmStatic val createReceivedMessageHashValuesTableCommand = "CREATE TABLE $receivedMessageHashValuesCache ($userID TEXT PRIMARY KEY, $receivedMessageHashValues TEXT);" @JvmStatic val createReceivedMessageHashValuesCacheCommand = "CREATE TABLE $receivedMessageHashValuesCache ($userID TEXT PRIMARY KEY, $receivedMessageHashValues TEXT);"
// Group chat auth token cache // Open group auth token cache
private val groupChatAuthTokenCache = "loki_api_group_chat_auth_token_database" private val openGroupAuthTokenCache = "loki_api_group_chat_auth_token_database"
private val server = "server" private val server = "server"
private val token = "token" private val token = "token"
@JvmStatic val createGroupChatAuthTokenTableCommand = "CREATE TABLE $groupChatAuthTokenCache ($server TEXT PRIMARY KEY, $token TEXT);" @JvmStatic val createOpenGroupAuthTokenCacheCommand = "CREATE TABLE $openGroupAuthTokenCache ($server TEXT PRIMARY KEY, $token TEXT);"
// Last message server ID cache // Last message server ID cache
private val lastMessageServerIDCache = "loki_api_last_message_server_id_cache" private val lastMessageServerIDCache = "loki_api_last_message_server_id_cache"
private val lastMessageServerIDCacheIndex = "loki_api_last_message_server_id_cache_index" private val lastMessageServerIDCacheIndex = "loki_api_last_message_server_id_cache_index"
private val lastMessageServerID = "last_message_server_id" private val lastMessageServerID = "last_message_server_id"
@JvmStatic val createLastMessageServerIDTableCommand = "CREATE TABLE $lastMessageServerIDCache ($lastMessageServerIDCacheIndex STRING PRIMARY KEY, $lastMessageServerID INTEGER DEFAULT 0);" @JvmStatic val createLastMessageServerIDCacheCommand = "CREATE TABLE $lastMessageServerIDCache ($lastMessageServerIDCacheIndex STRING PRIMARY KEY, $lastMessageServerID INTEGER DEFAULT 0);"
// Last deletion server ID cache // Last deletion server ID cache
private val lastDeletionServerIDCache = "loki_api_last_deletion_server_id_cache" private val lastDeletionServerIDCache = "loki_api_last_deletion_server_id_cache"
private val lastDeletionServerIDCacheIndex = "loki_api_last_deletion_server_id_cache_index" private val lastDeletionServerIDCacheIndex = "loki_api_last_deletion_server_id_cache_index"
private val lastDeletionServerID = "last_deletion_server_id" private val lastDeletionServerID = "last_deletion_server_id"
@JvmStatic val createLastDeletionServerIDTableCommand = "CREATE TABLE $lastDeletionServerIDCache ($lastDeletionServerIDCacheIndex STRING PRIMARY KEY, $lastDeletionServerID INTEGER DEFAULT 0);" @JvmStatic val createLastDeletionServerIDCacheCommand = "CREATE TABLE $lastDeletionServerIDCache ($lastDeletionServerIDCacheIndex STRING PRIMARY KEY, $lastDeletionServerID INTEGER DEFAULT 0);"
// Device link cache // Device link cache
private val deviceLinkCache = "loki_pairing_authorisation_cache" private val deviceLinkCache = "loki_pairing_authorisation_cache"
private val masterHexEncodedPublicKey = "primary_device" private val masterHexEncodedPublicKey = "primary_device"
private val slaveHexEncodedPublicKey = "secondary_device" private val slaveHexEncodedPublicKey = "secondary_device"
private val requestSignature = "request_signature" private val requestSignature = "request_signature"
private val authorizationSignature = "grant_signature" private val authorizationSignature = "grant_signature"
@JvmStatic val createDeviceLinkTableCommand = "CREATE TABLE $deviceLinkCache ($masterHexEncodedPublicKey TEXT, $slaveHexEncodedPublicKey TEXT, " + @JvmStatic val createDeviceLinkCacheCommand = "CREATE TABLE $deviceLinkCache ($masterHexEncodedPublicKey TEXT, $slaveHexEncodedPublicKey TEXT, " +
"$requestSignature TEXT NULLABLE DEFAULT NULL, $authorizationSignature TEXT NULLABLE DEFAULT NULL, PRIMARY KEY ($masterHexEncodedPublicKey, $slaveHexEncodedPublicKey));" "$requestSignature TEXT NULLABLE DEFAULT NULL, $authorizationSignature TEXT NULLABLE DEFAULT NULL, PRIMARY KEY ($masterHexEncodedPublicKey, $slaveHexEncodedPublicKey));"
// User count cache // User count cache
private val userCountCache = "loki_user_count_cache" private val userCountCache = "loki_user_count_cache"
private val publicChatID = "public_chat_id" private val publicChatID = "public_chat_id"
private val userCount = "user_count" private val userCount = "user_count"
@JvmStatic val createUserCountTableCommand = "CREATE TABLE $userCountCache ($publicChatID STRING PRIMARY KEY, $userCount INTEGER DEFAULT 0);" @JvmStatic val createUserCountCacheCommand = "CREATE TABLE $userCountCache ($publicChatID STRING PRIMARY KEY, $userCount INTEGER DEFAULT 0);"
// Session request timestamp cache // Session request timestamp cache
private val sessionRequestTimestampCache = "session_request_timestamp_cache" private val sessionRequestTimestampCache = "session_request_timestamp_cache"
private val publicKey = "public_key" private val publicKey = "public_key"
private val timestamp = "timestamp" private val timestamp = "timestamp"
@JvmStatic val createSessionRequestTimestampTableCommand = "CREATE TABLE $sessionRequestTimestampCache ($publicKey STRING PRIMARY KEY, $timestamp INTEGER DEFAULT 0);" @JvmStatic val createSessionRequestTimestampCacheCommand = "CREATE TABLE $sessionRequestTimestampCache ($publicKey STRING PRIMARY KEY, $timestamp INTEGER DEFAULT 0);"
} }
override fun getSnodePool(): Set<LokiAPITarget> { override fun getSnodePool(): Set<LokiAPITarget> {
val database = databaseHelper.readableDatabase val database = databaseHelper.readableDatabase
return database.get(snodePoolCache, "${Companion.dummyKey} = ?", wrap("dummy_key")) { cursor -> return database.get(snodePoolCache, "${Companion.dummyKey} = ?", wrap("dummy_key")) { cursor ->
val snodePoolAsString = cursor.getString(cursor.getColumnIndexOrThrow(snodePoolKey)) val snodePoolAsString = cursor.getString(cursor.getColumnIndexOrThrow(snodePool))
snodePoolAsString.split(", ").mapNotNull { snodeAsString -> snodePoolAsString.split(", ").mapNotNull { snodeAsString ->
val components = snodeAsString.split("-") val components = snodeAsString.split("-")
val address = components[0] val address = components[0]
@ -102,14 +102,14 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
} }
string string
} }
val row = wrap(mapOf(Companion.dummyKey to "dummy_key", snodePoolKey to snodePoolAsString)) val row = wrap(mapOf(Companion.dummyKey to "dummy_key", snodePool to snodePoolAsString))
database.insertOrUpdate(snodePoolCache, row, "${Companion.dummyKey} = ?", wrap("dummy_key")) database.insertOrUpdate(snodePoolCache, row, "${Companion.dummyKey} = ?", wrap("dummy_key"))
} }
override fun getPaths(): List<List<LokiAPITarget>> { override fun getOnionRequestPaths(): List<List<LokiAPITarget>> {
val database = databaseHelper.readableDatabase val database = databaseHelper.readableDatabase
fun get(indexPath: String): LokiAPITarget? { fun get(indexPath: String): LokiAPITarget? {
return database.get(pathCache, "${Companion.indexPath} = ?", wrap(indexPath)) { cursor -> return database.get(onionRequestPathCache, "${Companion.indexPath} = ?", wrap(indexPath)) { cursor ->
val snodeAsString = cursor.getString(cursor.getColumnIndexOrThrow(snode)) val snodeAsString = cursor.getString(cursor.getColumnIndexOrThrow(snode))
val components = snodeAsString.split("-") val components = snodeAsString.split("-")
val address = components[0] val address = components[0]
@ -129,17 +129,17 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
return listOf( listOf( path0Snode0, path0Snode1, path0Snode2 ), listOf( path1Snode0, path1Snode1, path1Snode2 ) ) return listOf( listOf( path0Snode0, path0Snode1, path0Snode2 ), listOf( path1Snode0, path1Snode1, path1Snode2 ) )
} }
fun clearPaths() { fun clearOnionRequestPaths() {
val database = databaseHelper.writableDatabase val database = databaseHelper.writableDatabase
fun delete(indexPath: String) { fun delete(indexPath: String) {
database.delete(pathCache, "${Companion.indexPath} = ?", wrap(indexPath)) database.delete(onionRequestPathCache, "${Companion.indexPath} = ?", wrap(indexPath))
} }
delete("0-0"); delete("0-1") delete("0-0"); delete("0-1")
delete("0-2"); delete("1-0") delete("0-2"); delete("1-0")
delete("1-1"); delete("1-2") delete("1-1"); delete("1-2")
} }
override fun setPaths(newValue: List<List<LokiAPITarget>>) { override fun setOnionRequestPaths(newValue: List<List<LokiAPITarget>>) {
// FIXME: This is a bit of a dirty approach that assumes 2 paths of length 3 each. We should do better than this. // FIXME: This is a bit of a dirty approach that assumes 2 paths of length 3 each. We should do better than this.
if (newValue.count() != 2) { return } if (newValue.count() != 2) { return }
val path0 = newValue[0] val path0 = newValue[0]
@ -154,14 +154,14 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
snodeAsString += "-${keySet.ed25519Key}-${keySet.x25519Key}" snodeAsString += "-${keySet.ed25519Key}-${keySet.x25519Key}"
} }
val row = wrap(mapOf(Companion.indexPath to indexPath, Companion.snode to snodeAsString)) val row = wrap(mapOf(Companion.indexPath to indexPath, Companion.snode to snodeAsString))
database.insertOrUpdate(pathCache, row, "${Companion.indexPath} = ?", wrap(indexPath)) database.insertOrUpdate(onionRequestPathCache, row, "${Companion.indexPath} = ?", wrap(indexPath))
} }
set("0-0", path0[0]); set("0-1", path0[1]) set("0-0", path0[0]); set("0-1", path0[1])
set("0-2", path0[2]); set("1-0", path1[0]) set("0-2", path0[2]); set("1-0", path1[0])
set("1-1", path1[1]); set("1-2", path1[2]) set("1-1", path1[1]); set("1-2", path1[2])
} }
override fun getSwarmCache(hexEncodedPublicKey: String): Set<LokiAPITarget>? { override fun getSwarm(hexEncodedPublicKey: String): Set<LokiAPITarget>? {
val database = databaseHelper.readableDatabase val database = databaseHelper.readableDatabase
return database.get(swarmCache, "${Companion.hexEncodedPublicKey} = ?", wrap(hexEncodedPublicKey)) { cursor -> return database.get(swarmCache, "${Companion.hexEncodedPublicKey} = ?", wrap(hexEncodedPublicKey)) { cursor ->
val swarmAsString = cursor.getString(cursor.getColumnIndexOrThrow(swarm)) val swarmAsString = cursor.getString(cursor.getColumnIndexOrThrow(swarm))
@ -176,7 +176,7 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
}?.toSet() }?.toSet()
} }
override fun setSwarmCache(hexEncodedPublicKey: String, newValue: Set<LokiAPITarget>) { override fun setSwarm(hexEncodedPublicKey: String, newValue: Set<LokiAPITarget>) {
val database = databaseHelper.writableDatabase val database = databaseHelper.writableDatabase
val swarmAsString = newValue.joinToString(", ") { target -> val swarmAsString = newValue.joinToString(", ") { target ->
var string = "${target.address}-${target.port}" var string = "${target.address}-${target.port}"
@ -220,7 +220,7 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
override fun getAuthToken(server: String): String? { override fun getAuthToken(server: String): String? {
val database = databaseHelper.readableDatabase val database = databaseHelper.readableDatabase
return database.get(groupChatAuthTokenCache, "${Companion.server} = ?", wrap(server)) { cursor -> return database.get(openGroupAuthTokenCache, "${Companion.server} = ?", wrap(server)) { cursor ->
cursor.getString(cursor.getColumnIndexOrThrow(token)) cursor.getString(cursor.getColumnIndexOrThrow(token))
} }
} }
@ -229,9 +229,9 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
val database = databaseHelper.writableDatabase val database = databaseHelper.writableDatabase
if (newValue != null) { if (newValue != null) {
val row = wrap(mapOf(Companion.server to server, token to newValue)) val row = wrap(mapOf(Companion.server to server, token to newValue))
database.insertOrUpdate(groupChatAuthTokenCache, row, "${Companion.server} = ?", wrap(server)) database.insertOrUpdate(openGroupAuthTokenCache, row, "${Companion.server} = ?", wrap(server))
} else { } else {
database.delete(groupChatAuthTokenCache, "${Companion.server} = ?", wrap(server)) database.delete(openGroupAuthTokenCache, "${Companion.server} = ?", wrap(server))
} }
} }