package org.thoughtcrime.securesms.loki import android.content.ContentValues import android.content.Context import org.thoughtcrime.securesms.database.Database import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper import org.thoughtcrime.securesms.util.TextSecurePreferences import org.whispersystems.signalservice.loki.api.LokiAPIDatabaseProtocol import org.whispersystems.signalservice.loki.api.LokiAPITarget // TODO: Clean this up a bit class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper), LokiAPIDatabaseProtocol { private val userPublicKey get() = TextSecurePreferences.getLocalNumber(context) companion object { // Swarm cache private val swarmCache = "loki_api_swarm_cache" private val hexEncodedPublicKey = "hex_encoded_public_key" private val swarm = "swarm" @JvmStatic val createSwarmCacheTableCommand = "CREATE TABLE $swarmCache ($hexEncodedPublicKey TEXT PRIMARY KEY, $swarm TEXT);" // 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" @JvmStatic val createLastMessageHashValueTableCommand = "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 groupChatAuthTokenTable = "loki_api_group_chat_auth_token_database" private val server = "server" private val token = "token" @JvmStatic val createGroupChatAuthTokenTableCommand = "CREATE TABLE $groupChatAuthTokenTable ($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);" // 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);" // Moderation permission cache private val moderationPermissionCache = "loki_api_moderation_permission_cache" private val moderationPermissionCacheIndex = "loki_api_moderation_permission_cache_index" private val isModerator = "is_moderator" @JvmStatic val createModerationPermissionTableCommand = "CREATE TABLE $moderationPermissionCache ($moderationPermissionCacheIndex STRING PRIMARY KEY, $isModerator INTEGER DEFAULT 0);" } override fun getSwarmCache(hexEncodedPublicKey: String): Set? { val database = databaseHelper.readableDatabase return database.get(swarmCache, "${Companion.hexEncodedPublicKey} = ?", wrap(hexEncodedPublicKey)) { cursor -> val swarmAsString = cursor.getString(cursor.getColumnIndexOrThrow(swarm)) swarmAsString.split(", ").map { targetAsString -> val components = targetAsString.split("?port=") LokiAPITarget(components[0], components[1].toInt()) } }?.toSet() } override fun setSwarmCache(hexEncodedPublicKey: String, newValue: Set) { val database = databaseHelper.writableDatabase val swarmAsString = newValue.joinToString(", ") { target -> "${target.address}?port=${target.port}" } val row = wrap(mapOf( Companion.hexEncodedPublicKey to hexEncodedPublicKey, swarm to swarmAsString )) database.insertOrUpdate(swarmCache, row, "${Companion.hexEncodedPublicKey} = ?", wrap(hexEncodedPublicKey)) } override fun getLastMessageHashValue(target: LokiAPITarget): String? { val database = databaseHelper.readableDatabase return database.get(lastMessageHashValueCache, "${Companion.target} = ?", wrap(target.address)) { cursor -> cursor.getString(cursor.getColumnIndexOrThrow(lastMessageHashValue)) } } override fun setLastMessageHashValue(target: LokiAPITarget, newValue: String) { val database = databaseHelper.writableDatabase val row = wrap(mapOf( Companion.target to target.address, lastMessageHashValue to newValue )) database.insertOrUpdate(lastMessageHashValueCache, row, "${Companion.target} = ?", wrap(target.address)) } override fun getReceivedMessageHashValues(): Set? { val database = databaseHelper.readableDatabase return database.get(receivedMessageHashValuesCache, "$userID = ?", wrap(userPublicKey)) { cursor -> val receivedMessageHashValuesAsString = cursor.getString(cursor.getColumnIndexOrThrow(receivedMessageHashValues)) receivedMessageHashValuesAsString.split(", ").toSet() } } override fun setReceivedMessageHashValues(newValue: Set) { val database = databaseHelper.writableDatabase val receivedMessageHashValuesAsString = newValue.joinToString(", ") val row = wrap(mapOf( userID to userPublicKey, receivedMessageHashValues to receivedMessageHashValuesAsString )) database.insertOrUpdate(receivedMessageHashValuesCache, row, "$userID = ?", wrap(userPublicKey)) } override fun getGroupChatAuthToken(server: String): String? { val database = databaseHelper.readableDatabase return database.get(groupChatAuthTokenTable, "${Companion.server} = ?", wrap(server)) { cursor -> cursor.getString(cursor.getColumnIndexOrThrow(token)) } } override fun setGroupChatAuthToken(server: String, newValue: String?) { val database = databaseHelper.writableDatabase if (newValue != null) { val row = wrap(mapOf(Companion.server to server, token to newValue)) database.insertOrUpdate(groupChatAuthTokenTable, row, "${Companion.server} = ?", wrap(server)) } else { database.delete(groupChatAuthTokenTable, "${Companion.server} = ?", wrap(server)) } } override fun getLastMessageServerID(group: Long, server: String): Long? { val database = databaseHelper.readableDatabase val index = "$server.$group" return database.get(lastMessageServerIDCache, "$lastMessageServerIDCacheIndex = ?", wrap(index)) { cursor -> cursor.getInt(lastMessageServerID) }?.toLong() } override fun setLastMessageServerID(group: Long, server: String, newValue: Long) { val database = databaseHelper.writableDatabase val index = "$server.$group" val row = wrap(mapOf( lastMessageServerIDCacheIndex to index, lastMessageServerID to newValue.toString() )) database.insertOrUpdate(lastMessageServerIDCache, row, "$lastMessageServerIDCacheIndex = ?", wrap(index)) } override fun getLastDeletionServerID(group: Long, server: String): Long? { val database = databaseHelper.readableDatabase val index = "$server.$group" return database.get(lastDeletionServerIDCache, "$lastDeletionServerIDCacheIndex = ?", wrap(index)) { cursor -> cursor.getInt(lastDeletionServerID) }?.toLong() } override fun setLastDeletionServerID(group: Long, server: String, newValue: Long) { val database = databaseHelper.writableDatabase val index = "$server.$group" val row = wrap(mapOf( lastDeletionServerIDCacheIndex to index, lastDeletionServerID to newValue.toString() )) database.insertOrUpdate(lastDeletionServerIDCache, row, "$lastDeletionServerIDCacheIndex = ?", wrap(index)) } fun isModerator(group: Long, server: String): Boolean { val database = databaseHelper.readableDatabase val index = "$server.$group" return database.get(moderationPermissionCache, "$moderationPermissionCacheIndex = ?", wrap(index)) { cursor -> cursor.getInt(isModerator) } == 1 } fun setIsModerator(group: Long, server: String, newValue: Boolean) { val database = databaseHelper.writableDatabase val index = "$server.$group" val row = wrap(mapOf( moderationPermissionCacheIndex to index, isModerator to (if (newValue) 1 else 0).toString() )) database.insertOrUpdate(moderationPermissionCache, row, "$moderationPermissionCacheIndex = ?", wrap(index)) } } // region Convenience private inline fun wrap(x: T): Array { return Array(1) { x } } private fun wrap(x: Map): ContentValues { val result = ContentValues(x.size) x.forEach { result.put(it.key, it.value) } return result } // endregion