Commit 36012136 authored by armin's avatar armin
Browse files

as

parent 390545a3
......@@ -103,7 +103,8 @@ SOURCES += api/meteorddp.cpp \
ddpRequests/rocketchatcreatepublicgrouprequest.cpp \
ddpRequests/rocketchatchangeuserpresencestatus.cpp \
ddpRequests/rocketchatsubscribeuserdata.cpp \
CustomModels/emojismodel.cpp
CustomModels/emojismodel.cpp \
repos/filesrepo.cpp
HEADERS += \
api/meteorddp.h \
......@@ -209,7 +210,8 @@ HEADERS += \
ddpRequests/rocketchatchangeuserpresencestatus.h \
ddpRequests/rocketchatchangeuserpresencedefaultstatus.h \
ddpRequests/rocketchatsubscribeuserdata.h \
CustomModels/emojismodel.h
CustomModels/emojismodel.h \
repos/filesrepo.h
linux{
......
......@@ -388,23 +388,24 @@ void PersistanceLayer::transaction( )
{
commitMutex.lock();
if ( mTransactionCounter == 0 ) {
if ( !mDb.transaction() ) {
qWarning() << mDb.lastError();
}
// if ( mTransactionCounter == 0 ) {
if ( !mDb.transaction() ) {
qWarning() << mDb.lastError();
}
// }
mTransactionCounter++;
commitMutex.unlock();
}
void PersistanceLayer::askForcommit()
{
commitMutex.lock();
// commitMutex.lock();
// mCommitCounter++;
commitMutex.unlock();
// commitMutex.unlock();
commit( );
}
......@@ -789,11 +790,15 @@ void PersistanceLayer::setSetting( const QString &pProperty, const QString &pVal
void PersistanceLayer::addChannel( const QString &pId, const QString &pName, const QString &pType, bool pJoined, bool pReadOnly, const QString &pMuted, bool pArchived, bool pBlocked, const QString &pUsername, const QString &pChatPartnerId )
{
transaction();
// transaction();
QSqlQuery queryAddChannel;
queryAddChannel.prepare( QStringLiteral( "REPLACE INTO rooms "
"(id,name,type,joined,read_only,muted,archived,blocked,username,chat_partner_id) VALUES(:id,:name,:type,:joined,:read_only,:muted,:archived,:blocked,:username,:chat_partner_id)" ) );
queryAddChannel.bindValue( QStringLiteral( ":id" ), pId );
queryAddChannel.bindValue( QStringLiteral( ":name" ), pName );
queryAddChannel.bindValue( QStringLiteral( ":joined" ), pJoined );
queryAddChannel.bindValue( QStringLiteral( ":type" ), pType );
queryAddChannel.bindValue( QStringLiteral( ":joined" ), pJoined );
queryAddChannel.bindValue( QStringLiteral( ":read_only" ), pReadOnly );
queryAddChannel.bindValue( QStringLiteral( ":muted" ), pMuted );
queryAddChannel.bindValue( QStringLiteral( ":archived" ), pArchived );
......@@ -804,9 +809,12 @@ void PersistanceLayer::addChannel( const QString &pId, const QString &pName, con
if ( !queryAddChannel.exec() ) {
qWarning() << queryAddChannel.lastError();
qWarning() << queryAddChannel.lastQuery();
}
askForcommit();
queryAddChannel.finish();
// askForcommit();
}
void PersistanceLayer::addChannel( const QString &pId, const QString &pName, const QString &pType, bool pJoined, const QString &pUsername, const QString &pMchatPartnerId )
......@@ -835,7 +843,11 @@ void PersistanceLayer::deleteChannel( const QString &pId )
void PersistanceLayer::addMessage( const QString &pId, const QString &pRid, const QString &pAuthor, qint64 pTs, const QString &pJson, const QString &pUserId )
{
transaction();
// transaction();
QSqlQuery queryAddMessage;
queryAddMessage.prepare( QStringLiteral( "REPLACE INTO messages "
"(id,rid,author,ts,json,read, user_id) VALUES (:id,:rid,:author,:ts,:json,0,:user_id)" ) );
queryAddMessage.bindValue( QStringLiteral( ":id" ), pId );
queryAddMessage.bindValue( QStringLiteral( ":rid" ), pRid );
queryAddMessage.bindValue( QStringLiteral( ":author" ), pAuthor );
......@@ -850,7 +862,7 @@ void PersistanceLayer::addMessage( const QString &pId, const QString &pRid, cons
qWarning() << queryAddMessage.lastError();
}
askForcommit();
// askForcommit();
}
void PersistanceLayer::addFileCacheEntry( const QString &pUrl, const QString &pPath )
......@@ -883,7 +895,11 @@ void PersistanceLayer::addCustomEmoji( const QString &pTag, const QString &pPath
}
void PersistanceLayer::addCustomEmoji( const QString &pTag, const QString &pPath, const QString &pHtml, const QString &pCategory, const QString &pUnicode, int pOrder )
{
transaction();
// transaction();
QSqlQuery queryAddEmoji;
queryAddEmoji.prepare( QStringLiteral( "REPLACE INTO custom_emojis (id,file,html,unicode,category,sort_order)"
" VALUES (:id,:file,:html,:unicode,:category,:sort_order)" ) );
queryAddEmoji.bindValue( QStringLiteral( ":id" ), pTag );
queryAddEmoji.bindValue( QStringLiteral( ":file" ), pPath );
queryAddEmoji.bindValue( QStringLiteral( ":html" ), pHtml );
......@@ -895,7 +911,7 @@ void PersistanceLayer::addCustomEmoji( const QString &pTag, const QString &pPath
qWarning() << queryAddEmoji.lastError();
}
askForcommit();
// askForcommit();
}
void PersistanceLayer::addUserToBlockList( const QString &pUserId, const QString &pUsername )
......@@ -1262,6 +1278,33 @@ QList< QHash<QString, QString > > PersistanceLayer::getCustomEmojis()
return returnList;
}
QList<QHash<QString, QString>> PersistanceLayer::getFiles()
{
QList<QHash<QString, QString> > returnList;
QSqlQuery getFilesQuery;
getFilesQuery.prepare( "SELECT path,url FROM file_cache " );
if ( !getFilesQuery.exec() ) {
qWarning() << getFilesQuery.lastError();
} else {
QSqlRecord rec = getFilesQuery.record();
int pathCol = rec.indexOf( QStringLiteral( "path" ) );
int urlCol = rec.indexOf( QStringLiteral( "url" ) );
while ( getFilesQuery.next() ) {
QHash<QString, QString> entry;
entry[QStringLiteral( "path" )] = getFilesQuery.value( pathCol ).toString();
entry[QStringLiteral( "url" )] = getFilesQuery.value( urlCol ).toString();
returnList.append( entry );
}
}
return returnList;
}
QString PersistanceLayer::getFileCacheEntry( const QString &pUrl )
{
queryGetFileCacheEntry.bindValue( QStringLiteral( ":url" ), pUrl );
......
......@@ -78,6 +78,7 @@ class PersistanceLayer : public QObject
Q_INVOKABLE QString getNewImagePath( void );
QString getFileCacheEntry( const QString &pUrl );
QList< QHash<QString, QString>> getFiles();
Q_INVOKABLE QString getSetting( const QString &pProperty );
......
#include "filesrepo.h"
FilesRepo::FilesRepo( QObject *parent ): QObject( parent )
{
mElements.reserve( 3000 );
}
#ifndef FILESREPO_H
#define FILESREPO_H
#include <QObject>
#include "abstractbaserepository.h"
#include "repos/entities/tempfile.h"
class FilesRepo : public QObject, public AbstractBaseRepository<TempFile>
{
Q_OBJECT
public:
explicit FilesRepo( QObject *parent = nullptr );
};
#endif // FILESREPO_H
......@@ -896,6 +896,8 @@ void RocketChat::onApplicationStateChanged( const Qt::ApplicationState &pState )
for ( auto server : mServerMap ) {
QMetaObject::invokeMethod( server, "setUserPresenceStatus", Q_ARG( int, 2 ) );
QMetaObject::invokeMethod( server, "disconnectFromServer" );
QMetaObject::invokeMethod( server, "onStateChanged", Q_ARG( Qt::ApplicationState, pState ) );
}
}
......
......@@ -28,11 +28,12 @@ RocketChatServerData::RocketChatServerData( QString pId, QString pBaseUrl, bool
{
qRegisterMetaType<User>( "User" );
qRegisterMetaType<QList<QSharedPointer<Emoji>>>( "QList<QSharedPointer<Emoji>>" );
connect( this, &RocketChatServerData::destroyed, this, &RocketChatServerData::persistData, Qt::UniqueConnection );
mUnsecureConnection = pUnsecure;
mApiUri = QStringLiteral( "/api/v1" );
mEmojiRepo = new EmojiRepo( this );
mFilesRepo = new FilesRepo( this );
}
......@@ -63,7 +64,7 @@ void RocketChatServerData::initConnections()
if ( Q_LIKELY( mChannels->contains( it.key() ) && !mChannels->get( it.key() ).isNull() ) ) {
MessageList messageList = messages->values( it.key() );
mChannels->get( it.key() )->addMessages( messageList );
mMessageService->persistMessages( messageList );
// mMessageService->persistMessages( messageList );
}
}
}
......@@ -110,7 +111,7 @@ void RocketChatServerData::initConnections()
setRestApi( new RestApi( this, protocol + mBaseUrl ) );
mFileService = new FileService( this );
mEmojiService = new EmojiService( this, mFileService, mStorage );
mEmojiService = new EmojiService( this, mFileService );
mDdpApi = new MeteorDDP( this, mBaseUrl, mUnsecureConnection );
......@@ -377,6 +378,10 @@ void RocketChatServerData::onStateChanged( const Qt::ApplicationState &pState )
} else {
setUserPresenceStatus( static_cast<int>( RocketChatUser::status::ONLINE ) );
}
if ( pState == Qt::ApplicationState::ApplicationSuspended || pState == Qt::ApplicationState::ApplicationInactive ) {
persistData();
}
}
void RocketChatServerData::createVideoCall( const QString &pRid )
......@@ -1677,7 +1682,7 @@ void RocketChatServerData::loadRecentHistory( const QString &pChannelId )
auto messageList = messages->values( pChannelId );
qDebug() << "load recent history successfull loaded " << messageList.size();
mChannels->get( pChannelId )->addMessages( messageList );
mMessageService->persistMessages( messageList );
// mMessageService->persistMessages( messageList );
delete messages;
}
......@@ -1707,7 +1712,7 @@ void RocketChatServerData::getCustomEmojis()
for ( auto &emoji : pEmojiList ) {
if ( !emoji.isNull() ) {
if ( !mEmojiRepo->contains( emoji->getIdentifier() ) ) {
mEmojiService->persistEmoji( emoji );
//mEmojiService->persistEmoji( emoji );
mEmojiRepo->add( emoji->getIdentifier(), emoji );
newEmojis.append( std::move( emoji ) );
}
......@@ -1998,7 +2003,20 @@ void RocketChatServerData::addUsersToChannel( const QString &pChannelId, const Q
}
}
void RocketChatServerData::persistData()
{
mMessageService->persist();
mChannelService->persist();
mEmojiService->persist();
}
ChannelRepository *RocketChatServerData::getChannels() const
{
return mChannels;
}
FilesRepo *RocketChatServerData::getFiles() const
{
return mFilesRepo;
}
......@@ -78,6 +78,7 @@
#include "services/requests/loadhistoryservicerequest.h"
#include "services/fileservice.h"
#include "repos/emojirepo.h"
#include "repos/filesrepo.h"
#include "rocketchatserverconfig.h"
#include "repos/channelrepository.h"
#include "services/emojiservice.h"
......@@ -120,6 +121,7 @@ class RocketChatServerData : public MessageListener
void setServerId( const QString &pValue );
void persistData();
public slots:
void init();
......@@ -221,6 +223,7 @@ class RocketChatServerData : public MessageListener
void getCustomEmojis();
RestApi *getRestApi() const;
ChannelRepository *getChannels() const;
FilesRepo *getFiles() const;
RocketChatChannelService *mChannelService = nullptr;
void handleChannelMessage( const QJsonObject &pMessage );
uint diffToLastDDPPing();
......@@ -239,6 +242,7 @@ class RocketChatServerData : public MessageListener
void setUserId( const QString &userId );
EmojiRepo *getEmojiRepo() const;
void setEmojiRepo( EmojiRepo *emojiRepo );
void createAccount( const QString &username, const QString &email, const QString &password );
......@@ -266,6 +270,7 @@ class RocketChatServerData : public MessageListener
LoginMethodsRepository mLoginMethodRepo;
ChannelRepository *mChannels = nullptr;
EmojiRepo *mEmojiRepo = nullptr;
FilesRepo *mFilesRepo = nullptr;
RocketChatServerConfig config;
bool mConnected = false;
bool mLoggedIn = false;
......
......@@ -29,8 +29,8 @@
EmojiService::EmojiService( RocketChatServerData *server, FileService *pFileService, PersistanceLayer *storage )
: server( server ), storage( storage ), mFileService( pFileService )
EmojiService::EmojiService( RocketChatServerData *server, FileService *pFileService )
: server( server ), storage( PersistanceLayer::instance() ), mFileService( pFileService )
{
mParsedCustomEmojisTemp.reserve( 50 );
}
......@@ -78,6 +78,18 @@ void EmojiService::persistEmoji( const QSharedPointer<Emoji> &pEmoji )
storage->addCustomEmoji( pEmoji->getIdentifier(), pEmoji->getFilePath(), pEmoji->getHtml(), QStringLiteral( "custom" ), QStringLiteral( "dontcare" ) );
}
void EmojiService::persist()
{
storage->transaction();
auto emojiRepo = server->getEmojiRepo();
for ( const auto &emoji : emojiRepo->getElements() ) {
persistEmoji( emoji );
}
storage->askForcommit();
}
void EmojiService::handleCustomEmojisReceived( EmojiData *data )
{
if ( data ) {
......
......@@ -49,11 +49,12 @@ class EmojiData
class EmojiService
{
public:
EmojiService( RocketChatServerData *server, FileService *pFileService, PersistanceLayer *storage );
EmojiService( RocketChatServerData *server, FileService *pFileService );
void loadCustomEmojis( std::function<void ( QList<QSharedPointer<Emoji>> )> success );
QList<QSharedPointer<Emoji>> loadEmojisFromDb( void );
void persistEmoji( const QSharedPointer<Emoji> &pEmoji );
protected:
void persist();
private:
RocketChatServerData *server;
void handleCustomEmojisReceived( EmojiData *data );
QSharedPointer<Emoji> parseEmoji( const QHash<QString, QString> &pEmojiData );
......
......@@ -52,6 +52,7 @@ FileService::FileService( RocketChatServerData *server )
}
mServer = server;
getFilesFromDb();
}
}
......@@ -127,27 +128,50 @@ bool FileService::getFileRessource( const QSharedPointer< FileRequest > &pReques
return false;
}
void FileService::getFilesFromDb()
{
auto files = mStorage->getFiles();
auto repo = mServer->getFiles();
for ( const auto &file : files ) {
auto url = file["url"];
auto path = file["path"];
repo->add( url, QSharedPointer<TempFile>::create( path, url ) );
}
}
QSharedPointer<TempFile> FileService::getFileFromCache( const QSharedPointer< FileRequest > &pRequest )
{
if ( !pRequest.isNull() ) {
auto repo = mServer->getFiles();
if ( repo == nullptr ) {
qDebug() << "repo is null";
}
QString baseUrl = mServer->getRestApi()->getBaseUrl();
QString fullUrl = baseUrl + pRequest->url ;
qDebug() << "file from cache requested" << fullUrl;
QString storageEntry = mStorage->getFileCacheEntry( fullUrl );
qDebug() << "cachehit:" << fullUrl << ":" << storageEntry;
qDebug() << "storageEntry len " << storageEntry.size();
if ( !storageEntry.size() ) {
return QSharedPointer<TempFile>( nullptr );
}
if ( repo->contains( fullUrl ) ) {
auto storageEntry = repo->get( fullUrl );
QFile cacheHit( storageEntry );
qDebug() << "cachehit:" << fullUrl << ":" << storageEntry->getFilePath();
if ( storageEntry.isNull() ) {
return QSharedPointer<TempFile>( nullptr );
}
QFile cacheHit( storageEntry->getFilePath() );
if ( cacheHit.exists() ) {
return storageEntry;
}
if ( cacheHit.exists() ) {
return QSharedPointer<TempFile>::create( storageEntry, pRequest->url );
mStorage->removeFileCacheEntry( pRequest->url, storageEntry->getFilePath() );
repo->remove( fullUrl );
}
mStorage->removeFileCacheEntry( pRequest->url, storageEntry );
return QSharedPointer<TempFile>( nullptr );
......@@ -218,6 +242,8 @@ void FileService::getFileFromServer( const QSharedPointer< FileRequest > &pReque
if ( location.size() ) {
if ( pRequest->showInline ) {
mStorage->addFileCacheEntry( url, location );
auto repo = mServer->getFiles();
repo->add( url, tempfile );
}
mCurrentDownloads.remove( fullUrl );
......@@ -239,6 +265,8 @@ void FileService::getFileFromServer( const QSharedPointer< FileRequest > &pReque
if ( pRequest->showInline ) {
mStorage->addFileCacheEntry( url, location );
auto repo = mServer->getFiles();
repo->add( url, tempfile );
}
pRequest->then( tempfile, pRequest->showInline );
......
......@@ -45,6 +45,7 @@ class FileService
public:
explicit FileService( RocketChatServerData *server );
bool getFileRessource( const QSharedPointer< FileRequest > &pRequest );
void getFilesFromDb();
protected:
QMutex mDownloadMutex;
QSharedPointer<TempFile> getFileFromCache( const QSharedPointer< FileRequest > &pRequest );
......
......@@ -106,6 +106,26 @@ void MessageService::persistMessages( const MessageList &pMessage )
mPersistanceLayer->askForcommit();
}
void MessageService::persist()
{
mPersistanceLayer->transaction();
auto channels = mServer->getChannels();
for ( const auto &channel : channels->getElements() ) {
if ( channel.isNull() ) {
auto messages = channel->getMessageRepo();
for ( const auto &message : messages->getElements() ) {
if ( !message.isNull() ) {
persistMessage( message );
}
}
}
}
mPersistanceLayer->askForcommit();
}
QSharedPointer<RocketChatMessage> MessageService::parseMessage( const QJsonObject &pMessageData,
bool linkify )
{
......
......@@ -57,6 +57,7 @@ class MessageService : public QObject
void sendMessage( const QSharedPointer<RocketChatMessage> &pMessage );
void persistMessage( const QSharedPointer<RocketChatMessage> &pMessage );
void persistMessages( const MessageList &pMessage );
void persist();
QSharedPointer<RocketChatMessage> parseMessage( const QJsonObject &pMessage, bool linkify = true );
void addUserToBlockList( const QString &pId );
void addChannelToBlockList( const QString &pId );
......
......@@ -182,7 +182,7 @@ QVector<QSharedPointer<RocketChatChannel> > RocketChatChannelService::processCha
vec.append( channel );
persistChannel( channel );
//persistChannel( channel );
}
}
......@@ -305,6 +305,17 @@ void RocketChatChannelService::persistChannel( const QSharedPointer<RocketChatCh
auto chatPartnerId = pChannel->getChatPartnerId();
mStorage->addChannel( id, name, type, joined, readonly, muted, archived, blocked, username, chatPartnerId );
}
void RocketChatChannelService::persist()
{
mStorage->transaction();
for ( const auto &channel : mChannels->getElements() ) {
persistChannel( channel );
}
mStorage->askForcommit();
}
/**
* Fills the channel with users, which are loaded from the server
*
......
......@@ -71,6 +71,7 @@ class RocketChatChannelService : public QObject
void loadJoinedChannelsFromServer( void );
void loadJoinedChannelsFromDb( void );
void persistChannel( const QSharedPointer<RocketChatChannel> &pChannel );
void persist();
void loadUsersOfChannel( const QSharedPointer<RocketChatChannel> &pChannel );
void subscribeChannel( const QSharedPointer<RocketChatChannel> &pChannel );
void subscribeChannel( const QString &pChannel );
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment