]> nmode's Git Repositories - signal-cli/commitdiff
Improve error handling for profile fetching
authorAsamK <asamk@gmx.de>
Sat, 25 Dec 2021 15:12:38 +0000 (16:12 +0100)
committerAsamK <asamk@gmx.de>
Sat, 25 Dec 2021 15:12:38 +0000 (16:12 +0100)
lib/src/main/java/org/asamk/signal/manager/helper/ProfileHelper.java

index e903d8073feb9367ab0324705adc8669b74c0e08..d871a73069c66c446ab6688664600b9b07e8d612 100644 (file)
@@ -190,33 +190,38 @@ public final class ProfileHelper {
             }
         }
 
-        if (encryptedProfile == null) {
-            profile = Profile.newBuilder().withLastUpdateTimestamp(now).build();
-        } else {
-            profile = decryptProfileIfKeyKnown(recipientId, encryptedProfile);
-        }
-
-        account.getProfileStore().storeProfile(recipientId, profile);
+        Profile newProfile = null;
+        if (encryptedProfile != null) {
+            var profileKey = account.getProfileStore().getProfileKey(recipientId);
+            if (profileKey != null) {
+                newProfile = decryptProfileAndDownloadAvatar(recipientId, profileKey, encryptedProfile);
+                if (newProfile == null) {
+                    account.getProfileStore().storeProfileKey(recipientId, null);
+                }
+            }
 
-        return profile;
-    }
+            if (newProfile == null) {
+                newProfile = (
+                        profile == null ? Profile.newBuilder() : Profile.newBuilder(profile)
+                ).withLastUpdateTimestamp(System.currentTimeMillis())
+                        .withUnidentifiedAccessMode(ProfileUtils.getUnidentifiedAccessMode(encryptedProfile, null))
+                        .withCapabilities(ProfileUtils.getCapabilities(encryptedProfile))
+                        .build();
+            }
+        }
 
-    private Profile decryptProfileIfKeyKnown(
-            final RecipientId recipientId, final SignalServiceProfile encryptedProfile
-    ) {
-        var profileKey = account.getProfileStore().getProfileKey(recipientId);
-        if (profileKey == null) {
-            return new Profile(System.currentTimeMillis(),
-                    null,
-                    null,
-                    null,
-                    null,
-                    null,
-                    ProfileUtils.getUnidentifiedAccessMode(encryptedProfile, null),
-                    ProfileUtils.getCapabilities(encryptedProfile));
+        if (newProfile == null) {
+            newProfile = (
+                    profile == null ? Profile.newBuilder() : Profile.newBuilder(profile)
+            ).withLastUpdateTimestamp(now)
+                    .withUnidentifiedAccessMode(Profile.UnidentifiedAccessMode.UNKNOWN)
+                    .withCapabilities(Set.of())
+                    .build();
         }
 
-        return decryptProfileAndDownloadAvatar(recipientId, profileKey, encryptedProfile);
+        account.getProfileStore().storeProfile(recipientId, newProfile);
+
+        return newProfile;
     }
 
     private SignalServiceProfile retrieveEncryptedProfile(RecipientId recipientId) {