+ return retrieveProfile(address, profileKey, unidentifiedAccess, requestType).doOnSuccess(p -> {
+ final var encryptedProfile = p.getProfile();
+
+ if (requestType == SignalServiceProfile.RequestType.PROFILE_AND_CREDENTIAL) {
+ final var profileKeyCredential = p.getProfileKeyCredential().orNull();
+ account.getProfileStore().storeProfileKeyCredential(recipientId, profileKeyCredential);
+ }
+
+ final var profile = account.getProfileStore().getProfile(recipientId);
+
+ Profile newProfile = null;
+ if (profileKey.isPresent()) {
+ newProfile = decryptProfileAndDownloadAvatar(recipientId, profileKey.get(), encryptedProfile);
+ }
+
+ if (newProfile == null) {
+ newProfile = (
+ profile == null ? Profile.newBuilder() : Profile.newBuilder(profile)
+ ).withLastUpdateTimestamp(System.currentTimeMillis())
+ .withUnidentifiedAccessMode(ProfileUtils.getUnidentifiedAccessMode(encryptedProfile, null))
+ .withCapabilities(ProfileUtils.getCapabilities(encryptedProfile))
+ .build();
+ }
+
+ account.getProfileStore().storeProfile(recipientId, newProfile);
+
+ try {
+ var newIdentity = account.getIdentityKeyStore()
+ .saveIdentity(recipientId,
+ new IdentityKey(Base64.getDecoder().decode(encryptedProfile.getIdentityKey())),
+ new Date());
+
+ if (newIdentity) {
+ account.getSessionStore().archiveSessions(recipientId);
+ account.getSenderKeyStore().deleteSharedWith(recipientId);
+ }
+ } catch (InvalidKeyException ignored) {
+ logger.warn("Got invalid identity key in profile for {}",
+ addressResolver.resolveSignalServiceAddress(recipientId).getIdentifier());
+ }
+ }).doOnError(e -> {
+ logger.warn("Failed to retrieve profile, ignoring: {}", e.getMessage());
+ final var profile = account.getProfileStore().getProfile(recipientId);
+ final var newProfile = (
+ profile == null ? Profile.newBuilder() : Profile.newBuilder(profile)
+ ).withLastUpdateTimestamp(System.currentTimeMillis())
+ .withUnidentifiedAccessMode(Profile.UnidentifiedAccessMode.UNKNOWN)
+ .withCapabilities(Set.of())
+ .build();
+
+ account.getProfileStore().storeProfile(recipientId, newProfile);
+ });