From c741ec5093214311252f36a249c4155aff33d64f Mon Sep 17 00:00:00 2001 From: 0x330a <92654767+0x330a@users.noreply.github.com> Date: Fri, 23 Jun 2023 14:26:40 +1000 Subject: [PATCH] fix: maybe fix avatar download for new messages --- .../securesms/database/Storage.kt | 6 +++++ .../sskenvironment/ProfileManager.kt | 25 ++++++++++++------- .../libsession/database/StorageProtocol.kt | 1 + 3 files changed, 23 insertions(+), 9 deletions(-) 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 b99ba34ab2..cb81991ddd 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt @@ -184,6 +184,12 @@ open class Storage(context: Context, helper: SQLCipherOpenHelper, private val co database.setProfileAvatar(recipient, profileAvatar) } + override fun setProfilePicture(recipient: Recipient, newProfilePicture: String?, newProfileKey: ByteArray?) { + val db = DatabaseComponent.get(context).recipientDatabase() + db.setProfileAvatar(recipient, newProfilePicture) + db.setProfileKey(recipient, newProfileKey) + } + override fun setUserProfilePicture(newProfilePicture: String?, newProfileKey: ByteArray?) { val ourRecipient = fromSerialized(getUserPublicKey()!!).let { Recipient.from(context, it, false) diff --git a/app/src/main/java/org/thoughtcrime/securesms/sskenvironment/ProfileManager.kt b/app/src/main/java/org/thoughtcrime/securesms/sskenvironment/ProfileManager.kt index 83b7e67f5d..2e65e94810 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/sskenvironment/ProfileManager.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/sskenvironment/ProfileManager.kt @@ -55,15 +55,18 @@ class ProfileManager(private val context: Context, private val configFactory: Co profilePictureURL: String?, profileKey: ByteArray? ) { - val job = RetrieveProfileAvatarJob(profilePictureURL, recipient.address) - if (DatabaseComponent - .get(context) - .sessionJobDatabase() - .getAllJobs(RetrieveProfileAvatarJob.KEY).none { - (it.value as? RetrieveProfileAvatarJob)?.recipientAddress == recipient.address - }) { - JobQueue.shared.add(job) - } + val hasPendingDownload = DatabaseComponent + .get(context) + .sessionJobDatabase() + .getAllJobs(RetrieveProfileAvatarJob.KEY).any { + (it.value as? RetrieveProfileAvatarJob)?.recipientAddress == recipient.address + } + val resolved = recipient.resolve() + DatabaseComponent.get(context).storage().setProfilePicture( + recipient = resolved, + newProfileKey = profileKey, + newProfilePicture = profilePictureURL + ) val sessionID = recipient.address.serialize() val contactDatabase = DatabaseComponent.get(context).sessionContactDatabase() var contact = contactDatabase.getContactWithSessionID(sessionID) @@ -75,6 +78,10 @@ class ProfileManager(private val context: Context, private val configFactory: Co contactDatabase.setContact(contact) } contactUpdatedInternal(contact) + if (!hasPendingDownload) { + val job = RetrieveProfileAvatarJob(profilePictureURL, recipient.address) + JobQueue.shared.add(job) + } } override fun setUnidentifiedAccessMode(context: Context, recipient: Recipient, unidentifiedAccessMode: Recipient.UnidentifiedAccessMode) { diff --git a/libsession/src/main/java/org/session/libsession/database/StorageProtocol.kt b/libsession/src/main/java/org/session/libsession/database/StorageProtocol.kt index c4509df09e..aad6bc5674 100644 --- a/libsession/src/main/java/org/session/libsession/database/StorageProtocol.kt +++ b/libsession/src/main/java/org/session/libsession/database/StorageProtocol.kt @@ -41,6 +41,7 @@ interface StorageProtocol { fun getUserX25519KeyPair(): ECKeyPair fun getUserProfile(): Profile fun setProfileAvatar(recipient: Recipient, profileAvatar: String?) + fun setProfilePicture(recipient: Recipient, newProfilePicture: String?, newProfileKey: ByteArray?) fun setUserProfilePicture(newProfilePicture: String?, newProfileKey: ByteArray?) fun clearUserPic() // Signal