From fb7dcf58ca1ab98a3aeffebd859b662a36669514 Mon Sep 17 00:00:00 2001 From: 0x330a <92654767+0x330a@users.noreply.github.com> Date: Mon, 22 May 2023 17:30:15 +1000 Subject: [PATCH] [wip] fix: trying to consolidate prof pic and key properly --- .../securesms/database/Storage.kt | 17 ++++------ .../jobs/RetrieveProfileAvatarJob.java | 11 ++++--- .../sskenvironment/ProfileManager.kt | 31 +++++++------------ .../libsession/database/StorageProtocol.kt | 2 +- .../ReceivedMessageHandler.kt | 8 ++--- .../libsession/utilities/SSKEnvironment.kt | 3 +- 6 files changed, 28 insertions(+), 44 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 ddfd689760..4c99e9fe66 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt @@ -178,11 +178,12 @@ open class Storage(context: Context, helper: SQLCipherOpenHelper, private val co return Profile(displayName, profileKey, profilePictureUrl) } - override fun setUserProfilePictureURL(newProfilePicture: String?) { + override fun setUserProfilePicture(newProfilePicture: String?, newProfileKey: ByteArray?) { val ourRecipient = fromSerialized(getUserPublicKey()!!).let { Recipient.from(context, it, false) } TextSecurePreferences.setProfilePictureURL(context, newProfilePicture) + TextSecurePreferences.setProfileKey(context, newProfileKey?.let { Base64.encodeBytes(it) }) ApplicationContext.getInstance(context).jobManager.add(RetrieveProfileAvatarJob(ourRecipient, newProfilePicture)) } @@ -427,10 +428,7 @@ open class Storage(context: Context, helper: SQLCipherOpenHelper, private val co clearUserPic() } else if (userPic.key.isNotEmpty() && userPic.url.isNotEmpty() && TextSecurePreferences.getProfilePictureURL(context) != userPic.url) { - val profileKey = Base64.encodeBytes(userPic.key) - ProfileKeyUtil.setEncodedProfileKey(context, profileKey) - profileManager.setProfileKey(context, recipient, userPic.key) - setUserProfilePictureURL(userPic.url) + setUserProfilePicture(userPic.url, userPic.key) } if (userProfile.getNtsPriority() == PRIORITY_HIDDEN) { // delete nts thread if needed @@ -458,10 +456,9 @@ open class Storage(context: Context, helper: SQLCipherOpenHelper, private val co TextSecurePreferences.setProfileKey(context, null) TextSecurePreferences.setProfileAvatarId(context, 0) ProfileKeyUtil.setEncodedProfileKey(context, null) - SSKEnvironment.shared.profileManager.setProfileKey(context, recipient, null) recipientDatabase.setProfileAvatar(recipient, null) - setUserProfilePictureURL(null) + setUserProfilePicture(null, null) Recipient.removeCached(fromSerialized(userPublicKey)) configFactory.user?.setPic(UserPic.DEFAULT) ConfigurationMessageUtilities.forceSyncConfigurationNowIfNeeded(context) @@ -1124,9 +1121,8 @@ open class Storage(context: Context, helper: SQLCipherOpenHelper, private val co if (contact.profilePicture != UserPic.DEFAULT) { val (url, key) = contact.profilePicture if (key.size != ProfileKeyUtil.PROFILE_KEY_BYTES) return@forEach - profileManager.setProfileKey(context, recipient, key) + profileManager.setProfilePicture(context, recipient, url, key) profileManager.setUnidentifiedAccessMode(context, recipient, Recipient.UnidentifiedAccessMode.UNKNOWN) - profileManager.setProfilePictureURL(context, recipient, url) } if (contact.priority == PRIORITY_HIDDEN) { getThreadId(fromSerialized(contact.id))?.let { conversationThreadId -> @@ -1342,9 +1338,8 @@ open class Storage(context: Context, helper: SQLCipherOpenHelper, private val co val profileKeyChanged = (sender.profileKey == null || !MessageDigest.isEqual(sender.profileKey, newProfileKey)) if ((profileKeyValid && profileKeyChanged) || (profileKeyValid && needsProfilePicture)) { - profileManager.setProfileKey(context, sender, newProfileKey!!) + profileManager.setProfilePicture(context, sender, profile.profilePictureURL!!, newProfileKey!!) profileManager.setUnidentifiedAccessMode(context, sender, Recipient.UnidentifiedAccessMode.UNKNOWN) - profileManager.setProfilePictureURL(context, sender, profile.profilePictureURL!!) } } threadDB.setHasSent(threadId, true) diff --git a/app/src/main/java/org/thoughtcrime/securesms/jobs/RetrieveProfileAvatarJob.java b/app/src/main/java/org/thoughtcrime/securesms/jobs/RetrieveProfileAvatarJob.java index 7331f879cb..b4f61a694b 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/jobs/RetrieveProfileAvatarJob.java +++ b/app/src/main/java/org/thoughtcrime/securesms/jobs/RetrieveProfileAvatarJob.java @@ -106,14 +106,15 @@ public class RetrieveProfileAvatarJob extends BaseJob { Util.copy(avatarStream, new FileOutputStream(decryptDestination)); decryptDestination.renameTo(AvatarHelper.getAvatarFile(context, recipient.getAddress())); + if (recipient.isLocalNumber()) { + TextSecurePreferences.setProfileAvatarId(context, new SecureRandom().nextInt()); + } + database.setProfileAvatar(recipient, profileAvatar); + } catch (Exception e) { + } finally { if (downloadDestination != null) downloadDestination.delete(); } - - if (recipient.isLocalNumber()) { - TextSecurePreferences.setProfileAvatarId(context, new SecureRandom().nextInt()); - } - database.setProfileAvatar(recipient, profileAvatar); } @Override 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 2ea5369390..878ac9a433 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/sskenvironment/ProfileManager.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/sskenvironment/ProfileManager.kt @@ -49,24 +49,12 @@ class ProfileManager(private val context: Context, private val configFactory: Co contactUpdatedInternal(contact) } - override fun setProfilePictureURL(context: Context, recipient: Recipient, profilePictureURL: String) { - val job = RetrieveProfileAvatarJob(recipient, profilePictureURL) - val jobManager = ApplicationContext.getInstance(context).jobManager - jobManager.add(job) - if (recipient.isLocalNumber) return - val sessionID = recipient.address.serialize() - val contactDatabase = DatabaseComponent.get(context).sessionContactDatabase() - var contact = contactDatabase.getContactWithSessionID(sessionID) - if (contact == null) contact = Contact(sessionID) - contact.threadID = DatabaseComponent.get(context).storage().getThreadId(recipient.address) - if (contact.profilePictureURL != profilePictureURL) { - contact.profilePictureURL = profilePictureURL - contactDatabase.setContact(contact) - } - contactUpdatedInternal(contact) - } - - override fun setProfileKey(context: Context, recipient: Recipient, profileKey: ByteArray?) { + override fun setProfilePicture( + context: Context, + recipient: Recipient, + profilePictureURL: String?, + profileKey: ByteArray? + ) { // New API val sessionID = recipient.address.serialize() // Old API @@ -78,13 +66,18 @@ class ProfileManager(private val context: Context, private val configFactory: Co var contact = contactDatabase.getContactWithSessionID(sessionID) if (contact == null) contact = Contact(sessionID) contact.threadID = DatabaseComponent.get(context).storage().getThreadId(recipient.address) - if (!contact.profilePictureEncryptionKey.contentEquals(profileKey)) { + if (!contact.profilePictureEncryptionKey.contentEquals(profileKey) || contact.profilePictureURL != profilePictureURL) { contact.profilePictureEncryptionKey = profileKey + contact.profilePictureURL = profilePictureURL contactDatabase.setContact(contact) } + val job = RetrieveProfileAvatarJob(recipient, profilePictureURL) + val jobManager = ApplicationContext.getInstance(context).jobManager + jobManager.add(job) contactUpdatedInternal(contact) } + override fun setUnidentifiedAccessMode(context: Context, recipient: Recipient, unidentifiedAccessMode: Recipient.UnidentifiedAccessMode) { val database = DatabaseComponent.get(context).recipientDatabase() database.setUnidentifiedAccessMode(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 59982218ed..d180114e34 100644 --- a/libsession/src/main/java/org/session/libsession/database/StorageProtocol.kt +++ b/libsession/src/main/java/org/session/libsession/database/StorageProtocol.kt @@ -40,7 +40,7 @@ interface StorageProtocol { fun getUserPublicKey(): String? fun getUserX25519KeyPair(): ECKeyPair fun getUserProfile(): Profile - fun setUserProfilePictureURL(newProfilePicture: String?) + fun setUserProfilePicture(newProfilePicture: String?, newProfileKey: ByteArray?) fun clearUserPic() // Signal fun getOrGenerateRegistrationID(): Int diff --git a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/ReceivedMessageHandler.kt b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/ReceivedMessageHandler.kt index 9db1b9b79b..f99d1c2cd7 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/ReceivedMessageHandler.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/ReceivedMessageHandler.kt @@ -188,10 +188,7 @@ private fun handleConfigurationMessage(message: ConfigurationMessage) { && TextSecurePreferences.getProfilePictureURL(context) != message.profilePicture) { val profileKey = Base64.encodeBytes(message.profileKey) ProfileKeyUtil.setEncodedProfileKey(context, profileKey) - profileManager.setProfileKey(context, recipient, message.profileKey) - if (!message.profilePicture.isNullOrEmpty() && TextSecurePreferences.getProfilePictureURL(context) != message.profilePicture) { - storage.setUserProfilePictureURL(message.profilePicture!!) - } + profileManager.setProfilePicture(context, recipient, message.profilePicture, message.profileKey) } storage.addContacts(message.contacts) } @@ -264,9 +261,8 @@ fun MessageReceiver.handleVisibleMessage( val profileKeyChanged = (recipient.profileKey == null || !MessageDigest.isEqual(recipient.profileKey, newProfileKey)) if ((profileKeyValid && profileKeyChanged) || (profileKeyValid && needsProfilePicture)) { - profileManager.setProfileKey(context, recipient, newProfileKey!!) + profileManager.setProfilePicture(context, recipient, profile.profilePictureURL, newProfileKey) profileManager.setUnidentifiedAccessMode(context, recipient, Recipient.UnidentifiedAccessMode.UNKNOWN) - profileManager.setProfilePictureURL(context, recipient, profile.profilePictureURL!!) } } } diff --git a/libsession/src/main/java/org/session/libsession/utilities/SSKEnvironment.kt b/libsession/src/main/java/org/session/libsession/utilities/SSKEnvironment.kt index d9a86f1745..1fe8e5a016 100644 --- a/libsession/src/main/java/org/session/libsession/utilities/SSKEnvironment.kt +++ b/libsession/src/main/java/org/session/libsession/utilities/SSKEnvironment.kt @@ -31,8 +31,7 @@ class SSKEnvironment( fun setNickname(context: Context, recipient: Recipient, nickname: String?) fun setName(context: Context, recipient: Recipient, name: String?) - fun setProfilePictureURL(context: Context, recipient: Recipient, profilePictureURL: String) - fun setProfileKey(context: Context, recipient: Recipient, profileKey: ByteArray?) + fun setProfilePicture(context: Context, recipient: Recipient, profilePictureURL: String?, profileKey: ByteArray?) fun setUnidentifiedAccessMode(context: Context, recipient: Recipient, unidentifiedAccessMode: Recipient.UnidentifiedAccessMode) fun contactUpdatedInternal(contact: Contact) }