session-android/src/org/thoughtcrime/securesms/loki/LokiAPIDatabase.kt

192 lines
11 KiB
Kotlin
Raw Normal View History

2019-06-04 01:35:18 +02:00
package org.thoughtcrime.securesms.loki
2019-06-04 03:05:03 +02:00
import android.content.ContentValues
2019-06-04 01:35:18 +02:00
import android.content.Context
import org.thoughtcrime.securesms.database.Database
import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper
import org.thoughtcrime.securesms.util.Base64
2019-06-11 01:46:42 +02:00
import org.thoughtcrime.securesms.util.TextSecurePreferences
2019-06-04 01:35:18 +02:00
import org.whispersystems.signalservice.loki.api.LokiAPIDatabaseProtocol
import org.whispersystems.signalservice.loki.api.LokiAPITarget
2019-10-07 06:30:20 +02:00
import org.whispersystems.signalservice.loki.api.PairingAuthorisation
2019-06-04 01:35:18 +02:00
2019-09-04 02:02:42 +02:00
// TODO: Clean this up a bit
2019-06-11 01:46:42 +02:00
class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper), LokiAPIDatabaseProtocol {
private val userPublicKey get() = TextSecurePreferences.getLocalNumber(context)
2019-06-04 01:35:18 +02:00
companion object {
2019-06-04 03:05:03 +02:00
// Swarm cache
private val swarmCache = "loki_api_swarm_cache"
private val hexEncodedPublicKey = "hex_encoded_public_key"
private val swarm = "swarm"
2019-06-04 06:05:42 +02:00
@JvmStatic val createSwarmCacheTableCommand = "CREATE TABLE $swarmCache ($hexEncodedPublicKey TEXT PRIMARY KEY, $swarm TEXT);"
2019-06-04 03:05:03 +02:00
// Last message hash value cache
private val lastMessageHashValueCache = "loki_api_last_message_hash_value_cache"
private val target = "target"
private val lastMessageHashValue = "last_message_hash_value"
2019-06-04 06:05:42 +02:00
@JvmStatic val createLastMessageHashValueTableCommand = "CREATE TABLE $lastMessageHashValueCache ($target TEXT PRIMARY KEY, $lastMessageHashValue TEXT);"
2019-06-04 03:05:03 +02:00
// 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"
2019-06-04 06:05:42 +02:00
@JvmStatic val createReceivedMessageHashValuesTableCommand = "CREATE TABLE $receivedMessageHashValuesCache ($userID TEXT PRIMARY KEY, $receivedMessageHashValues TEXT);"
2019-08-23 08:57:26 +02:00
// Group chat auth token cache
private val groupChatAuthTokenTable = "loki_api_group_chat_auth_token_database"
2019-08-28 04:09:05 +02:00
private val server = "server"
2019-08-23 08:57:26 +02:00
private val token = "token"
2019-08-28 04:09:05 +02:00
@JvmStatic val createGroupChatAuthTokenTableCommand = "CREATE TABLE $groupChatAuthTokenTable ($server TEXT PRIMARY KEY, $token TEXT);"
2019-08-26 05:55:45 +02:00
// Last message server ID cache
private val lastMessageServerIDCache = "loki_api_last_message_server_id_cache"
2019-08-28 04:09:05 +02:00
private val lastMessageServerIDCacheIndex = "loki_api_last_message_server_id_cache_index"
2019-08-26 05:55:45 +02:00
private val lastMessageServerID = "last_message_server_id"
2019-08-28 04:09:05 +02:00
@JvmStatic val createLastMessageServerIDTableCommand = "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);"
2019-10-07 07:15:06 +02:00
// Pairing authorisation cache
private val pairingAuthorisationCache = "loki_pairing_authorisation_cache"
private val primaryDevicePublicKey = "primary_device"
private val secondaryDevicePublicKey = "secondary_device"
private val requestSignature = "request_signature"
private val grantSignature = "grant_signature"
2019-10-07 07:15:06 +02:00
@JvmStatic val createPairingAuthorisationTableCommand = "CREATE TABLE $pairingAuthorisationCache ($primaryDevicePublicKey TEXT, $secondaryDevicePublicKey TEXT, " +
"$requestSignature TEXT NULLABLE DEFAULT NULL, $grantSignature TEXT NULLABLE DEFAULT NULL, PRIMARY KEY ($primaryDevicePublicKey, $secondaryDevicePublicKey));"
2019-06-04 01:35:18 +02:00
}
2019-07-15 05:19:58 +02:00
override fun getSwarmCache(hexEncodedPublicKey: String): Set<LokiAPITarget>? {
2019-06-04 04:12:40 +02:00
val database = databaseHelper.readableDatabase
return database.get(swarmCache, "${Companion.hexEncodedPublicKey} = ?", wrap(hexEncodedPublicKey)) { cursor ->
2019-06-04 03:05:03 +02:00
val swarmAsString = cursor.getString(cursor.getColumnIndexOrThrow(swarm))
2019-06-19 07:45:40 +02:00
swarmAsString.split(", ").map { targetAsString ->
val components = targetAsString.split("?port=")
LokiAPITarget(components[0], components[1].toInt())
}
2019-07-15 05:19:58 +02:00
}?.toSet()
2019-06-04 01:35:18 +02:00
}
2019-07-15 05:19:58 +02:00
override fun setSwarmCache(hexEncodedPublicKey: String, newValue: Set<LokiAPITarget>) {
2019-06-04 03:05:03 +02:00
val database = databaseHelper.writableDatabase
2019-06-19 07:45:40 +02:00
val swarmAsString = newValue.joinToString(", ") { target ->
"${target.address}?port=${target.port}"
}
2019-06-19 07:45:40 +02:00
val row = wrap(mapOf( Companion.hexEncodedPublicKey to hexEncodedPublicKey, swarm to swarmAsString ))
database.insertOrUpdate(swarmCache, row, "${Companion.hexEncodedPublicKey} = ?", wrap(hexEncodedPublicKey))
2019-06-04 01:35:18 +02:00
}
override fun getLastMessageHashValue(target: LokiAPITarget): String? {
2019-06-04 04:12:40 +02:00
val database = databaseHelper.readableDatabase
return database.get(lastMessageHashValueCache, "${Companion.target} = ?", wrap(target.address)) { cursor ->
2019-06-04 03:05:03 +02:00
cursor.getString(cursor.getColumnIndexOrThrow(lastMessageHashValue))
}
2019-06-04 01:35:18 +02:00
}
override fun setLastMessageHashValue(target: LokiAPITarget, newValue: String) {
2019-06-04 03:05:03 +02:00
val database = databaseHelper.writableDatabase
2019-06-19 07:45:40 +02:00
val row = wrap(mapOf( Companion.target to target.address, lastMessageHashValue to newValue ))
database.insertOrUpdate(lastMessageHashValueCache, row, "${Companion.target} = ?", wrap(target.address))
2019-06-04 01:35:18 +02:00
}
override fun getReceivedMessageHashValues(): Set<String>? {
2019-06-04 04:12:40 +02:00
val database = databaseHelper.readableDatabase
return database.get(receivedMessageHashValuesCache, "$userID = ?", wrap(userPublicKey)) { cursor ->
2019-06-04 03:05:03 +02:00
val receivedMessageHashValuesAsString = cursor.getString(cursor.getColumnIndexOrThrow(receivedMessageHashValues))
2019-06-19 07:45:40 +02:00
receivedMessageHashValuesAsString.split(", ").toSet()
2019-06-04 03:05:03 +02:00
}
2019-06-04 01:35:18 +02:00
}
override fun setReceivedMessageHashValues(newValue: Set<String>) {
2019-06-04 03:05:03 +02:00
val database = databaseHelper.writableDatabase
2019-06-19 07:45:40 +02:00
val receivedMessageHashValuesAsString = newValue.joinToString(", ")
val row = wrap(mapOf( userID to userPublicKey, receivedMessageHashValues to receivedMessageHashValuesAsString ))
database.insertOrUpdate(receivedMessageHashValuesCache, row, "$userID = ?", wrap(userPublicKey))
2019-06-04 01:35:18 +02:00
}
2019-08-23 08:57:26 +02:00
2019-10-07 06:30:20 +02:00
override fun getAuthToken(server: String): String? {
2019-08-23 08:57:26 +02:00
val database = databaseHelper.readableDatabase
2019-08-28 04:09:05 +02:00
return database.get(groupChatAuthTokenTable, "${Companion.server} = ?", wrap(server)) { cursor ->
2019-08-23 08:57:26 +02:00
cursor.getString(cursor.getColumnIndexOrThrow(token))
}
}
2019-10-07 06:30:20 +02:00
override fun setAuthToken(server: String, newValue: String?) {
2019-08-23 08:57:26 +02:00
val database = databaseHelper.writableDatabase
2019-08-26 05:55:45 +02:00
if (newValue != null) {
2019-08-28 04:09:05 +02:00
val row = wrap(mapOf(Companion.server to server, token to newValue))
database.insertOrUpdate(groupChatAuthTokenTable, row, "${Companion.server} = ?", wrap(server))
2019-08-23 08:57:26 +02:00
} else {
2019-08-28 04:09:05 +02:00
database.delete(groupChatAuthTokenTable, "${Companion.server} = ?", wrap(server))
2019-08-23 08:57:26 +02:00
}
}
2019-08-26 05:55:45 +02:00
2019-08-28 04:09:05 +02:00
override fun getLastMessageServerID(group: Long, server: String): Long? {
2019-08-26 05:55:45 +02:00
val database = databaseHelper.readableDatabase
2019-08-28 04:09:05 +02:00
val index = "$server.$group"
return database.get(lastMessageServerIDCache, "$lastMessageServerIDCacheIndex = ?", wrap(index)) { cursor ->
2019-08-26 05:55:45 +02:00
cursor.getInt(lastMessageServerID)
}?.toLong()
}
2019-08-28 04:09:05 +02:00
override fun setLastMessageServerID(group: Long, server: String, newValue: Long) {
2019-08-26 05:55:45 +02:00
val database = databaseHelper.writableDatabase
2019-08-28 04:09:05 +02:00
val index = "$server.$group"
val row = wrap(mapOf( lastMessageServerIDCacheIndex to index, lastMessageServerID to newValue.toString() ))
database.insertOrUpdate(lastMessageServerIDCache, row, "$lastMessageServerIDCacheIndex = ?", wrap(index))
2019-08-26 05:55:45 +02:00
}
override fun getLastDeletionServerID(group: Long, server: String): Long? {
2019-08-26 05:55:45 +02:00
val database = databaseHelper.readableDatabase
2019-08-28 04:09:05 +02:00
val index = "$server.$group"
return database.get(lastDeletionServerIDCache, "$lastDeletionServerIDCacheIndex = ?", wrap(index)) { cursor ->
cursor.getInt(lastDeletionServerID)
2019-08-26 05:55:45 +02:00
}?.toLong()
}
override fun setLastDeletionServerID(group: Long, server: String, newValue: Long) {
2019-08-26 05:55:45 +02:00
val database = databaseHelper.writableDatabase
2019-08-28 04:09:05 +02:00
val index = "$server.$group"
val row = wrap(mapOf( lastDeletionServerIDCacheIndex to index, lastDeletionServerID to newValue.toString() ))
database.insertOrUpdate(lastDeletionServerIDCache, row, "$lastDeletionServerIDCacheIndex = ?", wrap(index))
2019-08-26 05:55:45 +02:00
}
2019-10-07 06:30:20 +02:00
override fun getPairingAuthorisations(hexEncodedPublicKey: String): List<PairingAuthorisation> {
val database = databaseHelper.readableDatabase
2019-10-07 07:15:06 +02:00
return database.getAll(pairingAuthorisationCache, "$primaryDevicePublicKey = ? OR $secondaryDevicePublicKey = ?", arrayOf( hexEncodedPublicKey, hexEncodedPublicKey )) { cursor ->
val primaryDevicePubKey = cursor.getString(primaryDevicePublicKey)
val secondaryDevicePubKey = cursor.getString(secondaryDevicePublicKey)
val requestSignature: ByteArray? = if (cursor.isNull(cursor.getColumnIndexOrThrow(requestSignature))) null else cursor.getBase64EncodedData(requestSignature)
val grantSignature: ByteArray? = if (cursor.isNull(cursor.getColumnIndexOrThrow(grantSignature))) null else cursor.getBase64EncodedData(grantSignature)
2019-10-07 06:30:20 +02:00
PairingAuthorisation(primaryDevicePubKey, secondaryDevicePubKey, requestSignature, grantSignature)
}
}
2019-10-07 06:30:20 +02:00
override fun insertOrUpdatePairingAuthorisation(authorisation: PairingAuthorisation) {
val database = databaseHelper.writableDatabase
val values = ContentValues()
2019-10-07 07:15:06 +02:00
values.put(primaryDevicePublicKey, authorisation.primaryDevicePublicKey)
values.put(secondaryDevicePublicKey, authorisation.secondaryDevicePublicKey)
if (authorisation.requestSignature != null) { values.put(requestSignature, Base64.encodeBytes(authorisation.requestSignature)) }
if (authorisation.grantSignature != null) { values.put(grantSignature, Base64.encodeBytes(authorisation.grantSignature)) }
2019-10-07 07:15:06 +02:00
database.insertOrUpdate(pairingAuthorisationCache, values, "$primaryDevicePublicKey = ? AND $secondaryDevicePublicKey = ?", arrayOf( authorisation.primaryDevicePublicKey, authorisation.secondaryDevicePublicKey ))
}
2019-10-07 06:30:20 +02:00
override fun removePairingAuthorisations(hexEncodedPublicKey: String) {
val database = databaseHelper.readableDatabase
2019-10-07 07:15:06 +02:00
database.delete(pairingAuthorisationCache, "$primaryDevicePublicKey = ? OR $secondaryDevicePublicKey = ?", arrayOf( hexEncodedPublicKey, hexEncodedPublicKey ))
}
2019-06-04 03:05:03 +02:00
}
2019-06-04 04:12:40 +02:00
// region Convenience
2019-06-04 03:05:03 +02:00
private inline fun <reified T> wrap(x: T): Array<T> {
return Array(1) { x }
}
private fun wrap(x: Map<String, String>): ContentValues {
val result = ContentValues(x.size)
x.forEach { result.put(it.key, it.value) }
return result
}
// endregion