diff --git a/CustomModels/channelmodel.cpp b/CustomModels/channelmodel.cpp index e85ec706184c374d2d3177b1fc33b854d12177ac..7e80bbb71b3b5378ac5b8466f7f51f08cfb91b5c 100644 --- a/CustomModels/channelmodel.cpp +++ b/CustomModels/channelmodel.cpp @@ -75,14 +75,20 @@ QVariant ChannelModel::data( const QModelIndex &index, int role ) const } if ( role == readonly ) { - return currentChannel->getReadOnly() || currentChannel->getArchived(); + return currentChannel->getReadOnly() || currentChannel->getArchived() || currentChannel->getBlocked(); } if ( role == archived ) { return currentChannel->getArchived(); } - return QVariant(); + if ( role == blocked ) { + return currentChannel->getBlocked(); + } + + { + return QVariant(); + } } bool ChannelModel::addChannel( const QSharedPointer<RocketChatChannel> &channel ) @@ -90,7 +96,7 @@ bool ChannelModel::addChannel( const QSharedPointer<RocketChatChannel> &channel if ( !channel.isNull() && channel->getRoomId() != "" && !duplicateCheck.contains( channel->getRoomId() ) ) { connect( channel.data(), &RocketChatChannel::messageAdded, this, &ChannelModel::onNewerMessage, Qt::UniqueConnection ); connect( channel.data(), &RocketChatChannel::unreadMessagesChanged, this, &ChannelModel::onUnreadMessageChanged, Qt::UniqueConnection ); - + connect( channel.data(), &RocketChatChannel::dataChanged, this, &ChannelModel::onDataChanged, Qt::UniqueConnection ); duplicateCheck.insert( channel->getRoomId() ); int pos = channelList.findPosition( channel ); emit beginInsertRows( QModelIndex(), pos, pos ); @@ -113,6 +119,7 @@ QHash<int, QByteArray> ChannelModel::roleNames() const roles[lastMessageText] = "lastMessageText"; roles[readonly] = "readonly"; roles[archived] = "archived"; + roles[blocked] = "blocked"; return roles; } @@ -161,6 +168,32 @@ void ChannelModel::onUnreadMessageChanged( const QString &id, int number ) } } +void ChannelModel::onDataChanged( const QString &id, const QString &property ) +{ + int pos = 0; + bool found = false; + + for ( const auto ¤t : channelList ) { + if ( current->getRoomId() == id ) { + found = true; + break; + } + + pos++; + } + + if ( found ) { + auto roles = roleNames(); + auto roleKeys = roles.keys(); + + for ( auto roleKey : roleKeys ) { + if ( roles[roleKey] == property ) { + emit dataChanged( index( pos ), index( pos ), {roleKey} ); + } + } + } +} + void ChannelModel::addChannelSlot( const QSharedPointer<RocketChatChannel> &channel ) { addChannel( channel ); diff --git a/CustomModels/channelmodel.h b/CustomModels/channelmodel.h index d60af5667d0eff000e280bbf9da6ed65653ca518..ba4a270f27632a515fbd72f1983e711804eac825 100644 --- a/CustomModels/channelmodel.h +++ b/CustomModels/channelmodel.h @@ -45,7 +45,8 @@ class ChannelModel : public QAbstractListModel lastMessageText, channelType, readonly, - archived + archived, + blocked }; public: ChannelModel(); @@ -63,6 +64,7 @@ class ChannelModel : public QAbstractListModel QString type; void onNewerMessage( const QString &id, qint64 timestamp ); void onUnreadMessageChanged( const QString &id, int number ); + void onDataChanged( const QString &id, const QString &property ); QTimer timer; signals: void countChanged( void ); diff --git a/api/meteorddp.cpp b/api/meteorddp.cpp index d91a639bac90a1250dc6039e74c351400ab16e5a..26c6d91665716c5b3da11e9543d9196c1ca11979 100755 --- a/api/meteorddp.cpp +++ b/api/meteorddp.cpp @@ -76,7 +76,7 @@ void MeteorDDP::connectWithServer() void MeteorDDP::onTextMessageReceived( const QString &pMsg ) { #if defined(Q_OS_LINUX)|| defined(Q_OS_IOS) - qDebug() << pMsg; + qWarning() << pMsg << "\n"; #endif if ( Q_LIKELY( pMsg.length() ) ) { @@ -84,7 +84,6 @@ void MeteorDDP::onTextMessageReceived( const QString &pMsg ) QJsonObject objectResponse = jsonResponse.object(); if ( Q_LIKELY( mError->error == QJsonParseError::NoError && objectResponse.contains( "msg" ) ) ) { - QDateTime now = QDateTime::currentDateTime(); mLastPing = now.toTime_t(); QString messagePart = objectResponse["msg"].toString(); @@ -168,7 +167,7 @@ void MeteorDDP::sendJson( const QJsonObject &pMsgObj ) QJsonDocument msgJson = QJsonDocument( pMsgObj ); QString msgString = msgJson.toJson( QJsonDocument::Compact ); - qDebug() << "message String" << msgString; + qWarning() << "message String" << msgString; mWsClient->sendTextMessage( msgString ); } diff --git a/config.h b/config.h index 2cbfcc90d6efb4d8c0829931b10287232d93ba90..e9187bd7b4a22dd3b95af01cdedab040a26b4b59 100755 --- a/config.h +++ b/config.h @@ -1,7 +1,7 @@ #ifndef CONFIG_H #define CONFIG_H -#define DBVERSION 1 +#define DBVERSION 2 #endif // CONFIG_H diff --git a/ddpRequests/rocketchatblockuserrequest.cpp b/ddpRequests/rocketchatblockuserrequest.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9c0684cd4b42fe06482a4c2773a3bd0ea7fa07a0 --- /dev/null +++ b/ddpRequests/rocketchatblockuserrequest.cpp @@ -0,0 +1,11 @@ +#include "rocketchatblockuserrequest.h" + +RocketChatBlockUserRequest::RocketChatBlockUserRequest( const QString &pChannelId, const QString &pUserId ) +{ + QJsonArray params; + QJsonObject userData; + userData["rid"] = pChannelId; + userData["blocked"] = pUserId; + params.append( userData ); + buildRequest( "blockUser", params ); +} diff --git a/ddpRequests/rocketchatblockuserrequest.h b/ddpRequests/rocketchatblockuserrequest.h new file mode 100644 index 0000000000000000000000000000000000000000..efa5bb22586e047e5530ff64b649dba2392d4e3e --- /dev/null +++ b/ddpRequests/rocketchatblockuserrequest.h @@ -0,0 +1,12 @@ +#ifndef ROCKETCHATBLOCKUSERREQUEST_H +#define ROCKETCHATBLOCKUSERREQUEST_H + +#include "ddpmethodrequest.h" + +class RocketChatBlockUserRequest: public DDPMethodRequest +{ + public: + RocketChatBlockUserRequest( const QString &pChannelId, const QString &pUserId ); +}; + +#endif // ROCKETCHATBLOCKUSERREQUEST_H diff --git a/ddpRequests/rocketchatunblockuserrequest.cpp b/ddpRequests/rocketchatunblockuserrequest.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eb8ec2b320b4fc8d47b968a81eb13e6b969c51f0 --- /dev/null +++ b/ddpRequests/rocketchatunblockuserrequest.cpp @@ -0,0 +1,11 @@ +#include "rocketchatunblockuserrequest.h" + +RocketChatUnblockUserRequest::RocketChatUnblockUserRequest( const QString &pChannelId, const QString &pUserId ) +{ + QJsonArray params; + QJsonObject userData; + userData["rid"] = pChannelId; + userData["blocked"] = pUserId; + params.append( userData ); + buildRequest( "unblockUser", params ); +} diff --git a/ddpRequests/rocketchatunblockuserrequest.h b/ddpRequests/rocketchatunblockuserrequest.h new file mode 100644 index 0000000000000000000000000000000000000000..4e05ab8179d91d8148225f72ed703468ba376ac1 --- /dev/null +++ b/ddpRequests/rocketchatunblockuserrequest.h @@ -0,0 +1,12 @@ +#ifndef ROCKETCHATUNBLOCKUSERREQUEST_H +#define ROCKETCHATUNBLOCKUSERREQUEST_H + +#include "ddpmethodrequest.h" + +class RocketChatUnblockUserRequest : public DDPMethodRequest +{ + public: + RocketChatUnblockUserRequest( const QString &pChannelId, const QString &pUserId ); +}; + +#endif // ROCKETCHATUNBLOCKUSERREQUEST_H diff --git a/engine.pro b/engine.pro index d77ceb550240b66c9cc859275ef742db4e72a740..bdbee04702809ee7c79f531b6609d18cd066515a 100644 --- a/engine.pro +++ b/engine.pro @@ -85,7 +85,9 @@ SOURCES += api/meteorddp.cpp \ CustomModels/loginmethodsmodel.cpp \ repos/loginmethodsrepository.cpp \ repos/entities/loginmethod.cpp \ - restRequests/getserverinforequest.cpp + restRequests/getserverinforequest.cpp \ + ddpRequests/rocketchatblockuserrequest.cpp \ + ddpRequests/rocketchatunblockuserrequest.cpp HEADERS += \ api/meteorddp.h \ @@ -171,7 +173,9 @@ HEADERS += \ CustomModels/loginmethodsmodel.h \ repos/loginmethodsrepository.h \ repos/entities/loginmethod.h \ - restRequests/getserverinforequest.h + restRequests/getserverinforequest.h \ + ddpRequests/rocketchatblockuserrequest.h \ + ddpRequests/rocketchatunblockuserrequest.h linux{ diff --git a/persistancelayer.cpp b/persistancelayer.cpp index a9f4e5ab652f74c81e9db37e92bcb8d569d2d103..51688d6b6da07a4f6e6b0c7328792f0bd194a976 100755 --- a/persistancelayer.cpp +++ b/persistancelayer.cpp @@ -96,7 +96,8 @@ void PersistanceLayer::initShema() "read_only bool," "muted text," "archived bool," - "joined bool)" ); + "joined bool, " + "blocked bool DEFAULT 0)" ); if ( !query.exec() ) { qWarning() << query.lastError(); @@ -264,8 +265,10 @@ void PersistanceLayer::upgradeSchema() if ( dbVersion < DBVERSION ) { QFile sqlFile; + int currentTarget = 0; if ( dbVersion < 1 ) { + currentTarget = 1; sqlFile.setFileName( ":/sql/migrations/1.sql" ); query.prepare( "DELETE FROM custom_emojis" ); @@ -273,6 +276,9 @@ void PersistanceLayer::upgradeSchema() qWarning() << "truncation of, custom_emojis failed"; qWarning() << query.lastError(); } + } else if ( dbVersion < 2 ) { + currentTarget = 2; + sqlFile.setFileName( ":/sql/migrations/2.sql" ); } if ( !sqlFile.open( QIODevice::ReadOnly | QIODevice::Text ) ) { @@ -289,8 +295,9 @@ void PersistanceLayer::upgradeSchema() query.prepare( line ); if ( !query.exec() ) { - qWarning() << "migration to DB ver 1 failed"; + qWarning() << "migration to DB ver " << currentTarget << " failed"; qWarning() << query.lastError(); + exit( EXIT_FAILURE ); } } @@ -731,12 +738,12 @@ void PersistanceLayer::setSetting( QString pProperty, QString pValue ) } -void PersistanceLayer::addChannel( QString pId, QString pName, QString pType, bool pJoined, bool pReadOnly, QString pMuted, bool pArchived ) +void PersistanceLayer::addChannel( QString pId, QString pName, QString pType, bool pJoined, bool pReadOnly, QString pMuted, bool pArchived, bool pBlocked ) { transaction(); QSqlQuery setChannel; setChannel.prepare( "REPLACE INTO rooms " - "(id,name,type,joined,read_only,muted,archived) VALUES(:id,:name,:type,:joined,:read_only,:muted,:archived)" ); + "(id,name,type,joined,read_only,muted,archived,blocked) VALUES(:id,:name,:type,:joined,:read_only,:muted,:archived,:blocked)" ); setChannel.bindValue( ":id", pId ); setChannel.bindValue( ":name", pName ); setChannel.bindValue( ":joined", pJoined ); @@ -744,6 +751,7 @@ void PersistanceLayer::addChannel( QString pId, QString pName, QString pType, bo setChannel.bindValue( ":read_only", pReadOnly ); setChannel.bindValue( ":muted", pMuted ); setChannel.bindValue( ":archived", pArchived ); + setChannel.bindValue( ":blocked", pBlocked ); if ( !setChannel.exec() ) { qWarning() << setChannel.lastError(); @@ -757,7 +765,8 @@ void PersistanceLayer::addChannel( QString pId, QString pName, QString pType, bo bool readOnly = false; QString muted = ""; bool archived = false; - addChannel( pId, pName, pType, pJoined, readOnly, muted, archived ); + bool blocked = false; + addChannel( pId, pName, pType, pJoined, readOnly, muted, archived, blocked ); } void PersistanceLayer::addMessage( QString pId, QString pRid, QString pAuthor, qint64 pTs, QString pJson ) @@ -1114,6 +1123,7 @@ QList<QVariantHash> PersistanceLayer::getChannels( void ) int readOnlyCol = rec.indexOf( "read_only" ); int mutedCol = rec.indexOf( "muted" ); int archivedCol = rec.indexOf( "archived" ); + int blocked = rec.indexOf( "blocked" ); while ( rooms.next() ) { QVariantHash roomsHash; @@ -1126,6 +1136,7 @@ QList<QVariantHash> PersistanceLayer::getChannels( void ) roomsHash["muted"] = muted; roomsHash["list"] = muted.split( "," ); roomsHash["archived"] = rooms.value( archivedCol ).toBool(); + roomsHash["blocked"] = rooms.value( blocked ).toBool(); roomsList.append( roomsHash ); } } diff --git a/persistancelayer.h b/persistancelayer.h index 5879fe76b59967ba5908b97aa1e71b25fa457d3d..2d9cca8f757fcc7ec7372ad637110a2af09dff4e 100755 --- a/persistancelayer.h +++ b/persistancelayer.h @@ -32,21 +32,21 @@ class PersistanceLayer : public QObject static PersistanceLayer *persistanceLayer; static PersistanceLayer *instance(); -public slots: + public slots: void setUserData( QString pUser, QString pPass ); void setCurrentChannel( QString pChannelId, QString pName ); void setSetting( QString pProperty, QString pValue ); void setUserName( QString ); void setPassword( QString ); void setToken( QString, uint ); - void addChannel( QString pId, QString pName, QString pType, bool pJoined , bool pReadOnly, QString pMuted, bool pArchived ); + void addChannel( QString pId, QString pName, QString pType, bool pJoined, bool pReadOnly, QString pMuted, bool pArchived, bool pBlocked ); void addChannel( QString pId, QString pName, QString pType, bool pJoined ); void addMessage( QString pId, QString pRid, QString pAuthor, qint64 pTs, QString pJson ); void addFileCacheEntry( QString pUrl, QString pPath ); void addCustomEmoji( QString pTag, QString pPath, QString pHtml, QString pCategory ); void addCustomEmoji( QString pTag, QString pPath, QString pHtml, QString pCategory, QString pUnicode ); void addCustomEmoji( QString pTag, QString pPath, QString pHtml, QString pCategory, QString pUnicode, int pOrder ); -public: + public: QString addFileToTemp( QString pUrl, QString pPath ); QString addFileToDocuments( QString pUrl, QString pPath ); @@ -76,7 +76,7 @@ public: Q_INVOKABLE QString getSetting( QString pProperty ); void removeFileCacheEntry( QString pUrl ); - void removeFileCacheEntry( QString pUrl , QString pPath ); + void removeFileCacheEntry( QString pUrl, QString pPath ); void transaction( void ); void commit( void ); diff --git a/repos/channelrepository.cpp.orig b/repos/channelrepository.cpp.orig deleted file mode 100755 index f5df3a9f52945f9dda33c4f40a4e3364d1655558..0000000000000000000000000000000000000000 --- a/repos/channelrepository.cpp.orig +++ /dev/null @@ -1,105 +0,0 @@ -#include "channelrepository.h" -ChannelRepository::ChannelRepository(ChannelModel &channelsModel, ChannelModel &groupsModel, - ChannelModel &directModel, MessagesModel &messagesModel): - mChannelsModel(channelsModel), mGroupsModel(groupsModel),mDirectModel(directModel),mMessagesModel(messagesModel) -{ - mElements.reserve(50); -} - -bool ChannelRepository::add( QString id, QSharedPointer<RocketChatChannel> msg ) -{ - connect(msg.data(),&RocketChatChannel::messageAdded,this,&ChannelRepository::receiveTimestamp, Qt::UniqueConnection); - QString type = msg->getType(); - if(type == "c"){ - QMetaObject::invokeMethod(&mChannelsModel,"addChannelSlot", Q_ARG(QSharedPointer<RocketChatChannel>,msg)); - }else if (type == "d"){ - QMetaObject::invokeMethod(&mDirectModel,"addChannelSlot", Q_ARG(QSharedPointer<RocketChatChannel>,msg)); - }else if(type == "p"){ - QMetaObject::invokeMethod(&mGroupsModel,"addChannelSlot", Q_ARG(QSharedPointer<RocketChatChannel>,msg)); - } -<<<<<<< HEAD - QMetaObject::invokeMethod(&mMessagesModel,"addChannelSlot", Q_ARG(QSharedPointer<RocketChatChannel>,msg)); -======= - mNameIndex[msg->getName()] = msg; ->>>>>>> master - return AbstractBaseRepository::add( id, msg ); -} - -bool ChannelRepository::add(QSharedPointer<RocketChatChannel> pChannel) -{ - QString id = pChannel->getRoomId(); - mNameIndex[pChannel->getName()] = pChannel; - return add(id,pChannel); -} - -ChannelRepository::~ChannelRepository() -{ - -} - -void ChannelRepository::receiveTimestamp(QString pId, qint64 pTimestamp) -{ - QSharedPointer<RocketChatChannel> channel = get(pId); - - if(mChannelTimeIndex.contains(channel)){ - mTimestampIndex.remove(mChannelTimeIndex[channel]); - mChannelTimeIndex[channel] = pTimestamp; - }else{ - mChannelTimeIndex.insert(channel,pTimestamp); - } - mTimestampIndex.insert(pTimestamp, channel); - //TODO: depricated? - emit orderChanged(pId); -} - -QSharedPointer<RocketChatChannel> ChannelRepository::getChannelByName(QString pName) -{ - if(mNameIndex.contains(pName)){ - return mNameIndex[pName]; - }else{ - return nullptr; - } -} - -unsigned long ChannelRepository::getYoungestMessageDate() -{ - long oldest = LONG_MAX; - - - for ( auto channel : mElements ) { - auto longest = channel->getOldestMessage(); - - if ( Q_LIKELY(!longest.isNull()) ) { - long temp = longest->getTimestamp(); - - if ( temp < oldest ) { - oldest = temp; - } - } - } - - if ( oldest == LONG_MAX ) { - oldest = 0; - } - - - return oldest; -} - -unsigned long ChannelRepository::getOldestMessageDate() -{ - long youngest = 0; - - for ( auto channel : mElements ) { - if(!channel.isNull()){ - long temp = channel->getYoungestMessage()->getTimestamp(); - - if ( temp > youngest ) { - youngest = temp; - } - } - } - - return youngest; -} - diff --git a/repos/channelrepository.h.orig b/repos/channelrepository.h.orig deleted file mode 100755 index 0a37f6992d6064c0fcc1adfb0d8ce469f7c43228..0000000000000000000000000000000000000000 --- a/repos/channelrepository.h.orig +++ /dev/null @@ -1,47 +0,0 @@ -#ifndef CHANNELREPOSITORY_H -#define CHANNELREPOSITORY_H - -#include <QObject> -#include "utils.h" -#include "abstractbaserepository.h" -#include "repos/entities/rocketchatchannel.h" -#include "CustomModels/channelmodel.h" -class RocketChatChannel; -class ChannelModel; -class ChannelRepository : public QObject, public AbstractBaseRepository<RocketChatChannel> -{ - Q_OBJECT -public: - - explicit ChannelRepository(ChannelModel& channelsModel, ChannelModel& groupsModel, ChannelModel& directModel); - unsigned long getYoungestMessageDate(); - unsigned long getOldestMessageDate(); - QVariantMap getSortOder(void); - QHash<QString, QVariantMap> getSortOderPerCategory(void); - bool add(QString id, QSharedPointer<RocketChatChannel>); - bool add(QSharedPointer<RocketChatChannel> pChannel); - void receiveTimestamp(QString pId, qint64 pTimestamp); -<<<<<<< HEAD -======= - - QSharedPointer<RocketChatChannel> getChannelByName(QString pName); - ->>>>>>> master - ~ChannelRepository(); -private: - QHash<QSharedPointer<RocketChatChannel> ,qint64 > mChannelTimeIndex; - QMap<qint64, QSharedPointer<RocketChatChannel> > mTimestampIndex; -<<<<<<< HEAD -======= - ChannelModel &mChannelsModel, &mGroupsModel, &mDirectModel; - QHash<QString, QSharedPointer<RocketChatChannel> > mNameIndex; - ->>>>>>> master -signals: - void newMessage(QString channelid, int row, QString pMessageId); - void orderChanged(QString channelId); - void beginInsertList(QString channelId); - void endInsertList(QString channelId); -}; - -#endif // CHANNELREPOSITORY_H diff --git a/repos/entities/rocketchatchannel.cpp b/repos/entities/rocketchatchannel.cpp index fbeb6d7a6cbbcbe7b0f3faafe46fcb1b9cc7ebf1..780912fed1eab89a0cac78023b9bf86f3748cf78 100755 --- a/repos/entities/rocketchatchannel.cpp +++ b/repos/entities/rocketchatchannel.cpp @@ -244,3 +244,14 @@ int RocketChatChannel::operator >( RocketChatChannel &channel ) int timestamp2 = youngestMessage2.isNull() ? 0 : youngestMessage2->getTimestamp() ; return timestamp1 > timestamp2; } + +bool RocketChatChannel::getBlocked() const +{ + return mBlocked; +} + +void RocketChatChannel::setBlocked( bool blocked ) +{ + mBlocked = blocked; + emit dataChanged( mRoomId, "blocked" ); +} diff --git a/repos/entities/rocketchatchannel.h b/repos/entities/rocketchatchannel.h index 580c565508fbbd6baa09d860359df86061d4ab8c..d7589a94b03cd7438eef3d64372b61b19b5c38c4 100755 --- a/repos/entities/rocketchatchannel.h +++ b/repos/entities/rocketchatchannel.h @@ -103,10 +103,14 @@ class RocketChatChannel : public QObject void setParentRepo( ChannelRepository *pParentRepo ); int operator >( RocketChatChannel &channel ); + bool getBlocked() const; + void setBlocked( bool blocked ); + protected: bool mReadOnly = false; bool mArchived = false; + bool mBlocked = false; QStringList mMuted; QString mRoomId; QString mName; @@ -120,6 +124,7 @@ class RocketChatChannel : public QObject void messageListReceived( QString pChannelId, QList<ChatMessage> pMessages ); void messageAdded( QString id, qint64 timestamp ); void unreadMessagesChanged( QString id, int number ); + void dataChanged( QString id, QString property ); }; #endif // ROCKETCHATCHANNEL_H diff --git a/repos/messagerepository.cpp.orig b/repos/messagerepository.cpp.orig deleted file mode 100755 index f87235d5111d1cab75ab0791016f4163ff32aaf6..0000000000000000000000000000000000000000 --- a/repos/messagerepository.cpp.orig +++ /dev/null @@ -1,110 +0,0 @@ -#include "messagerepository.h" - -MessageRepository::MessageRepository() -{ - mElements.reserve(100); -} - -ChatMessage MessageRepository::youngest() -{ - - ChatMessage obj; - if(Q_LIKELY(mTimestampIndex.size())){ - obj = mTimestampIndex.last(); - } - - return obj; -} - -ChatMessage MessageRepository::oldest() -{ - ChatMessage obj; - if(Q_LIKELY(mTimestampIndex.size())){ - obj = mTimestampIndex.first(); - } - - return obj; -} - -QVector<QSharedPointer<RocketChatMessage> > MessageRepository::messagesAsObjects() -{ - QVector<QSharedPointer<RocketChatMessage>> list; - - for ( auto message : mTimestampIndex.values() ) { - if(!message.isNull()){ - QSharedPointer<RocketChatMessage> sortable( new RocketChatMessage( *message.data() ) ); - list.append( sortable ); - } - } - - return list; -} - -QMap<qint64, QSharedPointer<RocketChatMessage> > MessageRepository::timestampIndex() const -{ - return mTimestampIndex; -} - -void MessageRepository::setTimestampIndex( const QMap<qint64, QSharedPointer<RocketChatMessage> > ×tampIndex ) -{ - mTimestampIndex = timestampIndex; -} - -QList<QSharedPointer<RocketChatMessage> >& MessageRepository::order() -{ - return mOrder; -} - -int MessageRepository::count() -{ - return mOrder.count(); -} - -bool MessageRepository::add( QString pId, QSharedPointer<RocketChatMessage> pMsg ) -{ -<<<<<<< HEAD - mTimestampIndex[pMsg->getTimestamp()] = pMsg; - if(!mElements.contains(pId)){ - - } - bool newFlag = AbstractBaseRepository::add( pId, pMsg ); - - if ( newFlag ) { - int row = -1; - - if ( mOrder.isEmpty() ) { - emit beginInsertMessage(0); - mOrder.append( pMsg ); - row = 0; - } else { - for ( int i = 0; i < mOrder.count(); i++ ) { - if ( mOrder[i]->getTimestamp() > pMsg->getTimestamp() ) { - emit beginInsertMessage(i); - mOrder.insert( i, pMsg ); - row = i; - break; - } - } - if(row == -1){ - row = mOrder.count(); - emit beginInsertMessage(row); - mOrder.append( pMsg ); - - } - - } - - emit messageAdded( row, pMsg->getId() ); - - } - - return newFlag; -======= - if(!pMsg.isNull()){ - mTimestampIndex[pMsg->getTimestamp()] = pMsg; - return AbstractBaseRepository::add(pId,pMsg); - }else{ - return false; - } ->>>>>>> master -} diff --git a/rocketchat.cpp b/rocketchat.cpp index 2abb1cea2b23bde800dcd4b58d96852e5be57054..89712ffdada4da94b8c35e87e6a346d671299da2 100755 --- a/rocketchat.cpp +++ b/rocketchat.cpp @@ -214,6 +214,16 @@ void RocketChat::copyToClipboard( const QString &text ) QGuiApplication::clipboard()->setText( text ); } +void RocketChat::blockUser( const QString &pChannelId ) +{ + QMetaObject::invokeMethod( mServerMap.first(), "blockUser", Q_ARG( QString, pChannelId ) ); +} + +void RocketChat::unBlockUser( const QString &pChannelId ) +{ + QMetaObject::invokeMethod( mServerMap.first(), "unBlockUser", Q_ARG( QString, pChannelId ) ); +} + bool RocketChat::newServerByDomain( const QString &domain ) { newServerMutex.lock(); diff --git a/rocketchat.h b/rocketchat.h index 0d4b7777cd0ffc7b04cd52a3ad6dd675b3b9572b..c4c056b34bf9623ff01936aa82f3652157c17a87 100755 --- a/rocketchat.h +++ b/rocketchat.h @@ -121,6 +121,11 @@ class RocketChat : public QObject Q_INVOKABLE void copyToClipboard( const QString &text ); + Q_INVOKABLE void blockUser( const QString &pChannelId ); + + Q_INVOKABLE void unBlockUser( const QString &pChannelId ); + + void registerServer( RocketChatServerData *pServer ); void addLoginMethod( const QMap<QString, QVariant> &pMethod ); diff --git a/rocketchatserver.cpp b/rocketchatserver.cpp index fb3bb72d828eae95cae57c5f1bf5f2c396686512..dbff073e0a34c4b9a87a43b4bdcf4ccdcdd32800 100755 --- a/rocketchatserver.cpp +++ b/rocketchatserver.cpp @@ -198,7 +198,7 @@ void RocketChatServerData::switchChannel( const QString &pServer, const QString switchPossible = true; } } - } else { + } else if ( mChannels != nullptr ) { auto channel = mChannels->get( pRid ); readonly = channel->getReadOnly(); switchPossible = true; @@ -249,6 +249,7 @@ void RocketChatServerData::requestGetChannelDetails( const QString &pChannelId ) type = channel->getType(); + QVariantList mutedList; for ( const auto &entry : muted ) { @@ -263,6 +264,7 @@ void RocketChatServerData::requestGetChannelDetails( const QString &pChannelId ) details["selfMuted"] = selfMuted; details["type"] = type; details["name"] = channel->getName(); + details["blocked"] = channel->getBlocked(); emit channelDetailsReady( details, pChannelId ); } @@ -356,6 +358,22 @@ void RocketChatServerData::searchForRoomIdByName( const QString &pName ) mDdpApi->sendRequest( request ); } +void RocketChatServerData::blockUser( const QString &pChannelId ) +{ + auto userId = pChannelId; + userId.replace( mUserId, "" ); + QSharedPointer<RocketChatBlockUserRequest> blockRequest( new RocketChatBlockUserRequest( pChannelId, userId ) ); + mDdpApi->sendRequest( blockRequest ); +} + +void RocketChatServerData::unBlockUser( const QString &pChannelId ) +{ + auto userId = pChannelId; + userId.replace( mUserId, "" ); + QSharedPointer<RocketChatUnblockUserRequest> unBlockRequest( new RocketChatUnblockUserRequest( pChannelId, userId ) ); + mDdpApi->sendRequest( unBlockRequest ); +} + QString RocketChatServerData::getUserId() const { return mUserId; @@ -847,12 +865,11 @@ void RocketChatServerData::handleChannelMessage( const QJsonObject &pMessage ) QSharedPointer<RocketChatChannel> channel = channelService->createChannelObject( rid, name, newChannel["t"].toString() ); if ( !channel.isNull() ) { - QJsonDocument doc( channel->toJsonObject() ); channel->setJoined( true ); //TODO: check if ( mChannels->add( rid, channel ) ) { - emit channelFound( "default", doc.toJson() ); + } } } @@ -861,9 +878,19 @@ void RocketChatServerData::handleChannelMessage( const QJsonObject &pMessage ) QJsonObject data = args[1].toObject(); int unread = data["unread"].toInt(); QString rid = data["rid"].toString(); + bool blocked = false; + QString name = data["name"].toString(); + + if ( data.contains( "blocker" ) ) { + blocked = data["blocker"].toBool(); + } if ( mChannels->contains( rid ) && !mChannels->get( rid ).isNull() ) { - mChannels->get( rid )->setUnreadMessages( unread ); + auto channel = mChannels->get( rid ); + channel->setName( name ); + channel->setBlocked( blocked ); + channel->setUnreadMessages( unread ); + } else { qWarning() << "channel not in channels repo " << rid; } @@ -1312,7 +1339,6 @@ void RocketChatServerData::onChannelsLoaded( const QVector<QSharedPointer<Rocket { qDebug() << "on channels loaded "; - postProcessChannelData( pChannels ); loadHistories(); } @@ -1724,71 +1750,3 @@ ChannelRepository *RocketChatServerData::getChannels() const { return mChannels; } - -void RocketChatServerData::loadChannels() -{ - for ( const auto ¤tChannel : mChannels->getElements() ) { - if ( !currentChannel.isNull() ) { - QJsonDocument doc( currentChannel->toJsonObject() ); - emit channelFound( "default", doc.toJson() ); - } - } -} - -void RocketChatServerData::postProcessChannelData( QVector<QSharedPointer<RocketChatChannel>> pVec ) -{ - static int counter = 0; - counter++; - mStorage->transaction(); - - for ( auto currentChannel : pVec ) { - if ( !currentChannel.isNull() ) { - QString id = currentChannel->getRoomId(); - channelService->persistChannel( currentChannel ); - QList<QSharedPointer<RocketChatUser>> users = currentChannel->getUsers().values(); - - if ( currentChannel->getType() == "d" && users.length() > 1 ) { - - if ( !users[0].isNull() && !users[1].isNull() ) { - if ( users[0]->getUserName() == mUsername ) { - currentChannel->setName( users[1]->getUserName() ); - } else { - currentChannel->setName( users[0]->getUserName() ); - } - } - } - - //this is strange I know - //however we now have to use two ddp messages for all of the channel data - //this ensures the gui is only updated after both methods are called - if ( counter % 2 == 0 ) { - emit channelFound( mServerId, "doc.toJson()" ); - } - - /* if ( channels->add( id, currentChannel ) ) { - - QJsonDocument doc( currentChannel->toJsonObject() ); - emit channelFound( mServerId, doc.toJson() ); - } else {*/ - if ( mChannels->contains( id ) ) { - auto channel = mChannels->get( id ); - - if ( !channel.isNull() ) { - channel->setArchived( currentChannel->getArchived() ); - channel->setMuted( currentChannel->getMuted() ); - channel->setReadOnly( currentChannel->getReadOnly() ); - channel->setUsers( users ); - channel->setUnreadMessages( currentChannel->getUnreadMessages() ); - channelService->persistChannel( channel ); - } - } else { - qWarning() << "channel not in channels repo: " << id; - } - } - } - - //} - - - mStorage->commit(); -} diff --git a/rocketchatserver.h b/rocketchatserver.h index 2e80b7cc325ed71ab7bdac4bbe6b7d098d61bc9b..c2bec5c4983c5591887d5471688ba67a217707c9 100755 --- a/rocketchatserver.h +++ b/rocketchatserver.h @@ -63,6 +63,8 @@ #include "ddpRequests/ddpopenidloginrequest.h" #include "ddpRequests/rocketchatspotlightrequest.h" #include "ddpRequests/ddpsamlloginrequest.h" +#include "ddpRequests/rocketchatblockuserrequest.h" +#include "ddpRequests/rocketchatunblockuserrequest.h" #include "restRequests/restrequest.h" #include "restRequests/restlogoutrequest.h" #include "restRequests/getserverinforequest.h" @@ -154,8 +156,6 @@ class RocketChatServerData : public QObject void getServerInfo(); - void loadChannels(); - void logout( void ); void setConnectionState( const ConnectionState &pValue ); @@ -196,10 +196,12 @@ class RocketChatServerData : public QObject void searchForRoomIdByName( const QString &pName ); + void blockUser( const QString &pChannelId ); - public: + void unBlockUser( const QString &pChannelId ); + public: QString getServerId() const; bool isConnected() const; bool isWebsocketValid( void ) const; @@ -249,7 +251,6 @@ class RocketChatServerData : public QObject uint mTokenExpire = 0; QString mResumeToken; - MeteorDDP *mDdpApi = nullptr; RestApi *mRestApi = nullptr; UserRepository mUsers; @@ -257,6 +258,7 @@ class RocketChatServerData : public QObject ChannelRepository *mChannels = nullptr; EmojiRepo *mEmojiRepo = nullptr; QVariantMap mAutocompleteRecords; + RocketChatServerConfig config; bool mConnected = false; bool mLoggedIn = false; bool mResumeOperation = false; @@ -280,7 +282,6 @@ class RocketChatServerData : public QObject QString mPendingSwitchRoomRequest; void getServerSettings( void ); - void postProcessChannelData( QVector<QSharedPointer<RocketChatChannel>> pVec ); void onDDPConnected(); void onDDPAuthenticated(); void onDDPMessageReceived( const QJsonObject &pMessage ); @@ -311,7 +312,6 @@ class RocketChatServerData : public QObject void ddpConnected( const QString &pServerId ); void loggedIn( const QString &pServer ); void onHashLoggedIn( const QString &pServer ); - void channelFound( const QString &pServerid, const QString &pJson ); void privateChannelCreated( const QString &pServerId, const QString &pRid, const QString &pUsername ); void loginError( void ); void userJoined( const QString &pChannelId, const QString &pUsername ); diff --git a/services/messageservice.cpp b/services/messageservice.cpp index 812dc8627f0ac52428f58816374261d8ef29dfb0..624b9bf5f8ea3759ff386422625dbc18ec035875 100644 --- a/services/messageservice.cpp +++ b/services/messageservice.cpp @@ -211,83 +211,81 @@ void MessageService::loadHistoryFromServer( QSharedPointer<LoadHistoryRequestCon pContainer->mAlredayReceived = 0; //TODO: fix memory leak risk - QMultiMap<QString, ChatMessage> *list; QSharedPointer<QSet<QString>> duplicateCheck( new QSet<QString> ); if ( requests.length() ) { - list = new QMultiMap<QString, ChatMessage>; - } - - //loop over all channels - for ( const auto ¤tChannelRequest : requests ) { - if ( !currentChannelRequest.isNull() ) { - QString roomId = currentChannelRequest->getChannelIds().first(); - QJsonObject startObj ; - startObj["$date"] = currentChannelRequest->getStart(); - QJsonObject endObj ; - endObj["$date"] = currentChannelRequest->getEnd(); - QJsonArray params; - - int limit = currentChannelRequest->getLimit(); - - if ( currentChannelRequest->getEnd() < 0 ) { - if ( currentChannelRequest->getStart() < 0 ) { - params = {roomId, QJsonValue::Null, limit, QJsonValue::Null}; + QMultiMap<QString, ChatMessage> *list = new QMultiMap<QString, ChatMessage>; + + //loop over all channels + for ( const auto ¤tChannelRequest : requests ) { + if ( !currentChannelRequest.isNull() ) { + QString roomId = currentChannelRequest->getChannelIds().first(); + QJsonObject startObj ; + startObj["$date"] = currentChannelRequest->getStart(); + QJsonObject endObj ; + endObj["$date"] = currentChannelRequest->getEnd(); + QJsonArray params; + + int limit = currentChannelRequest->getLimit(); + + if ( currentChannelRequest->getEnd() < 0 ) { + if ( currentChannelRequest->getStart() < 0 ) { + params = {roomId, QJsonValue::Null, limit, QJsonValue::Null}; + } else { + params = {roomId, QJsonValue::Null, limit, startObj}; + } } else { - params = {roomId, QJsonValue::Null, limit, startObj}; + + params = {roomId, endObj, limit, startObj}; } - } else { - params = {roomId, endObj, limit, startObj}; - } + QSharedPointer<DDPMethodRequest> request( new DDPMethodRequest( "loadHistory", params ) ); + std::function<void ( QJsonObject, MeteorDDP * )> ddpSuccess = [ = ]( QJsonObject pResponse, MeteorDDP * pDdp ) { + Q_UNUSED( pDdp ); - QSharedPointer<DDPMethodRequest> request( new DDPMethodRequest( "loadHistory", params ) ); - std::function<void ( QJsonObject, MeteorDDP * )> ddpSuccess = [ = ]( QJsonObject pResponse, MeteorDDP * pDdp ) { - Q_UNUSED( pDdp ); + if ( Q_LIKELY( pResponse.contains( "result" ) ) ) { + QJsonObject result = pResponse["result"].toObject(); - if ( Q_LIKELY( pResponse.contains( "result" ) ) ) { - QJsonObject result = pResponse["result"].toObject(); + if ( Q_LIKELY( result.contains( "messages" ) ) ) { + QJsonArray messages = result["messages"].toArray(); - if ( Q_LIKELY( result.contains( "messages" ) ) ) { - QJsonArray messages = result["messages"].toArray(); + for ( auto currentMessage : messages ) { + RocketChatMessage message( currentMessage.toObject() ); + bool newMessage = true; - for ( auto currentMessage : messages ) { - RocketChatMessage message( currentMessage.toObject() ); - bool newMessage = true; + if ( mServer->getChannels()->contains( message.getRoomId() ) ) { + auto channel = mServer->getChannels()->get( message.getRoomId() ); + newMessage = !channel->getMessageRepo()->contains( message.getId() ); + } - if ( mServer->getChannels()->contains( message.getRoomId() ) ) { - auto channel = mServer->getChannels()->get( message.getRoomId() ); - newMessage = !channel->getMessageRepo()->contains( message.getId() ); - } + auto messageObject = parseMessage( currentMessage.toObject(), true ); - auto messageObject = parseMessage( currentMessage.toObject(), true ); - - if ( !messageObject.isNull() && !duplicateCheck->contains( messageObject->getId() ) && newMessage ) { - list->insert( messageObject->getRoomId(), messageObject ); - duplicateCheck->insert( messageObject->getId() ); + if ( !messageObject.isNull() && !duplicateCheck->contains( messageObject->getId() ) && newMessage ) { + list->insert( messageObject->getRoomId(), messageObject ); + duplicateCheck->insert( messageObject->getId() ); + } } } } - } - pContainer->mAlredayReceived++; + pContainer->mAlredayReceived++; - if ( pContainer->mAlredayReceived == pContainer->mRequests.count() ) { - auto containerSuccess = pContainer->getSuccess(); + if ( pContainer->mAlredayReceived == pContainer->mRequests.count() ) { + auto containerSuccess = pContainer->getSuccess(); - if ( containerSuccess ) { - containerSuccess( list ); + if ( containerSuccess ) { + containerSuccess( list ); + } } - } - }; + }; - request->setSuccess( ddpSuccess ); + request->setSuccess( ddpSuccess ); - mServer->sendDdprequest( request ); + mServer->sendDdprequest( request ); + } } } } - } void MessageService::loadHistoryFromServer( QSharedPointer<LoadHistoryServiceRequest> pRequest ) diff --git a/services/rocketchatchannelservice.cpp b/services/rocketchatchannelservice.cpp index 0cf08573d6886737d10fcedd9ae9f20aaf2ad7d0..f5f7c9528c0465a51a8ac5d8d29a50716e496aa5 100755 --- a/services/rocketchatchannelservice.cpp +++ b/services/rocketchatchannelservice.cpp @@ -56,6 +56,7 @@ QVector<QSharedPointer<RocketChatChannel> > RocketChatChannelService::processCha std::tuple<QString, QString> openChannelTupel = mStorage->getCurrentChannel(); QString openChannel = std::get<0>( openChannelTupel ); + mStorage->transaction(); for ( const auto ¤tChannel : pChannelArray ) { QJsonObject currentChannelObject = currentChannel.toObject(); @@ -65,61 +66,79 @@ QVector<QSharedPointer<RocketChatChannel> > RocketChatChannelService::processCha currentChannelObject.contains( "t" ) ) ) ) { QString id = currentChannelObject["rid"].toString(); + QString name = currentChannelObject["name"].toString(); + QString type = currentChannelObject["t"].toString(); if ( id == "" ) { id = currentChannelObject["_id"].toString(); } - QString name = currentChannelObject["name"].toString(); - QString type = currentChannelObject["t"].toString(); - bool readonly = currentChannelObject["ro"].toBool(); - bool archived = currentChannelObject["archived"].toBool(); - QVariantList mutedUsersVariantList = currentChannelObject["muted"].toArray().toVariantList(); - QStringList mutedUsers = QVariant( mutedUsersVariantList ).toStringList(); - QString username = currentChannelObject["username"].toString(); - int unread = currentChannelObject["unread"].toInt(); - QSharedPointer<RocketChatChannel> channel = createChannelObject( id, name, type ); + QSharedPointer<RocketChatChannel> channel = nullptr; + + if ( mChannels->contains( id ) ) { + channel = mChannels->get( id ); + } else { + channel = createChannelObject( id, name, type ); + } if ( !channel.isNull() ) { - if ( mutedUsers.contains( username ) ) { - channel->setSelfMuted( true ); + + + if ( !name.isEmpty() ) { + channel->setName( currentChannelObject["name"].toString() ); } - loadUsersOfChannel( channel ); - channel->setMuted( mutedUsers ); - channel->setReadOnly( readonly ); - channel->setArchived( archived ); - channel->setJoined( pJoined ); + if ( currentChannelObject.contains( "ro" ) ) { + channel->setReadOnly( currentChannelObject["ro"].toBool() ); + } + + if ( currentChannelObject.contains( "archived" ) ) { + channel->setArchived( currentChannelObject["archived"].toBool() ); + } + + if ( currentChannelObject.contains( "blocker" ) ) { + channel->setBlocked( currentChannelObject["blocker"].toBool() ); + } + + if ( currentChannelObject.contains( "muted" ) ) { + auto muted = currentChannelObject["muted"].toArray().toVariantList(); + auto mutedUsers = QVariant( muted ).toStringList(); + channel->setMuted( mutedUsers ); + + if ( currentChannelObject.contains( "username" ) && mutedUsers.contains( currentChannelObject["username"].toString() ) ) { + channel->setSelfMuted( true ); + } + } if ( id == openChannel ) { channel->setUnreadMessages( 0 ); - } else { - channel->setUnreadMessages( unread ); + } else if ( currentChannelObject.contains( "unread" ) ) { + channel->setUnreadMessages( currentChannelObject["unread"].toInt() ); } - if ( !archived ) { + channel->setJoined( pJoined ); + loadUsersOfChannel( channel ); + + + if ( channel->getArchived() ) { subscribeChannel( channel ); } + vec.append( channel ); + persistChannel( channel ); } } } - return vec; -} + mStorage->commit(); -void RocketChatChannelService::handleGetRooms( QJsonObject pData ) -{ - QJsonArray result = pData["result"].toArray(); - - if ( result.size() > 0 ) { - processChannelData( result, true ); - } + return vec; } + void RocketChatChannelService::loadJoinedChannelsFromServer( void ) { std::function<void ( QJsonObject, MeteorDDP * )> subscriptionSuccess = [ = ]( QJsonObject pResponse, MeteorDDP * pDdpApi ) { @@ -156,6 +175,11 @@ void RocketChatChannelService::loadJoinedChannelsFromDb() channelPointer->setArchived( roomHash["archived"].toBool() ); channelPointer->setReadOnly( roomHash["readOnly"].toBool() ); channelPointer->setJoined( roomHash["joined"].toBool() ); + + if ( roomHash.contains( "blocked" ) ) { + channelPointer->setBlocked( roomHash["blocked"].toBool() ); + } + channels.append( channelPointer ); } } @@ -171,7 +195,10 @@ void RocketChatChannelService::persistChannel( QSharedPointer<RocketChatChannel> QString type = pChannel->getType(); QString muted = pChannel->getMuted().join( "," ); bool joined = pChannel->getJoined(); - mStorage->addChannel( id, name, type, joined, pChannel->getReadOnly(), muted, pChannel->getArchived() ); + bool archived = pChannel->getArchived(); + bool readonly = pChannel->getReadOnly(); + bool blocked = pChannel->getBlocked(); + mStorage->addChannel( id, name, type, joined, readonly, muted, archived, blocked ); } /** * Fills the channel with users, which are loaded from the server diff --git a/services/rocketchatchannelservice.h b/services/rocketchatchannelservice.h index 8b61450479bb15f1173697df4da63e15a1c965e2..c09dd2f7f7136d084c1519fe984ff8093a8746f6 100755 --- a/services/rocketchatchannelservice.h +++ b/services/rocketchatchannelservice.h @@ -81,7 +81,6 @@ class RocketChatChannelService : public QObject ChannelRepository *mChannels = nullptr; QVector<QSharedPointer<RocketChatChannel>> processChannelData( QJsonArray pChannelArray, bool pJoined ); - void handleGetRooms( QJsonObject pData ); MessageService *mMessageService; signals: diff --git a/sql.qrc b/sql.qrc index 5baa0e1b0885563962f0fe83f3bc59b9ec2adfcc..fa43fd7a2d369990f03beb3b9d15ad8f25b52975 100644 --- a/sql.qrc +++ b/sql.qrc @@ -1,6 +1,7 @@ <RCC> <qresource prefix="/"> <file>sql/migrations/1.sql</file> - <file>sql/emojis.sql</file> + <file>sql/emojis.sql</file> + <file>sql/migrations/2.sql</file> </qresource> </RCC> diff --git a/sql/migrations/2.sql b/sql/migrations/2.sql new file mode 100644 index 0000000000000000000000000000000000000000..7781e213aaf3141357fd796e031c613aa616e043 --- /dev/null +++ b/sql/migrations/2.sql @@ -0,0 +1,2 @@ +ALTER TABLE rooms ADD blocked integer DEFAULT 0; +INSERT INTO app_info (db_ver) VALUES(2);