message view: update message view when reacting to invites

Change-Id: Idc4d3b74b181aa8538e782fe1699d54d5b1b580a
Gitlab: #432
diff --git a/callwidget.cpp b/callwidget.cpp
index b28df8a..f001bda 100644
--- a/callwidget.cpp
+++ b/callwidget.cpp
@@ -204,6 +204,11 @@
             ui->btnInvites->setChecked(false);
         });
 
+    connect(ui->messageView, &MessageWebView::conversationRemoved,
+        [this] {
+            backToWelcomePage();
+        });
+
     // set first view to welcome view
     ui->stackedWidget->setCurrentWidget(ui->welcomePage);
     ui->btnConversations->setChecked(true);
@@ -581,20 +586,27 @@
 
 void CallWidget::slotAcceptInviteClicked(const QModelIndex & index)
 {
-    auto convUid = index.data(static_cast<int>(SmartListModel::Role::UID)).value<QString>();
-    LRCInstance::getCurrentConversationModel()->makePermanent(convUid.toStdString());
+    auto convUid = index.data(static_cast<int>(SmartListModel::Role::UID)).value<QString>().toStdString();
+    LRCInstance::getCurrentConversationModel()->makePermanent(convUid);
+    ui->messageView->setInvitation(false);
 }
 
 void CallWidget::slotBlockInviteClicked(const QModelIndex & index)
 {
-    auto convUid = index.data(static_cast<int>(SmartListModel::Role::UID)).value<QString>();
-    LRCInstance::getCurrentConversationModel()->removeConversation(convUid.toStdString(), true);
+    auto convUid = index.data(static_cast<int>(SmartListModel::Role::UID)).value<QString>().toStdString();
+    if (!convUid.empty() && convUid == LRCInstance::getSelectedConvUid()) {
+        backToWelcomePage();
+    }
+    LRCInstance::getCurrentConversationModel()->removeConversation(convUid, true);
 }
 
 void CallWidget::slotIgnoreInviteClicked(const QModelIndex & index)
 {
-    auto convUid = index.data(static_cast<int>(SmartListModel::Role::UID)).value<QString>();
-    LRCInstance::getCurrentConversationModel()->removeConversation(convUid.toStdString(), false);
+    auto convUid = index.data(static_cast<int>(SmartListModel::Role::UID)).value<QString>().toStdString();
+    if (!convUid.empty() && convUid == LRCInstance::getSelectedConvUid()) {
+        backToWelcomePage();
+    }
+    LRCInstance::getCurrentConversationModel()->removeConversation(convUid, false);
 }
 
 void CallWidget::slotCustomContextMenuRequested(const QPoint& pos)
@@ -607,8 +619,20 @@
     try {
         auto accountList = LRCInstance::accountModel().getAccountList();
         setSelectedAccount(accountList.at(index));
+        auto& contactModel = LRCInstance::getCurrentAccountInfo().contactModel;
+        disconnect(contactAddedConnection_);
+        contactAddedConnection_ = connect(contactModel.get(), &lrc::api::ContactModel::contactAdded,
+            [this, &contactModel](const std::string & contactId) {
+                auto convModel = LRCInstance::getCurrentConversationModel();
+                auto currentConversation = Utils::getConversationFromUid(selectedConvUid(),
+                    *convModel);
+                if (contactId == contactModel.get()->getContact((*currentConversation).participants.at(0)).profileInfo.uri) {
+                    ui->messageView->clear();
+                    ui->messageView->printHistory(*convModel, currentConversation->interactions);
+                }
+            });
     } catch (...) {
-        qWarning() << "exception changing account";
+        qWarning() << "CallWidget::slotAccountChanged exception";
     }
 }
 
@@ -851,7 +875,7 @@
     auto currentConversation = Utils::getConversationFromUid(selectedConvUid(),
                                                              *convModel);
     ui->messageView->clear();
-    ui->messageView->printHistory(*convModel, currentConversation->interactions);
+    ui->messageView->printHistory(*convModel, currentConversation->interactions, true);
 
     // Contact Avatars
     auto accInfo = &LRCInstance::getCurrentAccountInfo();
@@ -927,6 +951,7 @@
 CallWidget::on_sendContactRequestButton_clicked()
 {
     LRCInstance::getCurrentConversationModel()->makePermanent(selectedConvUid());
+    ui->sendContactRequestButton->hide();
 }
 
 void
