refactor: config query and store use the same format as other platforms

This commit is contained in:
0x330a 2023-01-19 09:56:21 +11:00
parent 8bea5e73e6
commit f573c7deaf
No known key found for this signature in database
GPG Key ID: 267811D6E6A2698C
2 changed files with 10 additions and 11 deletions

View File

@ -19,10 +19,6 @@ class ConfigDatabase(context: Context, helper: SQLCipherOpenHelper): Database(co
"CREATE TABLE $TABLE_NAME ($VARIANT TEXT NOT NULL, $PUBKEY TEXT NOT NULL, $DATA BLOB, $COMBINED_MESSAGE_HASHES TEXT, PRIMARY KEY($VARIANT, $PUBKEY));"
private const val VARIANT_WHERE = "$VARIANT = ?"
private const val VARIANT_AND_PUBKEY_WHERE = "$VARIANT = ? AND $PUBKEY = ?"
const val USER_KEY = "user"
const val CONTACTS_KEY = "contacts"
// conversations use publicKey / URL
}
fun storeConfig(variant: String, publicKey: String, data: ByteArray) {

View File

@ -3,9 +3,10 @@ package org.thoughtcrime.securesms.dependencies
import network.loki.messenger.libsession_util.Contacts
import network.loki.messenger.libsession_util.UserProfile
import org.session.libsession.utilities.ConfigFactoryProtocol
import org.session.libsignal.protos.SignalServiceProtos.SharedConfigMessage
import org.thoughtcrime.securesms.database.ConfigDatabase
class ConfigFactory(private val configDatabase: ConfigDatabase, private val maybeGetUserEdSecretKey: ()->ByteArray?):
class ConfigFactory(private val configDatabase: ConfigDatabase, private val maybeGetUserInfo: ()->Pair<ByteArray, String>?):
ConfigFactoryProtocol {
fun keyPairChanged() { // this should only happen restoring or clearing data
@ -22,8 +23,8 @@ class ConfigFactory(private val configDatabase: ConfigDatabase, private val mayb
override val userConfig: UserProfile? = synchronized(userLock) {
if (_userConfig == null) {
val secretKey = maybeGetUserEdSecretKey() ?: return@synchronized null
val userDump = configDatabase.retrieveConfig(ConfigDatabase.USER_KEY)
val (secretKey, publicKey) = maybeGetUserInfo() ?: return@synchronized null
val userDump = configDatabase.retrieveConfig(SharedConfigMessage.Kind.USER_PROFILE.name, publicKey)
_userConfig = if (userDump != null) {
UserProfile.newInstance(secretKey, userDump)
} else {
@ -35,8 +36,8 @@ class ConfigFactory(private val configDatabase: ConfigDatabase, private val mayb
override val contacts: Contacts? = synchronized(contactLock) {
if (_contacts == null) {
val secretKey = maybeGetUserEdSecretKey() ?: return@synchronized null
val contactsDump = configDatabase.retrieveConfig(ConfigDatabase.CONTACTS_KEY)
val (secretKey, publicKey) = maybeGetUserInfo() ?: return@synchronized null
val contactsDump = configDatabase.retrieveConfig(SharedConfigMessage.Kind.CONTACTS.name, publicKey)
_contacts = if (contactsDump != null) {
Contacts.newInstance(secretKey, contactsDump)
} else {
@ -49,12 +50,14 @@ class ConfigFactory(private val configDatabase: ConfigDatabase, private val mayb
override fun saveUserConfigDump() {
val dumped = userConfig?.dump() ?: return
configDatabase.storeConfig(ConfigDatabase.USER_KEY, dumped)
val (_, publicKey) = maybeGetUserInfo() ?: return
configDatabase.storeConfig(SharedConfigMessage.Kind.USER_PROFILE.name, publicKey, dumped)
}
override fun saveContactConfigDump() {
val dumped = contacts?.dump() ?: return
configDatabase.storeConfig(ConfigDatabase.CONTACTS_KEY, dumped)
val (_, publicKey) = maybeGetUserInfo() ?: return
configDatabase.storeConfig(SharedConfigMessage.Kind.CONTACTS.name, publicKey, dumped)
}
}