]> nmode's Git Repositories - signal-cli/commitdiff
Store profile keys only in profile store
authorAsamK <asamk@gmx.de>
Sat, 21 Nov 2020 19:11:46 +0000 (20:11 +0100)
committerAsamK <asamk@gmx.de>
Sat, 21 Nov 2020 19:11:46 +0000 (20:11 +0100)
Fixes #328

src/main/java/org/asamk/signal/manager/Manager.java
src/main/java/org/asamk/signal/storage/contacts/ContactInfo.java
src/main/java/org/asamk/signal/storage/profiles/ProfileStore.java

index d80b522e5bbfdf780a41b9537c3110a62d55124f..6b2e7c96b7c85593ff5c329619057e9bf41d05fb 100644 (file)
@@ -267,6 +267,21 @@ public class Manager implements Closeable {
             account.setProfileKey(KeyUtils.createProfileKey());
             account.save();
         }
             account.setProfileKey(KeyUtils.createProfileKey());
             account.save();
         }
+        // Store profile keys only in profile store
+        for (ContactInfo contact : account.getContactStore().getContacts()) {
+            String profileKeyString = contact.profileKey;
+            if (profileKeyString == null) {
+                continue;
+            }
+            final ProfileKey profileKey;
+            try {
+                profileKey = new ProfileKey(Base64.decode(profileKeyString));
+            } catch (InvalidInputException | IOException e) {
+                continue;
+            }
+            contact.profileKey = null;
+            account.getProfileStore().storeProfileKey(contact.getAddress(), profileKey);
+        }
     }
 
     public void checkAccountState() throws IOException {
     }
 
     public void checkAccountState() throws IOException {
@@ -993,16 +1008,10 @@ public class Manager implements Closeable {
     }
 
     private byte[] getTargetUnidentifiedAccessKey(SignalServiceAddress recipient) {
     }
 
     private byte[] getTargetUnidentifiedAccessKey(SignalServiceAddress recipient) {
-        ContactInfo contact = account.getContactStore().getContact(recipient);
-        if (contact == null || contact.profileKey == null) {
+        ProfileKey theirProfileKey = account.getProfileStore().getProfileKey(recipient);
+        if (theirProfileKey == null) {
             return null;
         }
             return null;
         }
-        ProfileKey theirProfileKey;
-        try {
-            theirProfileKey = new ProfileKey(Base64.decode(contact.profileKey));
-        } catch (InvalidInputException | IOException e) {
-            throw new AssertionError(e);
-        }
         SignalProfile targetProfile;
         try {
             targetProfile = getRecipientProfile(recipient, Optional.absent(), theirProfileKey);
         SignalProfile targetProfile;
         try {
             targetProfile = getRecipientProfile(recipient, Optional.absent(), theirProfileKey);
@@ -1326,24 +1335,16 @@ public class Manager implements Closeable {
             }
         }
         if (message.getProfileKey().isPresent() && message.getProfileKey().get().length == 32) {
             }
         }
         if (message.getProfileKey().isPresent() && message.getProfileKey().get().length == 32) {
+            final ProfileKey profileKey;
+            try {
+                profileKey = new ProfileKey(message.getProfileKey().get());
+            } catch (InvalidInputException e) {
+                throw new AssertionError(e);
+            }
             if (source.matches(account.getSelfAddress())) {
             if (source.matches(account.getSelfAddress())) {
-                try {
-                    this.account.setProfileKey(new ProfileKey(message.getProfileKey().get()));
-                } catch (InvalidInputException ignored) {
-                }
-                ContactInfo contact = account.getContactStore().getContact(source);
-                if (contact != null) {
-                    contact.profileKey = Base64.encodeBytes(message.getProfileKey().get());
-                    account.getContactStore().updateContact(contact);
-                }
-            } else {
-                ContactInfo contact = account.getContactStore().getContact(source);
-                if (contact == null) {
-                    contact = new ContactInfo(source);
-                }
-                contact.profileKey = Base64.encodeBytes(message.getProfileKey().get());
-                account.getContactStore().updateContact(contact);
+                this.account.setProfileKey(profileKey);
             }
             }
+            this.account.getProfileStore().storeProfileKey(source, profileKey);
         }
         if (message.getPreviews().isPresent()) {
             final List<SignalServiceDataMessage.Preview> previews = message.getPreviews().get();
         }
         if (message.getPreviews().isPresent()) {
             final List<SignalServiceDataMessage.Preview> previews = message.getPreviews().get();
@@ -1690,7 +1691,7 @@ public class Manager implements Closeable {
                                     contact.color = c.getColor().get();
                                 }
                                 if (c.getProfileKey().isPresent()) {
                                     contact.color = c.getColor().get();
                                 }
                                 if (c.getProfileKey().isPresent()) {
-                                    contact.profileKey = Base64.encodeBytes(c.getProfileKey().get().serialize());
+                                    account.getProfileStore().storeProfileKey(address, c.getProfileKey().get());
                                 }
                                 if (c.getVerified().isPresent()) {
                                     final VerifiedMessage verifiedMessage = c.getVerified().get();
                                 }
                                 if (c.getVerified().isPresent()) {
                                     final VerifiedMessage verifiedMessage = c.getVerified().get();
@@ -1874,11 +1875,7 @@ public class Manager implements Closeable {
                         verifiedMessage = new VerifiedMessage(record.getAddress(), currentIdentity.getIdentityKey(), currentIdentity.getTrustLevel().toVerifiedState(), currentIdentity.getDateAdded().getTime());
                     }
 
                         verifiedMessage = new VerifiedMessage(record.getAddress(), currentIdentity.getIdentityKey(), currentIdentity.getTrustLevel().toVerifiedState(), currentIdentity.getDateAdded().getTime());
                     }
 
-                    ProfileKey profileKey = null;
-                    try {
-                        profileKey = record.profileKey == null ? null : new ProfileKey(Base64.decode(record.profileKey));
-                    } catch (InvalidInputException ignored) {
-                    }
+                    ProfileKey profileKey = account.getProfileStore().getProfileKey(record.getAddress());
                     out.write(new DeviceContact(record.getAddress(), Optional.fromNullable(record.name),
                             createContactAvatarAttachment(record.number), Optional.fromNullable(record.color),
                             Optional.fromNullable(verifiedMessage), Optional.fromNullable(profileKey), record.blocked,
                     out.write(new DeviceContact(record.getAddress(), Optional.fromNullable(record.name),
                             createContactAvatarAttachment(record.number), Optional.fromNullable(record.color),
                             Optional.fromNullable(verifiedMessage), Optional.fromNullable(profileKey), record.blocked,
index 4d3a5e95addd3aadcd40d44012c9ea19d9cc8e4b..3b15521070fdbfdd780f9fc820bcf1693b86ef65 100644 (file)
@@ -7,6 +7,8 @@ import org.whispersystems.signalservice.api.push.SignalServiceAddress;
 
 import java.util.UUID;
 
 
 import java.util.UUID;
 
+import static com.fasterxml.jackson.annotation.JsonProperty.Access.WRITE_ONLY;
+
 public class ContactInfo {
 
     @JsonProperty
 public class ContactInfo {
 
     @JsonProperty
@@ -24,7 +26,7 @@ public class ContactInfo {
     @JsonProperty(defaultValue = "0")
     public int messageExpirationTime;
 
     @JsonProperty(defaultValue = "0")
     public int messageExpirationTime;
 
-    @JsonProperty
+    @JsonProperty(access = WRITE_ONLY)
     public String profileKey;
 
     @JsonProperty(defaultValue = "false")
     public String profileKey;
 
     @JsonProperty(defaultValue = "false")
index 24b089684dcdd73b7b0fdceceb8ec17b9b90dba1..662d0161b1e85b675794461ff4d003f149873839 100644 (file)
@@ -20,7 +20,6 @@ import org.whispersystems.util.Base64;
 
 import java.io.IOException;
 import java.util.ArrayList;
 
 import java.io.IOException;
 import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.List;
 import java.util.UUID;
 
 import java.util.List;
 import java.util.UUID;
 
@@ -42,6 +41,15 @@ public class ProfileStore {
         return null;
     }
 
         return null;
     }
 
+    public ProfileKey getProfileKey(SignalServiceAddress serviceAddress) {
+        for (SignalProfileEntry entry : profiles) {
+            if (entry.getServiceAddress().matches(serviceAddress)) {
+                return entry.getProfileKey();
+            }
+        }
+        return null;
+    }
+
     public void updateProfile(SignalServiceAddress serviceAddress, ProfileKey profileKey, long now, SignalProfile profile) {
         SignalProfileEntry newEntry = new SignalProfileEntry(serviceAddress, profileKey, now, profile);
         for (int i = 0; i < profiles.size(); i++) {
     public void updateProfile(SignalServiceAddress serviceAddress, ProfileKey profileKey, long now, SignalProfile profile) {
         SignalProfileEntry newEntry = new SignalProfileEntry(serviceAddress, profileKey, now, profile);
         for (int i = 0; i < profiles.size(); i++) {
@@ -54,6 +62,20 @@ public class ProfileStore {
         profiles.add(newEntry);
     }
 
         profiles.add(newEntry);
     }
 
+    public void storeProfileKey(SignalServiceAddress serviceAddress, ProfileKey profileKey) {
+        SignalProfileEntry newEntry = new SignalProfileEntry(serviceAddress, profileKey, 0, null);
+        for (int i = 0; i < profiles.size(); i++) {
+            if (profiles.get(i).getServiceAddress().matches(serviceAddress)) {
+                if (!profiles.get(i).getProfileKey().equals(profileKey)) {
+                    profiles.set(i, newEntry);
+                }
+                return;
+            }
+        }
+
+        profiles.add(newEntry);
+    }
+
     public static class ProfileStoreDeserializer extends JsonDeserializer<List<SignalProfileEntry>> {
 
         @Override
     public static class ProfileStoreDeserializer extends JsonDeserializer<List<SignalProfileEntry>> {
 
         @Override