|
|
|
@ -20,13 +20,13 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
|
|
|
|
|
// Snode pool cache
|
|
|
|
|
private val snodePoolCache = "loki_snode_pool_cache"
|
|
|
|
|
private val dummyKey = "dummy_key"
|
|
|
|
|
private val snodePoolKey = "snode_pool_key"
|
|
|
|
|
@JvmStatic val createSnodePoolCacheCommand = "CREATE TABLE $snodePoolCache ($dummyKey TEXT PRIMARY KEY, $snodePoolKey TEXT);"
|
|
|
|
|
// Path cache
|
|
|
|
|
private val pathCache = "loki_path_cache"
|
|
|
|
|
private val snodePool = "snode_pool_key"
|
|
|
|
|
@JvmStatic val createSnodePoolCacheCommand = "CREATE TABLE $snodePoolCache ($dummyKey TEXT PRIMARY KEY, $snodePool TEXT);"
|
|
|
|
|
// Onion request path cache
|
|
|
|
|
private val onionRequestPathCache = "loki_path_cache"
|
|
|
|
|
private val indexPath = "index_path"
|
|
|
|
|
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
|
|
|
|
|
private val swarmCache = "loki_api_swarm_cache"
|
|
|
|
|
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 target = "target"
|
|
|
|
|
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
|
|
|
|
|
private val receivedMessageHashValuesCache = "loki_api_received_message_hash_values_cache"
|
|
|
|
|
private val userID = "user_id"
|
|
|
|
|
private val receivedMessageHashValues = "received_message_hash_values"
|
|
|
|
|
@JvmStatic val createReceivedMessageHashValuesTableCommand = "CREATE TABLE $receivedMessageHashValuesCache ($userID TEXT PRIMARY KEY, $receivedMessageHashValues TEXT);"
|
|
|
|
|
// Group chat auth token cache
|
|
|
|
|
private val groupChatAuthTokenCache = "loki_api_group_chat_auth_token_database"
|
|
|
|
|
@JvmStatic val createReceivedMessageHashValuesCacheCommand = "CREATE TABLE $receivedMessageHashValuesCache ($userID TEXT PRIMARY KEY, $receivedMessageHashValues TEXT);"
|
|
|
|
|
// Open group auth token cache
|
|
|
|
|
private val openGroupAuthTokenCache = "loki_api_group_chat_auth_token_database"
|
|
|
|
|
private val server = "server"
|
|
|
|
|
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
|
|
|
|
|
private val lastMessageServerIDCache = "loki_api_last_message_server_id_cache"
|
|
|
|
|
private val lastMessageServerIDCacheIndex = "loki_api_last_message_server_id_cache_index"
|
|
|
|
|
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
|
|
|
|
|
private val lastDeletionServerIDCache = "loki_api_last_deletion_server_id_cache"
|
|
|
|
|
private val lastDeletionServerIDCacheIndex = "loki_api_last_deletion_server_id_cache_index"
|
|
|
|
|
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
|
|
|
|
|
private val deviceLinkCache = "loki_pairing_authorisation_cache"
|
|
|
|
|
private val masterHexEncodedPublicKey = "primary_device"
|
|
|
|
|
private val slaveHexEncodedPublicKey = "secondary_device"
|
|
|
|
|
private val requestSignature = "request_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));"
|
|
|
|
|
// User count cache
|
|
|
|
|
private val userCountCache = "loki_user_count_cache"
|
|
|
|
|
private val publicChatID = "public_chat_id"
|
|
|
|
|
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
|
|
|
|
|
private val sessionRequestTimestampCache = "session_request_timestamp_cache"
|
|
|
|
|
private val publicKey = "public_key"
|
|
|
|
|
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> {
|
|
|
|
|
val database = databaseHelper.readableDatabase
|
|
|
|
|
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 ->
|
|
|
|
|
val components = snodeAsString.split("-")
|
|
|
|
|
val address = components[0]
|
|
|
|
@ -102,14 +102,14 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
|
|
|
|
|
}
|
|
|
|
|
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"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun getPaths(): List<List<LokiAPITarget>> {
|
|
|
|
|
override fun getOnionRequestPaths(): List<List<LokiAPITarget>> {
|
|
|
|
|
val database = databaseHelper.readableDatabase
|
|
|
|
|
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 components = snodeAsString.split("-")
|
|
|
|
|
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 ) )
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun clearPaths() {
|
|
|
|
|
fun clearOnionRequestPaths() {
|
|
|
|
|
val database = databaseHelper.writableDatabase
|
|
|
|
|
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-2"); delete("1-0")
|
|
|
|
|
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.
|
|
|
|
|
if (newValue.count() != 2) { return }
|
|
|
|
|
val path0 = newValue[0]
|
|
|
|
@ -154,14 +154,14 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
|
|
|
|
|
snodeAsString += "-${keySet.ed25519Key}-${keySet.x25519Key}"
|
|
|
|
|
}
|
|
|
|
|
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-2", path0[2]); set("1-0", path1[0])
|
|
|
|
|
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
|
|
|
|
|
return database.get(swarmCache, "${Companion.hexEncodedPublicKey} = ?", wrap(hexEncodedPublicKey)) { cursor ->
|
|
|
|
|
val swarmAsString = cursor.getString(cursor.getColumnIndexOrThrow(swarm))
|
|
|
|
@ -176,7 +176,7 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
|
|
|
|
|
}?.toSet()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun setSwarmCache(hexEncodedPublicKey: String, newValue: Set<LokiAPITarget>) {
|
|
|
|
|
override fun setSwarm(hexEncodedPublicKey: String, newValue: Set<LokiAPITarget>) {
|
|
|
|
|
val database = databaseHelper.writableDatabase
|
|
|
|
|
val swarmAsString = newValue.joinToString(", ") { target ->
|
|
|
|
|
var string = "${target.address}-${target.port}"
|
|
|
|
@ -220,7 +220,7 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
|
|
|
|
|
|
|
|
|
|
override fun getAuthToken(server: String): String? {
|
|
|
|
|
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))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -229,9 +229,9 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
|
|
|
|
|
val database = databaseHelper.writableDatabase
|
|
|
|
|
if (newValue != null) {
|
|
|
|
|
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 {
|
|
|
|
|
database.delete(groupChatAuthTokenCache, "${Companion.server} = ?", wrap(server))
|
|
|
|
|
database.delete(openGroupAuthTokenCache, "${Companion.server} = ?", wrap(server))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|