]> nmode's Git Repositories - signal-cli/blobdiff - lib/src/main/java/org/asamk/signal/manager/Manager.java
Only attempt to delete profile avatar if it exists
[signal-cli] / lib / src / main / java / org / asamk / signal / manager / Manager.java
index 11ee137d9fa0be905e761d907233e46beef52a50..9661b12c9672011a802fd67561b8b3653be4a036 100644 (file)
@@ -76,7 +76,6 @@ import org.whispersystems.libsignal.IdentityKey;
 import org.whispersystems.libsignal.IdentityKeyPair;
 import org.whispersystems.libsignal.InvalidKeyException;
 import org.whispersystems.libsignal.InvalidMessageException;
-import org.whispersystems.libsignal.InvalidVersionException;
 import org.whispersystems.libsignal.ecc.ECPublicKey;
 import org.whispersystems.libsignal.state.PreKeyRecord;
 import org.whispersystems.libsignal.state.SignedPreKeyRecord;
@@ -167,6 +166,7 @@ import java.util.Map;
 import java.util.Set;
 import java.util.UUID;
 import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 import java.util.stream.Collectors;
@@ -189,6 +189,8 @@ public class Manager implements Closeable {
     private final SignalServiceMessageReceiver messageReceiver;
     private final ClientZkProfileOperations clientZkProfileOperations;
 
+    private final ExecutorService executor = Executors.newCachedThreadPool();
+
     private SignalServiceMessagePipe messagePipe = null;
     private SignalServiceMessagePipe unidentifiedMessagePipe = null;
 
@@ -216,7 +218,6 @@ public class Manager implements Closeable {
                 new DynamicCredentialsProvider(account.getUuid(),
                         account.getUsername(),
                         account.getPassword(),
-                        account.getSignalingKey(),
                         account.getDeviceId()),
                 userAgent,
                 groupsV2Operations,
@@ -239,7 +240,6 @@ public class Manager implements Closeable {
                 account.getUsername(),
                 account.getPassword(),
                 account.getDeviceId(),
-                account.getSignalingKey(),
                 userAgent,
                 null,
                 timer,
@@ -349,7 +349,7 @@ public class Manager implements Closeable {
     }
 
     public void updateAccountAttributes() throws IOException {
-        accountManager.setAccountAttributes(account.getSignalingKey(),
+        accountManager.setAccountAttributes(null,
                 account.getSignalProtocolStore().getLocalRegistrationId(),
                 true,
                 // set legacy pin only if no KBS master key is set
@@ -362,22 +362,33 @@ public class Manager implements Closeable {
     }
 
     /**
-     * @param avatar if avatar is null the image from the local avatar store is used (if present),
-     *               if it's Optional.absent(), the avatar will be removed
+     * @param name       if null, the previous name will be kept
+     * @param about      if null, the previous about text will be kept
+     * @param aboutEmoji if null, the previous about emoji will be kept
+     * @param avatar     if avatar is null the image from the local avatar store is used (if present),
+     *                   if it's Optional.absent(), the avatar will be removed
      */
-    public void setProfile(String name, Optional<File> avatar) throws IOException {
-        // TODO
-        String about = null;
-        String aboutEmoji = null;
+    public void setProfile(String name, String about, String aboutEmoji, Optional<File> avatar) throws IOException {
+        SignalProfileEntry profileEntry = account.getProfileStore().getProfileEntry(getSelfAddress());
+        SignalProfile profile = profileEntry == null ? null : profileEntry.getProfile();
+        SignalProfile newProfile = new SignalProfile(profile == null ? null : profile.getIdentityKey(),
+                name != null ? name : profile == null || profile.getName() == null ? "" : profile.getName(),
+                about != null ? about : profile == null || profile.getAbout() == null ? "" : profile.getAbout(),
+                aboutEmoji != null
+                        ? aboutEmoji
+                        : profile == null || profile.getAboutEmoji() == null ? "" : profile.getAboutEmoji(),
+                profile == null ? null : profile.getUnidentifiedAccess(),
+                account.isUnrestrictedUnidentifiedAccess(),
+                profile == null ? null : profile.getCapabilities());
 
         try (final StreamDetails streamDetails = avatar == null
                 ? avatarStore.retrieveProfileAvatar(getSelfAddress())
                 : avatar.isPresent() ? Utils.createStreamDetailsFromFile(avatar.get()) : null) {
             accountManager.setVersionedProfile(account.getUuid(),
                     account.getProfileKey(),
-                    name,
-                    about,
-                    aboutEmoji,
+                    newProfile.getName(),
+                    newProfile.getAbout(),
+                    newProfile.getAboutEmoji(),
                     streamDetails);
         }
 
@@ -389,6 +400,12 @@ public class Manager implements Closeable {
                 avatarStore.deleteProfileAvatar(getSelfAddress());
             }
         }
+        account.getProfileStore()
+                .updateProfile(getSelfAddress(),
+                        account.getProfileKey(),
+                        System.currentTimeMillis(),
+                        newProfile,
+                        profileEntry == null ? null : profileEntry.getProfileKeyCredential());
 
         try {
             sendSyncMessage(SignalServiceSyncMessage.forFetchLatest(SignalServiceSyncMessage.FetchType.LOCAL_PROFILE));
@@ -509,7 +526,6 @@ public class Manager implements Closeable {
     }
 
     private SignalServiceMessageSender createMessageSender() {
-        final ExecutorService executor = null;
         return new SignalServiceMessageSender(serviceEnvironmentConfig.getSignalServiceConfiguration(),
                 account.getUuid(),
                 account.getUsername(),
@@ -1728,9 +1744,6 @@ public class Manager implements Closeable {
             } catch (TimeoutException e) {
                 if (returnOnTimeout) return;
                 continue;
-            } catch (InvalidVersionException e) {
-                logger.warn("Error while receiving messages, ignoring: {}", e.getMessage());
-                continue;
             }
 
             if (envelope.hasSource()) {
@@ -2516,6 +2529,8 @@ public class Manager implements Closeable {
     }
 
     void close(boolean closeAccount) throws IOException {
+        executor.shutdown();
+
         if (messagePipe != null) {
             messagePipe.shutdown();
             messagePipe = null;