Tidy up dependency injection

This commit is contained in:
SessionHero01 2024-10-07 15:53:51 +11:00
parent 8c9dfdf5cd
commit 4998cebfd3
No known key found for this signature in database
7 changed files with 51 additions and 61 deletions

View File

@ -3,6 +3,7 @@ package org.thoughtcrime.securesms.database
import android.content.Context import android.content.Context
import android.net.Uri import android.net.Uri
import com.goterl.lazysodium.utils.KeyPair import com.goterl.lazysodium.utils.KeyPair
import dagger.hilt.android.qualifiers.ApplicationContext
import java.security.MessageDigest import java.security.MessageDigest
import network.loki.messenger.libsession_util.ConfigBase.Companion.PRIORITY_HIDDEN 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.ConfigBase.Companion.PRIORITY_PINNED
@ -85,13 +86,15 @@ import org.thoughtcrime.securesms.groups.OpenGroupManager
import org.thoughtcrime.securesms.mms.PartAuthority import org.thoughtcrime.securesms.mms.PartAuthority
import org.thoughtcrime.securesms.util.SessionMetaProtocol import org.thoughtcrime.securesms.util.SessionMetaProtocol
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton
import network.loki.messenger.libsession_util.util.Contact as LibSessionContact import network.loki.messenger.libsession_util.util.Contact as LibSessionContact
import network.loki.messenger.libsession_util.util.GroupMember as LibSessionGroupMember import network.loki.messenger.libsession_util.util.GroupMember as LibSessionGroupMember
private const val TAG = "Storage" private const val TAG = "Storage"
@Singleton
open class Storage @Inject constructor( open class Storage @Inject constructor(
context: Context, @ApplicationContext context: Context,
helper: SQLCipherOpenHelper, helper: SQLCipherOpenHelper,
private val configFactory: ConfigFactory, private val configFactory: ConfigFactory,
private val jobDatabase: SessionJobDatabase, private val jobDatabase: SessionJobDatabase,

View File

@ -9,6 +9,7 @@ import dagger.hilt.EntryPoint
import dagger.hilt.InstallIn import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent import dagger.hilt.components.SingletonComponent
import org.session.libsession.database.StorageProtocol
import org.session.libsession.messaging.groups.GroupManagerV2 import org.session.libsession.messaging.groups.GroupManagerV2
import org.session.libsession.utilities.AppTextSecurePreferences import org.session.libsession.utilities.AppTextSecurePreferences
import org.session.libsession.utilities.ConfigFactoryProtocol import org.session.libsession.utilities.ConfigFactoryProtocol
@ -17,6 +18,7 @@ import org.session.libsession.utilities.TextSecurePreferences
import org.session.libsession.utilities.Toaster import org.session.libsession.utilities.Toaster
import org.session.libsignal.database.LokiAPIDatabaseProtocol import org.session.libsignal.database.LokiAPIDatabaseProtocol
import org.thoughtcrime.securesms.database.LokiAPIDatabase import org.thoughtcrime.securesms.database.LokiAPIDatabase
import org.thoughtcrime.securesms.database.Storage
import org.thoughtcrime.securesms.groups.GroupManagerV2Impl import org.thoughtcrime.securesms.groups.GroupManagerV2Impl
import org.thoughtcrime.securesms.repository.ConversationRepository import org.thoughtcrime.securesms.repository.ConversationRepository
import org.thoughtcrime.securesms.repository.DefaultConversationRepository import org.thoughtcrime.securesms.repository.DefaultConversationRepository
@ -42,8 +44,6 @@ abstract class AppModule {
@Binds @Binds
abstract fun bindConfigFactory(configFactory: ConfigFactory): ConfigFactoryProtocol abstract fun bindConfigFactory(configFactory: ConfigFactory): ConfigFactoryProtocol
@Binds
abstract fun bindLokiAPIDatabaseProtocol(lokiAPIDatabase: LokiAPIDatabase): LokiAPIDatabaseProtocol
} }
@Module @Module

View File

@ -5,6 +5,8 @@ import dagger.Module
import dagger.hilt.InstallIn import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent import dagger.hilt.components.SingletonComponent
import org.session.libsession.database.StorageProtocol import org.session.libsession.database.StorageProtocol
import org.session.libsignal.database.LokiAPIDatabaseProtocol
import org.thoughtcrime.securesms.database.LokiAPIDatabase
import org.thoughtcrime.securesms.database.Storage import org.thoughtcrime.securesms.database.Storage
@Module @Module
@ -14,4 +16,6 @@ abstract class DatabaseBindings {
@Binds @Binds
abstract fun bindStorageProtocol(storage: Storage): StorageProtocol abstract fun bindStorageProtocol(storage: Storage): StorageProtocol
@Binds
abstract fun bindLokiAPIDatabaseProtocol(lokiAPIDatabase: LokiAPIDatabase): LokiAPIDatabaseProtocol
} }

