From: AsamK Date: Sat, 25 Dec 2021 15:12:38 +0000 (+0100) Subject: Improve error handling for profile fetching X-Git-Tag: v0.10.1~38 X-Git-Url: https://git.nmode.ca/signal-cli/commitdiff_plain/7c9839b114305e9cd3e7ac49fa6b8eea9c75491d?ds=sidebyside Improve error handling for profile fetching --- diff --git a/lib/src/main/java/org/asamk/signal/manager/helper/ProfileHelper.java b/lib/src/main/java/org/asamk/signal/manager/helper/ProfileHelper.java index e903d807..d871a730 100644 --- a/lib/src/main/java/org/asamk/signal/manager/helper/ProfileHelper.java +++ b/lib/src/main/java/org/asamk/signal/manager/helper/ProfileHelper.java @@ -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) {