Fix for concurrent modification

Stephen Paul Weber created

Change summary

src/cheogram/java/eu/siacs/conversations/services/QuickConversationsService.java |  4 
src/main/java/eu/siacs/conversations/entities/Account.java                       | 26 
2 files changed, 17 insertions(+), 13 deletions(-)

Detailed changes

src/cheogram/java/eu/siacs/conversations/services/QuickConversationsService.java 🔗

@@ -114,8 +114,8 @@ public class QuickConversationsService extends AbstractQuickConversationsService
 
     protected Set<String> gateways(final Account account) {
         return Stream.concat(
-            ImmutableList.copyOf(account.getGateways("pstn")).stream(),
-            ImmutableList.copyOf(account.getGateways("sms")).stream()
+            account.getGateways("pstn").stream(),
+            account.getGateways("sms").stream()
         ).map(a -> a.getJid().asBareJid().toString()).collect(Collectors.toSet());
     }
 

src/main/java/eu/siacs/conversations/entities/Account.java 🔗

@@ -662,22 +662,26 @@ public class Account extends AbstractEntity implements AvatarService.Avatarable
     }
 
     public void refreshCapsFor(Contact contact) {
-        for (final var k : new HashSet<>(gateways.keySet())) {
-            gateways.remove(k, contact);
-        }
-        for (final var p : contact.getPresences().getPresences()) {
-            final var disco = p.getServiceDiscoveryResult();
-            if (disco == null) continue;
-            for (final var identity : disco.getIdentities()) {
-                if ("gateway".equals(identity.getCategory())) {
-                    gateways.put(identity.getType(), contact);
+        synchronized (gateways) {
+            for (final var k : new HashSet<>(gateways.keySet())) {
+                gateways.remove(k, contact);
+            }
+            for (final var p : contact.getPresences().getPresences()) {
+                final var disco = p.getServiceDiscoveryResult();
+                if (disco == null) continue;
+                for (final var identity : disco.getIdentities()) {
+                    if ("gateway".equals(identity.getCategory())) {
+                        gateways.put(identity.getType(), contact);
+                    }
                 }
             }
         }
     }
 
-    public Set<Contact> getGateways(final String type) {
-        return gateways.get(type);
+    public Collection<Contact> getGateways(final String type) {
+        synchronized (gateways) {
+            return ImmutableList.copyOf(gateways.get(type));
+        }
     }
 
     public Collection<Bookmark> getBookmarks() {