From dafecae218945bfc4c24be61da062f48f732778d Mon Sep 17 00:00:00 2001
From: Nicolas Jager <nicolas.jager@savoirfairelinux.com>
Date: Tue, 16 Jan 2018 15:28:06 -0500
Subject: [PATCH] delete obsolete history.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

- add ConversationModel::deleteObsoleteHistory. This method can be
  called from clients.

- add database::deleteObsoleteHistory. This method is used only by
  ConversationModel::deleteObsoleteHistory.

Change-Id: I4d1187efee13e2927e8c963d15b5d5e34ac88b4d
Reviewed-by: Anthony Léonard <anthony.leonard@savoirfairelinux.com>
---
 src/api/conversationmodel.h      |  5 +++++
 src/authority/databasehelper.cpp |  6 ++++++
 src/authority/databasehelper.h   |  7 +++++++
 src/conversationmodel.cpp        | 12 ++++++++++++
 4 files changed, 30 insertions(+)

diff --git a/src/api/conversationmodel.h b/src/api/conversationmodel.h
index 30dce411..3203e787 100644
--- a/src/api/conversationmodel.h
+++ b/src/api/conversationmodel.h
@@ -142,6 +142,11 @@ public:
      * clear all history
      */
      void clearAllHistory();
+    /**
+     * delete obsolete history from the database
+     * @param days, number of days from today. Below this date, interactions will be deleted
+     */
+    void deleteObsoleteHistory(int date);
 
 Q_SIGNALS:
     /**
diff --git a/src/authority/databasehelper.cpp b/src/authority/databasehelper.cpp
index 31066901..053b50d9 100644
--- a/src/authority/databasehelper.cpp
+++ b/src/authority/databasehelper.cpp
@@ -358,6 +358,12 @@ countUnreadFromInteractions(Database& db, const std::string& conversationId)
     return db.count("status", "interactions", "status='UNREAD' AND conversation_id='" + conversationId + "'");
 }
 
+void
+deleteObsoleteHistory(Database& db, long int date)
+{
+    db.deleteFrom("interactions", "timestamp<=:date", {{":date", std::to_string(date)}});
+}
+
 } // namespace database
 
 } // namespace authority
diff --git a/src/authority/databasehelper.h b/src/authority/databasehelper.h
index 57e3a77a..d750a313 100644
--- a/src/authority/databasehelper.h
+++ b/src/authority/databasehelper.h
@@ -194,6 +194,13 @@ void clearHistory(Database& db,
  */
 void clearAllHistoryFor(Database& db, const std::string& accountUri);
 
+/**
+ * delete obsolete histori from the database
+ * @param db
+ * @param date in second since epoch. Below this date, interactions will be deleted
+ */
+void deleteObsoleteHistory(Database& db, long int date);
+
 /**
  * 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 6197d672..3eb82442 100644
--- a/src/conversationmodel.cpp
+++ b/src/conversationmodel.cpp
@@ -378,6 +378,18 @@ ConversationModel::removeConversation(const std::string& uid, bool banned)
         owner.contactModel->removeContact(participant, banned);
 }
 
+void
+ConversationModel::deleteObsoleteHistory(int days)
+{
+    if(days < 1)
+        return; // unlimited history
+
+    auto currentTime = static_cast<long int>(std::time(nullptr)); // since epoch, in seconds...
+    auto date = currentTime - (days * 86400);
+
+    database::deleteObsoleteHistory(pimpl_->db, date);
+}
+
 void
 ConversationModelPimpl::placeCall(const std::string& uid, bool isAudioOnly)
 {
-- 
GitLab