mirror of
https://github.com/oxen-io/session-android.git
synced 2024-11-24 10:35:19 +00:00
Added more loki databases.
This commit is contained in:
parent
4f8af1b4f2
commit
bccde8baba
@ -29,7 +29,8 @@ class LokiAPIDatabase(private val userPublicKey: String, context: Context, helpe
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun getSwarmCache(hexEncodedPublicKey: String): List<LokiAPITarget>? {
|
override fun getSwarmCache(hexEncodedPublicKey: String): List<LokiAPITarget>? {
|
||||||
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))
|
val swarmAsString = cursor.getString(cursor.getColumnIndexOrThrow(swarm))
|
||||||
swarmAsString.split(",").map { targetAsString ->
|
swarmAsString.split(",").map { targetAsString ->
|
||||||
val components = targetAsString.split("?port=")
|
val components = targetAsString.split("?port=")
|
||||||
@ -47,7 +48,8 @@ class LokiAPIDatabase(private val userPublicKey: String, context: Context, helpe
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun getLastMessageHashValue(target: LokiAPITarget): String? {
|
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))
|
cursor.getString(cursor.getColumnIndexOrThrow(lastMessageHashValue))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -58,7 +60,8 @@ class LokiAPIDatabase(private val userPublicKey: String, context: Context, helpe
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun getReceivedMessageHashValues(): Set<String>? {
|
override fun getReceivedMessageHashValues(): Set<String>? {
|
||||||
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))
|
val receivedMessageHashValuesAsString = cursor.getString(cursor.getColumnIndexOrThrow(receivedMessageHashValues))
|
||||||
receivedMessageHashValuesAsString.split(",").toSet()
|
receivedMessageHashValuesAsString.split(",").toSet()
|
||||||
}
|
}
|
||||||
@ -69,22 +72,9 @@ class LokiAPIDatabase(private val userPublicKey: String, context: Context, helpe
|
|||||||
val receivedMessageHashValuesAsString = newValue.joinToString(",")
|
val receivedMessageHashValuesAsString = newValue.joinToString(",")
|
||||||
database.update(receivedMessageHashValuesCache, wrap(mapOf( receivedMessageHashValues to receivedMessageHashValuesAsString )), "$userID = ?", wrap(userPublicKey))
|
database.update(receivedMessageHashValuesCache, wrap(mapOf( receivedMessageHashValues to receivedMessageHashValuesAsString )), "$userID = ?", wrap(userPublicKey))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// region Convenience
|
// region Convenience
|
||||||
private fun <T> get(table: String, query: String, arguments: Array<String>, 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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private inline fun <reified T> wrap(x: T): Array<T> {
|
private inline fun <reified T> wrap(x: T): Array<T> {
|
||||||
return Array(1) { x }
|
return Array(1) { x }
|
||||||
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
17
src/org/thoughtcrime/securesms/loki/LokiDatabaseUtils.kt
Normal file
17
src/org/thoughtcrime/securesms/loki/LokiDatabaseUtils.kt
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package org.thoughtcrime.securesms.loki
|
||||||
|
|
||||||
|
import android.database.Cursor
|
||||||
|
import net.sqlcipher.database.SQLiteDatabase
|
||||||
|
|
||||||
|
fun <T> SQLiteDatabase.get(table: String, query: String, arguments: Array<String>, 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
|
||||||
|
}
|
@ -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) {
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user