diff --git a/src/org/thoughtcrime/securesms/loki/LokiAPIDatabase.kt b/src/org/thoughtcrime/securesms/loki/LokiAPIDatabase.kt index 9c847adc16..37cafa6b4b 100644 --- a/src/org/thoughtcrime/securesms/loki/LokiAPIDatabase.kt +++ b/src/org/thoughtcrime/securesms/loki/LokiAPIDatabase.kt @@ -29,7 +29,8 @@ class LokiAPIDatabase(private val userPublicKey: String, context: Context, helpe } override fun getSwarmCache(hexEncodedPublicKey: String): List? { - return get(swarmCache, "${Companion.hexEncodedPublicKey} = ?", wrap(hexEncodedPublicKey)) { cursor -> + 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=") @@ -47,7 +48,8 @@ class LokiAPIDatabase(private val userPublicKey: String, context: Context, helpe } override fun getLastMessageHashValue(target: LokiAPITarget): String? { - return get(lastMessageHashValueCache, "${Companion.target} = ?", wrap(target.address)) { cursor -> + val database = databaseHelper.readableDatabase + return database.get(lastMessageHashValueCache, "${Companion.target} = ?", wrap(target.address)) { cursor -> cursor.getString(cursor.getColumnIndexOrThrow(lastMessageHashValue)) } } @@ -58,7 +60,8 @@ class LokiAPIDatabase(private val userPublicKey: String, context: Context, helpe } override fun getReceivedMessageHashValues(): Set? { - return get(receivedMessageHashValuesCache, "$userID = ?", wrap(userPublicKey)) { cursor -> + val database = databaseHelper.readableDatabase + return database.get(receivedMessageHashValuesCache, "$userID = ?", wrap(userPublicKey)) { cursor -> val receivedMessageHashValuesAsString = cursor.getString(cursor.getColumnIndexOrThrow(receivedMessageHashValues)) receivedMessageHashValuesAsString.split(",").toSet() } @@ -69,23 +72,10 @@ class LokiAPIDatabase(private val userPublicKey: String, context: Context, helpe val receivedMessageHashValuesAsString = newValue.joinToString(",") database.update(receivedMessageHashValuesCache, wrap(mapOf( receivedMessageHashValues to receivedMessageHashValuesAsString )), "$userID = ?", wrap(userPublicKey)) } - - // region Convenience - private fun get(table: String, query: String, arguments: Array, get: (Cursor) -> T): T? { - val database = databaseHelper.readableDatabase - var cursor: Cursor? = null - try { - cursor = database.query(table, null, query, arguments, null, null, null) - if (cursor != null && cursor.moveToFirst()) { return get(cursor) } - } catch (e: Exception) { - // Do nothing - } finally { - cursor?.close() - } - return null - } } +// region Convenience + private inline fun wrap(x: T): Array { return Array(1) { x } } diff --git a/src/org/thoughtcrime/securesms/loki/LokiContactPreKeyDatabase.kt b/src/org/thoughtcrime/securesms/loki/LokiContactPreKeyDatabase.kt new file mode 100644 index 0000000000..028cc0ee65 --- /dev/null +++ b/src/org/thoughtcrime/securesms/loki/LokiContactPreKeyDatabase.kt @@ -0,0 +1,57 @@ +package org.thoughtcrime.securesms.loki + +import android.content.ContentValues +import android.content.Context +import net.sqlcipher.database.SQLiteDatabase +import org.thoughtcrime.securesms.crypto.PreKeyUtil +import org.thoughtcrime.securesms.database.Database +import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper +import org.whispersystems.libsignal.state.PreKeyRecord + +/** + * A database for associating a `PreKeyRecord` id to a contact public key. + */ +class LokiContactPreKeyDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper) { + companion object { + private val tableName = "loki_contact_pre_key_database" + private val pubKey = "pub_key" + private val preKeyId = "pre_key_id" + + @JvmStatic + val createTableCommand = "CREATE TABLE $tableName ($preKeyId INTEGER PRIMARY KEY, $pubKey TEXT);" + } + + fun hasPreKey(pubKey: String): Boolean { + val database = databaseHelper.readableDatabase + return database.get(tableName, "${Companion.pubKey} = ?", arrayOf(pubKey)) { cursor -> + cursor.count > 0 + } ?: false + } + + fun getPreKey(pubKey: String): PreKeyRecord? { + val database = databaseHelper.readableDatabase + return database.get(tableName, "${Companion.pubKey} = ?", arrayOf(pubKey)) { cursor -> + val preKeyId = cursor.getInt(cursor.getColumnIndexOrThrow(preKeyId)) + PreKeyUtil.loadPreKey(context, preKeyId) + } + } + + fun getOrCreatePreKey(pubKey: String): PreKeyRecord { + return getPreKey(pubKey) ?: generateAndStorePreKey(pubKey) + } + + private fun generateAndStorePreKey(pubKey: String): PreKeyRecord { + val records = PreKeyUtil.generatePreKeys(context, 1) + PreKeyUtil.storePreKeyRecords(context, records) + + val record = records.first() + val database = databaseHelper.writableDatabase + + val values = ContentValues() + values.put(Companion.pubKey, pubKey) + values.put(preKeyId, record.id) + + database.insertWithOnConflict(tableName, null, values, SQLiteDatabase.CONFLICT_REPLACE) + return record + } +} \ No newline at end of file diff --git a/src/org/thoughtcrime/securesms/loki/LokiDatabaseUtils.kt b/src/org/thoughtcrime/securesms/loki/LokiDatabaseUtils.kt new file mode 100644 index 0000000000..ec6233e5d9 --- /dev/null +++ b/src/org/thoughtcrime/securesms/loki/LokiDatabaseUtils.kt @@ -0,0 +1,17 @@ +package org.thoughtcrime.securesms.loki + +import android.database.Cursor +import net.sqlcipher.database.SQLiteDatabase + +fun SQLiteDatabase.get(table: String, query: String, arguments: Array, get: (Cursor) -> T): T? { + var cursor: Cursor? = null + try { + cursor = this.query(table, null, query, arguments, null, null, null) + if (cursor != null && cursor.moveToFirst()) { return get(cursor) } + } catch (e: Exception) { + // Do nothing + } finally { + cursor?.close() + } + return null +} \ No newline at end of file diff --git a/src/org/thoughtcrime/securesms/loki/LokiPreKeyBundleDatabase.kt b/src/org/thoughtcrime/securesms/loki/LokiPreKeyBundleDatabase.kt new file mode 100644 index 0000000000..810c4b15c5 --- /dev/null +++ b/src/org/thoughtcrime/securesms/loki/LokiPreKeyBundleDatabase.kt @@ -0,0 +1,8 @@ +package org.thoughtcrime.securesms.loki + +import android.content.Context +import org.thoughtcrime.securesms.database.Database +import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper + +class LokiPreKeyBundleDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper) { +} \ No newline at end of file