@ -6,7 +6,6 @@ import android.util.Log
import org.thoughtcrime.securesms.database.Database
import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper
import org.thoughtcrime.securesms.loki.utilities.*
import org.thoughtcrime.securesms.util.Base64
import org.thoughtcrime.securesms.util.TextSecurePreferences
import org.whispersystems.signalservice.loki.api.Snode
import org.whispersystems.signalservice.loki.database.LokiAPIDatabaseProtocol
@ -14,78 +13,73 @@ import org.whispersystems.signalservice.loki.protocol.shelved.multidevice.Device
class LokiAPIDatabase ( context : Context , helper : SQLCipherOpenHelper ) : Database ( context , helper ) , LokiAPIDatabaseProtocol {
private val userPublicKey get ( ) = TextSecurePreferences . getLocalNumber ( context )
companion object {
// Shared
private val publicKey = " public_key "
private val timestamp = " timestamp "
// Snode pool cache
private val snodePoolCache = " loki_snode_pool_cache "
private val snode = " snode "
// Snode pool
private val snodePoolTable = " loki_snode_pool_cache "
private val dummyKey = " dummy_key "
private val snodePool = " snode_pool_key "
@JvmStatic val createSnodePool CacheCommand = " CREATE TABLE $snodePoolCach e ( $dummyKey TEXT PRIMARY KEY, $snodePool TEXT); "
// Onion request path cache
private val onionRequestPath Cach e = " loki_path_cache "
@JvmStatic val createSnodePool TableCommand = " CREATE TABLE $snodePoolTabl e ( $dummyKey TEXT PRIMARY KEY, $snodePool TEXT); "
// Onion request path s
private val onionRequestPath Tabl e = " loki_path_cache "
private val indexPath = " index_path "
private val snode = " snode "
@JvmStatic val createOnionRequestPathCacheCommand = " CREATE TABLE $onionRequestPathCache ( $indexPath TEXT PRIMARY KEY, $snode TEXT); "
// Swarm cache
private val swarmCache = " loki_api_swarm_cache "
@JvmStatic val createOnionRequestPathTableCommand = " CREATE TABLE $onionRequestPathTable ( $indexPath TEXT PRIMARY KEY, $snode TEXT); "
// Swarms
private val swarmTable = " loki_api_swarm_cache "
private val swarmPublicKey = " hex_encoded_public_key "
private val swarm = " swarm "
@JvmStatic val createSwarmCacheCommand = " CREATE TABLE $swarmCache ( $swarmPublicKey TEXT PRIMARY KEY, $swarm TEXT); "
// Last message hash value cache
private val lastMessageHashValueCache = " loki_api_last_message_hash_value_cache "
private val target = " target "
@JvmStatic val createSwarmTableCommand = " CREATE TABLE $swarmTable ( $swarmPublicKey TEXT PRIMARY KEY, $swarm TEXT); "
// Last message hash values
private val lastMessageHashValueTable2 = " last_message_hash_value_table "
private val lastMessageHashValue = " last_message_hash_value "
@JvmStatic val createLastMessageHashValue CacheCommand = " 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 "
@JvmStatic val createLastMessageHashValue Table2Command
= " CREATE TABLE $lastMessageHashValueTable2 ( $snode TEXT, $publicKey TEXT, $lastMessageHashValue TEXT, PRIMARY KEY ( $snode , $publicKey )); "
// Received message hash values
private val receivedMessageHashValuesTable2 = " received_message_hash_values_table "
private val receivedMessageHashValues = " received_message_hash_values "
@JvmStatic val createReceivedMessageHashValuesCacheCommand = " CREATE TABLE $receivedMessageHashValuesCache ( $userID TEXT PRIMARY KEY, $receivedMessageHashValues TEXT); "
// Open group auth token cache
private val openGroupAuthTokenCache = " loki_api_group_chat_auth_token_database "
@JvmStatic val createReceivedMessageHashValuesTable2Command
= " CREATE TABLE $receivedMessageHashValuesTable2 ( $snode STRING, $publicKey STRING, $receivedMessageHashValues TEXT, PRIMARY KEY ( $snode , $publicKey )); "
// Open group auth tokens
private val openGroupAuthTokenTable = " loki_api_group_chat_auth_token_database "
private val server = " server "
private val token = " token "
@JvmStatic val createOpenGroupAuthToken CacheCommand = " CREATE TABLE $openGroupAuthTokenCach e ( $server TEXT PRIMARY KEY, $token TEXT); "
// Last message server ID cache
private val lastMessageServerID Cach e = " loki_api_last_message_server_id_cache "
private val lastMessageServerID Cach eIndex = " loki_api_last_message_server_id_cache_index "
@JvmStatic val createOpenGroupAuthToken TableCommand = " CREATE TABLE $openGroupAuthTokenTabl e ( $server TEXT PRIMARY KEY, $token TEXT); "
// Last message server ID s
private val lastMessageServerID Tabl e = " loki_api_last_message_server_id_cache "
private val lastMessageServerID Tabl eIndex = " loki_api_last_message_server_id_cache_index "
private val lastMessageServerID = " last_message_server_id "
@JvmStatic val createLastMessageServerID CacheCommand = " CREATE TABLE $lastMessageServerIDCache ( $lastMessageServerIDCach eIndex STRING PRIMARY KEY, $lastMessageServerID INTEGER DEFAULT 0); "
// Last deletion server ID cache
private val lastDeletionServerID Cach e = " loki_api_last_deletion_server_id_cache "
private val lastDeletionServerID Cach eIndex = " loki_api_last_deletion_server_id_cache_index "
@JvmStatic val createLastMessageServerID TableCommand = " CREATE TABLE $lastMessageServerIDTable ( $lastMessageServerIDTabl eIndex STRING PRIMARY KEY, $lastMessageServerID INTEGER DEFAULT 0); "
// Last deletion server ID s
private val lastDeletionServerID Tabl e = " loki_api_last_deletion_server_id_cache "
private val lastDeletionServerID Tabl eIndex = " loki_api_last_deletion_server_id_cache_index "
private val lastDeletionServerID = " last_deletion_server_id "
@JvmStatic val createLastDeletionServerIDCacheCommand = " CREATE TABLE $lastDeletionServerIDCache ( $lastDeletionServerIDCacheIndex STRING PRIMARY KEY, $lastDeletionServerID INTEGER DEFAULT 0); "
// Device link cache
private val deviceLinkCache = " loki_pairing_authorisation_cache "
private val masterPublicKey = " primary_device "
private val slavePublicKey = " secondary_device "
private val requestSignature = " request_signature "
private val authorizationSignature = " grant_signature "
@JvmStatic val createDeviceLinkCacheCommand = " CREATE TABLE $deviceLinkCache ( $masterPublicKey TEXT, $slavePublicKey TEXT, " +
" $requestSignature TEXT NULLABLE DEFAULT NULL, $authorizationSignature TEXT NULLABLE DEFAULT NULL, PRIMARY KEY ( $masterPublicKey , $slavePublicKey )); "
// User count cache
private val userCountCache = " loki_user_count_cache "
@JvmStatic val createLastDeletionServerIDTableCommand = " CREATE TABLE $lastDeletionServerIDTable ( $lastDeletionServerIDTableIndex STRING PRIMARY KEY, $lastDeletionServerID INTEGER DEFAULT 0); "
// User counts
private val userCountTable = " loki_user_count_cache "
private val publicChatID = " public_chat_id "
private val userCount = " user_count "
@JvmStatic val createUserCount CacheCommand = " CREATE TABLE $userCountCach e ( $publicChatID STRING PRIMARY KEY, $userCount INTEGER DEFAULT 0); "
// Session request sent timestamp cache
private val sessionRequestSentTimestamp Cach e = " session_request_sent_timestamp_cache "
@JvmStatic val createSessionRequestSentTimestamp CacheCommand = " CREATE TABLE $sessionRequestSentTimestampCach e ( $publicKey STRING PRIMARY KEY, $timestamp INTEGER DEFAULT 0); "
@JvmStatic val createUserCountTableCommand = " CREATE TABLE $userCountTable ( $publicChatID STRING PRIMARY KEY, $userCount INTEGER DEFAULT 0); "
// Session request sent timestamps
private val sessionRequestSentTimestampTable = " session_request_sent_timestamp_cache "
@JvmStatic val createSessionRequestSentTimestampTableCommand = " CREATE TABLE $sessionRequestSentTimestampTable ( $publicKey STRING PRIMARY KEY, $timestamp INTEGER DEFAULT 0); "
// Session request processed timestamp cache
private val sessionRequestProcessedTimestamp Cach e = " session_request_processed_timestamp_cache "
@JvmStatic val createSessionRequestProcessedTimestamp CacheCommand = " CREATE TABLE $sessionRequestProcessedTimestampCach e ( $publicKey STRING PRIMARY KEY, $timestamp INTEGER DEFAULT 0); "
private val sessionRequestProcessedTimestampTable = " session_request_processed_timestamp_cache "
@JvmStatic val createSessionRequestProcessedTimestamp TableCommand = " CREATE TABLE $sessionRequestProcessedTimestampTabl e ( $publicKey STRING PRIMARY KEY, $timestamp INTEGER DEFAULT 0); "
// Open group public keys
private val openGroupPublicKeyDB = " open_group_public_keys "
@JvmStatic val createOpenGroupPublicKeyDBCommand = " CREATE TABLE $openGroupPublicKeyDB ( $server STRING PRIMARY KEY, $publicKey INTEGER DEFAULT 0); "
private val openGroupPublicKeyTable = " open_group_public_keys "
@JvmStatic val createOpenGroupPublicKeyTableCommand = " CREATE TABLE $openGroupPublicKeyTable ( $server STRING PRIMARY KEY, $publicKey INTEGER DEFAULT 0); "
// region Deprecated
private val deviceLinkCache = " loki_pairing_authorisation_cache "
private val masterPublicKey = " primary_device "
private val slavePublicKey = " secondary_device "
private val requestSignature = " request_signature "
private val authorizationSignature = " grant_signature "
@JvmStatic val createDeviceLinkCacheCommand = " CREATE TABLE $deviceLinkCache ( $masterPublicKey STRING, $slavePublicKey STRING, " +
" $requestSignature STRING NULLABLE DEFAULT NULL, $authorizationSignature STRING NULLABLE DEFAULT NULL, PRIMARY KEY ( $masterPublicKey , $slavePublicKey )); "
private val sessionRequestTimestampCache = " session_request_timestamp_cache "
@JvmStatic val createSessionRequestTimestampCacheCommand = " CREATE TABLE $sessionRequestTimestampCache ( $publicKey STRING PRIMARY KEY, $timestamp STRING); "
// endregion
@ -93,7 +87,7 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
override fun getSnodePool ( ) : Set < Snode > {
val database = databaseHelper . readableDatabase
return database . get ( snodePool Cach e, " ${Companion.dummyKey} = ? " , wrap ( " dummy_key " ) ) { cursor ->
return database . get ( snodePool Tabl e, " ${Companion.dummyKey} = ? " , wrap ( " dummy_key " ) ) { cursor ->
val snodePoolAsString = cursor . getString ( cursor . getColumnIndexOrThrow ( snodePool ) )
snodePoolAsString . split ( " , " ) . mapNotNull { snodeAsString ->
val components = snodeAsString . split ( " - " )
@ -117,13 +111,13 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
string
}
val row = wrap ( mapOf ( Companion . dummyKey to " dummy_key " , snodePool to snodePoolAsString ) )
database . insertOrUpdate ( snodePool Cach e, row , " ${Companion.dummyKey} = ? " , wrap ( " dummy_key " ) )
database . insertOrUpdate ( snodePool Tabl e, row , " ${Companion.dummyKey} = ? " , wrap ( " dummy_key " ) )
}
override fun getOnionRequestPaths ( ) : List < List < Snode > > {
val database = databaseHelper . readableDatabase
fun get ( indexPath : String ) : Snode ? {
return database . get ( onionRequestPath Cach e, " ${Companion.indexPath} = ? " , wrap ( indexPath ) ) { cursor ->
return database . get ( onionRequestPath Tabl e, " ${Companion.indexPath} = ? " , wrap ( indexPath ) ) { cursor ->
val snodeAsString = cursor . getString ( cursor . getColumnIndexOrThrow ( snode ) )
val components = snodeAsString . split ( " - " )
val address = components [ 0 ]
@ -146,7 +140,7 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
fun clearOnionRequestPaths ( ) {
val database = databaseHelper . writableDatabase
fun delete ( indexPath : String ) {
database . delete ( onionRequestPath Cach e, " ${Companion.indexPath} = ? " , wrap ( indexPath ) )
database . delete ( onionRequestPath Tabl e, " ${Companion.indexPath} = ? " , wrap ( indexPath ) )
}
delete ( " 0-0 " ) ; delete ( " 0-1 " )
delete ( " 0-2 " ) ; delete ( " 1-0 " )
@ -154,7 +148,7 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
}
override fun setOnionRequestPaths ( newValue : List < List < Snode > > ) {
// FIXME: This is a bit of a dirty approach that assumes 2 paths of length 3 each. We should do better than this.
// TODO: Make this work with arbitrary paths
if ( newValue . count ( ) != 2 ) { return }
val path0 = newValue [ 0 ]
val path1 = newValue [ 1 ]
@ -168,7 +162,7 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
snodeAsString += " - ${keySet.ed25519Key} - ${keySet.x25519Key} "
}
val row = wrap ( mapOf ( Companion . indexPath to indexPath , Companion . snode to snodeAsString ) )
database . insertOrUpdate ( onionRequestPath Cach e, row , " ${Companion.indexPath} = ? " , wrap ( indexPath ) )
database . insertOrUpdate ( onionRequestPath Tabl e, row , " ${Companion.indexPath} = ? " , wrap ( indexPath ) )
}
set ( " 0-0 " , path0 [ 0 ] ) ; set ( " 0-1 " , path0 [ 1 ] )
set ( " 0-2 " , path0 [ 2 ] ) ; set ( " 1-0 " , path1 [ 0 ] )
@ -177,7 +171,7 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
override fun getSwarm ( publicKey : String ) : Set < Snode > ? {
val database = databaseHelper . readableDatabase
return database . get ( swarm Cach e, " ${Companion.swarmPublicKey} = ? " , wrap ( publicKey ) ) { cursor ->
return database . get ( swarm Tabl e, " ${Companion.swarmPublicKey} = ? " , wrap ( publicKey ) ) { cursor ->
val swarmAsString = cursor . getString ( cursor . getColumnIndexOrThrow ( swarm ) )
swarmAsString . split ( " , " ) . mapNotNull { targetAsString ->
val components = targetAsString . split ( " - " )
@ -201,40 +195,44 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
string
}
val row = wrap ( mapOf ( Companion . swarmPublicKey to publicKey , swarm to swarmAsString ) )
database . insertOrUpdate ( swarm Cach e, row , " ${Companion.swarmPublicKey} = ? " , wrap ( publicKey ) )
database . insertOrUpdate ( swarm Tabl e, row , " ${Companion.swarmPublicKey} = ? " , wrap ( publicKey ) )
}
override fun getLastMessageHashValue ( snode : Snode ): String ? {
override fun getLastMessageHashValue ( snode : Snode , publicKey : String ): String ? {
val database = databaseHelper . readableDatabase
return database . get ( lastMessageHashValueCache , " ${Companion.target} = ? " , wrap ( snode . address ) ) { cursor ->
val query = " ${Companion.snode} = ? AND ${Companion.publicKey} = ? "
return database . get ( lastMessageHashValueTable2 , query , arrayOf ( snode . toString ( ) , publicKey ) ) { cursor ->
cursor . getString ( cursor . getColumnIndexOrThrow ( lastMessageHashValue ) )
}
}
override fun setLastMessageHashValue ( snode : Snode , newValue: String ) {
override fun setLastMessageHashValue ( snode : Snode , publicKey: String , newValue: String ) {
val database = databaseHelper . writableDatabase
val row = wrap ( mapOf ( Companion . target to snode . address , lastMessageHashValue to newValue ) )
database . insertOrUpdate ( lastMessageHashValueCache , row , " ${Companion.target} = ? " , wrap ( snode . address ) )
val row = wrap ( mapOf ( Companion . snode to snode . toString ( ) , Companion . publicKey to publicKey , lastMessageHashValue to newValue ) )
val query = " ${Companion.snode} = ? AND ${Companion.publicKey} = ? "
database . insertOrUpdate ( lastMessageHashValueTable2 , row , query , arrayOf ( snode . toString ( ) , publicKey ) )
}
override fun getReceivedMessageHashValues ( ) : Set < String > ? {
override fun getReceivedMessageHashValues ( publicKey : String ) : Set < String > ? {
val database = databaseHelper . readableDatabase
return database . get ( receivedMessageHashValuesCache , " $userID = ? " , wrap ( userPublicKey ) ) { cursor ->
val query = " $Companion .publicKey = ? "
return database . get ( receivedMessageHashValuesTable2 , query , arrayOf ( publicKey ) ) { cursor ->
val receivedMessageHashValuesAsString = cursor . getString ( cursor . getColumnIndexOrThrow ( receivedMessageHashValues ) )
receivedMessageHashValuesAsString . split ( " , " ) . toSet ( )
receivedMessageHashValuesAsString . split ( " - " ) . toSet ( )
}
}
override fun setReceivedMessageHashValues ( newValue: Set < String > ) {
override fun setReceivedMessageHashValues ( publicKey: String , newValue: Set < String > ) {
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 ) )
val receivedMessageHashValuesAsString = newValue . joinToString ( " - " )
val row = wrap ( mapOf ( Companion . publicKey to publicKey , receivedMessageHashValues to receivedMessageHashValuesAsString ) )
val query = " $Companion .publicKey = ? "
database . insertOrUpdate ( receivedMessageHashValuesTable2 , row , query , arrayOf ( publicKey ) )
}
override fun getAuthToken ( server : String ) : String ? {
val database = databaseHelper . readableDatabase
return database . get ( openGroupAuthToken Cach e, " ${Companion.server} = ? " , wrap ( server ) ) { cursor ->
return database . get ( openGroupAuthToken Tabl e, " ${Companion.server} = ? " , wrap ( server ) ) { cursor ->
cursor . getString ( cursor . getColumnIndexOrThrow ( token ) )
}
}
@ -243,16 +241,16 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
val database = databaseHelper . writableDatabase
if ( newValue != null ) {
val row = wrap ( mapOf ( Companion . server to server , token to newValue ) )
database . insertOrUpdate ( openGroupAuthToken Cach e, row , " ${Companion.server} = ? " , wrap ( server ) )
database . insertOrUpdate ( openGroupAuthToken Tabl e, row , " ${Companion.server} = ? " , wrap ( server ) )
} else {
database . delete ( openGroupAuthToken Cach e, " ${Companion.server} = ? " , wrap ( server ) )
database . delete ( openGroupAuthToken Tabl e, " ${Companion.server} = ? " , wrap ( server ) )
}
}
override fun getLastMessageServerID ( group : Long , server : String ) : Long ? {
val database = databaseHelper . readableDatabase
val index = " $server . $group "
return database . get ( lastMessageServerID Cache, " $lastMessageServerIDCach eIndex = ? " , wrap ( index ) ) { cursor ->
return database . get ( lastMessageServerID Table, " $lastMessageServerIDTabl eIndex = ? " , wrap ( index ) ) { cursor ->
cursor . getInt ( lastMessageServerID )
} ?. toLong ( )
}
@ -260,20 +258,20 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
override fun setLastMessageServerID ( group : Long , server : String , newValue : Long ) {
val database = databaseHelper . writableDatabase
val index = " $server . $group "
val row = wrap ( mapOf ( lastMessageServerIDCach eIndex to index , lastMessageServerID to newValue . toString ( ) ) )
database . insertOrUpdate ( lastMessageServerID Cache, row , " $lastMessageServerIDCach eIndex = ? " , wrap ( index ) )
val row = wrap ( mapOf ( lastMessageServerIDTabl eIndex to index , lastMessageServerID to newValue . toString ( ) ) )
database . insertOrUpdate ( lastMessageServerID Table, row , " $lastMessageServerIDTabl eIndex = ? " , wrap ( index ) )
}
fun removeLastMessageServerID ( group : Long , server : String ) {
val database = databaseHelper . writableDatabase
val index = " $server . $group "
database . delete ( lastMessageServerID Cache, " $lastMessageServerIDCach eIndex = ? " , wrap ( index ) )
database . delete ( lastMessageServerID Table, " $lastMessageServerIDTabl eIndex = ? " , wrap ( index ) )
}
override fun getLastDeletionServerID ( group : Long , server : String ) : Long ? {
val database = databaseHelper . readableDatabase
val index = " $server . $group "
return database . get ( lastDeletionServerID Cache, " $lastDeletionServerIDCach eIndex = ? " , wrap ( index ) ) { cursor ->
return database . get ( lastDeletionServerID Table, " $lastDeletionServerIDTabl eIndex = ? " , wrap ( index ) ) { cursor ->
cursor . getInt ( lastDeletionServerID )
} ?. toLong ( )
}
@ -281,60 +279,20 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
override fun setLastDeletionServerID ( group : Long , server : String , newValue : Long ) {
val database = databaseHelper . writableDatabase
val index = " $server . $group "
val row = wrap ( mapOf ( lastDeletionServerIDCach eIndex to index , lastDeletionServerID to newValue . toString ( ) ) )
database . insertOrUpdate ( lastDeletionServerID Cache, row , " $lastDeletionServerIDCach eIndex = ? " , wrap ( index ) )
val row = wrap ( mapOf ( lastDeletionServerIDTabl eIndex to index , lastDeletionServerID to newValue . toString ( ) ) )
database . insertOrUpdate ( lastDeletionServerID Table, row , " $lastDeletionServerIDTabl eIndex = ? " , wrap ( index ) )
}
fun removeLastDeletionServerID ( group : Long , server : String ) {
val database = databaseHelper . writableDatabase
val index = " $server . $group "
database . delete ( lastDeletionServerIDCache , " $lastDeletionServerIDCacheIndex = ? " , wrap ( index ) )
}
override fun getDeviceLinks ( publicKey : String ) : Set < DeviceLink > {
return setOf ( )
/ *
val database = databaseHelper . readableDatabase
return database . getAll ( deviceLinkCache , " $masterPublicKey = ? OR $slavePublicKey = ? " , arrayOf ( publicKey , publicKey ) ) { cursor ->
val masterHexEncodedPublicKey = cursor . getString ( masterPublicKey )
val slaveHexEncodedPublicKey = cursor . getString ( slavePublicKey )
val requestSignature : ByteArray ? = if ( cursor . isNull ( cursor . getColumnIndexOrThrow ( requestSignature ) ) ) null else cursor . getBase64EncodedData ( requestSignature )
val authorizationSignature : ByteArray ? = if ( cursor . isNull ( cursor . getColumnIndexOrThrow ( authorizationSignature ) ) ) null else cursor . getBase64EncodedData ( authorizationSignature )
DeviceLink ( masterHexEncodedPublicKey , slaveHexEncodedPublicKey , requestSignature , authorizationSignature )
} . toSet ( )
* /
}
override fun clearDeviceLinks ( publicKey : String ) {
/ *
val database = databaseHelper . writableDatabase
database . delete ( deviceLinkCache , " $masterPublicKey = ? OR $slavePublicKey = ? " , arrayOf ( publicKey , publicKey ) )
* /
}
override fun addDeviceLink ( deviceLink : DeviceLink ) {
/ *
val database = databaseHelper . writableDatabase
val values = ContentValues ( )
values . put ( masterPublicKey , deviceLink . masterPublicKey )
values . put ( slavePublicKey , deviceLink . slavePublicKey )
if ( deviceLink . requestSignature != null ) { values . put ( requestSignature , Base64 . encodeBytes ( deviceLink . requestSignature ) ) }
if ( deviceLink . authorizationSignature != null ) { values . put ( authorizationSignature , Base64 . encodeBytes ( deviceLink . authorizationSignature ) ) }
database . insertOrUpdate ( deviceLinkCache , values , " $masterPublicKey = ? AND $slavePublicKey = ? " , arrayOf ( deviceLink . masterPublicKey , deviceLink . slavePublicKey ) )
* /
}
override fun removeDeviceLink ( deviceLink : DeviceLink ) {
/ *
val database = databaseHelper . writableDatabase
database . delete ( deviceLinkCache , " $masterPublicKey = ? OR $slavePublicKey = ? " , arrayOf ( deviceLink . masterPublicKey , deviceLink . slavePublicKey ) )
* /
database . delete ( lastDeletionServerIDTable , " $lastDeletionServerIDTableIndex = ? " , wrap ( index ) )
}
fun getUserCount ( group : Long , server : String ) : Int ? {
val database = databaseHelper . readableDatabase
val index = " $server . $group "
return database . get ( userCount Cach e, " $publicChatID = ? " , wrap ( index ) ) { cursor ->
return database . get ( userCountTable , " $publicChatID = ? " , wrap ( index ) ) { cursor ->
cursor . getInt ( userCount )
} ?. toInt ( )
}
@ -343,12 +301,12 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
val database = databaseHelper . writableDatabase
val index = " $server . $group "
val row = wrap ( mapOf ( publicChatID to index , Companion . userCount to newValue . toString ( ) ) )
database . insertOrUpdate ( userCount Cach e, row , " $publicChatID = ? " , wrap ( index ) )
database . insertOrUpdate ( userCount Tabl e, row , " $publicChatID = ? " , wrap ( index ) )
}
override fun getSessionRequestSentTimestamp ( publicKey : String ) : Long ? {
val database = databaseHelper . readableDatabase
return database . get ( sessionRequestSentTimestamp Cach e, " ${LokiAPIDatabase.publicKey} = ? " , wrap ( publicKey ) ) { cursor ->
return database . get ( sessionRequestSentTimestamp Tabl e, " ${LokiAPIDatabase.publicKey} = ? " , wrap ( publicKey ) ) { cursor ->
cursor . getInt ( LokiAPIDatabase . timestamp )
} ?. toLong ( )
}
@ -356,12 +314,12 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
override fun setSessionRequestSentTimestamp ( publicKey : String , newValue : Long ) {
val database = databaseHelper . writableDatabase
val row = wrap ( mapOf ( LokiAPIDatabase . publicKey to publicKey , LokiAPIDatabase . timestamp to newValue . toString ( ) ) )
database . insertOrUpdate ( sessionRequestSentTimestamp Cach e, row , " ${LokiAPIDatabase.publicKey} = ? " , wrap ( publicKey ) )
database . insertOrUpdate ( sessionRequestSentTimestamp Tabl e, row , " ${LokiAPIDatabase.publicKey} = ? " , wrap ( publicKey ) )
}
override fun getSessionRequestProcessedTimestamp ( publicKey : String ) : Long ? {
val database = databaseHelper . readableDatabase
return database . get ( sessionRequestProcessedTimestamp Cach e, " ${LokiAPIDatabase.publicKey} = ? " , wrap ( publicKey ) ) { cursor ->
return database . get ( sessionRequestProcessedTimestamp Tabl e, " ${LokiAPIDatabase.publicKey} = ? " , wrap ( publicKey ) ) { cursor ->
cursor . getInt ( LokiAPIDatabase . timestamp )
} ?. toLong ( )
}
@ -369,12 +327,12 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
override fun setSessionRequestProcessedTimestamp ( publicKey : String , newValue : Long ) {
val database = databaseHelper . writableDatabase
val row = wrap ( mapOf ( LokiAPIDatabase . publicKey to publicKey , LokiAPIDatabase . timestamp to newValue . toString ( ) ) )
database . insertOrUpdate ( sessionRequestProcessedTimestamp Cach e, row , " ${LokiAPIDatabase.publicKey} = ? " , wrap ( publicKey ) )
database . insertOrUpdate ( sessionRequestProcessedTimestamp Tabl e, row , " ${LokiAPIDatabase.publicKey} = ? " , wrap ( publicKey ) )
}
override fun getOpenGroupPublicKey ( server : String ) : String ? {
val database = databaseHelper . readableDatabase
return database . get ( openGroupPublicKey DB , " ${LokiAPIDatabase.server} = ? " , wrap ( server ) ) { cursor ->
return database . get ( openGroupPublicKey Table , " ${LokiAPIDatabase.server} = ? " , wrap ( server ) ) { cursor ->
cursor . getString ( LokiAPIDatabase . publicKey )
}
}
@ -382,8 +340,50 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
override fun setOpenGroupPublicKey ( server : String , newValue : String ) {
val database = databaseHelper . writableDatabase
val row = wrap ( mapOf ( LokiAPIDatabase . server to server , LokiAPIDatabase . publicKey to newValue ) )
database . insertOrUpdate ( openGroupPublicKeyDB , row , " ${LokiAPIDatabase.server} = ? " , wrap ( server ) )
database . insertOrUpdate ( openGroupPublicKeyTable , row , " ${LokiAPIDatabase.server} = ? " , wrap ( server ) )
}
// region Deprecated
override fun getDeviceLinks ( publicKey : String ) : Set < DeviceLink > {
return setOf ( )
/ *
val database = databaseHelper . readableDatabase
return database . getAll ( deviceLinkCache , " $masterPublicKey = ? OR $slavePublicKey = ? " , arrayOf ( publicKey , publicKey ) ) { cursor ->
val masterHexEncodedPublicKey = cursor . getString ( masterPublicKey )
val slaveHexEncodedPublicKey = cursor . getString ( slavePublicKey )
val requestSignature : ByteArray ? = if ( cursor . isNull ( cursor . getColumnIndexOrThrow ( requestSignature ) ) ) null else cursor . getBase64EncodedData ( requestSignature )
val authorizationSignature : ByteArray ? = if ( cursor . isNull ( cursor . getColumnIndexOrThrow ( authorizationSignature ) ) ) null else cursor . getBase64EncodedData ( authorizationSignature )
DeviceLink ( masterHexEncodedPublicKey , slaveHexEncodedPublicKey , requestSignature , authorizationSignature )
} . toSet ( )
* /
}
override fun clearDeviceLinks ( publicKey : String ) {
/ *
val database = databaseHelper . writableDatabase
database . delete ( deviceLinkCache , " $masterPublicKey = ? OR $slavePublicKey = ? " , arrayOf ( publicKey , publicKey ) )
* /
}
override fun addDeviceLink ( deviceLink : DeviceLink ) {
/ *
val database = databaseHelper . writableDatabase
val values = ContentValues ( )
values . put ( masterPublicKey , deviceLink . masterPublicKey )
values . put ( slavePublicKey , deviceLink . slavePublicKey )
if ( deviceLink . requestSignature != null ) { values . put ( requestSignature , Base64 . encodeBytes ( deviceLink . requestSignature ) ) }
if ( deviceLink . authorizationSignature != null ) { values . put ( authorizationSignature , Base64 . encodeBytes ( deviceLink . authorizationSignature ) ) }
database . insertOrUpdate ( deviceLinkCache , values , " $masterPublicKey = ? AND $slavePublicKey = ? " , arrayOf ( deviceLink . masterPublicKey , deviceLink . slavePublicKey ) )
* /
}
override fun removeDeviceLink ( deviceLink : DeviceLink ) {
/ *
val database = databaseHelper . writableDatabase
database . delete ( deviceLinkCache , " $masterPublicKey = ? OR $slavePublicKey = ? " , arrayOf ( deviceLink . masterPublicKey , deviceLink . slavePublicKey ) )
* /
}
// endregion
}
// region Convenience