From 74be9bd5fc620f8bf6b13b9170bf046440a34fc5 Mon Sep 17 00:00:00 2001
From: Armin Felder <armin.felder@osalliance.com>
Date: Sat, 22 Dec 2018 21:50:08 +0100
Subject: [PATCH] as

---
 repos/entities/tempfile.cpp | 10 ++++++++
 repos/entities/tempfile.h   |  4 +++
 rocketchat.cpp              |  6 ++---
 rocketchatserver.cpp        | 51 ++++++++++++++++++++++++-------------
 services/fileservice.cpp    | 22 ++++++++++++----
 5 files changed, 67 insertions(+), 26 deletions(-)

diff --git a/repos/entities/tempfile.cpp b/repos/entities/tempfile.cpp
index 7040c2f..0ce8eca 100644
--- a/repos/entities/tempfile.cpp
+++ b/repos/entities/tempfile.cpp
@@ -40,6 +40,16 @@ const QFileInfo &TempFile::getFileInfo() const
     return mFileInfo;
 }
 
+bool TempFile::getFileChecked() const
+{
+    return mFileChecked;
+}
+
+void TempFile::setFileChecked(bool fileChecked)
+{
+    mFileChecked = fileChecked;
+}
+
 const QString &TempFile::getFilePath() const
 {
     return mFilePath;
diff --git a/repos/entities/tempfile.h b/repos/entities/tempfile.h
index 6fc50f7..f40602b 100644
--- a/repos/entities/tempfile.h
+++ b/repos/entities/tempfile.h
@@ -44,8 +44,12 @@ class TempFile
 
         const QFileInfo &getFileInfo() const;
 
+        bool getFileChecked() const;
+        void setFileChecked( bool fileChecked );
+
     protected:
         TempFile() = default;
+        bool mFileChecked = false;
         QUrl mUrl;
         QString mFileName;
         QString mFilePath;
diff --git a/rocketchat.cpp b/rocketchat.cpp
index 9f2d3ee..3d1c085 100755
--- a/rocketchat.cpp
+++ b/rocketchat.cpp
@@ -296,9 +296,9 @@ void RocketChat::clearWebViewCookies()
 #endif
 }
 
-void RocketChat::deleteMessage(QString rid, QString id)
+void RocketChat::deleteMessage( QString rid, QString id )
 {
-    QMetaObject::invokeMethod( mServerMap.first(), "deleteMessage", Q_ARG( QString, rid ),Q_ARG( QString, id ) );
+    QMetaObject::invokeMethod( mServerMap.first(), "deleteMessage", Q_ARG( QString, rid ), Q_ARG( QString, id ) );
 }
 
 bool RocketChat::newServerByDomain( const QString &domain, bool pUnsecure = false )
@@ -676,10 +676,10 @@ void RocketChat::onUnreadCountChanged( const QString &pServerId, uint pUnread )
 
 void RocketChat::onLoggedIn( const QString &pServerId )
 {
+    emit loggedIn( pServerId );
 #if defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
     checkForpendingNotification();
 #endif
-    emit loggedIn( pServerId );
 }
 
 void RocketChat::onChannelSwitchRequest( const QSharedPointer<RocketChatChannel> &pChannel )
diff --git a/rocketchatserver.cpp b/rocketchatserver.cpp
index 1e67a2e..59c4345 100755
--- a/rocketchatserver.cpp
+++ b/rocketchatserver.cpp
@@ -60,11 +60,14 @@ void RocketChatServerData::initConnections()
                     MessageList messageList = messages->values( it.key() );
                     auto channel = mChannels->get( it.key() );
                     QList<QSharedPointer<RocketChatMessage>> newones = channel->addMessages( messageList );
-                    mMessageService->deleteMessagesNotInList(messages, it.key(),true);
-                    if(newones.count())
+                    mMessageService->deleteMessagesNotInList( messages, it.key(), true );
+
+                    if ( newones.count() ) {
                         mMessageService->persistMessages( newones );
+                    }
                 }
             }
+
             mStorage->askForcommit();
             delete messages;
         }
@@ -168,6 +171,8 @@ void RocketChatServerData::initConnections()
     }
 
     emit readyToCheckForPendingNotification();
+    qDebug() << " init ts : " << QDateTime::currentSecsSinceEpoch();
+
     mChannelService->loadJoinedChannelsFromDb();
 }
 
@@ -617,23 +622,23 @@ void RocketChatServerData::wipeDbAndReconnect()
     resume();
 }
 
