From a5544675c5fc65c91dc160435bebf6d7de37c769 Mon Sep 17 00:00:00 2001
From: Nicolas Jager <nicolas.jager@savoirfairelinux.com>
Date: Fri, 13 Oct 2017 09:47:04 -0400
Subject: [PATCH] fix for conferences

- bug : conferences ids are mixed with calls ids wich lead to several
bugs.

- fix : split conferences ids and calls ids.

Change-Id: Ia2e7985757c8ea8076858e16e48946ecc56acf7a
Reviewed-by: Guillaume Roguez <guillaume.roguez@savoirfairelinux.com>
---
 src/api/conversation.h    |  1 +
 src/callbackshandler.cpp  |  1 -
 src/conversationmodel.cpp | 51 ++++++++++++++++++++++++++++++++++-----
 src/newcallmodel.cpp      | 28 ++++-----------------
 4 files changed, 51 insertions(+), 30 deletions(-)

diff --git a/src/api/conversation.h b/src/api/conversation.h
index 62ba0d79..463389e4 100644
--- a/src/api/conversation.h
+++ b/src/api/conversation.h
@@ -40,6 +40,7 @@ struct Info
     std::string accountId;
     std::vector<std::string> participants;
     std::string callId;
+    std::string confId;
     std::map<uint64_t, interaction::Info> interactions;
     uint64_t lastMessageUid = 0;
     unsigned int unreadMessages = 0;
diff --git a/src/callbackshandler.cpp b/src/callbackshandler.cpp
index 58ed71fd..18e90d15 100644
--- a/src/callbackshandler.cpp
+++ b/src/callbackshandler.cpp
@@ -272,5 +272,4 @@ CallbacksHandler::slotAccountMessageStatusChanged(const QString& accountId,
                                      to.toStdString(), status);
 }
 
-
 } // namespace lrc
diff --git a/src/conversationmodel.cpp b/src/conversationmodel.cpp
index 022ecac5..22b3d140 100644
--- a/src/conversationmodel.cpp
+++ b/src/conversationmodel.cpp
@@ -187,6 +187,11 @@ public Q_SLOTS:
      * @param confId
      */
     void slotCallAddedToConference(const std::string& callId, const std::string& confId);
+    /**
+     * Listen from CallbacksHandler when a conference is deleted.
+     * @param confId
+     */
+    void slotConferenceRemoved(const std::string& confId);
 
 };
 
