From ec014d288231428da162e8065469b43789b5d985 Mon Sep 17 00:00:00 2001
From: Dennis Beier <nanovim@gmail.com>
Date: Fri, 8 Jun 2018 22:54:18 +0200
Subject: [PATCH] some code to fix the reapparence of deleted rooms

---
 services/rocketchatchannelservice.cpp | 28 +++++++++++++++++++++++----
 services/rocketchatchannelservice.h   |  2 +-
 2 files changed, 25 insertions(+), 5 deletions(-)

diff --git a/services/rocketchatchannelservice.cpp b/services/rocketchatchannelservice.cpp
index 8e57fad..83a77e3 100755
--- a/services/rocketchatchannelservice.cpp
+++ b/services/rocketchatchannelservice.cpp
@@ -50,7 +50,7 @@ QSharedPointer<RocketChatChannel> RocketChatChannelService::createChannelObject(
 }
 
 
-QVector<QSharedPointer<RocketChatChannel> > RocketChatChannelService::processChannelData( QJsonArray pChannelArray, bool pJoined )
+QVector<QSharedPointer<RocketChatChannel> > RocketChatChannelService::processChannelData( QJsonArray pChannelArray, bool pJoined, bool pUpdateOnly )
 {
     QVector<QSharedPointer<RocketChatChannel> > vec;
 
@@ -72,7 +72,15 @@ QVector<QSharedPointer<RocketChatChannel> > RocketChatChannelService::processCha
             if ( id == "" ) {
                 id = currentChannelObject["_id"].toString();
             }
-
+            //TODO: refactor me please!
+            if(pUpdateOnly && !mChannels->contains(id)){
+                continue;
+            }
+            if(currentChannelObject.contains("open")&&!currentChannelObject["open"].toBool()){
+                mChannels->remove(id);
+                mStorage->deleteChannel(id);
+                continue;
+            }
             QSharedPointer<RocketChatChannel> channel = nullptr;
 
             if ( mChannels->contains( id ) ) {
@@ -150,13 +158,22 @@ void RocketChatChannelService::loadJoinedChannelsFromServer( void )
 
         if ( Q_LIKELY( pResponse.contains( "result" ) ) ) {
             QJsonArray channels = pResponse["result"].toArray();
-            auto channelsParsed = processChannelData( channels, true );
+            auto channelsParsed = processChannelData( channels, true, false );
+            emit channelsLoaded( channelsParsed, true );
+        }
+    };
+    std::function<void ( QJsonObject, MeteorDDP * )> roomsSuccess = [ = ]( QJsonObject pResponse, MeteorDDP * pDdpApi ) {
+        Q_UNUSED( pDdpApi );
+
+        if ( Q_LIKELY( pResponse.contains( "result" ) ) ) {
+            QJsonArray channels = pResponse["result"].toArray();
+            auto channelsParsed = processChannelData( channels, true, true );
             emit channelsLoaded( channelsParsed, true );
         }
     };
     QSharedPointer<DDPRequest> subscriptions( new DDPMethodRequest( "subscriptions/get", {}, [ = ]( QJsonObject pResponse, MeteorDDP * pDdpApi ) {
         subscriptionSuccess( pResponse, pDdpApi );
-        QSharedPointer<DDPRequest> rooms( new DDPMethodRequest( "rooms/get", {}, subscriptionSuccess ) );
+        QSharedPointer<DDPRequest> rooms( new DDPMethodRequest( "rooms/get", {}, roomsSuccess ) );
         mServer->sendDdprequest( rooms );
     } ) );
     mServer->sendDdprequest( subscriptions );
@@ -312,6 +329,8 @@ void RocketChatChannelService::leaveChannel( const QString &pId )
         QSharedPointer<RocketChatLeaveRoomRequest> request( new RocketChatLeaveRoomRequest( pId ) );
         auto ddp = getDdp();
         ddp->sendRequest( request );
+        QSharedPointer<RocketChatHideRoomRequest> request2( new RocketChatHideRoomRequest( pId ) );
+        getDdp()->sendRequest(request2);
         deleteChannel( pId );
     }
 }
@@ -320,6 +339,7 @@ void RocketChatChannelService::deleteChannel( const QString &pId )
 {
     if ( mChannels->contains( pId ) ) {
         mChannels->get( pId )->setDeleted( true );
+        mChannels->remove(pId);
         mStorage->transaction();
         mStorage->deleteChannel( pId );
         mStorage->commit();
diff --git a/services/rocketchatchannelservice.h b/services/rocketchatchannelservice.h
index d8ccf0d..a47aff3 100755
--- a/services/rocketchatchannelservice.h
+++ b/services/rocketchatchannelservice.h
@@ -93,7 +93,7 @@ class RocketChatChannelService : public QObject
         ChannelRepository *mChannels = nullptr;
         MeteorDDP *mDdp;
 
-        QVector<QSharedPointer<RocketChatChannel>> processChannelData( QJsonArray pChannelArray, bool pJoined );
+        QVector<QSharedPointer<RocketChatChannel>> processChannelData( QJsonArray pChannelArray, bool pJoined , bool pUpdateOnly );
 
         MessageService *mMessageService;
     signals:
-- 
GitLab