From e06f9ee79a091c8f8c461beef0a3a9cbfa324cae Mon Sep 17 00:00:00 2001 From: 0x330a <92654767+0x330a@users.noreply.github.com> Date: Wed, 11 Oct 2023 10:40:50 +1100 Subject: [PATCH] refactor: change storage adding contacts to create one on ones with a non hidden thread priority and base for testing storage expiration merges from fake polling --- .../network/loki/messenger/LibSessionTests.kt | 51 ++++++++++++++++++- .../securesms/database/Storage.kt | 5 +- .../sending_receiving/pollers/Poller.kt | 1 - 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/app/src/androidTest/java/network/loki/messenger/LibSessionTests.kt b/app/src/androidTest/java/network/loki/messenger/LibSessionTests.kt index 03a1441d8f..fd776fd514 100644 --- a/app/src/androidTest/java/network/loki/messenger/LibSessionTests.kt +++ b/app/src/androidTest/java/network/loki/messenger/LibSessionTests.kt @@ -7,16 +7,22 @@ import androidx.test.filters.SmallTest import androidx.test.platform.app.InstrumentationRegistry import network.loki.messenger.libsession_util.ConfigBase import network.loki.messenger.libsession_util.Contacts +import network.loki.messenger.libsession_util.ConversationVolatileConfig import network.loki.messenger.libsession_util.util.Contact +import network.loki.messenger.libsession_util.util.Conversation import network.loki.messenger.libsession_util.util.ExpiryMode import org.junit.Before import org.junit.Test import org.junit.runner.RunWith import org.mockito.kotlin.argThat +import org.mockito.kotlin.argWhere import org.mockito.kotlin.eq import org.mockito.kotlin.spy import org.mockito.kotlin.verify import org.session.libsession.messaging.MessagingModuleConfiguration +import org.session.libsession.messaging.messages.ExpirationConfiguration +import org.session.libsession.snode.SnodeAPI +import org.session.libsession.utilities.Address import org.session.libsession.utilities.TextSecurePreferences import org.session.libsignal.utilities.KeyHelper import org.session.libsignal.utilities.hexEncodedPublicKey @@ -50,13 +56,22 @@ class LibSessionTests { private fun buildContactMessage(contactList: List): ByteArray { val (key,_) = maybeGetUserInfo()!! - val contacts = Contacts.Companion.newInstance(key) + val contacts = Contacts.newInstance(key) contactList.forEach { contact -> contacts.set(contact) } return contacts.push().config } + private fun buildVolatileMessage(conversations: List): ByteArray { + val (key, _) = maybeGetUserInfo()!! + val volatile = ConversationVolatileConfig.newInstance(key) + conversations.forEach { conversation -> + volatile.set(conversation) + } + return volatile.push().config + } + private fun fakePollNewConfig(configBase: ConfigBase, toMerge: ByteArray) { configBase.merge(nextFakeHash to toMerge) MessagingModuleConfiguration.shared.configFactory.persist(configBase, System.currentTimeMillis()) @@ -99,4 +114,38 @@ class LibSessionTests { verify(storageSpy).setRecipientApproved(argThat { address.serialize() == newContactId }, eq(true)) } + @Test + fun test_expected_configs() { + val app = InstrumentationRegistry.getInstrumentation().targetContext.applicationContext as ApplicationContext + val storageSpy = spy(app.storage) + app.storage = storageSpy + + val randomRecipient = randomSessionId() + val newContact = Contact( + id = randomRecipient, + approved = true, + expiryMode = ExpiryMode.AfterSend(1000) + ) + val newConvo = Conversation.OneToOne( + randomRecipient, + SnodeAPI.nowWithOffset, + false + ) + val volatiles = MessagingModuleConfiguration.shared.configFactory.convoVolatile!! + val contacts = MessagingModuleConfiguration.shared.configFactory.contacts!! + val newContactMerge = buildContactMessage(listOf(newContact)) + val newVolatileMerge = buildVolatileMessage(listOf(newConvo)) + val expConfig = ExpirationConfiguration() + val recipientAddress = Address.fromSerialized(randomRecipient) + fakePollNewConfig(contacts, newContactMerge) + fakePollNewConfig(volatiles, newVolatileMerge) + val threadId = storageSpy.getThreadId(recipientAddress)!! +// whenever(storageSpy.getExpirationConfiguration(eq(threadId))).thenReturn(expConfig) +// doNothing().whenever(storageSpy).setExpirationConfiguration(any()) +// verify(storageSpy).getExpirationConfiguration(eq(threadId)) + verify(storageSpy).setExpirationConfiguration(argWhere { config -> + config.expiryMode is ExpiryMode.AfterSend + }) + } + } \ No newline at end of file diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt b/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt index f6d3c25cae..92a3cc4322 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt @@ -1217,7 +1217,7 @@ open class Storage( deleteConversation(conversationThreadId) } } else { - getThreadId(fromSerialized(contact.id))?.let { conversationThreadId -> + getOrCreateThreadIdFor(fromSerialized(contact.id)).let { conversationThreadId -> setPinned(conversationThreadId, contact.priority == PRIORITY_PINNED) } } @@ -1231,8 +1231,7 @@ open class Storage( contact.expiryMode, timestamp ) - DatabaseComponent.get(context).expirationConfigurationDatabase() - .setExpirationConfiguration(expiration) + setExpirationConfiguration(expiration) } } setRecipientHash(recipient, contact.hashCode().toString()) diff --git a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/pollers/Poller.kt b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/pollers/Poller.kt index 8fbe7eb5f6..39ed79de1e 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/pollers/Poller.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/pollers/Poller.kt @@ -214,7 +214,6 @@ class Poller(private val configFactory: ConfigFactoryProtocol, debounceTimer: Ti val responseList = (rawResponses["results"] as List) // in case we had null configs, the array won't be fully populated // index of the sparse array key iterator should be the request index, with the key being the namespace - // TODO: add in specific ordering of config namespaces for processing listOfNotNull( configFactory.user?.configNamespace(), configFactory.contacts?.configNamespace(),