From 733ed3ff5a1ac8eefa8538ae78999b2821798a39 Mon Sep 17 00:00:00 2001 From: ryanzhao Date: Wed, 3 Jun 2020 17:03:35 +1000 Subject: [PATCH] Fix profile picture and display name updating from a slave device --- .../securesms/loki/activities/HomeActivity.kt | 1 + .../loki/protocol/MultiDeviceProtocol.kt | 2 +- .../loki/protocol/SessionMetaProtocol.kt | 26 +++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/org/thoughtcrime/securesms/loki/activities/HomeActivity.kt b/src/org/thoughtcrime/securesms/loki/activities/HomeActivity.kt index eb253a0765..8df5d5f99f 100644 --- a/src/org/thoughtcrime/securesms/loki/activities/HomeActivity.kt +++ b/src/org/thoughtcrime/securesms/loki/activities/HomeActivity.kt @@ -194,6 +194,7 @@ class HomeActivity : PassphraseRequiredActionBarActivity, ConversationClickListe override fun onResume() { super.onResume() + profileButton.update() val isMasterDevice = (TextSecurePreferences.getMasterHexEncodedPublicKey(this) == null) val hasViewedSeed = TextSecurePreferences.getHasViewedSeed(this) if (hasViewedSeed || !isMasterDevice) { diff --git a/src/org/thoughtcrime/securesms/loki/protocol/MultiDeviceProtocol.kt b/src/org/thoughtcrime/securesms/loki/protocol/MultiDeviceProtocol.kt index 72a22c856b..7e803c48b9 100644 --- a/src/org/thoughtcrime/securesms/loki/protocol/MultiDeviceProtocol.kt +++ b/src/org/thoughtcrime/securesms/loki/protocol/MultiDeviceProtocol.kt @@ -197,7 +197,7 @@ object MultiDeviceProtocol { TextSecurePreferences.setMultiDevice(context, true) LokiFileServerAPI.shared.addDeviceLink(deviceLink) org.thoughtcrime.securesms.loki.protocol.SessionMetaProtocol.handleProfileUpdateIfNeeded(context, content) - org.thoughtcrime.securesms.loki.protocol.SessionMetaProtocol.handleProfileKeyUpdateIfNeeded(context, content) + org.thoughtcrime.securesms.loki.protocol.SessionMetaProtocol.handleProfileKey(context, content) } @JvmStatic diff --git a/src/org/thoughtcrime/securesms/loki/protocol/SessionMetaProtocol.kt b/src/org/thoughtcrime/securesms/loki/protocol/SessionMetaProtocol.kt index bf4333059c..a66aed95a3 100644 --- a/src/org/thoughtcrime/securesms/loki/protocol/SessionMetaProtocol.kt +++ b/src/org/thoughtcrime/securesms/loki/protocol/SessionMetaProtocol.kt @@ -4,11 +4,15 @@ import android.content.Context import org.thoughtcrime.securesms.ApplicationContext import org.thoughtcrime.securesms.database.Address import org.thoughtcrime.securesms.database.DatabaseFactory +import org.thoughtcrime.securesms.database.RecipientDatabase +import org.thoughtcrime.securesms.jobs.RetrieveProfileAvatarJob import org.thoughtcrime.securesms.recipients.Recipient import org.thoughtcrime.securesms.util.TextSecurePreferences import org.whispersystems.signalservice.api.messages.SignalServiceContent +import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage import org.whispersystems.signalservice.loki.protocol.multidevice.MultiDeviceProtocol import org.whispersystems.signalservice.loki.protocol.todo.LokiThreadFriendRequestStatus +import java.security.MessageDigest object SessionMetaProtocol { @@ -39,6 +43,28 @@ object SessionMetaProtocol { if (!allUserDevices.contains(sender)) { val displayName = rawDisplayName + " (..." + sender.substring(sender.length - 8) + ")" DatabaseFactory.getLokiUserDatabase(context).setDisplayName(sender, displayName) + } else { + DatabaseFactory.getLokiUserDatabase(context).setDisplayName(sender, rawDisplayName) + } + } + + @JvmStatic + fun handleProfileKey(context: Context, content: SignalServiceContent) { + val message = content.dataMessage.get() + if (!message.profileKey.isPresent) { return } + + /* + If we get a profile key then we don't need to map it to the primary device. + For now a profile key is mapped one-to-one to avoid secondary devices setting the incorrect avatar for a primary device. + */ + val database = DatabaseFactory.getRecipientDatabase(context) + val recipient = Recipient.from(context, Address.fromSerialized(content.sender), false) + if (recipient.profileKey == null || !MessageDigest.isEqual(recipient.profileKey, message.profileKey.get())) { + database.setProfileKey(recipient, message.profileKey.get()) + database.setUnidentifiedAccessMode(recipient, RecipientDatabase.UnidentifiedAccessMode.UNKNOWN) + val url = content.senderProfilePictureURL.or("") + ApplicationContext.getInstance(context).jobManager.add(RetrieveProfileAvatarJob(recipient, url)) + handleProfileKeyUpdateIfNeeded(context, content) } }