150 lines
8.1 KiB
Kotlin
Raw Normal View History

2019-06-04 09:35:18 +10:00
package org.thoughtcrime.securesms.loki
2019-06-04 11:05:03 +10:00
import android.content.ContentValues
2019-06-04 09:35:18 +10:00
import android.content.Context
import org.thoughtcrime.securesms.database.Database
import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper
2019-06-11 09:46:42 +10:00
import org.thoughtcrime.securesms.util.TextSecurePreferences
2019-06-04 09:35:18 +10:00
import org.whispersystems.signalservice.loki.api.LokiAPIDatabaseProtocol
import org.whispersystems.signalservice.loki.api.LokiAPITarget
2019-06-11 09:46:42 +10:00
class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper), LokiAPIDatabaseProtocol {
private val userPublicKey get() = TextSecurePreferences.getLocalNumber(context)
2019-06-04 09:35:18 +10:00
companion object {
2019-06-04 11:05:03 +10:00
// Swarm cache
private val swarmCache = "loki_api_swarm_cache"
private val hexEncodedPublicKey = "hex_encoded_public_key"
private val swarm = "swarm"
2019-06-04 14:05:42 +10:00
@JvmStatic val createSwarmCacheTableCommand = "CREATE TABLE $swarmCache ($hexEncodedPublicKey TEXT PRIMARY KEY, $swarm TEXT);"
2019-06-04 11:05:03 +10: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 14:05:42 +10:00
@JvmStatic val createLastMessageHashValueTableCommand = "CREATE TABLE $lastMessageHashValueCache ($target TEXT PRIMARY KEY, $lastMessageHashValue TEXT);"
2019-06-04 11:05:03 +10: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 14:05:42 +10:00
@JvmStatic val createReceivedMessageHashValuesTableCommand = "CREATE TABLE $receivedMessageHashValuesCache ($userID TEXT PRIMARY KEY, $receivedMessageHashValues TEXT);"
2019-08-23 16:57:26 +10:00
// Group chat auth token cache
private val groupChatAuthTokenTable = "loki_api_group_chat_auth_token_database"
private val serverURL = "server_url"
private val token = "token"
@JvmStatic val createGroupChatAuthTokenTableCommand = "CREATE TABLE $groupChatAuthTokenTable ($serverURL TEXT PRIMARY KEY, $token TEXT);"
2019-08-26 13:55:45 +10:00
// Last message server ID cache
private val lastMessageServerIDCache = "loki_api_last_message_server_id_cache"
private val lastMessageServerIDCacheGroupID = "group_id"
private val lastMessageServerID = "last_message_server_id"
@JvmStatic val createLastMessageServerIDTableCommand = "CREATE TABLE $lastMessageServerIDCache ($lastMessageServerIDCacheGroupID INTEGER PRIMARY KEY, $lastMessageServerID INTEGER);"
// First message server ID cache
private val firstMessageServerIDCache = "loki_api_first_message_server_id_cache"
private val firstMessageServerIDCacheGroupID = "group_id"
private val firstMessageServerID = "first_message_server_id"
@JvmStatic val createFirstMessageServerIDTableCommand = "CREATE TABLE $firstMessageServerIDCache ($firstMessageServerIDCacheGroupID INTEGER PRIMARY KEY, $firstMessageServerID INTEGER);"
2019-06-04 09:35:18 +10:00
}
2019-07-15 13:19:58 +10:00
override fun getSwarmCache(hexEncodedPublicKey: String): Set<LokiAPITarget>? {
2019-06-04 12:12:40 +10:00
val database = databaseHelper.readableDatabase
return database.get(swarmCache, "${Companion.hexEncodedPublicKey} = ?", wrap(hexEncodedPublicKey)) { cursor ->
2019-06-04 11:05:03 +10:00
val swarmAsString = cursor.getString(cursor.getColumnIndexOrThrow(swarm))
2019-06-19 15:45:40 +10:00
swarmAsString.split(", ").map { targetAsString ->
val components = targetAsString.split("?port=")
LokiAPITarget(components[0], components[1].toInt())
}
2019-07-15 13:19:58 +10:00
}?.toSet()
2019-06-04 09:35:18 +10:00
}
2019-07-15 13:19:58 +10:00
override fun setSwarmCache(hexEncodedPublicKey: String, newValue: Set<LokiAPITarget>) {
2019-06-04 11:05:03 +10:00
val database = databaseHelper.writableDatabase
2019-06-19 15:45:40 +10:00
val swarmAsString = newValue.joinToString(", ") { target ->
"${target.address}?port=${target.port}"
}
2019-06-19 15:45:40 +10:00
val row = wrap(mapOf( Companion.hexEncodedPublicKey to hexEncodedPublicKey, swarm to swarmAsString ))
database.insertOrUpdate(swarmCache, row, "${Companion.hexEncodedPublicKey} = ?", wrap(hexEncodedPublicKey))
2019-06-04 09:35:18 +10:00
}
override fun getLastMessageHashValue(target: LokiAPITarget): String? {
2019-06-04 12:12:40 +10:00
val database = databaseHelper.readableDatabase
return database.get(lastMessageHashValueCache, "${Companion.target} = ?", wrap(target.address)) { cursor ->
2019-06-04 11:05:03 +10:00
cursor.getString(cursor.getColumnIndexOrThrow(lastMessageHashValue))
}
2019-06-04 09:35:18 +10:00
}
override fun setLastMessageHashValue(target: LokiAPITarget, newValue: String) {
2019-06-04 11:05:03 +10:00
val database = databaseHelper.writableDatabase
2019-06-19 15:45:40 +10: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 09:35:18 +10:00
}
override fun getReceivedMessageHashValues(): Set<String>? {
2019-06-04 12:12:40 +10:00
val database = databaseHelper.readableDatabase
return database.get(receivedMessageHashValuesCache, "$userID = ?", wrap(userPublicKey)) { cursor ->
2019-06-04 11:05:03 +10:00
val receivedMessageHashValuesAsString = cursor.getString(cursor.getColumnIndexOrThrow(receivedMessageHashValues))
2019-06-19 15:45:40 +10:00
receivedMessageHashValuesAsString.split(", ").toSet()
2019-06-04 11:05:03 +10:00
}
2019-06-04 09:35:18 +10:00
}
override fun setReceivedMessageHashValues(newValue: Set<String>) {
2019-06-04 11:05:03 +10:00
val database = databaseHelper.writableDatabase
2019-06-19 15:45:40 +10: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 09:35:18 +10:00
}
2019-08-23 16:57:26 +10:00
override fun getGroupChatAuthToken(serverURL: String): String? {
val database = databaseHelper.readableDatabase
return database.get(groupChatAuthTokenTable, "${Companion.serverURL} = ?", wrap(serverURL)) { cursor ->
cursor.getString(cursor.getColumnIndexOrThrow(token))
}
}
2019-08-26 13:55:45 +10:00
override fun setGroupChatAuthToken(serverURL: String, newValue: String?) {
2019-08-23 16:57:26 +10:00
val database = databaseHelper.writableDatabase
2019-08-26 13:55:45 +10:00
if (newValue != null) {
val row = wrap(mapOf(Companion.serverURL to serverURL, token to newValue!!))
2019-08-23 16:57:26 +10:00
database.insertOrUpdate(groupChatAuthTokenTable, row, "${Companion.serverURL} = ?", wrap(serverURL))
} else {
database.delete(groupChatAuthTokenTable, "${Companion.serverURL} = ?", wrap(serverURL))
}
}
2019-08-26 13:55:45 +10:00
override fun getLastMessageServerID(groupID: Long): Long? {
val database = databaseHelper.readableDatabase
return database.get(lastMessageServerIDCache, "$lastMessageServerIDCacheGroupID = ?", wrap(groupID.toString())) { cursor ->
cursor.getInt(lastMessageServerID)
}?.toLong()
}
override fun setLastMessageServerID(groupID: Long, newValue: Long) {
val database = databaseHelper.writableDatabase
val row = wrap(mapOf( lastMessageServerIDCacheGroupID to groupID.toString(), lastMessageServerID to newValue.toString() ))
database.insertOrUpdate(lastMessageServerIDCache, row, "$lastMessageServerIDCacheGroupID = ?", wrap(groupID.toString()))
}
override fun getFirstMessageServerID(groupID: Long): Long? {
val database = databaseHelper.readableDatabase
return database.get(firstMessageServerIDCache, "$firstMessageServerIDCacheGroupID = ?", wrap(groupID.toString())) { cursor ->
cursor.getInt(firstMessageServerID)
}?.toLong()
}
override fun setFirstMessageServerID(groupID: Long, newValue: Long) {
val database = databaseHelper.writableDatabase
val row = wrap(mapOf( firstMessageServerIDCacheGroupID to groupID.toString(), firstMessageServerID to newValue.toString() ))
database.insertOrUpdate(firstMessageServerIDCache, row, "$firstMessageServerIDCacheGroupID = ?", wrap(groupID.toString()))
}
2019-06-04 11:05:03 +10:00
}
2019-06-04 12:12:40 +10:00
// region Convenience
2019-06-04 11:05:03 +10: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