diff --git a/qml/pages/ChatPage.qml b/qml/pages/ChatPage.qml index 08d7af4..a6c2055 100644 --- a/qml/pages/ChatPage.qml +++ b/qml/pages/ChatPage.qml @@ -151,6 +151,9 @@ Page { onMessagesReceived: { chatView.positionViewAtEnd(); } + onNewMessageReceived: { + chatView.positionViewAtEnd(); + } } Timer { diff --git a/src/chatmodel.cpp b/src/chatmodel.cpp index f9c0d3f..9750d30 100644 --- a/src/chatmodel.cpp +++ b/src/chatmodel.cpp @@ -7,6 +7,7 @@ ChatModel::ChatModel(TDLibWrapper *tdLibWrapper) this->tdLibWrapper = tdLibWrapper; this->inReload = false; connect(this->tdLibWrapper, SIGNAL(messagesReceived(QVariantList)), this, SLOT(handleMessagesReceived(QVariantList))); + connect(this->tdLibWrapper, SIGNAL(newMessageReceived(QString, QVariantMap)), this, SLOT(handleNewMessageReceived(QString, QVariantMap))); } ChatModel::~ChatModel() @@ -65,6 +66,7 @@ void ChatModel::handleMessagesReceived(const QVariantList &messages) { qDebug() << "[ChatModel] Receiving new messages :)"; this->messagesMutex.lock(); + this->messagesToBeAdded.clear(); QListIterator messagesIterator(messages); while (messagesIterator.hasNext()) { @@ -74,6 +76,39 @@ void ChatModel::handleMessagesReceived(const QVariantList &messages) } } std::sort(this->messagesToBeAdded.begin(), this->messagesToBeAdded.end(), compareMessages); + + this->insertMessages(); + this->messagesMutex.unlock(); + + // First call only returns one message, we need to get a little more than that... + if (this->messagesToBeAdded.size() == 1 && !this->inReload) { + qDebug() << "[ChatModel] Only one message received in first call, loading more..."; + this->inReload = true; + this->tdLibWrapper->getChatHistory(this->chatId, this->messagesToBeAdded.first().toMap().value("id").toLongLong()); + } else { + qDebug() << "[ChatModel] Messages loaded, notifying chat UI..."; + this->inReload = false; + emit messagesReceived(); + } +} + +void ChatModel::handleNewMessageReceived(const QString &chatId, const QVariantMap &message) +{ + if (chatId == this->chatId) { + qDebug() << "[ChatModel] New message received for this chat"; + this->messagesMutex.lock(); + + this->messagesToBeAdded.clear(); + this->messagesToBeAdded.append(message); + + this->insertMessages(); + this->messagesMutex.unlock(); + emit newMessageReceived(); + } +} + +void ChatModel::insertMessages() +{ if (this->messages.isEmpty()) { beginResetModel(); this->messages.append(this->messagesToBeAdded); @@ -88,16 +123,4 @@ void ChatModel::handleMessagesReceived(const QVariantList &messages) this->insertRows(0, this->messagesToBeAdded.size()); } } - this->messagesMutex.unlock(); - - // First call only returns one message, we need to get a little more than that... - if (this->messagesToBeAdded.size() == 1 && !this->inReload) { - qDebug() << "[ChatModel] Only one message received in first call, loading more..."; - this->inReload = true; - this->tdLibWrapper->getChatHistory(this->chatId, this->messagesToBeAdded.first().toMap().value("id").toLongLong()); - } else { - qDebug() << "[ChatModel] Messages loaded, notifying chat UI..."; - this->inReload = false; - emit messagesReceived(); - } } diff --git a/src/chatmodel.h b/src/chatmodel.h index a8ea60d..8f89f9e 100644 --- a/src/chatmodel.h +++ b/src/chatmodel.h @@ -21,9 +21,11 @@ public: signals: void messagesReceived(); + void newMessageReceived(); public slots: void handleMessagesReceived(const QVariantList &messages); + void handleNewMessageReceived(const QString &chatId, const QVariantMap &message); private: @@ -34,6 +36,8 @@ private: QMutex messagesMutex; QString chatId; bool inReload; + + void insertMessages(); }; #endif // CHATMODEL_H diff --git a/src/tdlibreceiver.cpp b/src/tdlibreceiver.cpp index d2f35b9..8b9d538 100644 --- a/src/tdlibreceiver.cpp +++ b/src/tdlibreceiver.cpp @@ -70,6 +70,7 @@ void TDLibReceiver::processReceivedDocument(const QJsonDocument &receivedJsonDoc if (objectTypeName == "updateSupergroup") { this->processUpdateSuperGroup(receivedInformation); } if (objectTypeName == "updateChatOnlineMemberCount") { this->processChatOnlineMemberCountUpdated(receivedInformation); } if (objectTypeName == "messages") { this->processMessages(receivedInformation); } + if (objectTypeName == "updateNewMessage") { this->processUpdateNewMessage(receivedInformation); } } void TDLibReceiver::processUpdateOption(const QVariantMap &receivedInformation) @@ -194,6 +195,13 @@ void TDLibReceiver::processChatOnlineMemberCountUpdated(const QVariantMap &recei void TDLibReceiver::processMessages(const QVariantMap &receivedInformation) { - qDebug() << "[TDLibReceiver] Received new messages..." << receivedInformation.value("total_count").toString(); + qDebug() << "[TDLibReceiver] Received new messages, amount: " << receivedInformation.value("total_count").toString(); emit messagesReceived(receivedInformation.value("messages").toList()); } + +void TDLibReceiver::processUpdateNewMessage(const QVariantMap &receivedInformation) +{ + QString chatId = receivedInformation.value("message").toMap().value("chat_id").toString(); + qDebug() << "[TDLibReceiver] Received new message for chat " << chatId; + emit newMessageReceived(chatId, receivedInformation.value("message").toMap()); +} diff --git a/src/tdlibreceiver.h b/src/tdlibreceiver.h index d8791d3..234d8d3 100644 --- a/src/tdlibreceiver.h +++ b/src/tdlibreceiver.h @@ -53,6 +53,7 @@ signals: void superGroupUpdated(const QString &groupId, const QVariantMap &groupInformation); void chatOnlineMemberCountUpdated(const QString &chatId, const int &onlineMemberCount); void messagesReceived(const QVariantList &messages); + void newMessageReceived(const QString &chatId, const QVariantMap &message); private: void *tdLibClient; @@ -76,6 +77,7 @@ private: void processUpdateSuperGroup(const QVariantMap &receivedInformation); void processChatOnlineMemberCountUpdated(const QVariantMap &receivedInformation); void processMessages(const QVariantMap &receivedInformation); + void processUpdateNewMessage(const QVariantMap &receivedInformation); }; #endif // TDLIBRECEIVER_H diff --git a/src/tdlibwrapper.cpp b/src/tdlibwrapper.cpp index c5000eb..4c07529 100644 --- a/src/tdlibwrapper.cpp +++ b/src/tdlibwrapper.cpp @@ -53,6 +53,7 @@ TDLibWrapper::TDLibWrapper(QObject *parent) : QObject(parent) connect(this->tdLibReceiver, SIGNAL(superGroupUpdated(QString, QVariantMap)), this, SLOT(handleSuperGroupUpdated(QString, QVariantMap))); connect(this->tdLibReceiver, SIGNAL(chatOnlineMemberCountUpdated(QString, int)), this, SLOT(handleChatOnlineMemberCountUpdated(QString, int))); connect(this->tdLibReceiver, SIGNAL(messagesReceived(QVariantList)), this, SLOT(handleMessagesReceived(QVariantList))); + connect(this->tdLibReceiver, SIGNAL(newMessageReceived(QString, QVariantMap)), this, SLOT(handleNewMessageReceived(QString, QVariantMap))); this->tdLibReceiver->start(); @@ -390,6 +391,11 @@ void TDLibWrapper::handleMessagesReceived(const QVariantList &messages) emit messagesReceived(messages); } +void TDLibWrapper::handleNewMessageReceived(const QString &chatId, const QVariantMap &message) +{ + emit newMessageReceived(chatId, message); +} + void TDLibWrapper::setInitialParameters() { qDebug() << "[TDLibWrapper] Sending initial parameters to TD Lib"; diff --git a/src/tdlibwrapper.h b/src/tdlibwrapper.h index 98b974c..f8a8849 100644 --- a/src/tdlibwrapper.h +++ b/src/tdlibwrapper.h @@ -97,6 +97,7 @@ signals: void superGroupUpdated(const QString &groupId, const QVariantMap &groupInformation); void chatOnlineMemberCountUpdated(const QString &chatId, const int &onlineMemberCount); void messagesReceived(const QVariantList &messages); + void newMessageReceived(const QString &chatId, const QVariantMap &message); public slots: void handleVersionDetected(const QString &version); @@ -116,6 +117,7 @@ public slots: void handleSuperGroupUpdated(const QString &groupId, const QVariantMap &groupInformation); void handleChatOnlineMemberCountUpdated(const QString &chatId, const int &onlineMemberCount); void handleMessagesReceived(const QVariantList &messages); + void handleNewMessageReceived(const QString &chatId, const QVariantMap &message); private: void *tdLibClient;