]> nmode's Git Repositories - signal-cli/commitdiff
Store available profile data even if we don't have the profile key
authorAsamK <asamk@gmx.de>
Tue, 11 May 2021 16:37:18 +0000 (18:37 +0200)
committerAsamK <asamk@gmx.de>
Tue, 11 May 2021 16:37:18 +0000 (18:37 +0200)
lib/src/main/java/org/asamk/signal/manager/Manager.java
lib/src/main/java/org/asamk/signal/manager/storage/recipients/RecipientStore.java
lib/src/main/java/org/asamk/signal/manager/util/ProfileUtils.java

index 8d480044436f888ab26b102c88048d64c1af7bac..a78a19c159fa22255a71f92c1972ffda25d40368 100644 (file)
@@ -556,14 +556,6 @@ public class Manager implements Closeable {
     Profile getRecipientProfile(
             RecipientId recipientId, boolean force
     ) {
     Profile getRecipientProfile(
             RecipientId recipientId, boolean force
     ) {
-        var profileKey = account.getProfileStore().getProfileKey(recipientId);
-        if (profileKey == null) {
-            if (force) {
-                // retrieve profile to get identity key
-                retrieveEncryptedProfile(recipientId);
-            }
-            return null;
-        }
         var profile = account.getProfileStore().getProfile(recipientId);
 
         var now = new Date().getTime();
         var profile = account.getProfileStore().getProfile(recipientId);
 
         var now = new Date().getTime();
@@ -590,7 +582,18 @@ public class Manager implements Closeable {
             return null;
         }
 
             return null;
         }
 
-        profile = decryptProfileAndDownloadAvatar(recipientId, profileKey, encryptedProfile);
+        var profileKey = account.getProfileStore().getProfileKey(recipientId);
+        if (profileKey == null) {
+            profile = new Profile(new Date().getTime(),
+                    null,
+                    null,
+                    null,
+                    null,
+                    ProfileUtils.getUnidentifiedAccessMode(encryptedProfile, null),
+                    ProfileUtils.getCapabilities(encryptedProfile));
+        } else {
+            profile = decryptProfileAndDownloadAvatar(recipientId, profileKey, encryptedProfile);
+        }
         account.getProfileStore().storeProfile(recipientId, profile);
 
         return profile;
         account.getProfileStore().storeProfile(recipientId, profile);
 
         return profile;
index a323c6d99343b957ccc6e5afc3dcf620e86d2f1e..fb7394b5b53a1ba998e0f2b0a361464976ed6318 100644 (file)
@@ -233,6 +233,9 @@ public class RecipientStore implements ContactsStore, ProfileStore {
             final var newRecipient = Recipient.newBuilder(recipient)
                     .withProfileKey(profileKey)
                     .withProfileKeyCredential(null)
             final var newRecipient = Recipient.newBuilder(recipient)
                     .withProfileKey(profileKey)
                     .withProfileKeyCredential(null)
+                    .withProfile(recipient.getProfile() == null
+                            ? null
+                            : Profile.newBuilder(recipient.getProfile()).withLastUpdateTimestamp(0).build())
                     .build();
             storeRecipientLocked(recipientId, newRecipient);
         }
                     .build();
             storeRecipientLocked(recipientId, newRecipient);
         }
index b1a8ed94161f1d78e1efc91036150531f47e1d72..f865ab214d3da588d38f78119cf151796e6330e9 100644 (file)
@@ -21,43 +21,51 @@ public class ProfileUtils {
             var name = decryptName(encryptedProfile.getName(), profileCipher);
             var about = decryptName(encryptedProfile.getAbout(), profileCipher);
             var aboutEmoji = decryptName(encryptedProfile.getAboutEmoji(), profileCipher);
             var name = decryptName(encryptedProfile.getName(), profileCipher);
             var about = decryptName(encryptedProfile.getAbout(), profileCipher);
             var aboutEmoji = decryptName(encryptedProfile.getAboutEmoji(), profileCipher);
-            String unidentifiedAccess;
-            try {
-                unidentifiedAccess = encryptedProfile.getUnidentifiedAccess() == null
-                        || !profileCipher.verifyUnidentifiedAccess(Base64.getDecoder()
-                        .decode(encryptedProfile.getUnidentifiedAccess()))
-                        ? null
-                        : encryptedProfile.getUnidentifiedAccess();
-            } catch (IllegalArgumentException e) {
-                unidentifiedAccess = null;
-            }
+
             final var nameParts = splitName(name);
             final var nameParts = splitName(name);
-            final var capabilities = new HashSet<Profile.Capability>();
-            if (encryptedProfile.getCapabilities().isGv1Migration()) {
-                capabilities.add(Profile.Capability.gv1Migration);
-            }
-            if (encryptedProfile.getCapabilities().isGv2()) {
-                capabilities.add(Profile.Capability.gv2);
-            }
-            if (encryptedProfile.getCapabilities().isStorage()) {
-                capabilities.add(Profile.Capability.storage);
-            }
             return new Profile(new Date().getTime(),
                     nameParts.first(),
                     nameParts.second(),
                     about,
                     aboutEmoji,
             return new Profile(new Date().getTime(),
                     nameParts.first(),
                     nameParts.second(),
                     about,
                     aboutEmoji,
-                    encryptedProfile.isUnrestrictedUnidentifiedAccess()
-                            ? Profile.UnidentifiedAccessMode.UNRESTRICTED
-                            : unidentifiedAccess != null
-                                    ? Profile.UnidentifiedAccessMode.ENABLED
-                                    : Profile.UnidentifiedAccessMode.DISABLED,
-                    capabilities);
+                    getUnidentifiedAccessMode(encryptedProfile, profileCipher),
+                    getCapabilities(encryptedProfile));
         } catch (InvalidCiphertextException e) {
             return null;
         }
     }
 
         } catch (InvalidCiphertextException e) {
             return null;
         }
     }
 
+    public static Profile.UnidentifiedAccessMode getUnidentifiedAccessMode(
+            final SignalServiceProfile encryptedProfile, final ProfileCipher profileCipher
+    ) {
+        if (encryptedProfile.isUnrestrictedUnidentifiedAccess()) {
+            return Profile.UnidentifiedAccessMode.UNRESTRICTED;
+        }
+
+        if (encryptedProfile.getUnidentifiedAccess() != null && profileCipher != null) {
+            final var unidentifiedAccessVerifier = Base64.getDecoder().decode(encryptedProfile.getUnidentifiedAccess());
+            if (profileCipher.verifyUnidentifiedAccess(unidentifiedAccessVerifier)) {
+                return Profile.UnidentifiedAccessMode.ENABLED;
+            }
+        }
+
+        return Profile.UnidentifiedAccessMode.DISABLED;
+    }
+
+    public static HashSet<Profile.Capability> getCapabilities(final SignalServiceProfile encryptedProfile) {
+        final var capabilities = new HashSet<Profile.Capability>();
+        if (encryptedProfile.getCapabilities().isGv1Migration()) {
+            capabilities.add(Profile.Capability.gv1Migration);
+        }
+        if (encryptedProfile.getCapabilities().isGv2()) {
+            capabilities.add(Profile.Capability.gv2);
+        }
+        if (encryptedProfile.getCapabilities().isStorage()) {
+            capabilities.add(Profile.Capability.storage);
+        }
+        return capabilities;
+    }
+
     private static String decryptName(
             final String encryptedName, final ProfileCipher profileCipher
     ) throws InvalidCiphertextException {
     private static String decryptName(
             final String encryptedName, final ProfileCipher profileCipher
     ) throws InvalidCiphertextException {