Handle config migration (#818)

* Handle config migration

* Handle config migration
pull/1706/head
SessionHero01 4 months ago committed by GitHub
parent b961ec4a6c
commit 00b332a0b0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -2,6 +2,9 @@ package org.thoughtcrime.securesms.configs
import android.content.Context
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import network.loki.messenger.libsession_util.ConfigBase.Companion.PRIORITY_HIDDEN
import network.loki.messenger.libsession_util.ConfigBase.Companion.PRIORITY_PINNED
import network.loki.messenger.libsession_util.ReadableGroupInfoConfig
@ -63,6 +66,18 @@ class ConfigToDatabaseSync @Inject constructor(
private val profileManager: ProfileManager,
private val preferences: TextSecurePreferences,
) {
init {
if (!preferences.migratedToGroupV2Config) {
preferences.migratedToGroupV2Config = true
GlobalScope.launch(Dispatchers.Default) {
for (configType in UserConfigType.entries) {
syncUserConfigs(configType, null)
}
}
}
}
fun syncGroupConfigs(groupId: AccountId) {
val info = configFactory.withGroupConfigs(groupId) {
UpdateGroupInfo(it.groupInfo)
@ -71,7 +86,7 @@ class ConfigToDatabaseSync @Inject constructor(
updateGroup(info)
}
fun syncUserConfigs(userConfigType: UserConfigType, updateTimestamp: Long) {
fun syncUserConfigs(userConfigType: UserConfigType, updateTimestamp: Long?) {
val configUpdate = configFactory.withUserConfigs { configs ->
when (userConfigType) {
UserConfigType.USER_PROFILE -> UpdateUserInfo(configs.userProfile)
@ -104,7 +119,7 @@ class ConfigToDatabaseSync @Inject constructor(
)
}
private fun updateUser(userProfile: UpdateUserInfo, messageTimestamp: Long) {
private fun updateUser(userProfile: UpdateUserInfo, messageTimestamp: Long?) {
val userPublicKey = storage.getUserPublicKey() ?: return
// would love to get rid of recipient and context from this
val recipient = Recipient.from(context, fromSerialized(userPublicKey), false)
@ -139,11 +154,13 @@ class ConfigToDatabaseSync @Inject constructor(
}
// Set or reset the shared library to use latest expiration config
storage.getThreadId(recipient)?.let {
storage.setExpirationConfiguration(
storage.getExpirationConfiguration(it)?.takeIf { it.updatedTimestampMs > messageTimestamp } ?:
ExpirationConfiguration(it, userProfile.ntsExpiry, messageTimestamp)
)
if (messageTimestamp != null) {
storage.getThreadId(recipient)?.let { theadId ->
storage.setExpirationConfiguration(
storage.getExpirationConfiguration(theadId)?.takeIf { it.updatedTimestampMs > messageTimestamp } ?:
ExpirationConfiguration(theadId, userProfile.ntsExpiry, messageTimestamp)
)
}
}
}
@ -187,7 +204,7 @@ class ConfigToDatabaseSync @Inject constructor(
private data class UpdateContacts(val contacts: List<Contact>)
private fun updateContacts(contacts: UpdateContacts, messageTimestamp: Long) {
private fun updateContacts(contacts: UpdateContacts, messageTimestamp: Long?) {
storage.addLibSessionContacts(contacts.contacts, messageTimestamp)
}
@ -203,7 +220,7 @@ class ConfigToDatabaseSync @Inject constructor(
)
}
private fun updateUserGroups(userGroups: UpdateUserGroupsInfo, messageTimestamp: Long) {
private fun updateUserGroups(userGroups: UpdateUserGroupsInfo, messageTimestamp: Long?) {
val localUserPublicKey = storage.getUserPublicKey() ?: return Log.w(
TAG,
"No user public key when trying to update user groups from config"
@ -349,11 +366,14 @@ class ConfigToDatabaseSync @Inject constructor(
// Start polling
LegacyClosedGroupPollerV2.shared.startPolling(group.accountId)
}
storage.getThreadId(fromSerialized(groupId))?.let {
storage.setExpirationConfiguration(
storage.getExpirationConfiguration(it)?.takeIf { it.updatedTimestampMs > messageTimestamp }
?: ExpirationConfiguration(it, afterSend(group.disappearingTimer), messageTimestamp)
)
if (messageTimestamp != null) {
storage.getThreadId(fromSerialized(groupId))?.let { theadId ->
storage.setExpirationConfiguration(
storage.getExpirationConfiguration(theadId)?.takeIf { it.updatedTimestampMs > messageTimestamp }
?: ExpirationConfiguration(theadId, afterSend(group.disappearingTimer), messageTimestamp)
)
}
}
}
}

@ -1245,7 +1245,7 @@ open class Storage @Inject constructor(
return recipientDatabase.isAutoDownloadFlagSet(recipient)
}
override fun addLibSessionContacts(contacts: List<LibSessionContact>, timestamp: Long) {
override fun addLibSessionContacts(contacts: List<LibSessionContact>, timestamp: Long?) {
val mappingDb = blindedIdMappingDatabase
val moreContacts = contacts.filter { contact ->
val id = AccountId(contact.id)
@ -1285,11 +1285,13 @@ open class Storage @Inject constructor(
}
).also { setPinned(it, contact.priority == PRIORITY_PINNED) }
}
getThreadId(recipient)?.let {
setExpirationConfiguration(
getExpirationConfiguration(it)?.takeIf { it.updatedTimestampMs > timestamp }
?: ExpirationConfiguration(it, contact.expiryMode, timestamp)
)
if (timestamp != null) {
getThreadId(recipient)?.let {
setExpirationConfiguration(
getExpirationConfiguration(it)?.takeIf { it.updatedTimestampMs > timestamp }
?: ExpirationConfiguration(it, contact.expiryMode, timestamp)
)
}
}
setRecipientHash(recipient, contact.hashCode().toString())
}

@ -211,7 +211,7 @@ interface StorageProtocol {
fun setContact(contact: Contact)
fun getRecipientForThread(threadId: Long): Recipient?
fun getRecipientSettings(address: Address): RecipientSettings?
fun addLibSessionContacts(contacts: List<LibSessionContact>, timestamp: Long)
fun addLibSessionContacts(contacts: List<LibSessionContact>, timestamp: Long?)
fun hasAutoDownloadFlagBeenSet(recipient: Recipient): Boolean
fun addContacts(contacts: List<ConfigurationMessage.Contact>)
fun shouldAutoDownloadAttachments(recipient: Recipient): Boolean

@ -196,6 +196,8 @@ interface TextSecurePreferences {
fun getEnvironment(): Environment
fun setEnvironment(value: Environment)
var migratedToGroupV2Config: Boolean
companion object {
val TAG = TextSecurePreferences::class.simpleName
@ -284,6 +286,7 @@ interface TextSecurePreferences {
const val SELECTED_ACCENT_COLOR = "selected_accent_color"
const val LAST_VERSION_CHECK = "pref_last_version_check"
const val ENVIRONMENT = "debug_environment"
const val MIGRATED_TO_GROUP_V2_CONFIG = "migrated_to_group_v2_config"
const val HAS_RECEIVED_LEGACY_CONFIG = "has_received_legacy_config"
const val HAS_FORCED_NEW_CONFIG = "has_forced_new_config"
@ -989,6 +992,10 @@ class AppTextSecurePreferences @Inject constructor(
private val localNumberState = MutableStateFlow(getStringPreference(TextSecurePreferences.LOCAL_NUMBER_PREF, null))
override var migratedToGroupV2Config: Boolean
get() = getBooleanPreference(TextSecurePreferences.MIGRATED_TO_GROUP_V2_CONFIG, false)
set(value) = setBooleanPreference(TextSecurePreferences.MIGRATED_TO_GROUP_V2_CONFIG, value)
override fun getConfigurationMessageSynced(): Boolean {
return getBooleanPreference(TextSecurePreferences.CONFIGURATION_SYNCED, false)
}

Loading…
Cancel
Save