]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/org/asamk/signal/storage/SignalAccount.java
Cache profiles for 24h before retrieving them again
[signal-cli] / src / main / java / org / asamk / signal / storage / SignalAccount.java
index 9493544162bff1df6678ef4d842509a5c541c638..d9b37825b108fd114baa90139ddd44ca83bffd6a 100644 (file)
@@ -14,6 +14,7 @@ import org.asamk.signal.storage.contacts.ContactInfo;
 import org.asamk.signal.storage.contacts.JsonContactsStore;
 import org.asamk.signal.storage.groups.GroupInfo;
 import org.asamk.signal.storage.groups.JsonGroupStore;
+import org.asamk.signal.storage.profiles.ProfileStore;
 import org.asamk.signal.storage.protocol.JsonIdentityKeyStore;
 import org.asamk.signal.storage.protocol.JsonSignalProtocolStore;
 import org.asamk.signal.storage.protocol.RecipientStore;
@@ -38,6 +39,7 @@ import java.io.File;
 import java.io.IOException;
 import java.io.RandomAccessFile;
 import java.nio.channels.Channels;
+import java.nio.channels.ClosedChannelException;
 import java.nio.channels.FileChannel;
 import java.nio.channels.FileLock;
 import java.util.Collection;
@@ -66,6 +68,7 @@ public class SignalAccount implements Closeable {
     private JsonGroupStore groupStore;
     private JsonContactsStore contactStore;
     private RecipientStore recipientStore;
+    private ProfileStore profileStore;
 
     private SignalAccount(final FileChannel fileChannel, final FileLock lock) {
         this.fileChannel = fileChannel;
@@ -108,6 +111,7 @@ public class SignalAccount implements Closeable {
         account.groupStore = new JsonGroupStore();
         account.contactStore = new JsonContactsStore();
         account.recipientStore = new RecipientStore();
+        account.profileStore = new ProfileStore();
         account.registered = false;
 
         return account;
@@ -133,6 +137,7 @@ public class SignalAccount implements Closeable {
         account.groupStore = new JsonGroupStore();
         account.contactStore = new JsonContactsStore();
         account.recipientStore = new RecipientStore();
+        account.profileStore = new ProfileStore();
         account.registered = true;
         account.isMultiDevice = true;
 
@@ -244,6 +249,14 @@ public class SignalAccount implements Closeable {
             }
         }
 
+        JsonNode profileStoreNode = rootNode.get("profileStore");
+        if (profileStoreNode != null) {
+            profileStore = jsonProcessor.convertValue(profileStoreNode, ProfileStore.class);
+        }
+        if (profileStore == null) {
+            profileStore = new ProfileStore();
+        }
+
         JsonNode threadStoreNode = rootNode.get("threadStore");
         if (threadStoreNode != null) {
             LegacyJsonThreadStore threadStore = jsonProcessor.convertValue(threadStoreNode, LegacyJsonThreadStore.class);
@@ -290,6 +303,7 @@ public class SignalAccount implements Closeable {
                 .putPOJO("groupStore", groupStore)
                 .putPOJO("contactStore", contactStore)
                 .putPOJO("recipientStore", recipientStore)
+                .putPOJO("profileStore", profileStore)
         ;
         try {
             synchronized (fileChannel) {
@@ -346,6 +360,10 @@ public class SignalAccount implements Closeable {
         return recipientStore;
     }
 
+    public ProfileStore getProfileStore() {
+        return profileStore;
+    }
+
     public String getUsername() {
         return username;
     }
@@ -429,7 +447,10 @@ public class SignalAccount implements Closeable {
     @Override
     public void close() throws IOException {
         synchronized (fileChannel) {
-            lock.close();
+            try {
+                lock.close();
+            } catch (ClosedChannelException ignored) {
+            }
             fileChannel.close();
         }
     }