Merge pull request #160 from monich/updateChatPhoto

Update chat photo
This commit is contained in:
Sebastian Wolf 2020-11-15 17:19:57 +01:00 committed by GitHub
commit 1c82072c15
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 195 additions and 127 deletions

View file

@ -520,7 +520,7 @@ Page {
ProfileThumbnail {
id: chatPictureThumbnail
photoData: (typeof chatInformation.photo !== "undefined") ? chatInformation.photo.small : ""
photoData: chatModel.smallPhoto
replacementStringHint: chatNameText.text
width: chatOverviewColumn.height
height: chatOverviewColumn.height

View file

@ -295,6 +295,7 @@ ChatListModel::ChatListModel(TDLibWrapper *tdLibWrapper) : showHiddenChats(false
connect(tdLibWrapper, SIGNAL(chatOrderUpdated(QString, QString)), this, SLOT(handleChatOrderUpdated(QString, QString)));
connect(tdLibWrapper, SIGNAL(chatReadInboxUpdated(QString, QString, int)), this, SLOT(handleChatReadInboxUpdated(QString, QString, int)));
connect(tdLibWrapper, SIGNAL(chatReadOutboxUpdated(QString, QString)), this, SLOT(handleChatReadOutboxUpdated(QString, QString)));
connect(tdLibWrapper, SIGNAL(chatPhotoUpdated(qlonglong, QVariantMap)), this, SLOT(handleChatPhotoUpdated(qlonglong, QVariantMap)));
connect(tdLibWrapper, SIGNAL(messageSendSucceeded(QString, QString, QVariantMap)), this, SLOT(handleMessageSendSucceeded(QString, QString, QVariantMap)));
connect(tdLibWrapper, SIGNAL(chatNotificationSettingsUpdated(QString, QVariantMap)), this, SLOT(handleChatNotificationSettingsUpdated(QString, QVariantMap)));
connect(tdLibWrapper, SIGNAL(superGroupUpdated(qlonglong)), this, SLOT(handleGroupUpdated(qlonglong)));
@ -614,6 +615,26 @@ void ChatListModel::handleChatReadOutboxUpdated(const QString &id, const QString
}
}
void ChatListModel::handleChatPhotoUpdated(qlonglong chatId, const QVariantMap &photo)
{
if (chatIndexMap.contains(chatId)) {
LOG("Updating chat photo for" << chatId);
const int chatIndex = chatIndexMap.value(chatId);
ChatData *chat = chatList.at(chatIndex);
chat->chatData.insert(PHOTO, photo);
QVector<int> changedRoles;
changedRoles.append(ChatData::RolePhotoSmall);
const QModelIndex modelIndex(index(chatIndex));
emit dataChanged(modelIndex, modelIndex, changedRoles);
} else {
ChatData *chat = hiddenChats.value(chatId);
if (chat) {
LOG("Updating photo for hidden chat" << chatId);
chat->chatData.insert(PHOTO, photo);
}
}
}
void ChatListModel::handleMessageSendSucceeded(const QString &messageId, const QString &oldMessageId, const QVariantMap &message)
{
bool ok;

View file

@ -48,6 +48,7 @@ private slots:
void handleChatOrderUpdated(const QString &chatId, const QString &order);
void handleChatReadInboxUpdated(const QString &chatId, const QString &lastReadInboxMessageId, int unreadCount);
void handleChatReadOutboxUpdated(const QString &chatId, const QString &lastReadOutboxMessageId);
void handleChatPhotoUpdated(qlonglong chatId, const QVariantMap &photo);
void handleMessageSendSucceeded(const QString &messageId, const QString &oldMessageId, const QVariantMap &message);
void handleChatNotificationSettingsUpdated(const QString &chatId, const QVariantMap &chatNotificationSettings);
void handleGroupUpdated(qlonglong groupId);

View file

@ -23,24 +23,35 @@
#include <QByteArray>
#include <QBitArray>
ChatModel::ChatModel(TDLibWrapper *tdLibWrapper)
#define LOG(x) qDebug() << "[ChatModel]" << x
namespace {
const QString ID("id");
const QString CHAT_ID("chat_id");
const QString PHOTO("photo");
const QString SMALL("small");
}
ChatModel::ChatModel(TDLibWrapper *tdLibWrapper) :
chatId(0),
inReload(false),
inIncrementalUpdate(false)
{
this->tdLibWrapper = tdLibWrapper;
this->inReload = false;
this->inIncrementalUpdate = false;
connect(this->tdLibWrapper, SIGNAL(messagesReceived(QVariantList, int)), this, SLOT(handleMessagesReceived(QVariantList, int)));
connect(this->tdLibWrapper, SIGNAL(newMessageReceived(QString, QVariantMap)), this, SLOT(handleNewMessageReceived(QString, QVariantMap)));
connect(this->tdLibWrapper, SIGNAL(chatReadInboxUpdated(QString, QString, int)), this, SLOT(handleChatReadInboxUpdated(QString, QString, int)));
connect(this->tdLibWrapper, SIGNAL(chatReadOutboxUpdated(QString, QString)), this, SLOT(handleChatReadOutboxUpdated(QString, QString)));
connect(this->tdLibWrapper, SIGNAL(messageSendSucceeded(QString, QString, QVariantMap)), this, SLOT(handleMessageSendSucceeded(QString, QString, QVariantMap)));
connect(this->tdLibWrapper, SIGNAL(chatNotificationSettingsUpdated(QString, QVariantMap)), this, SLOT(handleChatNotificationSettingsUpdated(QString, QVariantMap)));
connect(this->tdLibWrapper, SIGNAL(chatPhotoUpdated(qlonglong, QVariantMap)), this, SLOT(handleChatPhotoUpdated(qlonglong, QVariantMap)));
connect(this->tdLibWrapper, SIGNAL(messageContentUpdated(QString, QString, QVariantMap)), this, SLOT(handleMessageContentUpdated(QString, QString, QVariantMap)));
connect(this->tdLibWrapper, SIGNAL(messagesDeleted(QString, QVariantList)), this, SLOT(handleMessagesDeleted(QString, QVariantList)));
}
ChatModel::~ChatModel()
{
qDebug() << "[ChatModel] Destroying myself...";
LOG("Destroying myself...");
}
int ChatModel::rowCount(const QModelIndex &) const
@ -58,7 +69,7 @@ QVariant ChatModel::data(const QModelIndex &index, int role) const
bool ChatModel::insertRows(int row, int count, const QModelIndex &parent)
{
qDebug() << "[ChatModel] Inserting at " << row << ", row count: " << count;
LOG("Inserting at" << row << ", row count:" << count);
beginInsertRows(parent, row, row + count - 1);
for (int i = 0; i < count; i++) {
this->messages.insert(row + i, this->messagesToBeAdded.at(i));
@ -70,23 +81,24 @@ bool ChatModel::insertRows(int row, int count, const QModelIndex &parent)
void ChatModel::initialize(const QVariantMap &chatInformation)
{
qDebug() << "[ChatModel] Initializing chat model...";
this->chatInformation = chatInformation;
LOG("Initializing chat model...");
beginResetModel();
this->chatInformation = chatInformation;
this->chatId = chatInformation.value(ID).toLongLong();
this->messages.clear();
this->messageIndexMap.clear();
this->messagesToBeAdded.clear();
endResetModel();
this->chatId = chatInformation.value("id").toString();
tdLibWrapper->getChatHistory(this->chatId);
emit smallPhotoChanged();
tdLibWrapper->getChatHistory(chatId);
}
void ChatModel::triggerLoadMoreHistory()
{
if (!this->inIncrementalUpdate && !messages.isEmpty()) {
qDebug() << "[ChatModel] Trigger loading older history...";
LOG("Trigger loading older history...");
this->inIncrementalUpdate = true;
this->tdLibWrapper->getChatHistory(this->chatId, this->messages.first().toMap().value("id").toLongLong());
this->tdLibWrapper->getChatHistory(chatId, messages.first().toMap().value(ID).toLongLong());
}
}
@ -95,7 +107,7 @@ QVariantMap ChatModel::getChatInformation()
return this->chatInformation;
}
QVariantMap ChatModel::getMessage(const int &index)
QVariantMap ChatModel::getMessage(int index)
{
if (index < this->messages.size()) {
return this->messages.at(index).toMap();
@ -104,23 +116,24 @@ QVariantMap ChatModel::getMessage(const int &index)
}
}
bool compareMessages(const QVariant &message1, const QVariant &message2)
QVariantMap ChatModel::smallPhoto() const
{
QVariantMap messageMap1 = message1.toMap();
QVariantMap messageMap2 = message2.toMap();
if (messageMap1.value("id").toLongLong() < messageMap2.value("id").toLongLong()) {
return true;
} else {
return false;
}
return chatInformation.value(PHOTO).toMap().value(SMALL).toMap();
}
void ChatModel::handleMessagesReceived(const QVariantList &messages, const int &totalCount)
static bool compareMessages(const QVariant &message1, const QVariant &message2)
{
qDebug() << "[ChatModel] Receiving new messages :)" << messages.size();
const QVariantMap messageMap1 = message1.toMap();
const QVariantMap messageMap2 = message2.toMap();
return messageMap1.value(ID).toLongLong() < messageMap2.value(ID).toLongLong();
}
void ChatModel::handleMessagesReceived(const QVariantList &messages, int totalCount)
{
LOG("Receiving new messages :)" << messages.size());
if (messages.size() == 0) {
qDebug() << "[ChatModel] No additional messages loaded, notifying chat UI...";
LOG("No additional messages loaded, notifying chat UI...");
this->inReload = false;
int listInboxPosition = this->calculateLastKnownMessageId();
int listOutboxPosition = this->calculateLastReadSentMessageId();
@ -136,7 +149,7 @@ void ChatModel::handleMessagesReceived(const QVariantList &messages, const int &
QListIterator<QVariant> messagesIterator(messages);
while (messagesIterator.hasNext()) {
QVariantMap currentMessage = messagesIterator.next().toMap();
if (currentMessage.value("chat_id").toString() == this->chatId) {
if (currentMessage.value(CHAT_ID).toLongLong() == chatId) {
this->messagesToBeAdded.append(currentMessage);
}
}
@ -147,11 +160,11 @@ void ChatModel::handleMessagesReceived(const QVariantList &messages, const int &
// First call only returns a few messages, we need to get a little more than that...
if (this->messagesToBeAdded.size() < 10 && !this->inReload) {
qDebug() << "[ChatModel] Only a few messages received in first call, loading more...";
LOG("Only a few messages received in first call, loading more...");
this->inReload = true;
this->tdLibWrapper->getChatHistory(this->chatId, this->messagesToBeAdded.first().toMap().value("id").toLongLong());
this->tdLibWrapper->getChatHistory(this->chatId, this->messagesToBeAdded.first().toMap().value(ID).toLongLong());
} else {
qDebug() << "[ChatModel] Messages loaded, notifying chat UI...";
LOG("Messages loaded, notifying chat UI...");
this->inReload = false;
int listInboxPosition = this->calculateLastKnownMessageId();
int listOutboxPosition = this->calculateLastReadSentMessageId();
@ -166,10 +179,10 @@ void ChatModel::handleMessagesReceived(const QVariantList &messages, const int &
}
void ChatModel::handleNewMessageReceived(const QString &chatId, const QVariantMap &message)
void ChatModel::handleNewMessageReceived(const QString &id, const QVariantMap &message)
{
if (chatId == this->chatId) {
qDebug() << "[ChatModel] New message received for this chat";
if (id.toLongLong() == chatId) {
LOG("New message received for this chat");
this->messagesMutex.lock();
this->messagesToBeAdded.clear();
@ -181,83 +194,92 @@ void ChatModel::handleNewMessageReceived(const QString &chatId, const QVariantMa
}
}
void ChatModel::handleChatReadInboxUpdated(const QString &chatId, const QString &lastReadInboxMessageId, const int &unreadCount)
void ChatModel::handleChatReadInboxUpdated(const QString &id, const QString &lastReadInboxMessageId, int unreadCount)
{
if (chatId == this->chatId) {
qDebug() << "[ChatModel] Updating chat unread count, unread messages " << unreadCount << ", last read message ID: " << lastReadInboxMessageId;
if (id.toLongLong() == chatId) {
LOG("Updating chat unread count, unread messages" << unreadCount << ", last read message ID:" << lastReadInboxMessageId);
this->chatInformation.insert("unread_count", unreadCount);
this->chatInformation.insert("last_read_inbox_message_id", lastReadInboxMessageId);
emit unreadCountUpdated(unreadCount, lastReadInboxMessageId);
}
}
void ChatModel::handleChatReadOutboxUpdated(const QString &chatId, const QString &lastReadOutboxMessageId)
void ChatModel::handleChatReadOutboxUpdated(const QString &id, const QString &lastReadOutboxMessageId)
{
if (chatId == this->chatId) {
if (id.toLongLong() == chatId) {
this->chatInformation.insert("last_read_outbox_message_id", lastReadOutboxMessageId);
int sentIndex = calculateLastReadSentMessageId();
qDebug() << "[ChatModel] Updating sent message ID, new index " << sentIndex;
LOG("Updating sent message ID, new index" << sentIndex);
emit lastReadSentMessageUpdated(sentIndex);
}
}
void ChatModel::handleMessageSendSucceeded(const QString &messageId, const QString &oldMessageId, const QVariantMap &message)
{
qDebug() << "[ChatModel] Message send succeeded, new message ID " << messageId << "old message ID " << oldMessageId << ", chat ID" << message.value("chat_id").toString();
qDebug() << "[ChatModel] index map: " << this->messageIndexMap.contains(oldMessageId) << ", index count: " << this->messageIndexMap.size() << ", message count: " << this->messages.size();
LOG("Message send succeeded, new message ID" << messageId << "old message ID" << oldMessageId << ", chat ID" << message.value(CHAT_ID).toString());
LOG("index map:" << messageIndexMap.contains(oldMessageId) << ", index count:" << messageIndexMap.size() << ", message count:" << messages.size());
if (this->messageIndexMap.contains(oldMessageId)) {
this->messagesMutex.lock();
qDebug() << "[ChatModel] Message was successfully sent " << oldMessageId;
LOG("Message was successfully sent" << oldMessageId);
int messageIndex = this->messageIndexMap.value(oldMessageId).toInt();
this->messages.replace(messageIndex, message);
this->calculateMessageIndexMap();
qDebug() << "[ChatModel] Message was replaced at index " << messageIndex;
LOG("Message was replaced at index" << messageIndex);
this->messagesMutex.unlock();
emit dataChanged(index(messageIndex), index(messageIndex));
emit lastReadSentMessageUpdated(calculateLastReadSentMessageId());
}
}
void ChatModel::handleChatNotificationSettingsUpdated(const QString &chatId, const QVariantMap &chatNotificationSettings)
void ChatModel::handleChatNotificationSettingsUpdated(const QString &id, const QVariantMap &chatNotificationSettings)
{
if (chatId == this->chatId) {
if (id.toLongLong() == chatId) {
this->chatInformation.insert("notification_settings", chatNotificationSettings);
qDebug() << "[ChatModel] Notification settings updated";
LOG("Notification settings updated");
emit notificationSettingsUpdated();
}
}
void ChatModel::handleMessageContentUpdated(const QString &chatId, const QString &messageId, const QVariantMap &newContent)
void ChatModel::handleChatPhotoUpdated(qlonglong id, const QVariantMap &photo)
{
qDebug() << "[ChatModel] Message content updated" << chatId << messageId;
if (chatId == this->chatId && this->messageIndexMap.contains(messageId)) {
if (id == chatId) {
LOG("Chat photo updated" << chatId);
chatInformation.insert(PHOTO, photo);
emit smallPhotoChanged();
}
}
void ChatModel::handleMessageContentUpdated(const QString &id, const QString &messageId, const QVariantMap &newContent)
{
LOG("Message content updated" << id << messageId);
if (id.toLongLong() == chatId && messageIndexMap.contains(messageId)) {
this->messagesMutex.lock();
qDebug() << "[ChatModel] We know the message that was updated " << messageId;
LOG("We know the message that was updated " << messageId);
int messageIndex = this->messageIndexMap.value(messageId).toInt();
QVariantMap messageToBeUpdated = this->messages.at(messageIndex).toMap();
messageToBeUpdated.insert("content", newContent);
this->messages.replace(messageIndex, messageToBeUpdated);
this->calculateMessageIndexMap();
qDebug() << "[ChatModel] Message was replaced at index " << messageIndex;
LOG("Message was replaced at index" << messageIndex);
this->messagesMutex.unlock();
emit messageUpdated(messageIndex);
emit dataChanged(index(messageIndex), index(messageIndex));
}
}
void ChatModel::handleMessagesDeleted(const QString &chatId, const QVariantList &messageIds)
void ChatModel::handleMessagesDeleted(const QString &id, const QVariantList &messageIds)
{
qDebug() << "[ChatModel] Messages were deleted in a chat" << chatId;
if (chatId == this->chatId) {
LOG("Messages were deleted in a chat" << id);
if (id.toLongLong() == chatId) {
this->messagesMutex.lock();
qDebug() << "[ChatModel] Messages in this chat were deleted...";
LOG("Messages in this chat were deleted...");
QListIterator<QVariant> messageIdIterator(messageIds);
while (messageIdIterator.hasNext()) {
QString messageId = messageIdIterator.next().toString();
if (this->messageIndexMap.contains(messageId)) {
int messageIndex = this->messageIndexMap.value(messageId).toInt();
beginRemoveRows(QModelIndex(), messageIndex, messageIndex);
qDebug() << "[ChatModel] ...and we even know this message!" << messageId << messageIndex;
LOG("...and we even know this message!" << messageId << messageIndex);
this->messages.removeAt(messageIndex);
this->calculateMessageIndexMap();
endRemoveRows();
@ -277,7 +299,7 @@ void ChatModel::insertMessages()
endResetModel();
} else {
// There is only an append or a prepend, tertium non datur! (probably ;))
if (this->messages.last().toMap().value("id").toLongLong() < this->messagesToBeAdded.first().toMap().value("id").toLongLong()) {
if (this->messages.last().toMap().value(ID).toLongLong() < this->messagesToBeAdded.first().toMap().value(ID).toLongLong()) {
// Append
this->insertRows(rowCount(QModelIndex()), this->messagesToBeAdded.size());
} else {
@ -320,36 +342,36 @@ QVariantMap ChatModel::enhanceMessage(const QVariantMap &message)
int ChatModel::calculateLastKnownMessageId()
{
qDebug() << "[ChatModel] calculateLastKnownMessageId";
LOG("calculateLastKnownMessageId");
QString lastKnownMessageId = this->chatInformation.value("last_read_inbox_message_id").toString();
qDebug() << "[ChatModel] lastKnownMessageId" << lastKnownMessageId;
qDebug() << "[ChatModel] size messageIndexMap" << this->messageIndexMap.size();
qDebug() << "[ChatModel] contains ID?" << this->messageIndexMap.contains(lastKnownMessageId);
LOG("lastKnownMessageId" << lastKnownMessageId);
LOG("size messageIndexMap" << messageIndexMap.size());
LOG("contains ID?" << messageIndexMap.contains(lastKnownMessageId));
int listInboxPosition = this->messageIndexMap.value(lastKnownMessageId, this->messages.size() - 1).toInt();
if (listInboxPosition > this->messages.size() - 1 ) {
listInboxPosition = this->messages.size() - 1;
}
qDebug() << "[ChatModel] Last known message is at position" << listInboxPosition;
LOG("Last known message is at position" << listInboxPosition);
return listInboxPosition;
}
int ChatModel::calculateLastReadSentMessageId()
{
qDebug() << "[ChatModel] calculateLastReadSentMessageId";
LOG("calculateLastReadSentMessageId");
QString lastReadSentMessageId = this->chatInformation.value("last_read_outbox_message_id").toString();
qDebug() << "[ChatModel] lastReadSentMessageId" << lastReadSentMessageId;
qDebug() << "[ChatModel] size messageIndexMap" << this->messageIndexMap.size();
qDebug() << "[ChatModel] contains ID?" << this->messageIndexMap.contains(lastReadSentMessageId);
LOG("lastReadSentMessageId" << lastReadSentMessageId);
LOG("size messageIndexMap" << messageIndexMap.size());
LOG("contains ID?" << messageIndexMap.contains(lastReadSentMessageId));
int listOutboxPosition = this->messageIndexMap.value(lastReadSentMessageId, this->messages.size() - 1).toInt();
qDebug() << "[ChatModel] Last read sent message is at position" << listOutboxPosition;
LOG("Last read sent message is at position" << listOutboxPosition);
return listOutboxPosition;
}
void ChatModel::calculateMessageIndexMap()
{
qDebug() << "[ChatModel] calculateMessageIndexMap";
LOG("calculateMessageIndexMap");
this->messageIndexMap.clear();
for (int i = 0; i < this->messages.size(); i++) {
this->messageIndexMap.insert(this->messages.at(i).toMap().value("id").toString(), i);
this->messageIndexMap.insert(this->messages.at(i).toMap().value(ID).toString(), i);
}
}

View file

@ -28,6 +28,8 @@
class ChatModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(QVariantMap smallPhoto READ smallPhoto NOTIFY smallPhotoChanged)
public:
ChatModel(TDLibWrapper *tdLibWrapper);
~ChatModel() override;
@ -36,40 +38,43 @@ public:
virtual QVariant data(const QModelIndex &index, int role) const override;
virtual bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
Q_INVOKABLE void initialize(const QVariantMap &chatInformation);
Q_INVOKABLE void triggerLoadMoreHistory();
Q_INVOKABLE QVariantMap getChatInformation();
Q_INVOKABLE QVariantMap getMessage(const int &index);
Q_INVOKABLE QVariantMap getMessage(int index);
QVariantMap smallPhoto() const;
signals:
void messagesReceived(const int &modelIndex, const int &lastReadSentIndex, const int &totalCount);
void messagesIncrementalUpdate(const int &modelIndex, const int &lastReadSentIndex);
void messagesReceived(int modelIndex, int lastReadSentIndex, int totalCount);
void messagesIncrementalUpdate(int modelIndex, int lastReadSentIndex);
void newMessageReceived(const QVariantMap &message);
void unreadCountUpdated(const int &unreadCount, const QString &lastReadInboxMessageId);
void lastReadSentMessageUpdated(const int &lastReadSentIndex);
void unreadCountUpdated(int unreadCount, const QString &lastReadInboxMessageId);
void lastReadSentMessageUpdated(int lastReadSentIndex);
void notificationSettingsUpdated();
void messageUpdated(const int &modelIndex);
void messageUpdated(int modelIndex);
void messagesDeleted();
void smallPhotoChanged();
public slots:
void handleMessagesReceived(const QVariantList &messages, const int &totalCount);
void handleMessagesReceived(const QVariantList &messages, int totalCount);
void handleNewMessageReceived(const QString &chatId, const QVariantMap &message);
void handleChatReadInboxUpdated(const QString &chatId, const QString &lastReadInboxMessageId, const int &unreadCount);
void handleChatReadInboxUpdated(const QString &chatId, const QString &lastReadInboxMessageId, int unreadCount);
void handleChatReadOutboxUpdated(const QString &chatId, const QString &lastReadOutboxMessageId);
void handleMessageSendSucceeded(const QString &messageId, const QString &oldMessageId, const QVariantMap &message);
void handleChatNotificationSettingsUpdated(const QString &chatId, const QVariantMap &chatNotificationSettings);
void handleChatPhotoUpdated(qlonglong chatId, const QVariantMap &photo);
void handleMessageContentUpdated(const QString &chatId, const QString &messageId, const QVariantMap &newContent);
void handleMessagesDeleted(const QString &chatId, const QVariantList &messageIds);
private:
TDLibWrapper *tdLibWrapper;
QVariantList messages;
QVariantList messagesToBeAdded;
QVariantMap messageIndexMap;
QMutex messagesMutex;
QVariantMap chatInformation;
QString chatId;
qlonglong chatId;
bool inReload;
bool inIncrementalUpdate;

View file

@ -30,12 +30,22 @@ namespace {
const QString ID("id");
const QString LIST("list");
const QString CHAT_ID("chat_id");
const QString USER_ID("user_id");
const QString MESSAGE_ID("message_id");
const QString MESSAGE_IDS("message_ids");
const QString MESSAGE("message");
const QString MESSAGES("messages");
const QString TITLE("title");
const QString NAME("name");
const QString VALUE("value");
const QString POSITION("position");
const QString POSITIONS("positions");
const QString PHOTO("photo");
const QString ORDER("order");
const QString BASIC_GROUP("basic_group");
const QString SUPERGROUP("supergroup");
const QString LAST_MESSAGE("last_message");
const QString TOTAL_COUNT("total_count");
const QString UNREAD_COUNT("unread_count");
const QString LAST_READ_INBOX_MESSAGE_ID("last_read_inbox_message_id");
const QString LAST_READ_OUTBOX_MESSAGE_ID("last_read_outbox_message_id");
@ -115,9 +125,11 @@ TDLibReceiver::TDLibReceiver(void *tdLibClient, QObject *parent) : QThread(paren
handlers.insert("updateSupergroupFullInfo", &TDLibReceiver::processUpdateSupergroupFullInfo);
handlers.insert("userProfilePhotos", &TDLibReceiver::processUserProfilePhotos);
handlers.insert("updateChatPermissions", &TDLibReceiver::processUpdateChatPermissions);
handlers.insert("updateChatPhoto", &TDLibReceiver::processUpdateChatPhoto);
handlers.insert("updateChatTitle", &TDLibReceiver::processUpdateChatTitle);
handlers.insert("users", &TDLibReceiver::processUsers);
handlers.insert("error", &TDLibReceiver::processError);
handlers.insert("ok", &TDLibReceiver::nop);
}
void TDLibReceiver::setActive(bool active)
@ -148,7 +160,7 @@ void TDLibReceiver::receiverLoop()
void TDLibReceiver::processReceivedDocument(const QJsonDocument &receivedJsonDocument)
{
QVariantMap receivedInformation = receivedJsonDocument.object().toVariantMap();
QString objectTypeName = receivedInformation.value("@type").toString();
QString objectTypeName = receivedInformation.value(TYPE).toString();
Handler handler = handlers.value(objectTypeName);
if (handler) {
@ -160,28 +172,28 @@ void TDLibReceiver::processReceivedDocument(const QJsonDocument &receivedJsonDoc
void TDLibReceiver::processUpdateOption(const QVariantMap &receivedInformation)
{
QString currentOption = receivedInformation.value("name").toString();
const QString currentOption = receivedInformation.value(NAME).toString();
const QVariant value = receivedInformation.value(VALUE).toMap().value(VALUE);
if (currentOption == "version") {
QString detectedVersion = receivedInformation.value("value").toMap().value("value").toString();
QString detectedVersion = value.toString();
LOG("TD Lib version detected: " << detectedVersion);
emit versionDetected(detectedVersion);
} else {
QVariant currentValue = receivedInformation.value("value").toMap().value("value");
LOG("Option updated: " << currentOption << currentValue);
emit optionUpdated(currentOption, currentValue);
LOG("Option updated: " << currentOption << value);
emit optionUpdated(currentOption, value);
}
}
void TDLibReceiver::processUpdateAuthorizationState(const QVariantMap &receivedInformation)
{
QString authorizationState = receivedInformation.value("authorization_state").toMap().value("@type").toString();
QString authorizationState = receivedInformation.value("authorization_state").toMap().value(TYPE).toString();
LOG("Authorization state changed: " << authorizationState);
emit authorizationStateChanged(authorizationState, receivedInformation);
}
void TDLibReceiver::processUpdateConnectionState(const QVariantMap &receivedInformation)
{
QString connectionState = receivedInformation.value("state").toMap().value("@type").toString();
QString connectionState = receivedInformation.value("state").toMap().value(TYPE).toString();
LOG("Connection state changed: " << connectionState);
emit connectionStateChanged(connectionState);
}
@ -189,21 +201,21 @@ void TDLibReceiver::processUpdateConnectionState(const QVariantMap &receivedInfo
void TDLibReceiver::processUpdateUser(const QVariantMap &receivedInformation)
{
QVariantMap userInformation = receivedInformation.value("user").toMap();
// LOG("User was updated: " << userInformation.value("username").toString() << userInformation.value("first_name").toString() << userInformation.value("last_name").toString());
VERBOSE("User was updated: " << userInformation.value("username").toString() << userInformation.value("first_name").toString() << userInformation.value("last_name").toString());
emit userUpdated(userInformation);
}
void TDLibReceiver::processUpdateUserStatus(const QVariantMap &receivedInformation)
{
QString userId = receivedInformation.value("user_id").toString();
const QString userId = receivedInformation.value(USER_ID).toString();
QVariantMap userStatusInformation = receivedInformation.value("status").toMap();
// LOG("User status was updated: " << receivedInformation.value("user_id").toString() << userStatusInformation.value("@type").toString());
VERBOSE("User status was updated: " << receivedInformation.value(USER_ID).toString() << userStatusInformation.value(TYPE).toString());
emit userStatusUpdated(userId, userStatusInformation);
}
void TDLibReceiver::processUpdateFile(const QVariantMap &receivedInformation)
{
QVariantMap fileInformation = receivedInformation.value("file").toMap();
const QVariantMap fileInformation = receivedInformation.value("file").toMap();
LOG("File was updated: " << fileInformation.value(ID).toString());
emit fileUpdated(fileInformation);
}
@ -216,31 +228,31 @@ void TDLibReceiver::processFile(const QVariantMap &receivedInformation)
void TDLibReceiver::processUpdateNewChat(const QVariantMap &receivedInformation)
{
QVariantMap chatInformation = receivedInformation.value("chat").toMap();
LOG("New chat discovered: " << chatInformation.value(ID).toString() << chatInformation.value("title").toString());
const QVariantMap chatInformation = receivedInformation.value("chat").toMap();
LOG("New chat discovered: " << chatInformation.value(ID).toString() << chatInformation.value(TITLE).toString());
emit newChatDiscovered(chatInformation);
}
void TDLibReceiver::processUpdateUnreadMessageCount(const QVariantMap &receivedInformation)
{
QVariantMap messageCountInformation;
messageCountInformation.insert("chat_list_type", receivedInformation.value("chat_list").toMap().value("@type"));
messageCountInformation.insert("unread_count", receivedInformation.value("unread_count"));
messageCountInformation.insert("chat_list_type", receivedInformation.value("chat_list").toMap().value(TYPE));
messageCountInformation.insert(UNREAD_COUNT, receivedInformation.value(UNREAD_COUNT));
messageCountInformation.insert("unread_unmuted_count", receivedInformation.value("unread_unmuted_count"));
LOG("Unread message count updated: " << messageCountInformation.value("chat_list_type").toString() << messageCountInformation.value("unread_count").toString());
LOG("Unread message count updated: " << messageCountInformation.value("chat_list_type").toString() << messageCountInformation.value(UNREAD_COUNT).toString());
emit unreadMessageCountUpdated(messageCountInformation);
}
void TDLibReceiver::processUpdateUnreadChatCount(const QVariantMap &receivedInformation)
{
QVariantMap chatCountInformation;
chatCountInformation.insert("chat_list_type", receivedInformation.value("chat_list").toMap().value("@type"));
chatCountInformation.insert("chat_list_type", receivedInformation.value("chat_list").toMap().value(TYPE));
chatCountInformation.insert("marked_as_unread_count", receivedInformation.value("marked_as_unread_count"));
chatCountInformation.insert("marked_as_unread_unmuted_count", receivedInformation.value("marked_as_unread_unmuted_count"));
chatCountInformation.insert("total_count", receivedInformation.value("total_count"));
chatCountInformation.insert("unread_count", receivedInformation.value("unread_count"));
chatCountInformation.insert(TOTAL_COUNT, receivedInformation.value(TOTAL_COUNT));
chatCountInformation.insert(UNREAD_COUNT, receivedInformation.value(UNREAD_COUNT));
chatCountInformation.insert("unread_unmuted_count", receivedInformation.value("unread_unmuted_count"));
LOG("Unread chat count updated: " << chatCountInformation.value("chat_list_type").toString() << chatCountInformation.value("unread_count").toString());
LOG("Unread chat count updated: " << chatCountInformation.value("chat_list_type").toString() << chatCountInformation.value(UNREAD_COUNT).toString());
emit unreadChatCountUpdated(chatCountInformation);
}
@ -254,7 +266,7 @@ void TDLibReceiver::processUpdateChatLastMessage(const QVariantMap &receivedInfo
order = receivedInformation.value(ORDER).toString();
}
const QVariantMap lastMessage = receivedInformation.value(LAST_MESSAGE).toMap();
LOG("Last message of chat" << chat_id << "updated, order" << order << "type" << lastMessage.value("@type").toString());
LOG("Last message of chat" << chat_id << "updated, order" << order << "type" << lastMessage.value(TYPE).toString());
emit chatLastMessageUpdated(chat_id, order, lastMessage);
}
@ -315,15 +327,16 @@ void TDLibReceiver::processChatOnlineMemberCountUpdated(const QVariantMap &recei
void TDLibReceiver::processMessages(const QVariantMap &receivedInformation)
{
LOG("Received new messages, amount: " << receivedInformation.value("total_count").toString());
emit messagesReceived(receivedInformation.value("messages").toList(), receivedInformation.value("total_count").toInt());
LOG("Received new messages, amount: " << receivedInformation.value(TOTAL_COUNT).toString());
emit messagesReceived(receivedInformation.value(MESSAGES).toList(), receivedInformation.value(TOTAL_COUNT).toInt());
}
void TDLibReceiver::processUpdateNewMessage(const QVariantMap &receivedInformation)
{
const QString chatId = receivedInformation.value("message").toMap().value(CHAT_ID).toString();
const QVariantMap message = receivedInformation.value(MESSAGE).toMap();
const QString chatId = message.value(CHAT_ID).toString();
LOG("Received new message for chat " << chatId);
emit newMessageReceived(chatId, receivedInformation.value("message").toMap());
emit newMessageReceived(chatId, message);
}
void TDLibReceiver::processMessage(const QVariantMap &receivedInformation)
@ -336,8 +349,8 @@ void TDLibReceiver::processMessage(const QVariantMap &receivedInformation)
void TDLibReceiver::processMessageSendSucceeded(const QVariantMap &receivedInformation)
{
QString oldMessageId = receivedInformation.value("old_message_id").toString();
QVariantMap message = receivedInformation.value("message").toMap();
const QString oldMessageId = receivedInformation.value("old_message_id").toString();
const QVariantMap message = receivedInformation.value(MESSAGE).toMap();
const QString messageId = message.value(ID).toString();
LOG("Message send succeeded " << messageId << oldMessageId);
emit messageSendSucceeded(messageId, oldMessageId, message);
@ -371,7 +384,7 @@ void TDLibReceiver::processUpdateChatNotificationSettings(const QVariantMap &rec
void TDLibReceiver::processUpdateMessageContent(const QVariantMap &receivedInformation)
{
const QString chatId = receivedInformation.value(CHAT_ID).toString();
QString messageId = receivedInformation.value("message_id").toString();
const QString messageId = receivedInformation.value(MESSAGE_ID).toString();
LOG("Message content updated " << chatId << messageId);
emit messageContentUpdated(chatId, messageId, receivedInformation.value("new_content").toMap());
}
@ -379,7 +392,7 @@ void TDLibReceiver::processUpdateMessageContent(const QVariantMap &receivedInfor
void TDLibReceiver::processUpdateDeleteMessages(const QVariantMap &receivedInformation)
{
const QString chatId = receivedInformation.value(CHAT_ID).toString();
QVariantList messageIds = receivedInformation.value("message_ids").toList();
const QVariantList messageIds = receivedInformation.value(MESSAGE_IDS).toList();
LOG("Some messages were deleted " << chatId);
emit messagesDeleted(chatId, messageIds);
}
@ -427,38 +440,31 @@ void TDLibReceiver::processChatMembers(const QVariantMap &receivedInformation)
{
LOG("Received super group members");
const QString extra = receivedInformation.value(EXTRA).toString();
emit chatMembers(extra, receivedInformation.value("members").toList(), receivedInformation.value("total_count").toInt());
emit chatMembers(extra, receivedInformation.value("members").toList(), receivedInformation.value(TOTAL_COUNT).toInt());
}
void TDLibReceiver::processUserFullInfo(const QVariantMap &receivedInformation)
{
LOG("Received UserFullInfo");
emit userFullInfo(receivedInformation);
}
void TDLibReceiver::processUpdateUserFullInfo(const QVariantMap &receivedInformation)
{
LOG("Received UserFullInfoUpdate");
emit userFullInfoUpdated(receivedInformation.value("user_id").toString(), receivedInformation.value("user_full_info").toMap());
emit userFullInfoUpdated(receivedInformation.value(USER_ID).toString(), receivedInformation.value("user_full_info").toMap());
}
void TDLibReceiver::processBasicGroupFullInfo(const QVariantMap &receivedInformation)
{
LOG("Received BasicGroupFullInfo");
const QString groupId = receivedInformation.value(EXTRA).toString();
emit basicGroupFullInfo(groupId, receivedInformation);
}
void TDLibReceiver::processUpdateBasicGroupFullInfo(const QVariantMap &receivedInformation)
{
LOG("Received BasicGroupFullInfoUpdate");
const QString groupId = receivedInformation.value("basic_group_id").toString();
emit basicGroupFullInfoUpdated(groupId, receivedInformation.value("basic_group_full_info").toMap());
}
@ -466,45 +472,52 @@ void TDLibReceiver::processSupergroupFullInfo(const QVariantMap &receivedInforma
{
LOG("Received SuperGroupFullInfoUpdate");
const QString groupId = receivedInformation.value(EXTRA).toString();
emit supergroupFullInfo(groupId, receivedInformation);
}
void TDLibReceiver::processUpdateSupergroupFullInfo(const QVariantMap &receivedInformation)
{
LOG("Received SuperGroupFullInfoUpdate");
const QString groupId = receivedInformation.value("supergroup_id").toString();
emit supergroupFullInfoUpdated(groupId, receivedInformation.value("supergroup_full_info").toMap());
}
void TDLibReceiver::processUserProfilePhotos(const QVariantMap &receivedInformation)
{
const QString extra = receivedInformation.value(EXTRA).toString();
emit userProfilePhotos(extra, receivedInformation.value("photos").toList(), receivedInformation.value("total_count").toInt());
emit userProfilePhotos(extra, receivedInformation.value("photos").toList(), receivedInformation.value(TOTAL_COUNT).toInt());
}
void TDLibReceiver::processUpdateChatPermissions(const QVariantMap &receivedInformation)
{
emit chatPermissionsUpdated(receivedInformation.value("chat_id").toString(), receivedInformation.value("permissions").toMap());
emit chatPermissionsUpdated(receivedInformation.value(CHAT_ID).toString(), receivedInformation.value("permissions").toMap());
}
void TDLibReceiver::processUpdateChatPhoto(const QVariantMap &receivedInformation)
{
const qlonglong chatId = receivedInformation.value(CHAT_ID).toLongLong();
LOG("Photo updated for chat" << chatId);
emit chatPhotoUpdated(chatId, receivedInformation.value(PHOTO).toMap());
}
void TDLibReceiver::processUpdateChatTitle(const QVariantMap &receivedInformation)
{
LOG("Received UpdateChatTitle");
emit chatTitleUpdated(receivedInformation.value("chat_id").toString(), receivedInformation.value("title").toString());
emit chatTitleUpdated(receivedInformation.value(CHAT_ID).toString(), receivedInformation.value(TITLE).toString());
}
void TDLibReceiver::processUsers(const QVariantMap &receivedInformation)
{
LOG("Received Users");
emit usersReceived(receivedInformation.value(EXTRA).toString(), receivedInformation.value("user_ids").toList(), receivedInformation.value("total_count").toInt());
emit usersReceived(receivedInformation.value(EXTRA).toString(), receivedInformation.value("user_ids").toList(), receivedInformation.value(TOTAL_COUNT).toInt());
}
void TDLibReceiver::processError(const QVariantMap &receivedInformation)
{
LOG("Received an error");
emit errorReceived(receivedInformation.value("code").toInt(), receivedInformation.value("message").toString());
emit errorReceived(receivedInformation.value("code").toInt(), receivedInformation.value(MESSAGE).toString());
}
void TDLibReceiver::nop(const QVariantMap &)
{
}

View file

@ -79,9 +79,11 @@ signals:
void supergroupFullInfoUpdated(const QString &groupId, const QVariantMap &groupFullInfo);
void userProfilePhotos(const QString &extra, const QVariantList &photos, int totalPhotos);
void chatPermissionsUpdated(const QString &chatId, const QVariantMap &chatPermissions);
void chatPhotoUpdated(qlonglong chatId, const QVariantMap &photo);
void chatTitleUpdated(const QString &chatId, const QString &title);
void usersReceived(const QString &extra, const QVariantList &userIds, int totalUsers);
void errorReceived(const int code, const QString &message);
private:
typedef void (TDLibReceiver::*Handler)(const QVariantMap &);
@ -135,9 +137,11 @@ private:
void processUpdateSupergroupFullInfo(const QVariantMap &receivedInformation);
void processUserProfilePhotos(const QVariantMap &receivedInformation);
void processUpdateChatPermissions(const QVariantMap &receivedInformation);
void processUpdateChatPhoto(const QVariantMap &receivedInformation);
void processUpdateChatTitle(const QVariantMap &receivedInformation);
void processUsers(const QVariantMap &receivedInformation);
void processError(const QVariantMap &receivedInformation);
void nop(const QVariantMap &receivedInformation);
};
#endif // TDLIBRECEIVER_H

View file

@ -110,6 +110,7 @@ TDLibWrapper::TDLibWrapper(AppSettings *appSettings, QObject *parent) : QObject(
connect(this->tdLibReceiver, SIGNAL(supergroupFullInfoUpdated(QString, QVariantMap)), this, SIGNAL(supergroupFullInfoUpdated(QString, QVariantMap)));
connect(this->tdLibReceiver, SIGNAL(userProfilePhotos(QString, QVariantList, int)), this, SIGNAL(userProfilePhotosReceived(QString, QVariantList, int)));
connect(this->tdLibReceiver, SIGNAL(chatPermissionsUpdated(QString, QVariantMap)), this, SIGNAL(chatPermissionsUpdated(QString, QVariantMap)));
connect(this->tdLibReceiver, SIGNAL(chatPhotoUpdated(qlonglong, QVariantMap)), this, SIGNAL(chatPhotoUpdated(qlonglong, QVariantMap)));
connect(this->tdLibReceiver, SIGNAL(chatTitleUpdated(QString, QString)), this, SIGNAL(chatTitleUpdated(QString, QString)));
connect(this->tdLibReceiver, SIGNAL(usersReceived(QString, QVariantList, int)), this, SIGNAL(usersReceived(QString, QVariantList, int)));
connect(this->tdLibReceiver, SIGNAL(errorReceived(int, QString)), this, SIGNAL(errorReceived(int, QString)));
@ -264,7 +265,7 @@ void TDLibWrapper::leaveChat(const QString &chatId)
this->sendRequest(requestObject);
}
void TDLibWrapper::getChatHistory(const QString &chatId, const qlonglong &fromMessageId, int offset, int limit, bool onlyLocal)
void TDLibWrapper::getChatHistory(qlonglong chatId, const qlonglong &fromMessageId, int offset, int limit, bool onlyLocal)
{
LOG("Retrieving chat history" << chatId << fromMessageId << offset << limit << onlyLocal);
QVariantMap requestObject;

View file

@ -120,7 +120,7 @@ public:
Q_INVOKABLE void closeChat(const QString &chatId);
Q_INVOKABLE void joinChat(const QString &chatId);
Q_INVOKABLE void leaveChat(const QString &chatId);
Q_INVOKABLE void getChatHistory(const QString &chatId, const qlonglong &fromMessageId = 0, int offset = 0, int limit = 50, bool onlyLocal = false);
Q_INVOKABLE void getChatHistory(qlonglong chatId, const qlonglong &fromMessageId = 0, int offset = 0, int limit = 50, bool onlyLocal = false);
Q_INVOKABLE void viewMessage(const QString &chatId, const QString &messageId, bool force);
Q_INVOKABLE void sendTextMessage(const QString &chatId, const QString &message, const QString &replyToMessageId = "0");
Q_INVOKABLE void sendPhotoMessage(const QString &chatId, const QString &filePath, const QString &message, const QString &replyToMessageId = "0");
@ -216,6 +216,7 @@ signals:
void supergroupFullInfoUpdated(const QString &groupId, const QVariantMap &groupFullInfo);
void userProfilePhotosReceived(const QString &extra, const QVariantList &photos, int totalPhotos);
void chatPermissionsUpdated(const QString &chatId, const QVariantMap &permissions);
void chatPhotoUpdated(qlonglong chatId, const QVariantMap &photo);
void chatTitleUpdated(const QString &chatId, const QString &title);
void usersReceived(const QString &extra, const QVariantList &userIds, int totalUsers);
void errorReceived(const int code, const QString &message);