]> nmode's Git Repositories - signal-cli/commitdiff
Refactor ProfileStore to handle name/uuid addresses correctly
authorAsamK <asamk@gmx.de>
Fri, 11 Sep 2020 14:22:30 +0000 (16:22 +0200)
committerAsamK <asamk@gmx.de>
Fri, 11 Sep 2020 14:22:30 +0000 (16:22 +0200)
src/main/java/org/asamk/signal/manager/Manager.java
src/main/java/org/asamk/signal/storage/profiles/ProfileStore.java
src/main/java/org/asamk/signal/storage/profiles/SignalProfileEntry.java

index c4969fa45860c00a59748b129a25de8c1840779c..2ce59cdc0fd71dc361f39ff2e3a3964b8b20dbe0 100644 (file)
@@ -462,8 +462,8 @@ public class Manager implements Closeable {
         // Profiles are cache for 24h before retrieving them again
         if (profileEntry == null || profileEntry.getProfile() == null || now - profileEntry.getLastUpdateTimestamp() > 24 * 60 * 60 * 1000) {
             SignalProfile profile = retrieveRecipientProfile(address, unidentifiedAccess, profileKey);
-            profileEntry = new SignalProfileEntry(profileKey, now, profile);
-            account.getProfileStore().updateProfile(address, profileEntry);
+            account.getProfileStore().updateProfile(address, profileKey, now, profile);
+            return profile;
         }
         return profileEntry.getProfile();
     }
index 43a39671befdf4c16c91a015f1382e8d245daada..24b089684dcdd73b7b0fdceceb8ec17b9b90dba1 100644 (file)
@@ -19,8 +19,9 @@ import org.whispersystems.signalservice.api.util.UuidUtil;
 import org.whispersystems.util.Base64;
 
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.HashMap;
-import java.util.Map;
+import java.util.List;
 import java.util.UUID;
 
 public class ProfileStore {
@@ -30,23 +31,36 @@ public class ProfileStore {
     @JsonProperty("profiles")
     @JsonDeserialize(using = ProfileStoreDeserializer.class)
     @JsonSerialize(using = ProfileStoreSerializer.class)
-    private final Map<SignalServiceAddress, SignalProfileEntry> profiles = new HashMap<>();
+    private final List<SignalProfileEntry> profiles = new ArrayList<>();
 
     public SignalProfileEntry getProfile(SignalServiceAddress serviceAddress) {
-        return profiles.get(serviceAddress);
+        for (SignalProfileEntry entry : profiles) {
+            if (entry.getServiceAddress().matches(serviceAddress)) {
+                return entry;
+            }
+        }
+        return null;
     }
 
-    public void updateProfile(SignalServiceAddress serviceAddress, SignalProfileEntry profile) {
-        profiles.put(serviceAddress, profile);
+    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++) {
+            if (profiles.get(i).getServiceAddress().matches(serviceAddress)) {
+                profiles.set(i, newEntry);
+                return;
+            }
+        }
+
+        profiles.add(newEntry);
     }
 
-    public static class ProfileStoreDeserializer extends JsonDeserializer<Map<SignalServiceAddress, SignalProfileEntry>> {
+    public static class ProfileStoreDeserializer extends JsonDeserializer<List<SignalProfileEntry>> {
 
         @Override
-        public Map<SignalServiceAddress, SignalProfileEntry> deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
+        public List<SignalProfileEntry> deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
             JsonNode node = jsonParser.getCodec().readTree(jsonParser);
 
-            Map<SignalServiceAddress, SignalProfileEntry> addresses = new HashMap<>();
+            List<SignalProfileEntry> addresses = new ArrayList<>();
 
             if (node.isArray()) {
                 for (JsonNode entry : node) {
@@ -64,7 +78,7 @@ public class ProfileStore {
                     }
                     long lastUpdateTimestamp = entry.get("lastUpdateTimestamp").asLong();
                     SignalProfile profile = jsonProcessor.treeToValue(entry.get("profile"), SignalProfile.class);
-                    addresses.put(serviceAddress, new SignalProfileEntry(profileKey, lastUpdateTimestamp, profile));
+                    addresses.add(new SignalProfileEntry(serviceAddress, profileKey, lastUpdateTimestamp, profile));
                 }
             }
 
@@ -72,14 +86,13 @@ public class ProfileStore {
         }
     }
 
-    public static class ProfileStoreSerializer extends JsonSerializer<Map<SignalServiceAddress, SignalProfileEntry>> {
+    public static class ProfileStoreSerializer extends JsonSerializer<List<SignalProfileEntry>> {
 
         @Override
-        public void serialize(Map<SignalServiceAddress, SignalProfileEntry> profiles, JsonGenerator json, SerializerProvider serializerProvider) throws IOException {
+        public void serialize(List<SignalProfileEntry> profiles, JsonGenerator json, SerializerProvider serializerProvider) throws IOException {
             json.writeStartArray();
-            for (Map.Entry<SignalServiceAddress, SignalProfileEntry> entry : profiles.entrySet()) {
-                final SignalServiceAddress address = entry.getKey();
-                final SignalProfileEntry profileEntry = entry.getValue();
+            for (SignalProfileEntry profileEntry : profiles) {
+                final SignalServiceAddress address = profileEntry.getServiceAddress();
                 json.writeStartObject();
                 if (address.getNumber().isPresent()) {
                     json.writeStringField("name", address.getNumber().get());
index 0423e12ed12f09f9dc327979e867f4361c3a80d2..ed1f7127e694160676ccfdc0a50d5d7990f5a6ba 100644 (file)
@@ -1,21 +1,29 @@
 package org.asamk.signal.storage.profiles;
 
 import org.signal.zkgroup.profiles.ProfileKey;
+import org.whispersystems.signalservice.api.push.SignalServiceAddress;
 
 public class SignalProfileEntry {
 
-    private ProfileKey profileKey;
+    private final SignalServiceAddress serviceAddress;
 
-    private long lastUpdateTimestamp;
+    private final ProfileKey profileKey;
 
-    private SignalProfile profile;
+    private final long lastUpdateTimestamp;
 
-    public SignalProfileEntry(final ProfileKey profileKey, final long lastUpdateTimestamp, final SignalProfile profile) {
+    private final SignalProfile profile;
+
+    public SignalProfileEntry(final SignalServiceAddress serviceAddress, final ProfileKey profileKey, final long lastUpdateTimestamp, final SignalProfile profile) {
+        this.serviceAddress = serviceAddress;
         this.profileKey = profileKey;
         this.lastUpdateTimestamp = lastUpdateTimestamp;
         this.profile = profile;
     }
 
+    public SignalServiceAddress getServiceAddress() {
+        return serviceAddress;
+    }
+
     public ProfileKey getProfileKey() {
         return profileKey;
     }