-void RocketChatServerData::deleteMessage(QString rid, QString id)
+void RocketChatServerData::deleteMessage( QString rid, QString id )
 {
-    QJsonArray params = { QJsonObject({{"_id", id }})};
-    auto request = QSharedPointer<DDPMethodRequest>::create("deleteMessage",params);
+    QJsonArray params = { QJsonObject( {{"_id", id }} )};
+    auto request = QSharedPointer<DDPMethodRequest>::create( "deleteMessage", params );
     DdpCallback success = [ = ]( QJsonObject, MeteorDDP * ) {
         try {
-            auto room = mChannels->get(rid);
-            mStorage->deleteMessage(id);
-            room->deleteMessage(id);
+            auto room = mChannels->get( rid );
+            mStorage->deleteMessage( id );
+            room->deleteMessage( id );
 
-        } catch (std::logic_error error) {
+        } catch ( std::logic_error error ) {
             qDebug() << error.what();
         }
 
     };
-    request->setSuccess(success);
-    sendDdprequest(request);
+    request->setSuccess( success );
+    sendDdprequest( request );
 }
 
 QSharedPointer<RocketChatUser> RocketChatServerData::getOwnUser() const
@@ -800,6 +805,7 @@ void RocketChatServerData::loginWithToken( const QString &pUsername, const QStri
 
         request->setSuccess( success );
         request->setError( error );
+        qDebug() << " login ts : " << QDateTime::currentSecsSinceEpoch();
 #ifdef Q_OS_IOS
         mDdpApi->sendRequest( request );
 #else
@@ -976,6 +982,8 @@ void RocketChatServerData::loginWithMethod( const QString &method, const QString
 
 void RocketChatServerData::onDDPConnected()
 {
+    qDebug() << " ddp connected ts : " << QDateTime::currentSecsSinceEpoch();
+
     auto request = QSharedPointer<PingRequest>::create();
     mDdpApi->sendRequest( request );
 
@@ -1575,6 +1583,8 @@ void RocketChatServerData::getServerSettings()
 
 void RocketChatServerData::onChannelsLoaded( const QVector<QSharedPointer<RocketChatChannel>> &pChannels )
 {
+    qDebug() << " channels loaded ts : " << QDateTime::currentSecsSinceEpoch();
+
     Q_UNUSED( pChannels );
     qDebug() << "on channels loaded ";
     loadHistories();
@@ -1769,6 +1779,18 @@ void RocketChatServerData::getCustomEmojis()
             mCustomEmojisReady = true;
         }
     };
+
+    if ( mCustomEmojisReady ) {
+        onLoggedIn();
+
+        qDebug() << "ddp authenticated";
+        this->initialised = true;
+    } else {
+
+        mEmojiService->loadCustomEmojis( std::move( success ) );
+    }
+
+
     auto notifyRoomsChanged = QSharedPointer<RocketChatSubscribeRoomsChanged>::create( this->mUserId );
     auto notifySubSubscription = QSharedPointer<RocketChatSubscribeUserNotify>::create( this->mUserId );
     auto notifyNoticeSubSubscription = QSharedPointer<RocketChatNotifyNoticesRequest>::create( this->mUserId );
@@ -1786,14 +1808,7 @@ void RocketChatServerData::getCustomEmojis()
     getServerSettings();
     emit registerForPush();
 
-    if ( mCustomEmojisReady ) {
-        onLoggedIn();
-
-        qDebug() << "ddp authenticated";
-        this->initialised = true;
-    }
 
-    mEmojiService->loadCustomEmojis( std::move( success ) );
 }
 
 void RocketChatServerData::openPrivateChannelWith( const QString &pUsername )
diff --git a/services/fileservice.cpp b/services/fileservice.cpp
index 768e8e5..d786884 100755
--- a/services/fileservice.cpp
+++ b/services/fileservice.cpp
@@ -69,6 +69,7 @@ FileService::FileService( RocketChatServerData *server )
  */
 bool FileService::getFileRessource( const QSharedPointer< FileRequest > &pRequest )
 {
+    auto repo = mServer->getFiles();
 
     if ( !pRequest.isNull() && pRequest->url.size() > 0 ) {
         if ( mCurrentDownloads.contains( pRequest->url ) ) {
@@ -106,12 +107,23 @@ bool FileService::getFileRessource( const QSharedPointer< FileRequest > &pReques
                     }
 
                 } else {
-                    QFile oldFile( tempFile->getFilePath() );
+                    if ( !tempFile->getFileChecked() ) {
+                        QFile oldFile( tempFile->getFilePath() );
+
+                        if ( oldFile.exists() ) {
+                            tempFile->setFileChecked( true );
+                        } else {
+                            mStorage->removeFileCacheEntry( pRequest->url, tempFile->getFilePath() );
+                            repo->remove( pRequest->url );
+                            getFileFromServer( pRequest );
+                            return false;
+                        }
 
-                    if ( oldFile.copy( location ) ) {
-                        tempFile = QSharedPointer<TempFile>::create( location, pRequest->url );
-                    } else {
-                        qWarning() << oldFile.errorString();
+                        /*if ( oldFile.copy( location ) ) {
+                            tempFile = QSharedPointer<TempFile>::create( location, pRequest->url );
+                        } else {
+                            qWarning() << oldFile.errorString();
+                        }*/
                     }
 
                 }
-- 
GitLab