minor refactor on storing display names

This commit is contained in:
Ryan ZHAO 2021-04-22 10:48:19 +10:00
parent 9f26436041
commit d78dc83307
6 changed files with 21 additions and 23 deletions

View File

@ -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)
}

View File

@ -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) {

View File

@ -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

View File

@ -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!!)

View File

@ -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<String> names = new LinkedList<>();

View File

@ -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)