]> nmode's Git Repositories - signal-cli/blobdiff - lib/src/main/java/org/asamk/signal/manager/SignalAccountFiles.java
Allow registering new accounts on both live and staging environments
[signal-cli] / lib / src / main / java / org / asamk / signal / manager / SignalAccountFiles.java
index 5f0f551c5dfc800afa3df596ff9d9480fd89cebb..f397b5348e769b2985723eddc6f38b405e9fc586 100644 (file)
@@ -41,14 +41,24 @@ public class SignalAccountFiles {
         this.serviceEnvironmentConfig = ServiceConfig.getServiceEnvironmentConfig(this.serviceEnvironment, userAgent);
         this.userAgent = userAgent;
         this.trustNewIdentity = trustNewIdentity;
-        this.accountsStore = new AccountsStore(pathConfig.dataPath());
+        this.accountsStore = new AccountsStore(pathConfig.dataPath(), serviceEnvironment, accountPath -> {
+            if (accountPath == null || !SignalAccount.accountFileExists(pathConfig.dataPath(), accountPath)) {
+                return null;
+            }
+
+            try {
+                return SignalAccount.load(pathConfig.dataPath(), accountPath, false, trustNewIdentity);
+            } catch (Exception e) {
+                return null;
+            }
+        });
     }
 
-    public Set<String> getAllLocalAccountNumbers() {
+    public Set<String> getAllLocalAccountNumbers() throws IOException {
         return accountsStore.getAllNumbers();
     }
 
-    public MultiAccountManager initMultiAccountManager() {
+    public MultiAccountManager initMultiAccountManager() throws IOException {
         final var managers = accountsStore.getAllAccounts().parallelStream().map(a -> {
             try {
                 return initManager(a.number(), a.path());
@@ -95,7 +105,7 @@ public class SignalAccountFiles {
 
         final var manager = new ManagerImpl(account,
                 pathConfig,
-                (newNumber, newAci) -> accountsStore.updateAccount(accountPath, newNumber, newAci),
+                new AccountFileUpdaterImpl(accountsStore, accountPath),
                 serviceEnvironmentConfig,
                 userAgent);
 
@@ -108,6 +118,7 @@ public class SignalAccountFiles {
 
         if (account.getServiceEnvironment() == null) {
             account.setServiceEnvironment(serviceEnvironment);
+            accountsStore.updateAccount(accountPath, account.getNumber(), account.getAci());
         }
 
         return manager;
@@ -155,7 +166,7 @@ public class SignalAccountFiles {
                     serviceEnvironmentConfig,
                     userAgent,
                     newManagerListener,
-                    (newNumber, newAci) -> accountsStore.updateAccount(newAccountPath, newNumber, newAci));
+                    new AccountFileUpdaterImpl(accountsStore, newAccountPath));
         }
 
         var account = SignalAccount.load(pathConfig.dataPath(), accountPath, true, trustNewIdentity);
@@ -169,6 +180,6 @@ public class SignalAccountFiles {
                 serviceEnvironmentConfig,
                 userAgent,
                 newManagerListener,
-                (newNumber, newAci) -> accountsStore.updateAccount(accountPath, newNumber, newAci));
+                new AccountFileUpdaterImpl(accountsStore, accountPath));
     }
 }