View File

@ -17,7 +17,7 @@ interface DatabaseComponent {
companion object { companion object {
@JvmStatic @JvmStatic
@Deprecated("Should use a properly DI instance of components") @Deprecated("Use Hilt to inject your dependencies instead")
fun get(context: Context) = ApplicationContext.getInstance(context).databaseComponent fun get(context: Context) = ApplicationContext.getInstance(context).databaseComponent
} }

View File

@ -8,11 +8,8 @@ import android.content.Context
import android.content.Intent import android.content.Intent
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.text.format.DateUtils
import android.widget.Toast import android.widget.Toast
import androidx.activity.viewModels import androidx.activity.viewModels
import androidx.annotation.PluralsRes
import androidx.annotation.StringRes
import androidx.core.os.bundleOf import androidx.core.os.bundleOf
import androidx.core.view.isInvisible import androidx.core.view.isInvisible
import androidx.core.view.isVisible import androidx.core.view.isVisible
@ -20,6 +17,8 @@ import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.bumptech.glide.RequestManager
import com.squareup.phrase.Phrase import com.squareup.phrase.Phrase
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@ -34,69 +33,50 @@ import network.loki.messenger.databinding.ActivityHomeBinding
import org.greenrobot.eventbus.EventBus import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode import org.greenrobot.eventbus.ThreadMode
import org.session.libsession.messaging.MessagingModuleConfiguration import org.session.libsession.messaging.groups.GroupManagerV2
import org.session.libsession.messaging.jobs.JobQueue import org.session.libsession.messaging.jobs.JobQueue
import org.session.libsession.messaging.jobs.LibSessionGroupLeavingJob import org.session.libsession.snode.SnodeClock
import org.session.libsession.messaging.sending_receiving.MessageSender
import org.session.libsession.snode.SnodeAPI
import org.session.libsession.utilities.Address import org.session.libsession.utilities.Address
import org.session.libsession.utilities.GroupUtil
import org.session.libsession.utilities.ProfilePictureModifiedEvent import org.session.libsession.utilities.ProfilePictureModifiedEvent
import org.session.libsession.utilities.TextSecurePreferences
import org.session.libsession.utilities.recipients.Recipient
import org.session.libsignal.utilities.AccountId
import org.session.libsignal.utilities.Log
import org.session.libsignal.utilities.ThreadUtils
import org.session.libsignal.utilities.toHexString
import org.session.libsession.utilities.StringSubstitutionConstants.COUNT_KEY
import org.session.libsession.utilities.StringSubstitutionConstants.GROUP_NAME_KEY import org.session.libsession.utilities.StringSubstitutionConstants.GROUP_NAME_KEY
import org.session.libsession.utilities.StringSubstitutionConstants.NAME_KEY import org.session.libsession.utilities.StringSubstitutionConstants.NAME_KEY
import org.session.libsession.utilities.TextSecurePreferences
import org.session.libsession.utilities.recipients.Recipient
import org.session.libsignal.utilities.Log
import org.thoughtcrime.securesms.ApplicationContext import org.thoughtcrime.securesms.ApplicationContext
import org.thoughtcrime.securesms.PassphraseRequiredActionBarActivity import org.thoughtcrime.securesms.PassphraseRequiredActionBarActivity
import org.thoughtcrime.securesms.conversation.start.StartConversationFragment import org.thoughtcrime.securesms.conversation.start.StartConversationFragment
import org.thoughtcrime.securesms.conversation.v2.ConversationActivityV2 import org.thoughtcrime.securesms.conversation.v2.ConversationActivityV2
import org.thoughtcrime.securesms.conversation.v2.menus.ConversationMenuHelper
import org.thoughtcrime.securesms.conversation.v2.utilities.NotificationUtils import org.thoughtcrime.securesms.conversation.v2.utilities.NotificationUtils
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil import org.thoughtcrime.securesms.crypto.IdentityKeyUtil
import org.thoughtcrime.securesms.database.GroupDatabase import org.thoughtcrime.securesms.database.GroupDatabase
import org.thoughtcrime.securesms.database.LokiThreadDatabase
import org.thoughtcrime.securesms.database.MmsSmsDatabase import org.thoughtcrime.securesms.database.MmsSmsDatabase
import org.thoughtcrime.securesms.database.RecipientDatabase import org.thoughtcrime.securesms.database.RecipientDatabase
import org.thoughtcrime.securesms.database.SessionJobDatabase
import org.thoughtcrime.securesms.database.Storage import org.thoughtcrime.securesms.database.Storage
import org.thoughtcrime.securesms.database.ThreadDatabase import org.thoughtcrime.securesms.database.ThreadDatabase
import org.thoughtcrime.securesms.database.model.ThreadRecord import org.thoughtcrime.securesms.database.model.ThreadRecord
import org.thoughtcrime.securesms.dependencies.ConfigFactory import org.thoughtcrime.securesms.dependencies.ConfigFactory
import org.thoughtcrime.securesms.dependencies.DatabaseComponent
import org.thoughtcrime.securesms.groups.OpenGroupManager import org.thoughtcrime.securesms.groups.OpenGroupManager
import org.thoughtcrime.securesms.home.search.GlobalSearchAdapter import org.thoughtcrime.securesms.home.search.GlobalSearchAdapter
import org.thoughtcrime.securesms.home.search.GlobalSearchInputLayout import org.thoughtcrime.securesms.home.search.GlobalSearchInputLayout
import org.thoughtcrime.securesms.home.search.GlobalSearchResult import org.thoughtcrime.securesms.home.search.GlobalSearchResult
import org.thoughtcrime.securesms.home.search.GlobalSearchViewModel import org.thoughtcrime.securesms.home.search.GlobalSearchViewModel
import org.thoughtcrime.securesms.messagerequests.MessageRequestsActivity import org.thoughtcrime.securesms.messagerequests.MessageRequestsActivity
import com.bumptech.glide.Glide
import com.bumptech.glide.RequestManager
import org.session.libsession.messaging.groups.GroupManagerV2
import org.thoughtcrime.securesms.conversation.v2.menus.ConversationMenuHelper
import org.thoughtcrime.securesms.permissions.Permissions import org.thoughtcrime.securesms.permissions.Permissions
import org.thoughtcrime.securesms.preferences.SettingsActivity import org.thoughtcrime.securesms.preferences.SettingsActivity
import org.thoughtcrime.securesms.recoverypassword.RecoveryPasswordActivity import org.thoughtcrime.securesms.recoverypassword.RecoveryPasswordActivity
import org.thoughtcrime.securesms.showMuteDialog import org.thoughtcrime.securesms.showMuteDialog
import org.thoughtcrime.securesms.showSessionDialog import org.thoughtcrime.securesms.showSessionDialog
import org.thoughtcrime.securesms.ui.setThemedContent import org.thoughtcrime.securesms.ui.setThemedContent
import org.thoughtcrime.securesms.util.ConfigurationMessageUtilities
import org.thoughtcrime.securesms.util.IP2Country import org.thoughtcrime.securesms.util.IP2Country
import org.thoughtcrime.securesms.util.RelativeDay
import org.thoughtcrime.securesms.util.disableClipping import org.thoughtcrime.securesms.util.disableClipping
import org.thoughtcrime.securesms.util.push import org.thoughtcrime.securesms.util.push
import org.thoughtcrime.securesms.util.show import org.thoughtcrime.securesms.util.show
import org.thoughtcrime.securesms.util.start import org.thoughtcrime.securesms.util.start
import java.io.IOException
import java.util.Calendar
import java.util.Locale
import javax.inject.Inject import javax.inject.Inject
import kotlin.math.abs
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.hours
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.seconds
// Intent extra keys so we know where we came from // Intent extra keys so we know where we came from
private const val NEW_ACCOUNT = "HomeActivity_NEW_ACCOUNT" private const val NEW_ACCOUNT = "HomeActivity_NEW_ACCOUNT"
@ -120,6 +100,9 @@ class HomeActivity : PassphraseRequiredActionBarActivity(),
@Inject lateinit var textSecurePreferences: TextSecurePreferences @Inject lateinit var textSecurePreferences: TextSecurePreferences
@Inject lateinit var configFactory: ConfigFactory @Inject lateinit var configFactory: ConfigFactory
@Inject lateinit var groupManagerV2: GroupManagerV2 @Inject lateinit var groupManagerV2: GroupManagerV2
@Inject lateinit var lokiThreadDatabase: LokiThreadDatabase
@Inject lateinit var sessionJobDatabase: SessionJobDatabase
@Inject lateinit var clock: SnodeClock
private val globalSearchViewModel by viewModels<GlobalSearchViewModel>() private val globalSearchViewModel by viewModels<GlobalSearchViewModel>()
private val homeViewModel by viewModels<HomeViewModel>() private val homeViewModel by viewModels<HomeViewModel>()
@ -460,7 +443,7 @@ class HomeActivity : PassphraseRequiredActionBarActivity(),
} }
else if (thread.recipient.isCommunityRecipient) { else if (thread.recipient.isCommunityRecipient) {
val threadId = threadDb.getThreadIdIfExistsFor(thread.recipient) val threadId = threadDb.getThreadIdIfExistsFor(thread.recipient)
val openGroup = DatabaseComponent.get(this@HomeActivity).lokiThreadDatabase().getOpenGroupChat(threadId) ?: return@onCopyConversationId Unit val openGroup = lokiThreadDatabase.getOpenGroupChat(threadId) ?: return@onCopyConversationId Unit
val clip = ClipData.newPlainText("Community URL", openGroup.joinURL) val clip = ClipData.newPlainText("Community URL", openGroup.joinURL)
val manager = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager val manager = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
@ -586,7 +569,7 @@ class HomeActivity : PassphraseRequiredActionBarActivity(),
private fun markAllAsRead(thread: ThreadRecord) { private fun markAllAsRead(thread: ThreadRecord) {
lifecycleScope.launch(Dispatchers.Default) { lifecycleScope.launch(Dispatchers.Default) {
MessagingModuleConfiguration.shared.storage.markConversationAsRead(thread.threadId, SnodeAPI.nowWithOffset) storage.markConversationAsRead(thread.threadId, clock.currentTimeMills())
} }
} }
@ -643,11 +626,11 @@ class HomeActivity : PassphraseRequiredActionBarActivity(),
lifecycleScope.launch(Dispatchers.Main) { lifecycleScope.launch(Dispatchers.Main) {
val context = this@HomeActivity val context = this@HomeActivity
// Cancel any outstanding jobs // Cancel any outstanding jobs
DatabaseComponent.get(context).sessionJobDatabase() sessionJobDatabase
.cancelPendingMessageSendJobs(threadID) .cancelPendingMessageSendJobs(threadID)
// Delete the conversation // Delete the conversation
val v2OpenGroup = DatabaseComponent.get(context).lokiThreadDatabase() val v2OpenGroup = lokiThreadDatabase
.getOpenGroupChat(threadID) .getOpenGroupChat(threadID)
if (v2OpenGroup != null) { if (v2OpenGroup != null) {
OpenGroupManager.delete( OpenGroupManager.delete(

View File

@ -22,6 +22,7 @@ import org.session.libsession.messaging.messages.visible.VisibleMessage
import org.session.libsession.messaging.open_groups.OpenGroupApi import org.session.libsession.messaging.open_groups.OpenGroupApi
import org.session.libsession.messaging.sending_receiving.MessageSender import org.session.libsession.messaging.sending_receiving.MessageSender
import org.session.libsession.snode.SnodeAPI import org.session.libsession.snode.SnodeAPI
import org.session.libsession.snode.SnodeClock
import org.session.libsession.snode.utilities.await import org.session.libsession.snode.utilities.await
import org.session.libsession.utilities.Address import org.session.libsession.utilities.Address
import org.session.libsession.utilities.GroupUtil import org.session.libsession.utilities.GroupUtil
@ -43,7 +44,6 @@ import org.thoughtcrime.securesms.database.ThreadDatabase
import org.thoughtcrime.securesms.database.model.MessageRecord import org.thoughtcrime.securesms.database.model.MessageRecord
import org.thoughtcrime.securesms.database.model.ThreadRecord import org.thoughtcrime.securesms.database.model.ThreadRecord
import org.thoughtcrime.securesms.dependencies.ConfigFactory import org.thoughtcrime.securesms.dependencies.ConfigFactory
import org.thoughtcrime.securesms.dependencies.DatabaseComponent
import javax.inject.Inject import javax.inject.Inject
interface ConversationRepository { interface ConversationRepository {
@ -90,6 +90,7 @@ class DefaultConversationRepository @Inject constructor(
private val configFactory: ConfigFactory, private val configFactory: ConfigFactory,
private val contentResolver: ContentResolver, private val contentResolver: ContentResolver,
private val groupManager: GroupManagerV2, private val groupManager: GroupManagerV2,
private val clock: SnodeClock,
) : ConversationRepository { ) : ConversationRepository {
override fun maybeGetRecipientForThreadId(threadId: Long): Recipient? { override fun maybeGetRecipientForThreadId(threadId: Long): Recipient? {
@ -134,13 +135,13 @@ class DefaultConversationRepository @Inject constructor(
val openGroup = lokiThreadDb.getOpenGroupChat(threadId) ?: return val openGroup = lokiThreadDb.getOpenGroupChat(threadId) ?: return
for (contact in contacts) { for (contact in contacts) {
val message = VisibleMessage() val message = VisibleMessage()
message.sentTimestamp = SnodeAPI.nowWithOffset message.sentTimestamp = clock.currentTimeMills()
val openGroupInvitation = OpenGroupInvitation().apply { val openGroupInvitation = OpenGroupInvitation().apply {
name = openGroup.name name = openGroup.name
url = openGroup.joinURL url = openGroup.joinURL
} }
message.openGroupInvitation = openGroupInvitation message.openGroupInvitation = openGroupInvitation
val expirationConfig = DatabaseComponent.get(context).threadDatabase().getOrCreateThreadIdFor(contact).let(storage::getExpirationConfiguration) val expirationConfig = threadDb.getOrCreateThreadIdFor(contact).let(storage::getExpirationConfiguration)
val expiresInMillis = expirationConfig?.expiryMode?.expiryMillis ?: 0 val expiresInMillis = expirationConfig?.expiryMode?.expiryMillis ?: 0
val expireStartedAt = if (expirationConfig?.expiryMode is ExpiryMode.AfterSend) message.sentTimestamp!! else 0 val expireStartedAt = if (expirationConfig?.expiryMode is ExpiryMode.AfterSend) message.sentTimestamp!! else 0
val outgoingTextMessage = OutgoingTextMessage.fromOpenGroupInvitation( val outgoingTextMessage = OutgoingTextMessage.fromOpenGroupInvitation(

View File

@ -1,34 +1,39 @@
package org.thoughtcrime.securesms.sskenvironment package org.thoughtcrime.securesms.sskenvironment
import android.content.Context import android.content.Context
import dagger.hilt.android.qualifiers.ApplicationContext
import network.loki.messenger.libsession_util.util.UserPic import network.loki.messenger.libsession_util.util.UserPic
import org.session.libsession.database.StorageProtocol
import org.session.libsession.messaging.contacts.Contact import org.session.libsession.messaging.contacts.Contact
import org.session.libsession.messaging.jobs.JobQueue import org.session.libsession.messaging.jobs.JobQueue
import org.session.libsession.messaging.jobs.RetrieveProfileAvatarJob import org.session.libsession.messaging.jobs.RetrieveProfileAvatarJob
import org.session.libsession.utilities.ConfigFactoryProtocol
import org.session.libsession.utilities.SSKEnvironment import org.session.libsession.utilities.SSKEnvironment
import org.session.libsession.utilities.TextSecurePreferences import org.session.libsession.utilities.TextSecurePreferences
import org.session.libsession.utilities.recipients.Recipient import org.session.libsession.utilities.recipients.Recipient
import org.session.libsignal.utilities.AccountId import org.session.libsignal.utilities.AccountId
import org.session.libsignal.utilities.IdPrefix import org.session.libsignal.utilities.IdPrefix
import org.thoughtcrime.securesms.dependencies.ConfigFactory import org.thoughtcrime.securesms.database.RecipientDatabase
import org.thoughtcrime.securesms.dependencies.DatabaseComponent import org.thoughtcrime.securesms.database.SessionContactDatabase
import org.thoughtcrime.securesms.database.SessionJobDatabase
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class ProfileManager @Inject constructor( class ProfileManager @Inject constructor(
@ApplicationContext private val context: Context, private val configFactory: ConfigFactoryProtocol,
private val configFactory: ConfigFactory private val storage: StorageProtocol,
private val contactDatabase: SessionContactDatabase,
private val recipientDatabase: RecipientDatabase,
private val jobDatabase: SessionJobDatabase,
private val preferences: TextSecurePreferences,
) : SSKEnvironment.ProfileManagerProtocol { ) : SSKEnvironment.ProfileManagerProtocol {
override fun setNickname(context: Context, recipient: Recipient, nickname: String?) { override fun setNickname(context: Context, recipient: Recipient, nickname: String?) {
if (recipient.isLocalNumber) return if (recipient.isLocalNumber) return
val accountID = recipient.address.serialize() val accountID = recipient.address.serialize()
val contactDatabase = DatabaseComponent.get(context).sessionContactDatabase()
var contact = contactDatabase.getContactWithAccountID(accountID) var contact = contactDatabase.getContactWithAccountID(accountID)
if (contact == null) contact = Contact(accountID) if (contact == null) contact = Contact(accountID)
contact.threadID = DatabaseComponent.get(context).storage().getThreadId(recipient.address) contact.threadID = storage.getThreadId(recipient.address)
if (contact.nickname != nickname) { if (contact.nickname != nickname) {
contact.nickname = nickname contact.nickname = nickname
contactDatabase.setContact(contact) contactDatabase.setContact(contact)
@ -40,17 +45,15 @@ class ProfileManager @Inject constructor(
// New API // New API
if (recipient.isLocalNumber) return if (recipient.isLocalNumber) return
val accountID = recipient.address.serialize() val accountID = recipient.address.serialize()
val contactDatabase = DatabaseComponent.get(context).sessionContactDatabase()
var contact = contactDatabase.getContactWithAccountID(accountID) var contact = contactDatabase.getContactWithAccountID(accountID)
if (contact == null) contact = Contact(accountID) if (contact == null) contact = Contact(accountID)
contact.threadID = DatabaseComponent.get(context).storage().getThreadId(recipient.address) contact.threadID = storage.getThreadId(recipient.address)
if (contact.name != name) { if (contact.name != name) {
contact.name = name contact.name = name
contactDatabase.setContact(contact) contactDatabase.setContact(contact)
} }
// Old API // Old API
val database = DatabaseComponent.get(context).recipientDatabase() recipientDatabase.setProfileName(recipient, name)
database.setProfileName(recipient, name)
recipient.notifyListeners() recipient.notifyListeners()
contactUpdatedInternal(contact) contactUpdatedInternal(contact)
} }
@ -61,23 +64,20 @@ class ProfileManager @Inject constructor(
profilePictureURL: String?, profilePictureURL: String?,
profileKey: ByteArray? profileKey: ByteArray?
) { ) {
val hasPendingDownload = DatabaseComponent val hasPendingDownload = jobDatabase
.get(context)
.sessionJobDatabase()
.getAllJobs(RetrieveProfileAvatarJob.KEY).any { .getAllJobs(RetrieveProfileAvatarJob.KEY).any {
(it.value as? RetrieveProfileAvatarJob)?.recipientAddress == recipient.address (it.value as? RetrieveProfileAvatarJob)?.recipientAddress == recipient.address
} }
val resolved = recipient.resolve() val resolved = recipient.resolve()
DatabaseComponent.get(context).storage().setProfilePicture( storage.setProfilePicture(
recipient = resolved, recipient = resolved,
newProfileKey = profileKey, newProfileKey = profileKey,
newProfilePicture = profilePictureURL newProfilePicture = profilePictureURL
) )
val accountID = recipient.address.serialize() val accountID = recipient.address.serialize()
val contactDatabase = DatabaseComponent.get(context).sessionContactDatabase()
var contact = contactDatabase.getContactWithAccountID(accountID) var contact = contactDatabase.getContactWithAccountID(accountID)
if (contact == null) contact = Contact(accountID) if (contact == null) contact = Contact(accountID)
contact.threadID = DatabaseComponent.get(context).storage().getThreadId(recipient.address) contact.threadID = storage.getThreadId(recipient.address)
if (!contact.profilePictureEncryptionKey.contentEquals(profileKey) || contact.profilePictureURL != profilePictureURL) { if (!contact.profilePictureEncryptionKey.contentEquals(profileKey) || contact.profilePictureURL != profilePictureURL) {
contact.profilePictureEncryptionKey = profileKey contact.profilePictureEncryptionKey = profileKey
contact.profilePictureURL = profilePictureURL contact.profilePictureURL = profilePictureURL
@ -91,12 +91,11 @@ class ProfileManager @Inject constructor(
} }
override fun setUnidentifiedAccessMode(context: Context, recipient: Recipient, unidentifiedAccessMode: Recipient.UnidentifiedAccessMode) { override fun setUnidentifiedAccessMode(context: Context, recipient: Recipient, unidentifiedAccessMode: Recipient.UnidentifiedAccessMode) {
val database = DatabaseComponent.get(context).recipientDatabase() recipientDatabase.setUnidentifiedAccessMode(recipient, unidentifiedAccessMode)
database.setUnidentifiedAccessMode(recipient, unidentifiedAccessMode)
} }
override fun contactUpdatedInternal(contact: Contact): String? { override fun contactUpdatedInternal(contact: Contact): String? {
if (contact.accountID == TextSecurePreferences.getLocalNumber(context)) return null if (contact.accountID == preferences.getLocalNumber()) return null
val accountId = AccountId(contact.accountID) val accountId = AccountId(contact.accountID)
if (accountId.prefix != IdPrefix.STANDARD) return null // only internally store standard account IDs if (accountId.prefix != IdPrefix.STANDARD) return null // only internally store standard account IDs
return configFactory.withMutableUserConfigs { return configFactory.withMutableUserConfigs {