]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/org/asamk/signal/storage/SignalAccount.java
Show contact name for message Sender
[signal-cli] / src / main / java / org / asamk / signal / storage / SignalAccount.java
index 4378d052a64871850b6c52c4d4a30fdb7f91b1e9..1ba78f6b83796049cfbd2aa02f6bc150cd1b6a5a 100644 (file)
@@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.SerializationFeature;
 import com.fasterxml.jackson.databind.node.ObjectNode;
+
 import org.asamk.signal.storage.contacts.JsonContactsStore;
 import org.asamk.signal.storage.groups.JsonGroupStore;
 import org.asamk.signal.storage.protocol.JsonSignalProtocolStore;
@@ -20,7 +21,7 @@ import org.whispersystems.libsignal.state.PreKeyRecord;
 import org.whispersystems.libsignal.state.SignedPreKeyRecord;
 import org.whispersystems.libsignal.util.Medium;
 import org.whispersystems.signalservice.api.push.SignalServiceAddress;
-import org.whispersystems.signalservice.internal.util.Base64;
+import org.whispersystems.util.Base64;
 
 import java.io.File;
 import java.io.IOException;
@@ -129,13 +130,19 @@ public class SignalAccount {
     }
 
     private void load() throws IOException {
-        JsonNode rootNode = jsonProcessor.readTree(Channels.newInputStream(fileChannel));
+        JsonNode rootNode;
+        synchronized (fileChannel) {
+            fileChannel.position(0);
+            rootNode = jsonProcessor.readTree(Channels.newInputStream(fileChannel));
+        }
 
         JsonNode node = rootNode.get("deviceId");
         if (node != null) {
             deviceId = node.asInt();
         }
-        if (rootNode.has("isMultiDevice")) isMultiDevice = Util.getNotNullNode(rootNode, "isMultiDevice").asBoolean();
+        if (rootNode.has("isMultiDevice")) {
+            isMultiDevice = Util.getNotNullNode(rootNode, "isMultiDevice").asBoolean();
+        }
         username = Util.getNotNullNode(rootNode, "username").asText();
         password = Util.getNotNullNode(rootNode, "password").asText();
         JsonNode pinNode = rootNode.get("registrationLockPin");
@@ -204,10 +211,12 @@ public class SignalAccount {
                 .putPOJO("threadStore", threadStore)
         ;
         try {
-            fileChannel.position(0);
-            jsonProcessor.writeValue(Channels.newOutputStream(fileChannel), rootNode);
-            fileChannel.truncate(fileChannel.position());
-            fileChannel.force(false);
+            synchronized (fileChannel) {
+                fileChannel.position(0);
+                jsonProcessor.writeValue(Channels.newOutputStream(fileChannel), rootNode);
+                fileChannel.truncate(fileChannel.position());
+                fileChannel.force(false);
+            }
         } catch (Exception e) {
             System.err.println(String.format("Error saving file: %s", e.getMessage()));
         }
@@ -262,6 +271,10 @@ public class SignalAccount {
         return username;
     }
 
+    public SignalServiceAddress getSelfAddress() {
+        return new SignalServiceAddress(null, username);
+    }
+
     public int getDeviceId() {
         return deviceId;
     }