feat: add in more config factory for volatile

This commit is contained in:
0x330a
2023-02-06 14:28:00 +11:00
parent f7c5953f5d
commit 28e5794624
5 changed files with 94 additions and 49 deletions

View File

@@ -1,6 +1,7 @@
package org.thoughtcrime.securesms.dependencies
import network.loki.messenger.libsession_util.Contacts
import network.loki.messenger.libsession_util.ConversationVolatileConfig
import network.loki.messenger.libsession_util.UserProfile
import org.session.libsession.utilities.ConfigFactoryProtocol
import org.session.libsignal.protos.SignalServiceProtos.SharedConfigMessage
@@ -12,16 +13,20 @@ class ConfigFactory(private val configDatabase: ConfigDatabase, private val mayb
fun keyPairChanged() { // this should only happen restoring or clearing data
_userConfig?.free()
_contacts?.free()
_convoVolatileConfig?.free()
_userConfig = null
_contacts = null
_convoVolatileConfig = null
}
private val userLock = Object()
private var _userConfig: UserProfile? = null
private val contactLock = Object()
private var _contacts: Contacts? = null
private val convoVolatileLock = Object()
private var _convoVolatileConfig: ConversationVolatileConfig? = null
override val userConfig: UserProfile? = synchronized(userLock) {
override val user: UserProfile? = synchronized(userLock) {
if (_userConfig == null) {
val (secretKey, publicKey) = maybeGetUserInfo() ?: return@synchronized null
val userDump = configDatabase.retrieveConfig(SharedConfigMessage.Kind.USER_PROFILE.name, publicKey)
@@ -47,9 +52,22 @@ class ConfigFactory(private val configDatabase: ConfigDatabase, private val mayb
_contacts
}
override val convoVolatile: ConversationVolatileConfig? = synchronized(convoVolatileLock) {
if (_convoVolatileConfig == null) {
val (secretKey, publicKey) = maybeGetUserInfo() ?: return@synchronized null
val convoDump = configDatabase.retrieveConfig(SharedConfigMessage.Kind.CONVO_INFO_VOLATILE.name, publicKey)
_convoVolatileConfig = if (convoDump != null) {
ConversationVolatileConfig.newInstance(secretKey, convoDump)
} else {
ConversationVolatileConfig.newInstance(secretKey)
}
}
_convoVolatileConfig
}
override fun saveUserConfigDump() {
val dumped = userConfig?.dump() ?: return
val dumped = user?.dump() ?: return
val (_, publicKey) = maybeGetUserInfo() ?: return
configDatabase.storeConfig(SharedConfigMessage.Kind.USER_PROFILE.name, publicKey, dumped)
}
@@ -60,4 +78,9 @@ class ConfigFactory(private val configDatabase: ConfigDatabase, private val mayb
configDatabase.storeConfig(SharedConfigMessage.Kind.CONTACTS.name, publicKey, dumped)
}
override fun saveConvoVolatileConfigDump() {
val dumped = convoVolatile?.dump() ?: return
val (_, publicKey) = maybeGetUserInfo() ?: return
configDatabase.storeConfig(SharedConfigMessage.Kind.CONVO_INFO_VOLATILE.name, publicKey, dumped)
}
}