diff --git a/callwidget.h b/callwidget.h
index e955dad..a785154 100644
--- a/callwidget.h
+++ b/callwidget.h
@@ -165,4 +165,6 @@
     QMetaObject::Connection selectedCallChanged_;
     QMetaObject::Connection smartlistSelectionConnection_;
     QMetaObject::Connection interactionRemovedConnection_;
+    QMetaObject::Connection contactAddedConnection_;
+
 };
diff --git a/messagewebview.cpp b/messagewebview.cpp
index 2a9dfa2..73af56d 100644
--- a/messagewebview.cpp
+++ b/messagewebview.cpp
@@ -196,11 +196,12 @@
 void
 MessageWebView::printHistory(lrc::api::ConversationModel& conversationModel,
                              const std::map<uint64_t,
-                             lrc::api::interaction::Info> interactions)
+                             lrc::api::interaction::Info> interactions,
+                             bool fadeIn)
 {
     auto interactionsStr = interactionsToJsonArrayObject(conversationModel, interactions).toUtf8();
-    QString s = QString::fromLatin1("printHistory(%1);")
-        .arg(interactionsStr.constData());
+    QString s = QString::fromLatin1("printHistory(%1,%2);")
+        .arg(interactionsStr.constData()).arg(fadeIn);
     page()->runJavaScript(s, QWebEngineScript::MainWorld);
 }
 
@@ -358,6 +359,7 @@
     try {
         auto convUid = LRCInstance::getSelectedConvUid();
         LRCInstance::getCurrentConversationModel()->makePermanent(convUid);
+        qobject_cast<MessageWebView*>(this->parent())->setInvitation(false);
     } catch (...) {
         qDebug() << "JS bridging - exception during acceptInvitation";
     }
@@ -370,6 +372,10 @@
     try {
         auto convUid = LRCInstance::getSelectedConvUid();
         LRCInstance::getCurrentConversationModel()->removeConversation(convUid, false);
+        if (auto messageView = qobject_cast<MessageWebView*>(this->parent())) {
+            messageView->setInvitation(false);
+            emit messageView->conversationRemoved();
+        }
     } catch (...) {
         qDebug() << "JS bridging - exception during refuseInvitation";
     }
@@ -382,6 +388,10 @@
     try {
         auto convUid = LRCInstance::getSelectedConvUid();
         LRCInstance::getCurrentConversationModel()->removeConversation(convUid, true);
+        if (auto messageView = qobject_cast<MessageWebView*>(this->parent())) {
+            messageView->setInvitation(false);
+            emit messageView->conversationRemoved();
+        }
     } catch (...) {
         qDebug() << "JS bridging - exception during blockConversation";
     }
diff --git a/messagewebview.h b/messagewebview.h
index 5c65ac6..aa83318 100644
--- a/messagewebview.h
+++ b/messagewebview.h
@@ -67,12 +67,18 @@
     void removeInteraction(uint64_t interactionId);
     void printHistory(lrc::api::ConversationModel& conversationModel,
                       const std::map<uint64_t,
-                      lrc::api::interaction::Info> interactions);
+                      lrc::api::interaction::Info> interactions,
+                      bool fadeIn = false);
     void setSenderImage(const std::string& sender,
                         const std::string& senderImage);
-    void setInvitation(bool show, const std::string& contactUri, const std::string& contactId);
+    void setInvitation(bool show,
+                       const std::string& contactUri = "",
+                       const std::string& contactId = "");
     void hideMessages();
 
+signals:
+    void conversationRemoved();
+
 private slots:
     void slotLoadFinished();
 
diff --git a/web/chatview.js b/web/chatview.js
index 2e7d9c8..8a29488 100644
--- a/web/chatview.js
+++ b/web/chatview.js
@@ -1479,9 +1479,11 @@
  * @param messages_array should contain history to be printed
  */
 /* exported printHistory */
-function printHistory(messages_array)
+function printHistory(messages_array, fadein = false)
 {
-    hideBody()
+    if (fadein) {
+        hideBody()
+    }
 
     historyBuffer = messages_array
     historyBufferIndex = 0
@@ -1490,7 +1492,9 @@
     printHistoryPart(messages, 0)
     isInitialLoading = false
 
-    document.body.classList.remove('fade');
+    if (fadein) {
+        document.body.classList.remove('fade');
+    }
 }
 
 /**