From d78dc833070d04db99388ed580189dfd842482cf Mon Sep 17 00:00:00 2001 From: Ryan ZHAO Date: Thu, 22 Apr 2021 10:48:19 +1000 Subject: [PATCH] minor refactor on storing display names --- .../org/thoughtcrime/securesms/database/Storage.kt | 14 -------------- .../securesms/sskenvironment/ProfileManager.kt | 14 ++++++++++++-- .../libsession/messaging/StorageProtocol.kt | 3 --- .../sending_receiving/MessageReceiverHandler.kt | 8 +++++--- .../messaging/threads/recipients/Recipient.java | 2 ++ .../session/libsession/utilities/SSKEnvironment.kt | 3 ++- 6 files changed, 21 insertions(+), 23 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 0a58ac42fd..97ba450ddd 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt @@ -93,12 +93,6 @@ class Storage(context: Context, helper: SQLCipherOpenHelper) : Database(context, return database.getDisplayName(recipientPublicKey) } - override fun setProfileKeyForRecipient(recipientPublicKey: String, profileKey: ByteArray) { - val address = Address.fromSerialized(recipientPublicKey) - val recipient = Recipient.from(context, address, false) - DatabaseFactory.getRecipientDatabase(context).setProfileKey(recipient, profileKey) - } - override fun getOrGenerateRegistrationID(): Int { var registrationID = TextSecurePreferences.getLocalRegistrationId(context) if (registrationID == 0) { @@ -529,14 +523,6 @@ class Storage(context: Context, helper: SQLCipherOpenHelper) : Database(context, return DatabaseFactory.getLokiUserDatabase(context).getDisplayName(publicKey) } - override fun setDisplayName(publicKey: String, newName: String) { - DatabaseFactory.getLokiUserDatabase(context).setDisplayName(publicKey, newName) - } - - override fun getServerDisplayName(serverID: String, publicKey: String): String? { - return DatabaseFactory.getLokiUserDatabase(context).getServerDisplayName(serverID, publicKey) - } - override fun getProfilePictureURL(publicKey: String): String? { return DatabaseFactory.getLokiUserDatabase(context).getProfilePictureURL(publicKey) } 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 53781d1ab7..f213bb3057 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/sskenvironment/ProfileManager.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/sskenvironment/ProfileManager.kt @@ -5,12 +5,22 @@ import org.session.libsession.messaging.threads.recipients.Recipient import org.session.libsession.utilities.SSKEnvironment import org.thoughtcrime.securesms.ApplicationContext import org.thoughtcrime.securesms.database.DatabaseFactory -import org.thoughtcrime.securesms.database.RecipientDatabase import org.thoughtcrime.securesms.jobs.RetrieveProfileAvatarJob class ProfileManager: SSKEnvironment.ProfileManagerProtocol { override fun setDisplayName(context: Context, recipient: Recipient, displayName: String) { - DatabaseFactory.getLokiUserDatabase(context).setDisplayName(recipient.address.serialize(), displayName) + val database = DatabaseFactory.getLokiUserDatabase(context) + val publicKey = recipient.address.serialize() + if (recipient.profileName == null) { + // Migrate the profile name in LokiUserDatabase to recipient + database.getDisplayName(publicKey)?.let { setProfileName(context, recipient, it) } + } + database.setDisplayName(publicKey, displayName) + } + + override fun setProfileName(context: Context, recipient: Recipient, profileName: String) { + val database = DatabaseFactory.getRecipientDatabase(context) + database.setProfileName(recipient, profileName) } override fun setProfilePictureURL(context: Context, recipient: Recipient, profilePictureURL: String) { diff --git a/libsession/src/main/java/org/session/libsession/messaging/StorageProtocol.kt b/libsession/src/main/java/org/session/libsession/messaging/StorageProtocol.kt index da84c8f6e8..236555234e 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/StorageProtocol.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/StorageProtocol.kt @@ -36,7 +36,6 @@ interface StorageProtocol { fun getProfileKeyForRecipient(recipientPublicKey: String): ByteArray? fun getDisplayNameForRecipient(recipientPublicKey: String): String? - fun setProfileKeyForRecipient(recipientPublicKey: String, profileKey: ByteArray) // Signal Protocol @@ -145,8 +144,6 @@ interface StorageProtocol { // Loki User fun getDisplayName(publicKey: String): String? - fun setDisplayName(publicKey: String, newName: String) - fun getServerDisplayName(serverID: String, publicKey: String): String? fun getProfilePictureURL(publicKey: String): String? // Recipient diff --git a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageReceiverHandler.kt b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageReceiverHandler.kt index fc1b28e1cc..2deea295c5 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageReceiverHandler.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageReceiverHandler.kt @@ -129,14 +129,16 @@ private fun MessageReceiver.handleConfigurationMessage(message: ConfigurationMes if (allOpenGroups.contains(openGroup)) continue storage.addOpenGroup(openGroup, 1) } + val profileManager = SSKEnvironment.shared.profileManager + val recipient = Recipient.from(context, Address.fromSerialized(userPublicKey), false) if (message.displayName.isNotEmpty()) { TextSecurePreferences.setProfileName(context, message.displayName) - storage.setDisplayName(userPublicKey, message.displayName) + profileManager.setProfileName(context, recipient, message.displayName) } if (message.profileKey.isNotEmpty()) { val profileKey = Base64.encodeBytes(message.profileKey) ProfileKeyUtil.setEncodedProfileKey(context, profileKey) - storage.setProfileKeyForRecipient(userPublicKey, message.profileKey) + profileManager.setProfileKey(context, recipient, message.profileKey) // handle profile photo if (!message.profilePicture.isNullOrEmpty() && TextSecurePreferences.getProfilePictureURL(context) != message.profilePicture) { storage.setUserProfilePictureUrl(message.profilePicture!!) @@ -160,7 +162,7 @@ fun MessageReceiver.handleVisibleMessage(message: VisibleMessage, proto: SignalS // Update the user's local name if the message came from their master device TextSecurePreferences.setProfileName(context, displayName) } - profileManager.setDisplayName(context, recipient, displayName) + profileManager.setProfileName(context, recipient, displayName) } if (recipient.profileKey == null || !MessageDigest.isEqual(recipient.profileKey, newProfile.profileKey)) { profileManager.setProfileKey(context, recipient, newProfile.profileKey!!) diff --git a/libsession/src/main/java/org/session/libsession/messaging/threads/recipients/Recipient.java b/libsession/src/main/java/org/session/libsession/messaging/threads/recipients/Recipient.java index 1c9463ec38..b985eb1c42 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/threads/recipients/Recipient.java +++ b/libsession/src/main/java/org/session/libsession/messaging/threads/recipients/Recipient.java @@ -289,6 +289,8 @@ public class Recipient implements RecipientModifiedListener { String displayName = MessagingConfiguration.shared.getStorage().getDisplayName(this.address.toString()); if (displayName != null) { return displayName; } + if (this.profileName != null) { return this.profileName; } + if (this.name == null && isMmsGroupRecipient()) { List names = new LinkedList<>(); 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 081300bc96..603a449f1a 100644 --- a/libsession/src/main/java/org/session/libsession/utilities/SSKEnvironment.kt +++ b/libsession/src/main/java/org/session/libsession/utilities/SSKEnvironment.kt @@ -29,7 +29,8 @@ class SSKEnvironment( const val NAME_PADDED_LENGTH = 26 } - fun setDisplayName(context: Context, recipient: Recipient, displayName: String) + fun setDisplayName(context: Context, recipient: Recipient, displayName: String) // Client-side Nickname + fun setProfileName(context: Context, recipient: Recipient, profileName: String) fun setProfilePictureURL(context: Context, recipient: Recipient, profilePictureURL: String) fun setProfileKey(context: Context, recipient: Recipient, profileKey: ByteArray) fun setUnidentifiedAccessMode(context: Context, recipient: Recipient, unidentifiedAccessMode: Recipient.UnidentifiedAccessMode)