Skeleton for chat model

This commit is contained in:
Sebastian J. Wolf 2020-08-22 17:30:02 +02:00
parent 8dc8dd3651
commit 2750764de9
9 changed files with 104 additions and 0 deletions

View file

@ -16,6 +16,7 @@ CONFIG += sailfishapp sailfishapp_i18n
SOURCES += src/harbour-fernschreiber.cpp \ SOURCES += src/harbour-fernschreiber.cpp \
src/chatlistmodel.cpp \ src/chatlistmodel.cpp \
src/chatmodel.cpp \
src/tdlibreceiver.cpp \ src/tdlibreceiver.cpp \
src/tdlibwrapper.cpp src/tdlibwrapper.cpp
@ -76,6 +77,7 @@ INSTALLS += telegram 86.png 108.png 128.png 172.png 256.png \
HEADERS += \ HEADERS += \
src/chatlistmodel.h \ src/chatlistmodel.h \
src/chatmodel.h \
src/tdlibreceiver.h \ src/tdlibreceiver.h \
src/tdlibsecrets.h \ src/tdlibsecrets.h \
src/tdlibwrapper.h src/tdlibwrapper.h

View file

@ -101,6 +101,7 @@ Page {
isChannel = chatGroupInformation.is_channel; isChannel = chatGroupInformation.is_channel;
updateGroupStatusText(); updateGroupStatusText();
} }
tdLibWrapper.getChatHistory(chatInformation.id);
chatPage.loading = false; chatPage.loading = false;
} }

37
src/chatmodel.cpp Normal file
View file

@ -0,0 +1,37 @@
#include "chatmodel.h"
ChatModel::ChatModel(TDLibWrapper *tdLibWrapper)
{
this->tdLibWrapper = tdLibWrapper;
}
ChatModel::~ChatModel()
{
}
int ChatModel::rowCount(const QModelIndex &) const
{
return messages.size();
}
QVariant ChatModel::data(const QModelIndex &index, int role) const
{
if(index.isValid() && role == Qt::DisplayRole) {
return QVariant(messages.value(index.row()));
}
return QVariant();
}
bool ChatModel::insertRows(int row, int count, const QModelIndex &parent)
{
qDebug() << "[ChatModel] Inserting at " << row << ", row count: " << count;
beginInsertRows(parent, row, row + count - 1);
this->messages.insert(row, this->messagesToBeAdded);
this->messageIndexMap.clear();
for (int i = 0; i < this->messages.size(); i++) {
this->messageIndexMap.insert(this->messages.at(i).toMap().value("id").toString(), i);
}
endInsertRows();
return true;
}

29
src/chatmodel.h Normal file
View file

@ -0,0 +1,29 @@
#ifndef CHATMODEL_H
#define CHATMODEL_H
#include <QAbstractListModel>
#include <QDebug>
#include <QMutex>
#include "tdlibwrapper.h"
class ChatModel : public QAbstractListModel
{
Q_OBJECT
public:
ChatModel(TDLibWrapper *tdLibWrapper);
~ChatModel() override;
virtual int rowCount(const QModelIndex&) const override;
virtual QVariant data(const QModelIndex &index, int role) const override;
virtual bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
private:
TDLibWrapper *tdLibWrapper;
QVariantList messages;
QVariantList messagesToBeAdded;
QVariantMap messageIndexMap;
QMutex chatListMutex;
};
#endif // CHATMODEL_H

View file

@ -31,6 +31,7 @@
#include "tdlibwrapper.h" #include "tdlibwrapper.h"
#include "chatlistmodel.h" #include "chatlistmodel.h"
#include "chatmodel.h"
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
@ -46,6 +47,9 @@ int main(int argc, char *argv[])
ChatListModel chatListModel(tdLibWrapper); ChatListModel chatListModel(tdLibWrapper);
context->setContextProperty("chatListModel", &chatListModel); context->setContextProperty("chatListModel", &chatListModel);
ChatModel chatModel(tdLibWrapper);
context->setContextProperty("chatModel", &chatModel);
view->setSource(SailfishApp::pathTo("qml/harbour-fernschreiber.qml")); view->setSource(SailfishApp::pathTo("qml/harbour-fernschreiber.qml"));
view->show(); view->show();
return app->exec(); return app->exec();

View file

