diff --git a/CustomModels/channelmodel.cpp b/CustomModels/channelmodel.cpp index beeb1d9de738b0bb5f6389c5354ad3c7642b2977..aaa6df6dfbd7632c481a7ff5928edfdcde3eeb38 100644 --- a/CustomModels/channelmodel.cpp +++ b/CustomModels/channelmodel.cpp @@ -58,8 +58,9 @@ QVariant ChannelModel::data( const QModelIndex &index, int role ) const if ( message->getMessageType() == QStringLiteral( "file" ) || message->getMessageType() == QStringLiteral( "image" ) || message->getMessageType() == QStringLiteral( "audio" ) ) { return tr( QByteArrayLiteral( "file upload" ) ); } - auto messageText =message->getMessageString(); - messageText = messageText.replace("getMessageString(); + messageText = messageText.replace( " &channel connect( channel.data(), &RocketChatChannel::unreadMessagesChanged, this, &ChannelModel::onUnreadMessageChanged, Qt::UniqueConnection ); connect( channel.data(), &RocketChatChannel::dataChanged, this, &ChannelModel::onDataChanged, Qt::UniqueConnection ); connect( channel.data(), &RocketChatChannel::channelDeleted, this, &ChannelModel::onDeleted, Qt::UniqueConnection ); - connect(channel.data(), &RocketChatChannel::updatedChanged, this, &ChannelModel::onChannelOrderChanged); + connect( channel.data(), &RocketChatChannel::updatedChanged, this, &ChannelModel::onChannelOrderChanged ); duplicateCheck.insert( channel->getRoomId() ); int pos = channelList.findPosition( channel ); - if(pos <0){ + + if ( pos < 0 ) { pos = 0; } + beginInsertRows( QModelIndex(), pos, pos ); channelList.insertSort( channel ); endInsertRows(); @@ -165,35 +168,38 @@ bool ChannelModel::addChannel( const QSharedPointer &channel } -bool ChannelModel::addChannelsSlot(const QVector > &pChannels) +bool ChannelModel::addChannelsSlot( const QVector > &pChannels ) { - // beginResetModel(); - int from =-1; + // beginResetModel(); + int from = -1; int to = -1; - for(const auto &channel: pChannels){ - if ( !channel.isNull() && channel->getRoomId() != "" && !duplicateCheck.contains( channel->getRoomId() ) ) { + + for ( const auto &channel : pChannels ) { + if ( !channel.isNull() && !channel->getRoomId().isEmpty() && !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 ); connect( channel.data(), &RocketChatChannel::channelDeleted, this, &ChannelModel::onDeleted, Qt::UniqueConnection ); - connect(channel.data(), &RocketChatChannel::updatedChanged, this, &ChannelModel::onChannelOrderChanged); + connect( channel.data(), &RocketChatChannel::updatedChanged, this, &ChannelModel::onChannelOrderChanged ); duplicateCheck.insert( channel->getRoomId() ); auto index = channelList.insertSort( channel ); - if(from == -1){ + if ( from == -1 ) { from = index; to = index; } - if(indexto){ + } else if ( index > to ) { to = index; } } } + beginInsertRows( QModelIndex(), from, to ); endInsertRows(); - // endResetModel(); + // endResetModel(); return true; } @@ -237,7 +243,7 @@ void ChannelModel::onNewerMessage( const QString &id, qint64 timestamp ) //emit dataChanged(index(pos),index(pos),{lastMessageText}); } - // sortChanged(); + // sortChanged(); } void ChannelModel::onUnreadMessageChanged( const QString &id, int number ) @@ -308,14 +314,13 @@ void ChannelModel::onDeleted( const QString &pId, bool deleted ) } -void ChannelModel::onChannelOrderChanged(const QString &pId, qint64 pTimestamp) +void ChannelModel::onChannelOrderChanged( const QString &pId, qint64 pTimestamp ) { - static int counter = 0; - counter++; - qDebug() << counter; - beginResetModel(); - channelList.reOrder(); - endResetModel(); + static int counter = 0; + counter++; + qDebug() << counter; + auto changed = channelList.reOrder(); + emit dataChanged( createIndex( std::get<0>( changed ), 0 ), createIndex( std::get<1>( changed ), 0 ) ); } void ChannelModel::addChannelSlot( const QSharedPointer &channel ) diff --git a/container/sortedvector.cpp b/container/sortedvector.cpp index 4b84726392cede512122024d4c057574d6cdfb83..1978f690ae3ec78e29ae30a357f7f5bf46867809 100644 --- a/container/sortedvector.cpp +++ b/container/sortedvector.cpp @@ -35,13 +35,9 @@ int SortedVector::insertSort( const QSharedPointer &pointer ) return ( *first ) > ( *second ); } ); - row = std::distance(this->begin(), elementSmallerThanNew); + row = std::distance( this->begin(), elementSmallerThanNew ); this->insert( elementSmallerThanNew, pointer ); - if(row <0 || row>1000){ - qDebug()<::findInsertPosition( const QSharedPointer &pointer ) cons return ( *first ) < ( *second ); } ); - return elementSmallerThanNew - this->begin();; + return std::distance( this->begin(), elementSmallerThanNew ); } @@ -62,14 +58,39 @@ template int SortedVector::findPosition( const QSharedPointer &pointer ) const { auto pos = find( this->begin(), this->end(), pointer ); - return pos - this->begin(); + return std::distance( this->begin(), pos ); } template -void SortedVector::reOrder() +std::tuple SortedVector::reOrder() { + std::vector> indices( this->size() ); + + for ( const auto &elem : *this ) { + indices.push_back( elem ); + } + std::sort( this->begin(), this->end(), [ = ]( const QSharedPointer &first, const QSharedPointer &second )->bool{ return ( *first ) > ( *second ); } ); + + int from = -1; + int to = -1; + + for ( int i = 0; i < this->size() - 1; i++ ) { + if ( indices[i] != ( *this )[i] ) { + from = i; + break; + } + } + + for ( int i = this->size() - 1; i >= 0; i-- ) { + if ( indices[i] != ( *this )[i] ) { + to = i; + break; + } + } + + return std::make_tuple( from, to ); } diff --git a/container/sortedvector.h b/container/sortedvector.h index e5df47247a13b9a4f69d86d24aeb3edda35e5d01..bf463986d9e1cce69c264ea845b92f954ff498d1 100644 --- a/container/sortedvector.h +++ b/container/sortedvector.h @@ -26,9 +26,8 @@ #include #include -struct SortedVectorProperties -{ - enum class sortOrder{ +struct SortedVectorProperties { + enum class sortOrder { asc, dsc }; @@ -40,14 +39,14 @@ template class SortedVector : public QVector> int insertSort( const QSharedPointer & ); int findInsertPosition( const QSharedPointer & ) const; int findPosition( const QSharedPointer & ) const; - void reOrder(); + std::tuple reOrder(); bool operator =( const QVector> &o ) { this->clear(); this->append( o ); return true; } -private: + private: mutable QMutex mSortMutex; }; #include "sortedvector.cpp" diff --git a/repos/entities/rocketchatchannel.cpp b/repos/entities/rocketchatchannel.cpp index 25e90f705f4c9e2dc9d10b9e40ad6938ee351823..0ed4f13e2ac48471dd1f05cb1153fa176dc9f4f3 100755 --- a/repos/entities/rocketchatchannel.cpp +++ b/repos/entities/rocketchatchannel.cpp @@ -83,7 +83,7 @@ const QString &RocketChatChannel::getName() const void RocketChatChannel::setName( const QString &value ) { - if ( mName != value ) { + if ( mName != value && !value.isEmpty() ) { mName = value; emit dataChanged( mName, "name" ); } @@ -167,9 +167,11 @@ QList> RocketChatChannel::addMessages( const Q } } } - if(newMessages.count()){ - // emit messageAdded( getRoomId(), 0 ); + + if ( newMessages.count() ) { + // emit messageAdded( getRoomId(), 0 ); } + return newMessages; } @@ -192,7 +194,8 @@ unsigned int RocketChatChannel::getUnreadMessages() const void RocketChatChannel::setUnreadMessages( unsigned int value ) { qDebug() << "set unread to: " << value; - if(mUnreadMessages != value){ + + if ( mUnreadMessages != value ) { mUnreadMessages = value; emit unreadMessagesChanged( mRoomId, value ); } @@ -205,7 +208,7 @@ const QString &RocketChatChannel::getType() const void RocketChatChannel::setType( const QString &pType ) { - if ( mType != pType ) { + if ( mType != pType && !pType.isEmpty() ) { mType = pType; emit dataChanged( mRoomId, "type" ); } @@ -290,7 +293,7 @@ const QString &RocketChatChannel::getOwnerName() const void RocketChatChannel::setOwnerName( const QString &pOwner ) { - if ( mOwnerName != pOwner ) { + if ( mOwnerName != pOwner && !pOwner.isEmpty() ) { mOwnerName = pOwner; emit dataChanged( mRoomId, "ownerName" ); } @@ -303,7 +306,7 @@ const QString &RocketChatChannel::getOwnerId() const void RocketChatChannel::setOwnerId( const QString &ownerId ) { - if ( mOwnerId != ownerId ) { + if ( mOwnerId != ownerId && !ownerId.isEmpty() ) { mOwnerId = ownerId; emit dataChanged( mRoomId, "ownerId" ); } @@ -316,7 +319,7 @@ const QString &RocketChatChannel::getUsername() const void RocketChatChannel::setUsername( const QString &username ) { - if ( mUsername != username ) { + if ( mUsername != username && !username.isEmpty() ) { mUsername = username; emit dataChanged( mRoomId, "username" ); } @@ -329,7 +332,7 @@ const QSharedPointer &RocketChatChannel::getChatPartner() const void RocketChatChannel::setChatPartner( const QSharedPointer &chatPartner ) { - if(mChatPartner != chatPartner){ + if ( mChatPartner != chatPartner && !chatPartner.isNull() ) { mChatPartner = chatPartner; connect( chatPartner.data(), &RocketChatUser::statusChanged, this, [ = ]() { emit dataChanged( mRoomId, "userStatus" ); @@ -344,7 +347,7 @@ const QString &RocketChatChannel::getChatPartnerId() const void RocketChatChannel::setChatPartnerId( const QString &chatPartnerId ) { - if ( mChatPartnerId != chatPartnerId ) { + if ( mChatPartnerId != chatPartnerId && !chatPartnerId.isEmpty() ) { mChatPartnerId = chatPartnerId; // emit dataChanged( mRoomId, "username" ); } @@ -357,7 +360,7 @@ QSharedPointer RocketChatChannel::getAvatarImg() const void RocketChatChannel::setAvatarImg( const QSharedPointer &pAvatar ) { - if ( mAvatarImg != pAvatar ) { + if ( mAvatarImg != pAvatar && !pAvatar.isNull() ) { mAvatarImg = pAvatar; emit dataChanged( mRoomId, "avatarImg" ); } @@ -371,10 +374,11 @@ qint64 RocketChatChannel::getUpdatedAt() const void RocketChatChannel::setUpdatedAt( const qint64 &updatedAt ) { //skip signal the first time - if(mUpdatedAt != updatedAt && updatedAt != -1){ - if(mUpdatedAt != -1 ){ - emit updatedChanged(mRoomId,updatedAt); + if ( mUpdatedAt != updatedAt && updatedAt != -1 ) { + if ( mUpdatedAt != -1 ) { + emit updatedChanged( mRoomId, updatedAt ); } + mUpdatedAt = updatedAt; } } @@ -386,7 +390,7 @@ qint64 RocketChatChannel::getCreatedAt() const void RocketChatChannel::setCreatedAt( const qint64 &createdAt ) { - mCreatedAt = createdAt; + mCreatedAt = createdAt; } bool RocketChatChannel::getBlocked() const @@ -402,34 +406,39 @@ void RocketChatChannel::setBlocked( bool blocked ) } } -QStringList RocketChatChannel::whiteList(const QList ids, const qint64 newest,const qint64 oldest) +QStringList RocketChatChannel::whiteList( const QList ids, const qint64 newest, const qint64 oldest ) { auto idSet = ids.toSet(); auto currentList = mMessages.getElements().keys().toSet(); auto blacklist = currentList - idSet; QList buffer = {}; - std::copy_if (blacklist.begin(), blacklist.end(), std::back_inserter(buffer), - [=](const QString id){ + std::copy_if( blacklist.begin(), blacklist.end(), std::back_inserter( buffer ), + [ = ]( const QString id ) { bool keep = true; - if(mMessages.contains(id) && mMessages.get(id)->getRoomId() == getRoomId()){ - auto message = mMessages.get(id); - keep = message->inTimeIntervall(newest,oldest); + + if ( mMessages.contains( id ) && mMessages.get( id )->getRoomId() == getRoomId() ) { + auto message = mMessages.get( id ); + keep = message->inTimeIntervall( newest, oldest ); } + return keep; } - ); + ); blacklist = buffer.toSet(); - for(QString id : blacklist){ - mMessages.remove(id); + + for ( QString id : blacklist ) { + mMessages.remove( id ); } + return blacklist.toList(); } -void RocketChatChannel::deleteMessage(const QString id) +void RocketChatChannel::deleteMessage( const QString id ) { - emit messageDeleted(getRoomId(),id); - if(mMessages.contains(id)){ - mMessages.remove(id); + emit messageDeleted( getRoomId(), id ); + + if ( mMessages.contains( id ) ) { + mMessages.remove( id ); } diff --git a/services/messageservice.cpp b/services/messageservice.cpp index ba557c40420bc0f62c1ae2cef740cb70af26db36..ed27224c915c58c92a220cc1a1ca530e929bb20a 100644 --- a/services/messageservice.cpp +++ b/services/messageservice.cpp @@ -110,141 +110,142 @@ void MessageService::persistMessages( const MessageList &pMessage ) void MessageService::persist() { - if(mServer){ - mPersistanceLayer->transaction(); - auto channels = mServer->getChannels(); + if ( mServer ) { + mPersistanceLayer->transaction(); + auto channels = mServer->getChannels(); - for ( const auto &channel : channels->getElements() ) { - if ( channel.isNull() ) { - auto messages = channel->getMessageRepo(); + for ( const auto &channel : channels->getElements() ) { + if ( channel.isNull() ) { + auto messages = channel->getMessageRepo(); - for ( const auto &message : messages->getElements() ) { - if ( !message.isNull() ) { - persistMessage( message ); + for ( const auto &message : messages->getElements() ) { + if ( !message.isNull() ) { + persistMessage( message ); + } } } } - } - mPersistanceLayer->askForcommit(); + mPersistanceLayer->askForcommit(); } } QSharedPointer MessageService::parseMessage( const QJsonObject &pMessageData, bool linkify ) { - if(mServer){ - ChatMessage message( nullptr ); - QString userId = mServer->getUserId(); - bool blocked = false; + if ( mServer ) { + ChatMessage message( nullptr ); + QString userId = mServer->getUserId(); + bool blocked = false; - if ( Q_LIKELY( pMessageData.contains( QStringLiteral( "msg" ) ) ) ) { - //parse message String - QString msgString = pMessageData[QStringLiteral( "msg" )].toString(); + if ( Q_LIKELY( pMessageData.contains( QStringLiteral( "msg" ) ) ) ) { + //parse message String + QString msgString = pMessageData[QStringLiteral( "msg" )].toString(); - if ( linkify ) { - msgString = Utils::removeUtf8Emojis( msgString ); + if ( linkify ) { + msgString = Utils::removeUtf8Emojis( msgString ); - msgString = Utils::linkiFy( msgString ); - msgString = msgString.replace( QStringLiteral( "\n" ), QStringLiteral( "
" ) ); - msgString = Utils::escapeHtml(msgString); - msgString = Utils::emojiFy( msgString, mEmojiRepo ); + msgString = Utils::linkiFy( msgString ); + msgString = msgString.replace( QStringLiteral( "\n" ), QStringLiteral( "
" ) ); + msgString = Utils::escapeHtml( msgString ); + msgString = Utils::emojiFy( msgString, mEmojiRepo ); - } + } - QString msgType; - QString author; - QString authorId; + QString msgType; + QString author; + QString authorId; - if ( Q_LIKELY( pMessageData.contains( "t" ) ) ) { - msgType = pMessageData["t"].toString(); - } + if ( Q_LIKELY( pMessageData.contains( "t" ) ) ) { + msgType = pMessageData["t"].toString(); + } - if ( Q_UNLIKELY( mServer->getUsername() == "apple.store" ) && msgType == "jitsi_call_started" ) { - return nullptr; - } + if ( Q_UNLIKELY( mServer->getUsername() == "apple.store" ) && msgType == "jitsi_call_started" ) { + return nullptr; + } - QJsonObject userObject = pMessageData["u"].toObject(); + QJsonObject userObject = pMessageData["u"].toObject(); - if ( Q_LIKELY( userObject.contains( QStringLiteral( "username" ) ) ) ) { - author = userObject[QStringLiteral( "username" )].toString(); - } + if ( Q_LIKELY( userObject.contains( QStringLiteral( "username" ) ) ) ) { + author = userObject[QStringLiteral( "username" )].toString(); + } - // if alias given, us it instead of the username, required for bridges - if ( Q_LIKELY( pMessageData.contains( QStringLiteral( "alias" ) ) ) ) { - author = pMessageData[QStringLiteral( "alias" )].toString(); - } + // if alias given, us it instead of the username, required for bridges + if ( Q_LIKELY( pMessageData.contains( QStringLiteral( "alias" ) ) ) ) { + author = pMessageData[QStringLiteral( "alias" )].toString(); + } - if ( Q_LIKELY( userObject.contains( "_id" ) ) ) { - authorId = userObject[QStringLiteral( "_id" )].toString(); - } + if ( Q_LIKELY( userObject.contains( "_id" ) ) ) { + authorId = userObject[QStringLiteral( "_id" )].toString(); + } - bool ownMessage = false; + bool ownMessage = false; - if ( authorId == userId ) { - ownMessage = true; - } + if ( authorId == userId ) { + ownMessage = true; + } - QJsonObject timestampObject = pMessageData[QStringLiteral( "ts" )].toObject(); - qint64 timestamp = 0; + QJsonObject timestampObject = pMessageData[QStringLiteral( "ts" )].toObject(); + qint64 timestamp = 0; - if ( Q_LIKELY( timestampObject.contains( QStringLiteral( "$date" ) ) ) ) { - timestamp = static_cast( timestampObject[QStringLiteral( "$date" )].toDouble() ); - } + if ( Q_LIKELY( timestampObject.contains( QStringLiteral( "$date" ) ) ) ) { + timestamp = static_cast( timestampObject[QStringLiteral( "$date" )].toDouble() ); + } - QDateTime date = QDateTime::fromMSecsSinceEpoch( timestamp ); + QDateTime date = QDateTime::fromMSecsSinceEpoch( timestamp ); - QString formattedDate = date.toString( QStringLiteral( "dd.MM.yyyy" ) ); - QString formattedTime = date.toString( QStringLiteral( "hh:mm" ) ); - auto attachments = processAttachments( pMessageData ); - message = QSharedPointer::create( pMessageData ); - message->setAttachments( attachments ); - message->setOwnMessage( ownMessage ); - message->setAuthorId( authorId ); + QString formattedDate = date.toString( QStringLiteral( "dd.MM.yyyy" ) ); + QString formattedTime = date.toString( QStringLiteral( "hh:mm" ) ); + auto attachments = processAttachments( pMessageData ); + message = QSharedPointer::create( pMessageData ); + message->setAttachments( attachments ); + message->setOwnMessage( ownMessage ); + message->setAuthorId( authorId ); - if ( !attachments.isEmpty() ) { - auto firstAttachment = attachments.first(); + if ( !attachments.isEmpty() ) { + auto firstAttachment = attachments.first(); - if ( !firstAttachment.isNull() ) { - if ( firstAttachment->getType() == "replyMessage" ) { - QString messageText = msgString; - messageText.replace( mReplyRegeEx, "" ); - msgString = messageText; - } else { - msgString = firstAttachment->getTitle(); + if ( !firstAttachment.isNull() ) { + if ( firstAttachment->getType() == "replyMessage" ) { + QString messageText = msgString; + messageText.replace( mReplyRegeEx, "" ); + msgString = messageText; + } else { + msgString = firstAttachment->getTitle(); + } } } - } - //TODO: place RocketChatUserObject inside Message instead... - auto then = [ message ]( QSharedPointer tempfile, bool showInline ) { - message->setAvatarImg( tempfile ); - }; - auto avatarUrl = "/avatar/" + author + ".jpg"; - auto avatarRequest = QSharedPointer::create( avatarUrl, "temp", then, true ); - mFileService->getFileRessource( avatarRequest ); - - message->setAuthor( author ); - message->setFormattedDate( formattedDate ); - message->setFormattedTime( formattedTime ); - message->setMessageString( msgString ); - message->setMessageType( msgType ); - - if ( mBlockedUsers.contains( author ) ) { - blocked = true; + //TODO: place RocketChatUserObject inside Message instead... + auto then = [ message ]( QSharedPointer tempfile, bool showInline ) { + message->setAvatarImg( tempfile ); + }; + auto avatarUrl = "/avatar/" + author + ".jpg"; + auto avatarRequest = QSharedPointer::create( avatarUrl, "temp", then, true ); + //mFileService->getFileRessource( avatarRequest ); + + message->setAuthor( author ); + message->setFormattedDate( formattedDate ); + message->setFormattedTime( formattedTime ); + message->setMessageString( msgString ); + message->setMessageType( msgType ); + + if ( mBlockedUsers.contains( author ) ) { + blocked = true; + } } - } - if ( Q_UNLIKELY( message.isNull() ) ) { - qDebug() << "invalid messag"; - } + if ( Q_UNLIKELY( message.isNull() ) ) { + qDebug() << "invalid messag"; + } - message->setServer( mServer->getBaseUrl() ); + message->setServer( mServer->getBaseUrl() ); - if ( !blocked ) { - return message; + if ( !blocked ) { + return message; + } } -} + return nullptr; } @@ -258,50 +259,50 @@ void MessageService::addUserToBlockList( const QString &pId ) void MessageService::addChannelToBlockList( const QString &pId ) { - if(mServer){ - QString roomId = pId; - QString userId = mServer->getUserId(); - QString otherUserId = roomId.replace( userId, "" ); - addUserToBlockList( otherUserId ); + if ( mServer ) { + QString roomId = pId; + QString userId = mServer->getUserId(); + QString otherUserId = roomId.replace( userId, "" ); + addUserToBlockList( otherUserId ); } } void MessageService::searchMessage( const QString &pTerm, const QString &pRoom ) { - if(mServer){ - DdpCallback ddpSuccess = [ = ]( const QJsonObject & pResponse, - MeteorDDP * pDdp ) { - Q_UNUSED( pDdp ) + if ( mServer ) { + DdpCallback ddpSuccess = [ = ]( const QJsonObject & pResponse, + MeteorDDP * pDdp ) { + Q_UNUSED( pDdp ) - if ( pResponse.contains( "result" ) ) { - QJsonObject result = pResponse["result"].toObject(); + if ( pResponse.contains( "result" ) ) { + QJsonObject result = pResponse["result"].toObject(); - if ( result.contains( "messages" ) ) { - QJsonArray messagesArray = result["messages"].toArray(); - MessageList messageList; + if ( result.contains( "messages" ) ) { + QJsonArray messagesArray = result["messages"].toArray(); + MessageList messageList; - for ( const auto message : messagesArray ) { - messageList.append( parseMessage( message.toObject() ) ); - } + for ( const auto message : messagesArray ) { + messageList.append( parseMessage( message.toObject() ) ); + } - if ( !messageList.isEmpty() ) { - auto result = QMetaObject::invokeMethod( mSearchResults, "setSearchResults", Q_ARG( MessageList, messageList ) ); - Q_ASSERT( result ); + if ( !messageList.isEmpty() ) { + auto result = QMetaObject::invokeMethod( mSearchResults, "setSearchResults", Q_ARG( MessageList, messageList ) ); + Q_ASSERT( result ); + } } } - } - }; + }; - auto request = QSharedPointer::create( pTerm, pRoom ); - request->setSuccess( ddpSuccess ); + auto request = QSharedPointer::create( pTerm, pRoom ); + request->setSuccess( ddpSuccess ); - mServer->sendDdprequest( request ); + mServer->sendDdprequest( request ); } } void MessageService::loadHistoryFromServer( const QSharedPointer &pContainer ) { - if ( !pContainer.isNull()&&mServer ) { + if ( !pContainer.isNull() && mServer ) { auto requests = pContainer->getRequests(); pContainer->mAlredayReceived = 0; @@ -349,7 +350,7 @@ void MessageService::loadHistoryFromServer( const QSharedPointercontains( messageObject->getId() )) { + if ( !messageObject.isNull() && !duplicateCheck->contains( messageObject->getId() ) ) { list->insert( messageObject->getRoomId(), messageObject ); duplicateCheck->insert( messageObject->getId() ); } @@ -575,26 +576,31 @@ QList> MessageService::processAttachments( return attachmentsList; } -void MessageService::deleteMessagesNotInList(MessageMap *pMessages,QString pChannelId,bool pCheckForYounger) +void MessageService::deleteMessagesNotInList( MessageMap *pMessages, QString pChannelId, bool pCheckForYounger ) { - QMap> timeIndex; + QMap> timeIndex; QList ids; - if(mServer){ - auto channel = mServer->getChannels()->get(pChannelId); - for(const auto message : pMessages->values(pChannelId)){ - timeIndex[message->getTimestamp()] = message; - ids.append(message->getId()); - } - qint64 oldest = timeIndex.first()->getTimestamp(); - qint64 newest = timeIndex.last()->getTimestamp(); - qint64 youngest = channel->getYoungestMessage()->getTimestamp(); - if(pCheckForYounger) - newest = newest < youngest ? youngest : newest; - - auto blacklist = channel->whiteList(ids,newest,oldest); - for(QString messageId : blacklist){ - mPersistanceLayer->deleteMessage(messageId); - } + if ( mServer ) { + auto channel = mServer->getChannels()->get( pChannelId ); + + for ( const auto message : pMessages->values( pChannelId ) ) { + timeIndex[message->getTimestamp()] = message; + ids.append( message->getId() ); + } + + qint64 oldest = timeIndex.first()->getTimestamp(); + qint64 newest = timeIndex.last()->getTimestamp(); + qint64 youngest = channel->getYoungestMessage()->getTimestamp(); + + if ( pCheckForYounger ) { + newest = newest < youngest ? youngest : newest; + } + + auto blacklist = channel->whiteList( ids, newest, oldest ); + + for ( QString messageId : blacklist ) { + mPersistanceLayer->deleteMessage( messageId ); + } } } diff --git a/services/rocketchatchannelservice.cpp b/services/rocketchatchannelservice.cpp index 7f56a054c59b4e0b3b84ad799ea3ceb50850be71..2ad3e93ea305a3eb80402e5b024c6c0f50be973d 100755 --- a/services/rocketchatchannelservice.cpp +++ b/services/rocketchatchannelservice.cpp @@ -38,51 +38,51 @@ RocketChatChannelService::RocketChatChannelService( QObject *parent, RocketChatS QSharedPointer RocketChatChannelService::createChannelObject( const QString &pRoomId, const QString &pName, const QString &pType, bool insertIntoRepo ) { - if(mServer){ - auto ptr = QSharedPointer::create( mServer, mMessageService, pRoomId, pName, pType ); + if ( mServer ) { + auto ptr = QSharedPointer::create( mServer, mMessageService, pRoomId, pName, pType ); - if ( Q_LIKELY( mChannels ) ) { - mChannels->add( ptr ); - } else { - qCritical() << "Channelsrepo not available to ChannelService"; - } + if ( Q_LIKELY( mChannels ) ) { + mChannels->add( ptr ); + } else { + qCritical() << "Channelsrepo not available to ChannelService"; + } - fillChannelWithMessages( ptr ); + fillChannelWithMessages( ptr ); - return ptr; - }else{ + return ptr; + } else { return nullptr; } } QSharedPointer RocketChatChannelService::createChannelObject( const QString &pRoomId, const QString &pName, const QString &pType, const QString &pUsername, bool insertIntoRepo ) { - if(mServer){ - auto ptr = QSharedPointer::create( mServer, mMessageService, pRoomId, pName, pType ); + if ( mServer ) { + auto ptr = QSharedPointer::create( mServer, mMessageService, pRoomId, pName, pType ); + + if ( pType == "d" ) { + ptr->setUsername( pUsername ); + + //TODO: place RocketChatUserObject inside Message instead... + auto then = [ ptr ]( QSharedPointer tempfile, bool showInline ) { + ptr->setAvatarImg( tempfile ); + }; + auto avatarUrl = "/avatar/" + pUsername + ".jpg"; + auto avatarRequest = QSharedPointer::create( avatarUrl, "temp", then, true ); + mFileService->getFileRessource( avatarRequest ); + } - if ( pType == "d" ) { - ptr->setUsername( pUsername ); + //TODO: this should not be done here! + if ( insertIntoRepo && Q_LIKELY( mChannels ) ) { + mChannels->add( ptr ); + } else if ( !mChannels ) { + qCritical() << "Channelsrepo not available to ChannelService"; + } - //TODO: place RocketChatUserObject inside Message instead... - auto then = [ ptr ]( QSharedPointer tempfile, bool showInline ) { - ptr->setAvatarImg( tempfile ); - }; - auto avatarUrl = "/avatar/" + pUsername + ".jpg"; - auto avatarRequest = QSharedPointer::create( avatarUrl, "temp", then, true ); - mFileService->getFileRessource( avatarRequest ); - } + fillChannelWithMessages( ptr ); - //TODO: this should not be done here! - if (insertIntoRepo && Q_LIKELY( mChannels ) ) { - mChannels->add( ptr ); + return ptr; } else { - qCritical() << "Channelsrepo not available to ChannelService"; - } - - fillChannelWithMessages( ptr ); - - return ptr; - }else{ return nullptr; } } @@ -92,131 +92,131 @@ QVector > RocketChatChannelService::processCha { QVector > vec; - if(mServer){ - std::tuple openChannelTupel = mStorage->getCurrentChannel(); - QString openChannel = std::get<0>( openChannelTupel ); - mStorage->transaction(); + if ( mServer ) { + std::tuple openChannelTupel = mStorage->getCurrentChannel(); + QString openChannel = std::get<0>( openChannelTupel ); + mStorage->transaction(); - for ( const auto ¤tChannel : pChannelArray ) { - QJsonObject currentChannelObject = currentChannel.toObject(); + for ( const auto ¤tChannel : pChannelArray ) { + QJsonObject currentChannelObject = currentChannel.toObject(); - if ( Q_LIKELY( ( currentChannelObject.contains( "rid" ) && - currentChannelObject.contains( "name" ) ) || ( currentChannelObject.contains( "_id" ) && - currentChannelObject.contains( "t" ) ) ) ) { + if ( Q_LIKELY( ( currentChannelObject.contains( "rid" ) && + currentChannelObject.contains( "name" ) ) || ( currentChannelObject.contains( "_id" ) && + currentChannelObject.contains( "t" ) ) ) ) { - QString id = currentChannelObject["rid"].toString(); - QString name = currentChannelObject["name"].toString(); - QString type = currentChannelObject["t"].toString(); - QString username = ""; - QString chatPartnerId ; + QString id = currentChannelObject["rid"].toString(); + QString name = currentChannelObject["name"].toString(); + QString type = currentChannelObject["t"].toString(); + QString username = ""; + QString chatPartnerId ; - qint64 updatedAt = -1; + qint64 updatedAt = -1; - if ( currentChannelObject.contains( QStringLiteral( "ls" ) ) ) { - auto updatedObj = currentChannelObject[QStringLiteral( "ls" )].toObject(); - updatedAt = static_cast( updatedObj[QStringLiteral( "$date" )].toDouble() ); - } + if ( currentChannelObject.contains( QStringLiteral( "ls" ) ) ) { + auto updatedObj = currentChannelObject[QStringLiteral( "ls" )].toObject(); + updatedAt = static_cast( updatedObj[QStringLiteral( "$date" )].toDouble() ); + } - if ( id == "" ) { - id = currentChannelObject["_id"].toString(); - } + if ( id == "" ) { + id = currentChannelObject["_id"].toString(); + } - if ( type == "d" ) { - username = name; - name = currentChannelObject["fname"].toString(); + if ( type == "d" ) { + username = name; + name = currentChannelObject["fname"].toString(); - QString ownUser = mServer->getUserId(); - chatPartnerId = id; - chatPartnerId.replace( ownUser, "" ); - } + QString ownUser = mServer->getUserId(); + chatPartnerId = id; + chatPartnerId.replace( ownUser, "" ); + } - //TODO: refactor me please! - if ( pUpdateOnly && !mChannels->contains( id ) ) { - continue; - } + //TODO: refactor me please! + if ( pUpdateOnly && !mChannels->contains( id ) ) { + continue; + } - if ( currentChannelObject.contains( "open" ) && !currentChannelObject["open"].toBool() ) { - mChannels->remove( id ); - mStorage->deleteChannel( id ); - continue; - } + if ( currentChannelObject.contains( "open" ) && !currentChannelObject["open"].toBool() ) { + mChannels->remove( id ); + mStorage->deleteChannel( id ); + continue; + } - QSharedPointer channel = nullptr; + QSharedPointer channel = nullptr; - if ( mChannels->contains( id ) ) { - channel = mChannels->get( id ); - } else { - channel = createChannelObject( id, name, type, username ); - } + if ( mChannels->contains( id ) ) { + channel = mChannels->get( id ); + } else { + channel = createChannelObject( id, name, type, username ); + } - if ( !channel.isNull() ) { + if ( !channel.isNull() ) { - if ( type == "d" ) { - channel->setChatPartnerId( chatPartnerId ); - } + if ( type == "d" ) { + channel->setChatPartnerId( chatPartnerId ); + } - if ( !name.isEmpty() ) { - channel->setName( name ); - } + if ( !name.isEmpty() ) { + channel->setName( name ); + } - if ( currentChannelObject.contains( "ro" ) ) { - channel->setReadOnly( currentChannelObject["ro"].toBool() ); - } + if ( currentChannelObject.contains( "ro" ) ) { + channel->setReadOnly( currentChannelObject["ro"].toBool() ); + } - if ( currentChannelObject.contains( "archived" ) ) { - channel->setArchived( currentChannelObject["archived"].toBool() ); - } + if ( currentChannelObject.contains( "archived" ) ) { + channel->setArchived( currentChannelObject["archived"].toBool() ); + } - if ( currentChannelObject.contains( "blocker" ) ) { - channel->setBlocked( currentChannelObject["blocker"].toBool() ); + if ( currentChannelObject.contains( "blocker" ) ) { + channel->setBlocked( currentChannelObject["blocker"].toBool() ); - if ( currentChannelObject["blocker"].toBool() ) { - mMessageService->addChannelToBlockList( channel->getRoomId() ); + if ( currentChannelObject["blocker"].toBool() ) { + mMessageService->addChannelToBlockList( channel->getRoomId() ); + } } - } - if ( currentChannelObject.contains( "u" ) ) { - auto userObj = currentChannelObject["u"].toObject(); + if ( currentChannelObject.contains( "u" ) ) { + auto userObj = currentChannelObject["u"].toObject(); - if ( userObj.contains( "username" ) && userObj.contains( "_id" ) ) { - channel->setOwnerName( userObj["username"].toString() ); - channel->setOwnerId( userObj["_id"].toString() ); + if ( userObj.contains( "username" ) && userObj.contains( "_id" ) ) { + channel->setOwnerName( userObj["username"].toString() ); + channel->setOwnerId( userObj["_id"].toString() ); + } } - } - if ( currentChannelObject.contains( "muted" ) ) { - auto muted = currentChannelObject["muted"].toArray().toVariantList(); - auto mutedUsers = QVariant( muted ).toStringList(); - channel->setMuted( mutedUsers ); + 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 ( currentChannelObject.contains( "username" ) && mutedUsers.contains( currentChannelObject["username"].toString() ) ) { - channel->setSelfMuted( true ); + if ( id == openChannel ) { + channel->setUnreadMessages( 0 ); + } else if ( currentChannelObject.contains( "unread" ) ) { + channel->setUnreadMessages( static_cast( currentChannelObject["unread"].toInt() ) ); } - } - if ( id == openChannel ) { - channel->setUnreadMessages( 0 ); - } else if ( currentChannelObject.contains( "unread" ) ) { - channel->setUnreadMessages( static_cast( currentChannelObject["unread"].toInt() ) ); - } + channel->setJoined( pJoined ); + loadUsersOfChannel( channel ); - channel->setJoined( pJoined ); - loadUsersOfChannel( channel ); + subscribeChannel( channel ); - subscribeChannel( channel ); + channel->setUpdatedAt( updatedAt ); - channel->setUpdatedAt( updatedAt ); + vec.append( channel ); + persistChannel( channel ); + } - vec.append( channel ); - persistChannel( channel ); } - } - } - mStorage->askForcommit(); -} + mStorage->askForcommit(); + } return vec; } @@ -295,7 +295,7 @@ void RocketChatChannelService::loadJoinedChannelsFromDb() channelPointer->setReadOnly( roomHash["readOnly"].toBool() ); channelPointer->setJoined( roomHash["joined"].toBool() ); channelPointer->setChatPartnerId( roomHash["chatPartnerId"].toString() ); - channelPointer->setUpdatedAt(roomHash["updatedAt"].toLongLong()); + channelPointer->setUpdatedAt( roomHash["updatedAt"].toLongLong() ); if ( channelPointer->getType() == "d" ) { auto user = users->getUserById( channelPointer->getChatPartnerId() ); @@ -316,18 +316,20 @@ void RocketChatChannelService::loadJoinedChannelsFromDb() } channels.append( channelPointer ); - if(channelPointer->getType() == "p"){ - channelsP.append(channelPointer); - }else if(channelPointer->getType() == "c"){ - channelsC.append(channelPointer); - }else if(channelPointer->getType() == "d"){ - channelsD.append(channelPointer); + + if ( channelPointer->getType() == "p" ) { + channelsP.append( channelPointer ); + } else if ( channelPointer->getType() == "c" ) { + channelsC.append( channelPointer ); + } else if ( channelPointer->getType() == "d" ) { + channelsD.append( channelPointer ); } } } - mChannels->add("p",channelsP); - mChannels->add("c",channelsC); - mChannels->add("d",channelsD); + + mChannels->add( "p", channelsP ); + mChannels->add( "c", channelsC ); + mChannels->add( "d", channelsD ); emit channelsLoaded( channels, true ); } } @@ -505,7 +507,9 @@ ChannelRepository *RocketChatChannelService::getChannels() const void RocketChatChannelService::setChannels( ChannelRepository *pChannels ) { - mChannels = pChannels; + if ( pChannels ) { + mChannels = pChannels; + } } void RocketChatChannelService::leaveChannel( const QString &pId )