@@ -295,8 +300,11 @@ ConversationModel::selectConversation(const std::string& uid) const
 
     auto& conversation = pimpl_->conversations.at(conversationIdx);
     try  {
-        auto call = owner.callModel->getCall(conversation.callId);
-        switch (call.status) {
+        if (not conversation.confId.empty()) {
+            emit pimpl_->behaviorController.showCallView(owner.id, conversation);
+        } else {
+            auto call = owner.callModel->getCall(conversation.callId);
+            switch (call.status) {
             case call::Status::INCOMING_RINGING:
             case call::Status::OUTGOING_RINGING:
             case call::Status::CONNECTING:
@@ -320,6 +328,7 @@ ConversationModel::selectConversation(const std::string& uid) const
             default:
                 // We are not in a call, show the chatview
                 emit pimpl_->behaviorController.showChatView(owner.id, conversation);
+            }
         }
     } catch (const std::out_of_range&) {
         emit pimpl_->behaviorController.showChatView(owner.id, conversation);
@@ -507,10 +516,26 @@ ConversationModel::joinConversations(const std::string& uidA, const std::string&
         return;
     auto& conversationA = pimpl_->conversations[conversationAIdx];
     auto& conversationB = pimpl_->conversations[conversationBIdx];
-    // NOTE: To create a conference, we must be in call for now.
+
     if (conversationA.callId.empty() || conversationB.callId.empty())
         return;
-    owner.callModel->joinCalls(conversationA.callId, conversationB.callId);
+
+    if (conversationA.confId.empty()) {
+        if(conversationB.confId.empty()){
+            owner.callModel->joinCalls(conversationA.callId, conversationB.callId);
+        }else{
+            owner.callModel->joinCalls(conversationA.callId, conversationB.confId);
+            conversationA.confId = conversationB.confId;
+        }
+    } else {
+        if(conversationB.confId.empty()){
+            owner.callModel->joinCalls(conversationA.confId, conversationB.callId);
+            conversationB.confId = conversationA.confId;
+        }else{
+            owner.callModel->joinCalls(conversationA.confId, conversationB.confId);
+            conversationB.confId = conversationA.confId;
+        }
+    }
 }
 
 void
@@ -600,6 +625,10 @@ ConversationModelPimpl::ConversationModelPimpl(const ConversationModel& linked,
             &lrc::api::NewCallModel::callAddedToConference,
             this,
             &ConversationModelPimpl::slotCallAddedToConference);
+    connect(&callbacksHandler,
+            &CallbacksHandler::conferenceRemoved,
+            this,
+            &ConversationModelPimpl::slotConferenceRemoved);
 }
 
 ConversationModelPimpl::~ConversationModelPimpl()
@@ -908,7 +937,7 @@ ConversationModelPimpl::slotIncomingCallMessage(const std::string& callId, const
     if (call.type == call::Type::CONFERENCE) {
         // Show messages in all conversations for conferences.
         for (const auto& conversation: conversations) {
-            if (conversation.callId == callId) {
+            if (conversation.confId == callId) {
                 if (conversation.participants.empty()) continue;
                 auto authorProfileId = database::getOrInsertProfile(db, from);
                 addIncomingMessage(conversation.participants.front(), body, authorProfileId);
@@ -955,7 +984,7 @@ ConversationModelPimpl::slotCallAddedToConference(const std::string& callId, con
 {
     for (auto& conversation: conversations) {
         if (conversation.callId == callId) {
-            conversation.callId = confId;
+            conversation.confId = confId;
             dirtyConversations = true;
             emit linked.selectConversation(conversation.uid);
         }
@@ -1011,6 +1040,16 @@ ConversationModelPimpl::slotUpdateInteractionStatus(const std::string& accountId
     }
 }
 
+void
+ConversationModelPimpl::slotConferenceRemoved(const std::string& confId)
+{
+    // Get conversation
+    for(auto& i : conversations){
+        if (i.confId == confId)
+            i.confId = "";
+    }
+}
+
 } // namespace lrc
 
 #include "api/moc_conversationmodel.cpp"
diff --git a/src/newcallmodel.cpp b/src/newcallmodel.cpp
index 8ce31a3d..3d1a0fb2 100644
--- a/src/newcallmodel.cpp
+++ b/src/newcallmodel.cpp
@@ -96,11 +96,6 @@ public Q_SLOTS:
      * @param callId
      */
     void slotConferenceCreated(const std::string& callId);
-    /**
-     * Listen from CallbacksHandler when a conference is deleted.
-     * @param callId
-     */
-    void slotConferenceRemoved(const std::string& callId);
 };
 
 NewCallModel::NewCallModel(const account::Info& owner, const CallbacksHandler& callbacksHandler)
@@ -338,7 +333,6 @@ NewCallModelPimpl::NewCallModelPimpl(const NewCallModel& linked, const Callbacks
     connect(&VideoRendererManager::instance(), &VideoRendererManager::remotePreviewStarted, this, &NewCallModelPimpl::slotRemotePreviewStarted);
     connect(&callbacksHandler, &CallbacksHandler::incomingVCardChunk, this, &NewCallModelPimpl::slotincomingVCardChunk);
     connect(&callbacksHandler, &CallbacksHandler::conferenceCreated, this , &NewCallModelPimpl::slotConferenceCreated);
-    connect(&callbacksHandler, &CallbacksHandler::conferenceRemoved, this , &NewCallModelPimpl::slotConferenceRemoved);
 }
 
 NewCallModelPimpl::~NewCallModelPimpl()
@@ -463,32 +457,20 @@ NewCallModel::hasCall(const std::string& callId)
 }
 
 void
-NewCallModelPimpl::slotConferenceCreated(const std::string& callId)
+NewCallModelPimpl::slotConferenceCreated(const std::string& confId)
 {
-    if (calls.find(callId) != calls.end()) return;
     auto callInfo = std::make_shared<call::Info>();
-    callInfo->id = callId;
+    callInfo->id = confId;
     callInfo->status =  call::Status::IN_PROGRESS;
     callInfo->type =  call::Type::CONFERENCE;
     callInfo->startTime = std::chrono::steady_clock::now();
-    calls[callId] = callInfo;
-    QStringList callList = CallManager::instance().getParticipantList(callId.c_str());
+    calls[confId] = callInfo;
+    QStringList callList = CallManager::instance().getParticipantList(confId.c_str());
     foreach(const auto& call, callList) {
-        emit linked.callAddedToConference(call.toStdString(), callId);
+        emit linked.callAddedToConference(call.toStdString(), confId);
     }
 }
 
-void
-NewCallModelPimpl::slotConferenceRemoved(const std::string& callId)
-{
-    auto it = calls.find(callId);
-    if (it == calls.end()) return;
-    auto& call = it->second;
-    call->status =  call::Status::ENDED;
-    emit linked.callEnded(callId);
-    emit linked.callStatusChanged(callId);
-}
-
 } // namespace lrc
 
 #include "api/moc_newcallmodel.cpp"
-- 
GitLab