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
|
2019-06-04 11:05:03 +10:00
|
|
|
import android.database.Cursor
|
2019-06-04 09:35:18 +10:00
|
|
|
import org.thoughtcrime.securesms.database.Database
|
|
|
|
import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper
|
|
|
|
import org.whispersystems.signalservice.loki.api.LokiAPIDatabaseProtocol
|
|
|
|
import org.whispersystems.signalservice.loki.api.LokiAPITarget
|
|
|
|
|
2019-06-04 11:07:20 +10:00
|
|
|
class LokiAPIDatabase(private val userPublicKey: String, context: Context, helper: SQLCipherOpenHelper) : Database(context, helper), LokiAPIDatabaseProtocol {
|
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"
|
|
|
|
@JvmStatic val createSwarmCacheTableCommand = "CREATE TABLE $swarmCache ($hexEncodedPublicKey TEXT, $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, $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, $receivedMessageHashValues TEXT);"
|
2019-06-04 09:35:18 +10:00
|
|
|
}
|
|
|
|
|
2019-06-04 11:05:03 +10:00
|
|
|
override fun getSwarmCache(hexEncodedPublicKey: String): List<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-04 11:23:44 +10:00
|
|
|
swarmAsString.split(",").map { targetAsString ->
|
|
|
|
val components = targetAsString.split("?port=")
|
|
|
|
LokiAPITarget(components[0], components[1].toInt())
|
|
|
|
}
|
2019-06-04 11:05:03 +10:00
|
|
|
}
|
2019-06-04 09:35:18 +10:00
|
|
|
}
|
|
|
|
|
2019-06-04 11:05:03 +10:00
|
|
|
override fun setSwarmCache(hexEncodedPublicKey: String, newValue: List<LokiAPITarget>) {
|
|
|
|
val database = databaseHelper.writableDatabase
|
2019-06-04 11:23:44 +10:00
|
|
|
val swarmAsString = newValue.joinToString(",") { target ->
|
|
|
|
"${target.address}?port=${target.port}"
|
|
|
|
}
|
2019-06-04 11:05:03 +10:00
|
|
|
database.update(swarmCache, wrap(mapOf( swarm to swarmAsString )), "${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
|
|
|
|
database.update(lastMessageHashValueCache, wrap(mapOf( lastMessageHashValue to newValue )), "${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))
|
|
|
|
receivedMessageHashValuesAsString.split(",").toSet()
|
|
|
|
}
|
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
|
|
|
|
val receivedMessageHashValuesAsString = newValue.joinToString(",")
|
2019-06-04 11:07:20 +10:00
|
|
|
database.update(receivedMessageHashValuesCache, wrap(mapOf( receivedMessageHashValues to receivedMessageHashValuesAsString )), "$userID = ?", wrap(userPublicKey))
|
2019-06-04 09:35:18 +10:00
|
|
|
}
|
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
|