From 1f7b84cd7b8cb71fd2d5eb525e3130807b6842fa Mon Sep 17 00:00:00 2001
From: Nicolas Jager <nicolas.jager@savoirfairelinux.com>
Date: Thu, 11 Jan 2018 09:45:53 -0500
Subject: [PATCH] ConversationModel : add clearAllHistory
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

- ConversationModel::clearAllHistory erase all interaction stored
by the database with database::clearAllHistoryFor. Then it clean
the ConversationModel itself.

Change-Id: Ib78783a28381a31904e04046c886e7c216126638
Reviewed-by: Anthony Léonard <anthony.leonard@savoirfairelinux.com>
---
 src/api/conversationmodel.h      |  9 +++++++++
 src/authority/databasehelper.cpp | 10 ++++++++++
 src/authority/databasehelper.h   |  7 +++++++
 src/conversationmodel.cpp        | 11 +++++++++++
 4 files changed, 37 insertions(+)

diff --git a/src/api/conversationmodel.h b/src/api/conversationmodel.h
index c14fae9a..30dce411 100644
--- a/src/api/conversationmodel.h
+++ b/src/api/conversationmodel.h
@@ -138,6 +138,10 @@ public:
      * @param msgId, id of the interaction
      */
     void setInteractionRead(const std::string& convId, const uint64_t& msgId);
+    /**
+     * clear all history
+     */
+     void clearAllHistory();
 
 Q_SIGNALS:
     /**
@@ -178,6 +182,11 @@ Q_SIGNALS:
      * @param uid
      */
     void conversationRemoved(const std::string& uid) const;
+    /**
+     * Emitted after all history were cleared
+     * @note the client must connect this signal to know when update the view of the list
+     */
+    void allHistoryCleared() const;
 
 private:
     std::unique_ptr<ConversationModelPimpl> pimpl_;
diff --git a/src/authority/databasehelper.cpp b/src/authority/databasehelper.cpp
index 79601e12..31066901 100644
--- a/src/authority/databasehelper.cpp
+++ b/src/authority/databasehelper.cpp
@@ -280,6 +280,16 @@ void clearHistory(Database& db,
     db.deleteFrom("interactions", "conversation_id=:id", {{":id", conversationId}});
 }
 
+void clearAllHistoryFor(Database& db, const std::string& accountUri)
+{
+    auto accountId = db.select("id", "profiles","uri=:uri", {{":uri", accountUri}}).payloads;
+
+    if (accountId.empty())
+        return;
+
+    db.deleteFrom("interactions", "account_id=:account_id", {{":account_id", accountId[0]}});
+}
+
 void
 removeContact(Database& db, const std::string& accountUri, const std::string& contactUri)
 {
diff --git a/src/authority/databasehelper.h b/src/authority/databasehelper.h
index b61d6167..57e3a77a 100644
--- a/src/authority/databasehelper.h
+++ b/src/authority/databasehelper.h
@@ -187,6 +187,13 @@ void updateInteractionStatus(Database& db, unsigned int id,
 void clearHistory(Database& db,
                   const std::string& conversationId);
 
+/**
+ * Clear all history stored in the database for the account uri
+ * @param  db
+ * @param accountUri
+ */
+void clearAllHistoryFor(Database& db, const std::string& accountUri);
+
 /**
  * Remove a conversation between an account and a contact. Remove corresponding entries in
  * the conversations table and profiles if the profile is not present in conversations.
diff --git a/src/conversationmodel.cpp b/src/conversationmodel.cpp
index 77388bbd..6197d672 100644
--- a/src/conversationmodel.cpp
+++ b/src/conversationmodel.cpp
@@ -601,6 +601,17 @@ ConversationModel::clearHistory(const std::string& uid)
     emit conversationCleared(uid);
 }
 
+void
+ConversationModel::clearAllHistory()
+{
+    database::clearAllHistoryFor(pimpl_->db, owner.profileInfo.uri);
+
+    for (auto& conversation : pimpl_->conversations) {
+        conversation.interactions.clear();
+        database::getHistory(pimpl_->db, conversation);
+    }
+}
+
 void
 ConversationModel::setInteractionRead(const std::string& convId, const uint64_t& msgId)
 {
-- 
GitLab