@ -69,6 +69,7 @@ void TDLibReceiver::processReceivedDocument(const QJsonDocument &receivedJsonDoc
if (objectTypeName == "updateBasicGroup") { this->processUpdateBasicGroup(receivedInformation); } if (objectTypeName == "updateBasicGroup") { this->processUpdateBasicGroup(receivedInformation); }
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); }
} }
void TDLibReceiver::processUpdateOption(const QVariantMap &receivedInformation) void TDLibReceiver::processUpdateOption(const QVariantMap &receivedInformation)
@ -190,3 +191,9 @@ void TDLibReceiver::processChatOnlineMemberCountUpdated(const QVariantMap &recei
qDebug() << "[TDLibReceiver] Online member count updated for chat " << chatId; qDebug() << "[TDLibReceiver] Online member count updated for chat " << chatId;
emit chatOnlineMemberCountUpdated(chatId, receivedInformation.value("online_member_count").toInt()); emit chatOnlineMemberCountUpdated(chatId, receivedInformation.value("online_member_count").toInt());
} }
void TDLibReceiver::processMessages(const QVariantMap &receivedInformation)
{
qDebug() << "[TDLibReceiver] Received new messages..." << receivedInformation.value("total_count").toString();
emit messagesReceived(receivedInformation.value("messages").toList());
}

View file

@ -52,6 +52,7 @@ signals:
void basicGroupUpdated(const QString &groupId, const QVariantMap &groupInformation); void basicGroupUpdated(const QString &groupId, const QVariantMap &groupInformation);
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);
private: private:
void *tdLibClient; void *tdLibClient;
@ -74,6 +75,7 @@ private:
void processUpdateBasicGroup(const QVariantMap &receivedInformation); void processUpdateBasicGroup(const QVariantMap &receivedInformation);
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);
}; };
#endif // TDLIBRECEIVER_H #endif // TDLIBRECEIVER_H

View file

@ -52,6 +52,7 @@ TDLibWrapper::TDLibWrapper(QObject *parent) : QObject(parent)
connect(this->tdLibReceiver, SIGNAL(basicGroupUpdated(QString, QVariantMap)), this, SLOT(handleBasicGroupUpdated(QString, QVariantMap))); connect(this->tdLibReceiver, SIGNAL(basicGroupUpdated(QString, QVariantMap)), this, SLOT(handleBasicGroupUpdated(QString, QVariantMap)));
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)));
this->tdLibReceiver->start(); this->tdLibReceiver->start();
@ -152,6 +153,19 @@ void TDLibWrapper::closeChat(const QString &chatId)
this->sendRequest(requestObject); this->sendRequest(requestObject);
} }
void TDLibWrapper::getChatHistory(const QString &chatId, const qlonglong &fromMessageId, const int &offset, const int &limit, const bool &onlyLocal)
{
qDebug() << "[TDLibWrapper] Retrieving chat history " << chatId << fromMessageId << offset << limit << onlyLocal;
QVariantMap requestObject;
requestObject.insert("@type", "getChatHistory");
requestObject.insert("chat_id", chatId);
requestObject.insert("from_message_id", fromMessageId);
requestObject.insert("offset", offset);
requestObject.insert("limit", limit);
requestObject.insert("only_local", onlyLocal);
this->sendRequest(requestObject);
}
QVariantMap TDLibWrapper::getUserInformation() QVariantMap TDLibWrapper::getUserInformation()
{ {
return this->userInformation; return this->userInformation;
@ -358,6 +372,11 @@ void TDLibWrapper::handleChatOnlineMemberCountUpdated(const QString &chatId, con
emit chatOnlineMemberCountUpdated(chatId, onlineMemberCount); emit chatOnlineMemberCountUpdated(chatId, onlineMemberCount);
} }
void TDLibWrapper::handleMessagesReceived(const QVariantList &messages)
{
emit messagesReceived(messages);
}
void TDLibWrapper::setInitialParameters() void TDLibWrapper::setInitialParameters()
{ {
qDebug() << "[TDLibWrapper] Sending initial parameters to TD Lib"; qDebug() << "[TDLibWrapper] Sending initial parameters to TD Lib";

View file

@ -76,6 +76,7 @@ public:
Q_INVOKABLE void downloadFile(const QString &fileId); Q_INVOKABLE void downloadFile(const QString &fileId);
Q_INVOKABLE void openChat(const QString &chatId); Q_INVOKABLE void openChat(const QString &chatId);
Q_INVOKABLE void closeChat(const QString &chatId); Q_INVOKABLE void closeChat(const QString &chatId);
Q_INVOKABLE void getChatHistory(const QString &chatId, const qlonglong &fromMessageId = 0, const int &offset = 0, const int &limit = 50, const bool &onlyLocal = false);
signals: signals:
void versionDetected(const QString &version); void versionDetected(const QString &version);
@ -94,6 +95,7 @@ signals:
void basicGroupUpdated(const QString &groupId, const QVariantMap &groupInformation); void basicGroupUpdated(const QString &groupId, const QVariantMap &groupInformation);
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);
public slots: public slots:
void handleVersionDetected(const QString &version); void handleVersionDetected(const QString &version);
@ -112,6 +114,7 @@ public slots:
void handleBasicGroupUpdated(const QString &groupId, const QVariantMap &groupInformation); void handleBasicGroupUpdated(const QString &groupId, const QVariantMap &groupInformation);
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);
private: private:
void *tdLibClient; void *tdLibClient;