React to new messages in chat
This commit is contained in:
parent
59a7c8381d
commit
c472e70775
7 changed files with 61 additions and 13 deletions
|
@ -151,6 +151,9 @@ Page {
|
||||||
onMessagesReceived: {
|
onMessagesReceived: {
|
||||||
chatView.positionViewAtEnd();
|
chatView.positionViewAtEnd();
|
||||||
}
|
}
|
||||||
|
onNewMessageReceived: {
|
||||||
|
chatView.positionViewAtEnd();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Timer {
|
Timer {
|
||||||
|
|
|
@ -7,6 +7,7 @@ ChatModel::ChatModel(TDLibWrapper *tdLibWrapper)
|
||||||
this->tdLibWrapper = tdLibWrapper;
|
this->tdLibWrapper = tdLibWrapper;
|
||||||
this->inReload = false;
|
this->inReload = false;
|
||||||
connect(this->tdLibWrapper, SIGNAL(messagesReceived(QVariantList)), this, SLOT(handleMessagesReceived(QVariantList)));
|
connect(this->tdLibWrapper, SIGNAL(messagesReceived(QVariantList)), this, SLOT(handleMessagesReceived(QVariantList)));
|
||||||
|
connect(this->tdLibWrapper, SIGNAL(newMessageReceived(QString, QVariantMap)), this, SLOT(handleNewMessageReceived(QString, QVariantMap)));
|
||||||
}
|
}
|
||||||
|
|
||||||
ChatModel::~ChatModel()
|
ChatModel::~ChatModel()
|
||||||
|
@ -65,6 +66,7 @@ void ChatModel::handleMessagesReceived(const QVariantList &messages)
|
||||||
{
|
{
|
||||||
qDebug() << "[ChatModel] Receiving new messages :)";
|
qDebug() << "[ChatModel] Receiving new messages :)";
|
||||||
this->messagesMutex.lock();
|
this->messagesMutex.lock();
|
||||||
|
|
||||||
this->messagesToBeAdded.clear();
|
this->messagesToBeAdded.clear();
|
||||||
QListIterator<QVariant> messagesIterator(messages);
|
QListIterator<QVariant> messagesIterator(messages);
|
||||||
while (messagesIterator.hasNext()) {
|
while (messagesIterator.hasNext()) {
|
||||||
|
@ -74,6 +76,39 @@ void ChatModel::handleMessagesReceived(const QVariantList &messages)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::sort(this->messagesToBeAdded.begin(), this->messagesToBeAdded.end(), compareMessages);
|
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()) {
|
if (this->messages.isEmpty()) {
|
||||||
beginResetModel();
|
beginResetModel();
|
||||||
this->messages.append(this->messagesToBeAdded);
|
this->messages.append(this->messagesToBeAdded);
|
||||||
|
@ -88,16 +123,4 @@ void ChatModel::handleMessagesReceived(const QVariantList &messages)
|
||||||
this->insertRows(0, this->messagesToBeAdded.size());
|
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,9 +21,11 @@ public:
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void messagesReceived();
|
void messagesReceived();
|
||||||
|
void newMessageReceived();
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void handleMessagesReceived(const QVariantList &messages);
|
void handleMessagesReceived(const QVariantList &messages);
|
||||||
|
void handleNewMessageReceived(const QString &chatId, const QVariantMap &message);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
@ -34,6 +36,8 @@ private:
|
||||||
QMutex messagesMutex;
|
QMutex messagesMutex;
|
||||||
QString chatId;
|
QString chatId;
|
||||||
bool inReload;
|
bool inReload;
|
||||||
|
|
||||||
|
void insertMessages();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // CHATMODEL_H
|
#endif // CHATMODEL_H
|
||||||
|
|
|
@ -70,6 +70,7 @@ void TDLibReceiver::processReceivedDocument(const QJsonDocument &receivedJsonDoc
|
||||||
if (objectTypeName == "updateSupergroup") { this->processUpdateSuperGroup(receivedInformation); }
|
if (objectTypeName == "updateSupergroup") { this->processUpdateSuperGroup(receivedInformation); }
|
||||||
if (objectTypeName == "updateChatOnlineMemberCount") { this->processChatOnlineMemberCountUpdated(receivedInformation); }
|
if (objectTypeName == "updateChatOnlineMemberCount") { this->processChatOnlineMemberCountUpdated(receivedInformation); }
|
||||||
if (objectTypeName == "messages") { this->processMessages(receivedInformation); }
|
if (objectTypeName == "messages") { this->processMessages(receivedInformation); }
|
||||||
|
if (objectTypeName == "updateNewMessage") { this->processUpdateNewMessage(receivedInformation); }
|
||||||
}
|
}
|
||||||
|
|
||||||
void TDLibReceiver::processUpdateOption(const QVariantMap &receivedInformation)
|
void TDLibReceiver::processUpdateOption(const QVariantMap &receivedInformation)
|
||||||
|
@ -194,6 +195,13 @@ void TDLibReceiver::processChatOnlineMemberCountUpdated(const QVariantMap &recei
|
||||||
|
|
||||||
void TDLibReceiver::processMessages(const QVariantMap &receivedInformation)
|
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());
|
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());
|
||||||
|
}
|
||||||
|
|
|
@ -53,6 +53,7 @@ signals:
|
||||||
void superGroupUpdated(const QString &groupId, const QVariantMap &groupInformation);
|
void superGroupUpdated(const QString &groupId, const QVariantMap &groupInformation);
|
||||||
void chatOnlineMemberCountUpdated(const QString &chatId, const int &onlineMemberCount);
|
void chatOnlineMemberCountUpdated(const QString &chatId, const int &onlineMemberCount);
|
||||||
void messagesReceived(const QVariantList &messages);
|
void messagesReceived(const QVariantList &messages);
|
||||||
|
void newMessageReceived(const QString &chatId, const QVariantMap &message);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void *tdLibClient;
|
void *tdLibClient;
|
||||||
|
@ -76,6 +77,7 @@ private:
|
||||||
void processUpdateSuperGroup(const QVariantMap &receivedInformation);
|
void processUpdateSuperGroup(const QVariantMap &receivedInformation);
|
||||||
void processChatOnlineMemberCountUpdated(const QVariantMap &receivedInformation);
|
void processChatOnlineMemberCountUpdated(const QVariantMap &receivedInformation);
|
||||||
void processMessages(const QVariantMap &receivedInformation);
|
void processMessages(const QVariantMap &receivedInformation);
|
||||||
|
void processUpdateNewMessage(const QVariantMap &receivedInformation);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // TDLIBRECEIVER_H
|
#endif // TDLIBRECEIVER_H
|
||||||
|
|
|
@ -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(superGroupUpdated(QString, QVariantMap)), this, SLOT(handleSuperGroupUpdated(QString, QVariantMap)));
|
||||||
connect(this->tdLibReceiver, SIGNAL(chatOnlineMemberCountUpdated(QString, int)), this, SLOT(handleChatOnlineMemberCountUpdated(QString, int)));
|
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(messagesReceived(QVariantList)), this, SLOT(handleMessagesReceived(QVariantList)));
|
||||||
|
connect(this->tdLibReceiver, SIGNAL(newMessageReceived(QString, QVariantMap)), this, SLOT(handleNewMessageReceived(QString, QVariantMap)));
|
||||||
|
|
||||||
this->tdLibReceiver->start();
|
this->tdLibReceiver->start();
|
||||||
|
|
||||||
|
@ -390,6 +391,11 @@ void TDLibWrapper::handleMessagesReceived(const QVariantList &messages)
|
||||||
emit messagesReceived(messages);
|
emit messagesReceived(messages);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TDLibWrapper::handleNewMessageReceived(const QString &chatId, const QVariantMap &message)
|
||||||
|
{
|
||||||
|
emit newMessageReceived(chatId, message);
|
||||||
|
}
|
||||||
|
|
||||||
void TDLibWrapper::setInitialParameters()
|
void TDLibWrapper::setInitialParameters()
|
||||||
{
|
{
|
||||||
qDebug() << "[TDLibWrapper] Sending initial parameters to TD Lib";
|
qDebug() << "[TDLibWrapper] Sending initial parameters to TD Lib";
|
||||||
|
|
|
@ -97,6 +97,7 @@ signals:
|
||||||
void superGroupUpdated(const QString &groupId, const QVariantMap &groupInformation);
|
void superGroupUpdated(const QString &groupId, const QVariantMap &groupInformation);
|
||||||
void chatOnlineMemberCountUpdated(const QString &chatId, const int &onlineMemberCount);
|
void chatOnlineMemberCountUpdated(const QString &chatId, const int &onlineMemberCount);
|
||||||
void messagesReceived(const QVariantList &messages);
|
void messagesReceived(const QVariantList &messages);
|
||||||
|
void newMessageReceived(const QString &chatId, const QVariantMap &message);
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void handleVersionDetected(const QString &version);
|
void handleVersionDetected(const QString &version);
|
||||||
|
@ -116,6 +117,7 @@ public slots:
|
||||||
void handleSuperGroupUpdated(const QString &groupId, const QVariantMap &groupInformation);
|
void handleSuperGroupUpdated(const QString &groupId, const QVariantMap &groupInformation);
|
||||||
void handleChatOnlineMemberCountUpdated(const QString &chatId, const int &onlineMemberCount);
|
void handleChatOnlineMemberCountUpdated(const QString &chatId, const int &onlineMemberCount);
|
||||||
void handleMessagesReceived(const QVariantList &messages);
|
void handleMessagesReceived(const QVariantList &messages);
|
||||||
|
void handleNewMessageReceived(const QString &chatId, const QVariantMap &message);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void *tdLibClient;
|
void *tdLibClient;
|
||||||
|
|
Loading…
Reference in a new issue