From 950e32e9a633116467401784da700ffc5913909e Mon Sep 17 00:00:00 2001
From: Andreas Hatziiliou <andreas.hatziiliou@savoirfairelinux.com>
Date: Fri, 15 Nov 2024 17:04:45 -0500
Subject: [PATCH] avatars: fix duplication glitch when switching accounts

There was a timing issue between when a new account is connected
and the moment where the avatar cache is cleared. Thus, the ui
would use the cache of the previous account for certain
conversations.

GitLab: #1832
GitLab: #1559
Change-Id: I0319b3d81e0b6dfec9b5408806dfd48ff220ea9d
---
 src/app/avatarregistry.cpp                         | 14 ++++++++++++--
 src/app/avatarregistry.h                           |  2 ++
 .../mainview/components/AccountComboBoxPopup.qml   |  7 ++++++-
 3 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/src/app/avatarregistry.cpp b/src/app/avatarregistry.cpp
index 24d9f7d60..19b8074db 100644
--- a/src/app/avatarregistry.cpp
+++ b/src/app/avatarregistry.cpp
@@ -56,11 +56,15 @@ AvatarRegistry::addOrUpdateImage(const QString& id)
     }
     return uid;
 }
-
+// HACK: There is still a timing issue with when this function is called.
+// The reason that avatar duplication was happening is that when the LRC account id is changed via
+// the account combobox, the ui updates itself and calls getUID for the avatars that it needs to
+// load, although by this point, the cache has not yet been cleared here. This ends up executing
+// after the getUID calls.
 void
 AvatarRegistry::connectAccount()
 {
-    uidMap_.clear();
+    clearCache();
     connect(lrcInstance_->getCurrentContactModel(),
             &ContactModel::profileUpdated,
             this,
@@ -93,3 +97,9 @@ AvatarRegistry::getUid(const QString& id)
     }
     return it.value();
 }
+
+void
+AvatarRegistry::clearCache()
+{
+    uidMap_.clear();
+}
diff --git a/src/app/avatarregistry.h b/src/app/avatarregistry.h
index 6076896a6..1b4640096 100644
--- a/src/app/avatarregistry.h
+++ b/src/app/avatarregistry.h
@@ -45,6 +45,8 @@ public:
     // add or update a specific image in the cache
     Q_SLOT QString addOrUpdateImage(const QString& id);
 
+    Q_INVOKABLE void clearCache();
+
 Q_SIGNALS:
     void avatarUidChanged(const QString& id);
 
diff --git a/src/app/mainview/components/AccountComboBoxPopup.qml b/src/app/mainview/components/AccountComboBoxPopup.qml
index 35118d233..a63c0ec6f 100644
--- a/src/app/mainview/components/AccountComboBoxPopup.qml
+++ b/src/app/mainview/components/AccountComboBoxPopup.qml
@@ -24,6 +24,7 @@ import net.jami.Models 1.1
 import net.jami.Adapters 1.1
 import net.jami.Constants 1.1
 import net.jami.Enums 1.1
+import net.jami.Helpers 1.1
 import "../../commoncomponents"
 
 Popup {
@@ -35,7 +36,7 @@ Popup {
 
     // limit the number of accounts shown at once
     implicitHeight: {
-        return visible ? Math.min(JamiTheme.accountListItemHeight * Math.min(6, listView.model.count + 1) + 96, appWindow.height - parent.height) : 0;
+        return visible ? Math.min(JamiTheme.accountListItemHeight * Math.min(6, listView.model.count + 1) + 91, appWindow.height - parent.height) : 0;
     }
     padding: 0
     modal: true
@@ -224,8 +225,12 @@ Popup {
             delegate: AccountItemDelegate {
                 height: JamiTheme.accountListItemHeight
                 width: root.width
+
                 onClicked: {
                     root.close();
+                    // This is a workaround for the synchronicity issue
+                    // in AvatarRegistry::connectAccount()
+                    AvatarRegistry.clearCache();
                     LRCInstance.currentAccountId = ID;
                 }
             }
-- 
GitLab