From 9195cb0bc5c4f322846439ff14969037d53fe194 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Blin?=
 <sebastien.blin@savoirfairelinux.com>
Date: Thu, 27 Oct 2022 12:10:51 -0400
Subject: [PATCH] replytorow: correctly update row if original post is editted

Change-Id: I2a76104b96d5eba554113e5aace1661170bbbb5d
---
 src/app/commoncomponents/ReplyToRow.qml | 19 +++++++--------
 src/libclient/messagelistmodel.cpp      | 32 ++++++++++++++++++-------
 src/libclient/messagelistmodel.h        |  3 ++-
 3 files changed, 35 insertions(+), 19 deletions(-)

diff --git a/src/app/commoncomponents/ReplyToRow.qml b/src/app/commoncomponents/ReplyToRow.qml
index 08c6c6b72..57d6833f9 100644
--- a/src/app/commoncomponents/ReplyToRow.qml
+++ b/src/app/commoncomponents/ReplyToRow.qml
@@ -28,7 +28,15 @@ Item {
 
     visible: ReplyTo !== ""
     width: visible ? replyToRow.width : 0
-    height: replyToRow.height + replyToRow.anchors.topMargin 
+    height: replyToRow.height + replyToRow.anchors.topMargin
+
+    Component.onCompleted: {
+        // Make sure we show the original post
+        // In the future, we may just want to load the previous interaction of the thread
+        // and not show it, but for now we can simplify.
+        if (ReplyTo !== "")
+            MessagesAdapter.loadConversationUntil(ReplyTo)
+    }
 
     MouseArea {
 
@@ -41,15 +49,6 @@ Item {
 
             property bool isSelf: ReplyToAuthor === CurrentAccount.uri || ReplyToAuthor === ""
 
-            onVisibleChanged: {
-                if (visible) {
-                    // Make sure we show the original post
-                    // In the future, we may just want to load the previous interaction of the thread
-                    // and not show it, but for now we can simplify.
-                    MessagesAdapter.loadConversationUntil(ReplyTo)
-                }
-            }
-
             Label {
                 id: replyTo
 
diff --git a/src/libclient/messagelistmodel.cpp b/src/libclient/messagelistmodel.cpp
index a2c5cb43e..e96a258b6 100644
--- a/src/libclient/messagelistmodel.cpp
+++ b/src/libclient/messagelistmodel.cpp
@@ -290,25 +290,31 @@ MessageListModel::moveMessage(const QString& msgId, const QString& parentId)
 }
 
 void
-MessageListModel::insertMessage(int index, item_t& message)
+MessageListModel::updateReplies(item_t& message)
 {
-    Q_EMIT beginInsertRows(QModelIndex(), index, index);
-    interactions_.insert(index, message);
-    Q_EMIT endInsertRows();
     auto replyId = message.second.commit["reply-to"];
     auto commitId = message.second.commit["id"];
-    if (!replyId.isEmpty())
-        replyTo_[replyId].append(commitId);
+    if (!replyId.isEmpty()) {
+        replyTo_[replyId].insert(commitId);
+    }
     for (const auto& msgId : replyTo_[commitId]) {
         int index = getIndexOfMessage(msgId);
         if (index == -1)
             continue;
         QModelIndex modelIndex = QAbstractListModel::index(index, 0);
-        Q_EMIT dataChanged(modelIndex, modelIndex, {Role::ReplyToAuthor});
-        Q_EMIT dataChanged(modelIndex, modelIndex, {Role::ReplyToBody});
+        Q_EMIT dataChanged(modelIndex, modelIndex, {Role::ReplyToAuthor, Role::ReplyToBody});
     }
 }
 
+void
+MessageListModel::insertMessage(int index, item_t& message)
+{
+    Q_EMIT beginInsertRows(QModelIndex(), index, index);
+    interactions_.insert(index, message);
+    Q_EMIT endInsertRows();
+    updateReplies(message);
+}
+
 iterator
 MessageListModel::insertMessage(iterator it, item_t& message)
 {
@@ -316,6 +322,7 @@ MessageListModel::insertMessage(iterator it, item_t& message)
     Q_EMIT beginInsertRows(QModelIndex(), index, index);
     auto insertion = interactions_.insert(it, message);
     Q_EMIT endInsertRows();
+    updateReplies(message);
     return insertion;
 }
 
@@ -591,6 +598,15 @@ MessageListModel::editMessage(const QString& msgId, interaction::Info& info)
         info.body = it->rbegin()->body;
         editedBodies_.erase(it);
         emitDataChanged(msgId, {MessageList::Role::Body, MessageList::Role::PreviousBodies});
+
+        // Body changed, replies should update
+        for (const auto& replyId : replyTo_[msgId]) {
+            int index = getIndexOfMessage(replyId);
+            if (index == -1)
+                continue;
+            QModelIndex modelIndex = QAbstractListModel::index(index, 0);
+            Q_EMIT dataChanged(modelIndex, modelIndex, {Role::ReplyToBody});
+        }
     }
 }
 
diff --git a/src/libclient/messagelistmodel.h b/src/libclient/messagelistmodel.h
index 241e59bd7..fbbf3936f 100644
--- a/src/libclient/messagelistmodel.h
+++ b/src/libclient/messagelistmodel.h
@@ -147,7 +147,8 @@ private:
     // to allow quick access.
     QMap<QString, QString> lastDisplayedMessageUid_;
     QMap<QString, QStringList> messageToReaders_;
-    QMap<QString, QStringList> replyTo_;
+    QMap<QString, QSet<QString>> replyTo_;
+    void updateReplies(item_t& message);
     QMap<QString, QVector<interaction::Body>> editedBodies_;
 
     void moveMessage(const QString& msgId, const QString& parentId);
-- 
GitLab