From 50a9dbe585d6a1436f35e15640123b1b4384c59c Mon Sep 17 00:00:00 2001
From: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com>
Date: Mon, 26 Jul 2021 11:51:30 -0400
Subject: [PATCH] profiles: remove vcard when removing a contact

Change-Id: I60db7cd6bc8ce6819ae3af4e20585055da4c6800
---
 src/authority/storagehelper.cpp | 13 ++++++++++++-
 src/authority/storagehelper.h   | 14 ++++++++++++--
 src/contactmodel.cpp            |  9 ++++++---
 src/conversationmodel.cpp       |  7 ++++---
 4 files changed, 34 insertions(+), 9 deletions(-)

diff --git a/src/authority/storagehelper.cpp b/src/authority/storagehelper.cpp
index fcdcc0ad..8690270f 100644
--- a/src/authority/storagehelper.cpp
+++ b/src/authority/storagehelper.cpp
@@ -268,6 +268,17 @@ createOrUpdateProfile(const QString& accountId,
     vcard::setProfile(accountId, profileInfo, isPeer);
 }
 
+void
+removeProfile(const QString& accountId, const QString& peerUri)
+{
+    auto accountLocalPath = getPath() + accountId + QDir::separator();
+    auto fileName = QString(peerUri.toUtf8().toBase64());
+    auto path = accountLocalPath + "profiles" + QDir::separator() + fileName + ".vcf";
+    if (!QFile::remove(path)) {
+        qWarning() << "Couldn't remove vcard for" << peerUri << "at" << path;
+    }
+}
+
 QString
 getAccountAvatar(const QString& accountId)
 {
@@ -672,7 +683,7 @@ deleteObsoleteHistory(Database& db, long int date)
 }
 
 void
-removeContact(Database& db, const QString& contactUri)
+removeContactConversations(Database& db, const QString& contactUri)
 {
     // Get common conversations
     auto conversations = getConversationsWithPeer(db, contactUri);
diff --git a/src/authority/storagehelper.h b/src/authority/storagehelper.h
index 09326ac7..9afaa6f8 100644
--- a/src/authority/storagehelper.h
+++ b/src/authority/storagehelper.h
@@ -113,6 +113,13 @@ void createOrUpdateProfile(const QString& accountId,
                            const api::profile::Info& profileInfo,
                            const bool isPeer = false);
 
+/**
+ * Remove a profile vCard
+ * @param accountId
+ * @param peerUri
+ */
+void removeProfile(const QString& accountId, const QString& peerUri);
+
 /**
  * Gets the account's avatar from the profile.vcf file
  * @param  accountId
@@ -241,7 +248,9 @@ QString getInteractionExtraDataById(Database& db, const QString& id, const QStri
  * @param daemon id
  * @param interaction
  */
-void updateDataTransferInteractionForDaemonId(Database& db, const QString& daemonId, api::interaction::Info& interaction);
+void updateDataTransferInteractionForDaemonId(Database& db,
+                                              const QString& daemonId,
+                                              api::interaction::Info& interaction);
 
 /**
  * Change the body of an interaction
@@ -300,10 +309,11 @@ void deleteObsoleteHistory(Database& db, long int date);
 /**
  * Remove all conversation with a contact. Remove corresponding entries in
  * the conversations table.
+ * @param accountId
  * @param db
  * @param contactUri
  */
-void removeContact(Database& db, const QString& contactUri);
+void removeContactConversations(Database& db, const QString& contactUri);
 
 /**
  * count number of 'UNREAD' from 'interactions' table.
diff --git a/src/contactmodel.cpp b/src/contactmodel.cpp
index b73de18f..bb19a1de 100644
--- a/src/contactmodel.cpp
+++ b/src/contactmodel.cpp
@@ -325,12 +325,14 @@ ContactModel::removeContact(const QString& contactUri, bool banned)
                 return;
             }
             pimpl_->contacts.remove(contactUri);
-            storage::removeContact(pimpl_->db, contactUri);
+            storage::removeContactConversations(pimpl_->db, contactUri);
+            storage::removeProfile(owner.id, contactUri);
             emitContactRemoved = true;
         } else if (owner.profileInfo.type == profile::Type::SIP) {
             // Remove contact from db
             pimpl_->contacts.remove(contactUri);
-            storage::removeContact(pimpl_->db, contactUri);
+            storage::removeContactConversations(pimpl_->db, contactUri);
+            storage::removeProfile(owner.id, contactUri);
             emitContactRemoved = true;
         }
     }
@@ -862,7 +864,8 @@ ContactModelPimpl::slotContactRemoved(const QString& accountId,
                     bannedContacts.erase(it);
                 }
             }
-            storage::removeContact(db, contactUri);
+            storage::removeContactConversations(db, contactUri);
+            storage::removeProfile(linked.owner.id, contactUri);
             contacts.remove(contactUri);
         }
     }
diff --git a/src/conversationmodel.cpp b/src/conversationmodel.cpp
index bd6fd473..94007e85 100644
--- a/src/conversationmodel.cpp
+++ b/src/conversationmodel.cpp
@@ -2372,7 +2372,7 @@ ConversationModelPimpl::slotConversationReady(const QString& accountId,
                 // remove non swarm conversation
                 if (conversation.isLegacy()) {
                     eraseConversation(conversation.uid);
-                    storage::removeContact(db, member["uri"]);
+                    storage::removeContactConversations(db, member["uri"]);
                     invalidateModel();
                     emit linked.conversationRemoved(conversation.uid);
                     emit linked.modelChanged();
@@ -2561,6 +2561,7 @@ ConversationModelPimpl::slotContactAdded(const QString& contactUri)
     if (removeConversation) {
         eraseConversation(indexOf(contactUri));
         invalidateModel();
+        Q_EMIT linked.conversationRemoved(contactUri);
         emit linked.modelChanged();
     }
 }
@@ -2604,7 +2605,7 @@ ConversationModelPimpl::addConversationRequest(const MapStringString& convReques
             // update conversation and remoe conversation from db
             conv.mode = mode;
             conv.uid = convId;
-            storage::removeContact(db, peer);
+            storage::removeContactConversations(db, peer);
             invalidateModel();
             emit linked.modelChanged();
             return;
@@ -2756,7 +2757,7 @@ ConversationModelPimpl::addSwarmConversation(const QString& convId)
                 eraseConversation(conv.uid);
                 invalidateModel();
                 Q_EMIT linked.conversationRemoved(conv.uid);
-                storage::removeContact(db, participantId);
+                storage::removeContactConversations(db, participantId);
             }
         } catch (...) {
         }
-- 
GitLab