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 { ProfileThumbnail {
id: chatPictureThumbnail id: chatPictureThumbnail
photoData: (typeof chatInformation.photo !== "undefined") ? chatInformation.photo.small : "" photoData: chatModel.smallPhoto
replacementStringHint: chatNameText.text replacementStringHint: chatNameText.text
width: chatOverviewColumn.height width: chatOverviewColumn.height
height: 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(chatOrderUpdated(QString, QString)), this, SLOT(handleChatOrderUpdated(QString, QString)));
connect(tdLibWrapper, SIGNAL(chatReadInboxUpdated(QString, QString, int)), this, SLOT(handleChatReadInboxUpdated(QString, QString, int))); 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(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(messageSendSucceeded(QString, QString, QVariantMap)), this, SLOT(handleMessageSendSucceeded(QString, QString, QVariantMap)));
connect(tdLibWrapper, SIGNAL(chatNotificationSettingsUpdated(QString, QVariantMap)), this, SLOT(handleChatNotificationSettingsUpdated(QString, QVariantMap))); connect(tdLibWrapper, SIGNAL(chatNotificationSettingsUpdated(QString, QVariantMap)), this, SLOT(handleChatNotificationSettingsUpdated(QString, QVariantMap)));
connect(tdLibWrapper, SIGNAL(superGroupUpdated(qlonglong)), this, SLOT(handleGroupUpdated(qlonglong))); 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) void ChatListModel::handleMessageSendSucceeded(const QString &messageId, const QString &oldMessageId, const QVariantMap &message)
{ {
bool ok; bool ok;

View file

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

View file

@ -23,24 +23,35 @@
#include <QByteArray> #include <QByteArray>
#include <QBitArray> #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->tdLibWrapper = tdLibWrapper;
this->inReload = false;
this->inIncrementalUpdate = false;
connect(this->tdLibWrapper, SIGNAL(messagesReceived(QVariantList, int)), this, SLOT(handleMessagesReceived(QVariantList, int))); 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(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(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(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(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(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(messageContentUpdated(QString, QString, QVariantMap)), this, SLOT(handleMessageContentUpdated(QString, QString, QVariantMap)));
connect(this->tdLibWrapper, SIGNAL(messagesDeleted(QString, QVariantList)), this, SLOT(handleMessagesDeleted(QString, QVariantList))); connect(this->tdLibWrapper, SIGNAL(messagesDeleted(QString, QVariantList)), this, SLOT(handleMessagesDeleted(QString, QVariantList)));
} }
ChatModel::~ChatModel() ChatModel::~ChatModel()
{ {
qDebug() << "[ChatModel] Destroying myself..."; LOG("Destroying myself...");
} }
int ChatModel::rowCount(const QModelIndex &) const 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) 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); beginInsertRows(parent, row, row + count - 1);
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
this->messages.insert(row + i, this->messagesToBeAdded.at(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) void ChatModel::initialize(const QVariantMap &chatInformation)
{ {
qDebug() << "[ChatModel] Initializing chat model..."; LOG("Initializing chat model...");
this->chatInformation = chatInformation;
beginResetModel(); beginResetModel();
this->chatInformation = chatInformation;
this->chatId = chatInformation.value(ID).toLongLong();
this->messages.clear(); this->messages.clear();
this->messageIndexMap.clear(); this->messageIndexMap.clear();
this->messagesToBeAdded.clear(); this->messagesToBeAdded.clear();
endResetModel(); endResetModel();
this->chatId = chatInformation.value("id").toString(); emit smallPhotoChanged();
tdLibWrapper->getChatHistory(this->chatId); tdLibWrapper->getChatHistory(chatId);
} }
void ChatModel::triggerLoadMoreHistory() void ChatModel::triggerLoadMoreHistory()
{ {
if (!this->inIncrementalUpdate && !messages.isEmpty()) { if (!this->inIncrementalUpdate && !messages.isEmpty()) {
qDebug() << "[ChatModel] Trigger loading older history..."; LOG("Trigger loading older history...");
this->inIncrementalUpdate = true; 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; return this->chatInformation;
} }
QVariantMap ChatModel::getMessage(const int &index) QVariantMap ChatModel::getMessage(int index)
{ {
if (index < this->messages.size()) { if (index < this->messages.size()) {
return this->messages.at(index).toMap(); 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(); return chatInformation.value(PHOTO).toMap().value(SMALL).toMap();
QVariantMap messageMap2 = message2.toMap();
if (messageMap1.value("id").toLongLong() < messageMap2.value("id").toLongLong()) {
return true;
} else {
return false;
}
} }
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) { if (messages.size() == 0) {
qDebug() << "[ChatModel] No additional messages loaded, notifying chat UI..."; LOG("No additional messages loaded, notifying chat UI...");
this->inReload = false; this->inReload = false;
int listInboxPosition = this->calculateLastKnownMessageId(); int listInboxPosition = this->calculateLastKnownMessageId();
int listOutboxPosition = this->calculateLastReadSentMessageId(); int listOutboxPosition = this->calculateLastReadSentMessageId();
@ -136,7 +149,7 @@ void ChatModel::handleMessagesReceived(const QVariantList &messages, const int &
QListIterator<QVariant> messagesIterator(messages); QListIterator<QVariant> messagesIterator(messages);
while (messagesIterator.hasNext()) { while (messagesIterator.hasNext()) {
QVariantMap currentMessage = messagesIterator.next().toMap(); QVariantMap currentMessage = messagesIterator.next().toMap();
if (currentMessage.value("chat_id").toString() == this->chatId) { if (currentMessage.value(CHAT_ID).toLongLong() == chatId) {
this->messagesToBeAdded.append(currentMessage); 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... // First call only returns a few messages, we need to get a little more than that...
if (this->messagesToBeAdded.size() < 10 && !this->inReload) { 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->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 { } else {
qDebug() << "[ChatModel] Messages loaded, notifying chat UI..."; LOG("Messages loaded, notifying chat UI...");
this->inReload = false; this->inReload = false;
int listInboxPosition = this->calculateLastKnownMessageId(); int listInboxPosition = this->calculateLastKnownMessageId();
int listOutboxPosition = this->calculateLastReadSentMessageId(); 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) { if (id.toLongLong() == chatId) {
qDebug() << "[ChatModel] New message received for this chat"; LOG("New message received for this chat");
this->messagesMutex.lock(); this->messagesMutex.lock();
this->messagesToBeAdded.clear(); 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) { if (id.toLongLong() == chatId) {
qDebug() << "[ChatModel] Updating chat unread count, unread messages " << unreadCount << ", last read message ID: " << lastReadInboxMessageId; LOG("Updating chat unread count, unread messages" << unreadCount << ", last read message ID:" << lastReadInboxMessageId);
this->chatInformation.insert("unread_count", unreadCount); this->chatInformation.insert("unread_count", unreadCount);
this->chatInformation.insert("last_read_inbox_message_id", lastReadInboxMessageId); this->chatInformation.insert("last_read_inbox_message_id", lastReadInboxMessageId);
emit unreadCountUpdated(unreadCount, 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); this->chatInformation.insert("last_read_outbox_message_id", lastReadOutboxMessageId);
int sentIndex = calculateLastReadSentMessageId(); int sentIndex = calculateLastReadSentMessageId();
qDebug() << "[ChatModel] Updating sent message ID, new index " << sentIndex; LOG("Updating sent message ID, new index" << sentIndex);
emit lastReadSentMessageUpdated(sentIndex); emit lastReadSentMessageUpdated(sentIndex);
} }
} }
void ChatModel::handleMessageSendSucceeded(const QString &messageId, const QString &oldMessageId, const QVariantMap &message) 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(); LOG("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("index map:" << messageIndexMap.contains(oldMessageId) << ", index count:" << messageIndexMap.size() << ", message count:" << messages.size());
if (this->messageIndexMap.contains(oldMessageId)) { if (this->messageIndexMap.contains(oldMessageId)) {
this->messagesMutex.lock(); this->messagesMutex.lock();
qDebug() << "[ChatModel] Message was successfully sent " << oldMessageId; LOG("Message was successfully sent" << oldMessageId);
int messageIndex = this->messageIndexMap.value(oldMessageId).toInt(); int messageIndex = this->messageIndexMap.value(oldMessageId).toInt();
this->messages.replace(messageIndex, message); this->messages.replace(messageIndex, message);
this->calculateMessageIndexMap(); this->calculateMessageIndexMap();
qDebug() << "[ChatModel] Message was replaced at index " << messageIndex; LOG("Message was replaced at index" << messageIndex);
this->messagesMutex.unlock(); this->messagesMutex.unlock();
emit dataChanged(index(messageIndex), index(messageIndex)); emit dataChanged(index(messageIndex), index(messageIndex));
emit lastReadSentMessageUpdated(calculateLastReadSentMessageId()); 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); this->chatInformation.insert("notification_settings", chatNotificationSettings);
qDebug() << "[ChatModel] Notification settings updated"; LOG("Notification settings updated");
emit notificationSettingsUpdated(); 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 (id == chatId) {
if (chatId == this->chatId && this->messageIndexMap.contains(messageId)) { 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(); 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(); int messageIndex = this->messageIndexMap.value(messageId).toInt();
QVariantMap messageToBeUpdated = this->messages.at(messageIndex).toMap(); QVariantMap messageToBeUpdated = this->messages.at(messageIndex).toMap();
messageToBeUpdated.insert("content", newContent); messageToBeUpdated.insert("content", newContent);
this->messages.replace(messageIndex, messageToBeUpdated); this->messages.replace(messageIndex, messageToBeUpdated);
this->calculateMessageIndexMap(); this->calculateMessageIndexMap();
qDebug() << "[ChatModel] Message was replaced at index " << messageIndex; LOG("Message was replaced at index" << messageIndex);
this->messagesMutex.unlock(); this->messagesMutex.unlock();
emit messageUpdated(messageIndex); emit messageUpdated(messageIndex);
emit dataChanged(index(messageIndex), index(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; LOG("Messages were deleted in a chat" << id);
if (chatId == this->chatId) { if (id.toLongLong() == chatId) {
this->messagesMutex.lock(); this->messagesMutex.lock();
qDebug() << "[ChatModel] Messages in this chat were deleted..."; LOG("Messages in this chat were deleted...");
QListIterator<QVariant> messageIdIterator(messageIds); QListIterator<QVariant> messageIdIterator(messageIds);
while (messageIdIterator.hasNext()) { while (messageIdIterator.hasNext()) {
QString messageId = messageIdIterator.next().toString(); QString messageId = messageIdIterator.next().toString();
if (this->messageIndexMap.contains(messageId)) { if (this->messageIndexMap.contains(messageId)) {
int messageIndex = this->messageIndexMap.value(messageId).toInt(); int messageIndex = this->messageIndexMap.value(messageId).toInt();
beginRemoveRows(QModelIndex(), messageIndex, messageIndex); 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->messages.removeAt(messageIndex);
this->calculateMessageIndexMap(); this->calculateMessageIndexMap();
endRemoveRows(); endRemoveRows();
@ -277,7 +299,7 @@ void ChatModel::insertMessages()
endResetModel(); endResetModel();
} else { } else {
// There is only an append or a prepend, tertium non datur! (probably ;)) // 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 // Append
this->insertRows(rowCount(QModelIndex()), this->messagesToBeAdded.size()); this->insertRows(rowCount(QModelIndex()), this->messagesToBeAdded.size());
} else { } else {
@ -320,36 +342,36 @@ QVariantMap ChatModel::enhanceMessage(const QVariantMap &message)
int ChatModel::calculateLastKnownMessageId() int ChatModel::calculateLastKnownMessageId()
{ {
qDebug() << "[ChatModel] calculateLastKnownMessageId"; LOG("calculateLastKnownMessageId");
QString lastKnownMessageId = this->chatInformation.value("last_read_inbox_message_id").toString(); QString lastKnownMessageId = this->chatInformation.value("last_read_inbox_message_id").toString();
qDebug() << "[ChatModel] lastKnownMessageId" << lastKnownMessageId; LOG("lastKnownMessageId" << lastKnownMessageId);
qDebug() << "[ChatModel] size messageIndexMap" << this->messageIndexMap.size(); LOG("size messageIndexMap" << messageIndexMap.size());
qDebug() << "[ChatModel] contains ID?" << this->messageIndexMap.contains(lastKnownMessageId); LOG("contains ID?" << messageIndexMap.contains(lastKnownMessageId));
int listInboxPosition = this->messageIndexMap.value(lastKnownMessageId, this->messages.size() - 1).toInt(); int listInboxPosition = this->messageIndexMap.value(lastKnownMessageId, this->messages.size() - 1).toInt();
if (listInboxPosition > this->messages.size() - 1 ) { if (listInboxPosition > this->messages.size() - 1 ) {
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; return listInboxPosition;
} }
int ChatModel::calculateLastReadSentMessageId() int ChatModel::calculateLastReadSentMessageId()
{ {
qDebug() << "[ChatModel] calculateLastReadSentMessageId"; LOG("calculateLastReadSentMessageId");
QString lastReadSentMessageId = this->chatInformation.value("last_read_outbox_message_id").toString(); QString lastReadSentMessageId = this->chatInformation.value("last_read_outbox_message_id").toString();
qDebug() << "[ChatModel] lastReadSentMessageId" << lastReadSentMessageId; LOG("lastReadSentMessageId" << lastReadSentMessageId);
qDebug() << "[ChatModel] size messageIndexMap" << this->messageIndexMap.size(); LOG("size messageIndexMap" << messageIndexMap.size());
qDebug() << "[ChatModel] contains ID?" << this->messageIndexMap.contains(lastReadSentMessageId); LOG("contains ID?" << messageIndexMap.contains(lastReadSentMessageId));
int listOutboxPosition = this->messageIndexMap.value(lastReadSentMessageId, this->messages.size() - 1).toInt(); 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; return listOutboxPosition;
} }
void ChatModel::calculateMessageIndexMap() void ChatModel::calculateMessageIndexMap()
{ {
qDebug() << "[ChatModel] calculateMessageIndexMap"; LOG("calculateMessageIndexMap");
this->messageIndexMap.clear(); this->messageIndexMap.clear();
for (int i = 0; i < this->messages.size(); i++) { 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 class ChatModel : public QAbstractListModel
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(QVariantMap smallPhoto READ smallPhoto NOTIFY smallPhotoChanged)
public: public:
ChatModel(TDLibWrapper *tdLibWrapper); ChatModel(TDLibWrapper *tdLibWrapper);
~ChatModel() override; ~ChatModel() override;
@ -36,40 +38,43 @@ public:
virtual QVariant data(const QModelIndex &index, int role) const override; virtual QVariant data(const QModelIndex &index, int role) const override;
virtual bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override; virtual bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
Q_INVOKABLE void initialize(const QVariantMap &chatInformation); Q_INVOKABLE void initialize(const QVariantMap &chatInformation);
Q_INVOKABLE void triggerLoadMoreHistory(); Q_INVOKABLE void triggerLoadMoreHistory();
Q_INVOKABLE QVariantMap getChatInformation(); Q_INVOKABLE QVariantMap getChatInformation();
Q_INVOKABLE QVariantMap getMessage(const int &index); Q_INVOKABLE QVariantMap getMessage(int index);
QVariantMap smallPhoto() const;
signals: signals:
void messagesReceived(const int &modelIndex, const int &lastReadSentIndex, const int &totalCount); void messagesReceived(int modelIndex, int lastReadSentIndex, int totalCount);
void messagesIncrementalUpdate(const int &modelIndex, const int &lastReadSentIndex); void messagesIncrementalUpdate(int modelIndex, int lastReadSentIndex);
void newMessageReceived(const QVariantMap &message); void newMessageReceived(const QVariantMap &message);
void unreadCountUpdated(const int &unreadCount, const QString &lastReadInboxMessageId); void unreadCountUpdated(int unreadCount, const QString &lastReadInboxMessageId);
void lastReadSentMessageUpdated(const int &lastReadSentIndex); void lastReadSentMessageUpdated(int lastReadSentIndex);
void notificationSettingsUpdated(); void notificationSettingsUpdated();
void messageUpdated(const int &modelIndex); void messageUpdated(int modelIndex);
void messagesDeleted(); void messagesDeleted();
void smallPhotoChanged();
public slots: 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 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 handleChatReadOutboxUpdated(const QString &chatId, const QString &lastReadOutboxMessageId);
void handleMessageSendSucceeded(const QString &messageId, const QString &oldMessageId, const QVariantMap &message); void handleMessageSendSucceeded(const QString &messageId, const QString &oldMessageId, const QVariantMap &message);
void handleChatNotificationSettingsUpdated(const QString &chatId, const QVariantMap &chatNotificationSettings); 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 handleMessageContentUpdated(const QString &chatId, const QString &messageId, const QVariantMap &newContent);
void handleMessagesDeleted(const QString &chatId, const QVariantList &messageIds); void handleMessagesDeleted(const QString &chatId, const QVariantList &messageIds);
private: private:
TDLibWrapper *tdLibWrapper; TDLibWrapper *tdLibWrapper;
QVariantList messages; QVariantList messages;
QVariantList messagesToBeAdded; QVariantList messagesToBeAdded;
QVariantMap messageIndexMap; QVariantMap messageIndexMap;
QMutex messagesMutex; QMutex messagesMutex;
QVariantMap chatInformation; QVariantMap chatInformation;
QString chatId; qlonglong chatId;
bool inReload; bool inReload;
bool inIncrementalUpdate; bool inIncrementalUpdate;

View file

@ -30,12 +30,22 @@ namespace {
const QString ID("id"); const QString ID("id");
const QString LIST("list"); const QString LIST("list");
const QString CHAT_ID("chat_id"); 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 POSITION("position");
const QString POSITIONS("positions"); const QString POSITIONS("positions");
const QString PHOTO("photo");
const QString ORDER("order"); const QString ORDER("order");
const QString BASIC_GROUP("basic_group"); const QString BASIC_GROUP("basic_group");
const QString SUPERGROUP("supergroup"); const QString SUPERGROUP("supergroup");
const QString LAST_MESSAGE("last_message"); const QString LAST_MESSAGE("last_message");
const QString TOTAL_COUNT("total_count");
const QString UNREAD_COUNT("unread_count"); const QString UNREAD_COUNT("unread_count");
const QString LAST_READ_INBOX_MESSAGE_ID("last_read_inbox_message_id"); const QString LAST_READ_INBOX_MESSAGE_ID("last_read_inbox_message_id");
const QString LAST_READ_OUTBOX_MESSAGE_ID("last_read_outbox_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("updateSupergroupFullInfo", &TDLibReceiver::processUpdateSupergroupFullInfo);
handlers.insert("userProfilePhotos", &TDLibReceiver::processUserProfilePhotos); handlers.insert("userProfilePhotos", &TDLibReceiver::processUserProfilePhotos);
handlers.insert("updateChatPermissions", &TDLibReceiver::processUpdateChatPermissions); handlers.insert("updateChatPermissions", &TDLibReceiver::processUpdateChatPermissions);
handlers.insert("updateChatPhoto", &TDLibReceiver::processUpdateChatPhoto);
handlers.insert("updateChatTitle", &TDLibReceiver::processUpdateChatTitle); handlers.insert("updateChatTitle", &TDLibReceiver::processUpdateChatTitle);
handlers.insert("users", &TDLibReceiver::processUsers); handlers.insert("users", &TDLibReceiver::processUsers);
handlers.insert("error", &TDLibReceiver::processError); handlers.insert("error", &TDLibReceiver::processError);
handlers.insert("ok", &TDLibReceiver::nop);
} }
void TDLibReceiver::setActive(bool active) void TDLibReceiver::setActive(bool active)
@ -148,7 +160,7 @@ void TDLibReceiver::receiverLoop()
void TDLibReceiver::processReceivedDocument(const QJsonDocument &receivedJsonDocument) void TDLibReceiver::processReceivedDocument(const QJsonDocument &receivedJsonDocument)
{ {
QVariantMap receivedInformation = receivedJsonDocument.object().toVariantMap(); QVariantMap receivedInformation = receivedJsonDocument.object().toVariantMap();
QString objectTypeName = receivedInformation.value("@type").toString(); QString objectTypeName = receivedInformation.value(TYPE).toString();
Handler handler = handlers.value(objectTypeName); Handler handler = handlers.value(objectTypeName);
if (handler) { if (handler) {
@ -160,28 +172,28 @@ void TDLibReceiver::processReceivedDocument(const QJsonDocument &receivedJsonDoc
void TDLibReceiver::processUpdateOption(const QVariantMap &receivedInformation) 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") { if (currentOption == "version") {
QString detectedVersion = receivedInformation.value("value").toMap().value("value").toString(); QString detectedVersion = value.toString();
LOG("TD Lib version detected: " << detectedVersion); LOG("TD Lib version detected: " << detectedVersion);
emit versionDetected(detectedVersion); emit versionDetected(detectedVersion);
} else { } else {
QVariant currentValue = receivedInformation.value("value").toMap().value("value"); LOG("Option updated: " << currentOption << value);
LOG("Option updated: " << currentOption << currentValue); emit optionUpdated(currentOption, value);
emit optionUpdated(currentOption, currentValue);
} }
} }
void TDLibReceiver::processUpdateAuthorizationState(const QVariantMap &receivedInformation) 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); LOG("Authorization state changed: " << authorizationState);
emit authorizationStateChanged(authorizationState, receivedInformation); emit authorizationStateChanged(authorizationState, receivedInformation);
} }
void TDLibReceiver::processUpdateConnectionState(const QVariantMap &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); LOG("Connection state changed: " << connectionState);
emit connectionStateChanged(connectionState); emit connectionStateChanged(connectionState);
} }
@ -189,21 +201,21 @@ void TDLibReceiver::processUpdateConnectionState(const QVariantMap &receivedInfo
void TDLibReceiver::processUpdateUser(const QVariantMap &receivedInformation) void TDLibReceiver::processUpdateUser(const QVariantMap &receivedInformation)
{ {
QVariantMap userInformation = receivedInformation.value("user").toMap(); 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); emit userUpdated(userInformation);
} }
void TDLibReceiver::processUpdateUserStatus(const QVariantMap &receivedInformation) 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(); 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); emit userStatusUpdated(userId, userStatusInformation);
} }
void TDLibReceiver::processUpdateFile(const QVariantMap &receivedInformation) 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()); LOG("File was updated: " << fileInformation.value(ID).toString());
emit fileUpdated(fileInformation); emit fileUpdated(fileInformation);
} }
@ -216,31 +228,31 @@ void TDLibReceiver::processFile(const QVariantMap &receivedInformation)
void TDLibReceiver::processUpdateNewChat(const QVariantMap &receivedInformation) void TDLibReceiver::processUpdateNewChat(const QVariantMap &receivedInformation)
{ {
QVariantMap chatInformation = receivedInformation.value("chat").toMap(); const QVariantMap chatInformation = receivedInformation.value("chat").toMap();
LOG("New chat discovered: " << chatInformation.value(ID).toString() << chatInformation.value("title").toString()); LOG("New chat discovered: " << chatInformation.value(ID).toString() << chatInformation.value(TITLE).toString());
emit newChatDiscovered(chatInformation); emit newChatDiscovered(chatInformation);
} }
void TDLibReceiver::processUpdateUnreadMessageCount(const QVariantMap &receivedInformation) void TDLibReceiver::processUpdateUnreadMessageCount(const QVariantMap &receivedInformation)
{ {
QVariantMap messageCountInformation; QVariantMap messageCountInformation;
messageCountInformation.insert("chat_list_type", receivedInformation.value("chat_list").toMap().value("@type")); messageCountInformation.insert("chat_list_type", receivedInformation.value("chat_list").toMap().value(TYPE));
messageCountInformation.insert("unread_count", receivedInformation.value("unread_count")); messageCountInformation.insert(UNREAD_COUNT, receivedInformation.value(UNREAD_COUNT));
messageCountInformation.insert("unread_unmuted_count", receivedInformation.value("unread_unmuted_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); emit unreadMessageCountUpdated(messageCountInformation);
} }
void TDLibReceiver::processUpdateUnreadChatCount(const QVariantMap &receivedInformation) void TDLibReceiver::processUpdateUnreadChatCount(const QVariantMap &receivedInformation)
{ {
QVariantMap chatCountInformation; 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_count", receivedInformation.value("marked_as_unread_count"));
chatCountInformation.insert("marked_as_unread_unmuted_count", receivedInformation.value("marked_as_unread_unmuted_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(TOTAL_COUNT, receivedInformation.value(TOTAL_COUNT));
chatCountInformation.insert("unread_count", receivedInformation.value("unread_count")); chatCountInformation.insert(UNREAD_COUNT, receivedInformation.value(UNREAD_COUNT));
chatCountInformation.insert("unread_unmuted_count", receivedInformation.value("unread_unmuted_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); emit unreadChatCountUpdated(chatCountInformation);
} }
@ -254,7 +266,7 @@ void TDLibReceiver::processUpdateChatLastMessage(const QVariantMap &receivedInfo
order = receivedInformation.value(ORDER).toString(); order = receivedInformation.value(ORDER).toString();
} }
const QVariantMap lastMessage = receivedInformation.value(LAST_MESSAGE).toMap(); 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); emit chatLastMessageUpdated(chat_id, order, lastMessage);
} }
@ -315,15 +327,16 @@ void TDLibReceiver::processChatOnlineMemberCountUpdated(const QVariantMap &recei
void TDLibReceiver::processMessages(const QVariantMap &receivedInformation) void TDLibReceiver::processMessages(const QVariantMap &receivedInformation)
{ {
LOG("Received new messages, amount: " << receivedInformation.value("total_count").toString()); LOG("Received new messages, amount: " << receivedInformation.value(TOTAL_COUNT).toString());
emit messagesReceived(receivedInformation.value("messages").toList(), receivedInformation.value("total_count").toInt()); emit messagesReceived(receivedInformation.value(MESSAGES).toList(), receivedInformation.value(TOTAL_COUNT).toInt());
} }
void TDLibReceiver::processUpdateNewMessage(const QVariantMap &receivedInformation) 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); LOG("Received new message for chat " << chatId);
emit newMessageReceived(chatId, receivedInformation.value("message").toMap()); emit newMessageReceived(chatId, message);
} }
void TDLibReceiver::processMessage(const QVariantMap &receivedInformation) void TDLibReceiver::processMessage(const QVariantMap &receivedInformation)
@ -336,8 +349,8 @@ void TDLibReceiver::processMessage(const QVariantMap &receivedInformation)
void TDLibReceiver::processMessageSendSucceeded(const QVariantMap &receivedInformation) void TDLibReceiver::processMessageSendSucceeded(const QVariantMap &receivedInformation)
{ {
QString oldMessageId = receivedInformation.value("old_message_id").toString(); const QString oldMessageId = receivedInformation.value("old_message_id").toString();
QVariantMap message = receivedInformation.value("message").toMap(); const QVariantMap message = receivedInformation.value(MESSAGE).toMap();
const QString messageId = message.value(ID).toString(); const QString messageId = message.value(ID).toString();
LOG("Message send succeeded " << messageId << oldMessageId); LOG("Message send succeeded " << messageId << oldMessageId);
emit messageSendSucceeded(messageId, oldMessageId, message); emit messageSendSucceeded(messageId, oldMessageId, message);
@ -371,7 +384,7 @@ void TDLibReceiver::processUpdateChatNotificationSettings(const QVariantMap &rec
void TDLibReceiver::processUpdateMessageContent(const QVariantMap &receivedInformation) void TDLibReceiver::processUpdateMessageContent(const QVariantMap &receivedInformation)
{ {
const QString chatId = receivedInformation.value(CHAT_ID).toString(); 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); LOG("Message content updated " << chatId << messageId);
emit messageContentUpdated(chatId, messageId, receivedInformation.value("new_content").toMap()); 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) void TDLibReceiver::processUpdateDeleteMessages(const QVariantMap &receivedInformation)
{ {
const QString chatId = receivedInformation.value(CHAT_ID).toString(); 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); LOG("Some messages were deleted " << chatId);
emit messagesDeleted(chatId, messageIds); emit messagesDeleted(chatId, messageIds);
} }
@ -427,38 +440,31 @@ void TDLibReceiver::processChatMembers(const QVariantMap &receivedInformation)
{ {
LOG("Received super group members"); LOG("Received super group members");
const QString extra = receivedInformation.value(EXTRA).toString(); 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) void TDLibReceiver::processUserFullInfo(const QVariantMap &receivedInformation)
{ {
LOG("Received UserFullInfo"); LOG("Received UserFullInfo");
emit userFullInfo(receivedInformation); emit userFullInfo(receivedInformation);
} }
void TDLibReceiver::processUpdateUserFullInfo(const QVariantMap &receivedInformation) void TDLibReceiver::processUpdateUserFullInfo(const QVariantMap &receivedInformation)
{ {
LOG("Received UserFullInfoUpdate"); 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) void TDLibReceiver::processBasicGroupFullInfo(const QVariantMap &receivedInformation)
{ {
LOG("Received BasicGroupFullInfo"); LOG("Received BasicGroupFullInfo");
const QString groupId = receivedInformation.value(EXTRA).toString(); const QString groupId = receivedInformation.value(EXTRA).toString();
emit basicGroupFullInfo(groupId, receivedInformation); emit basicGroupFullInfo(groupId, receivedInformation);
} }
void TDLibReceiver::processUpdateBasicGroupFullInfo(const QVariantMap &receivedInformation) void TDLibReceiver::processUpdateBasicGroupFullInfo(const QVariantMap &receivedInformation)
{ {
LOG("Received BasicGroupFullInfoUpdate"); LOG("Received BasicGroupFullInfoUpdate");
const QString groupId = receivedInformation.value("basic_group_id").toString(); const QString groupId = receivedInformation.value("basic_group_id").toString();
emit basicGroupFullInfoUpdated(groupId, receivedInformation.value("basic_group_full_info").toMap()); emit basicGroupFullInfoUpdated(groupId, receivedInformation.value("basic_group_full_info").toMap());
} }
@ -466,45 +472,52 @@ void TDLibReceiver::processSupergroupFullInfo(const QVariantMap &receivedInforma
{ {
LOG("Received SuperGroupFullInfoUpdate"); LOG("Received SuperGroupFullInfoUpdate");
const QString groupId = receivedInformation.value(EXTRA).toString(); const QString groupId = receivedInformation.value(EXTRA).toString();
emit supergroupFullInfo(groupId, receivedInformation); emit supergroupFullInfo(groupId, receivedInformation);
} }
void TDLibReceiver::processUpdateSupergroupFullInfo(const QVariantMap &receivedInformation) void TDLibReceiver::processUpdateSupergroupFullInfo(const QVariantMap &receivedInformation)
{ {
LOG("Received SuperGroupFullInfoUpdate"); LOG("Received SuperGroupFullInfoUpdate");
const QString groupId = receivedInformation.value("supergroup_id").toString(); const QString groupId = receivedInformation.value("supergroup_id").toString();
emit supergroupFullInfoUpdated(groupId, receivedInformation.value("supergroup_full_info").toMap()); emit supergroupFullInfoUpdated(groupId, receivedInformation.value("supergroup_full_info").toMap());
} }
void TDLibReceiver::processUserProfilePhotos(const QVariantMap &receivedInformation) void TDLibReceiver::processUserProfilePhotos(const QVariantMap &receivedInformation)
{ {
const QString extra = receivedInformation.value(EXTRA).toString(); 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) 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) void TDLibReceiver::processUpdateChatTitle(const QVariantMap &receivedInformation)
{ {
LOG("Received UpdateChatTitle"); 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) void TDLibReceiver::processUsers(const QVariantMap &receivedInformation)
{ {
LOG("Received Users"); 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) void TDLibReceiver::processError(const QVariantMap &receivedInformation)
{ {
LOG("Received an error"); 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 supergroupFullInfoUpdated(const QString &groupId, const QVariantMap &groupFullInfo);
void userProfilePhotos(const QString &extra, const QVariantList &photos, int totalPhotos); void userProfilePhotos(const QString &extra, const QVariantList &photos, int totalPhotos);
void chatPermissionsUpdated(const QString &chatId, const QVariantMap &chatPermissions); void chatPermissionsUpdated(const QString &chatId, const QVariantMap &chatPermissions);
void chatPhotoUpdated(qlonglong chatId, const QVariantMap &photo);
void chatTitleUpdated(const QString &chatId, const QString &title); void chatTitleUpdated(const QString &chatId, const QString &title);
void usersReceived(const QString &extra, const QVariantList &userIds, int totalUsers); void usersReceived(const QString &extra, const QVariantList &userIds, int totalUsers);
void errorReceived(const int code, const QString &message); void errorReceived(const int code, const QString &message);
private: private:
typedef void (TDLibReceiver::*Handler)(const QVariantMap &); typedef void (TDLibReceiver::*Handler)(const QVariantMap &);
@ -135,9 +137,11 @@ private:
void processUpdateSupergroupFullInfo(const QVariantMap &receivedInformation); void processUpdateSupergroupFullInfo(const QVariantMap &receivedInformation);
void processUserProfilePhotos(const QVariantMap &receivedInformation); void processUserProfilePhotos(const QVariantMap &receivedInformation);
void processUpdateChatPermissions(const QVariantMap &receivedInformation); void processUpdateChatPermissions(const QVariantMap &receivedInformation);
void processUpdateChatPhoto(const QVariantMap &receivedInformation);
void processUpdateChatTitle(const QVariantMap &receivedInformation); void processUpdateChatTitle(const QVariantMap &receivedInformation);
void processUsers(const QVariantMap &receivedInformation); void processUsers(const QVariantMap &receivedInformation);
void processError(const QVariantMap &receivedInformation); void processError(const QVariantMap &receivedInformation);
void nop(const QVariantMap &receivedInformation);
}; };
#endif // TDLIBRECEIVER_H #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(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(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(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(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(usersReceived(QString, QVariantList, int)), this, SIGNAL(usersReceived(QString, QVariantList, int)));
connect(this->tdLibReceiver, SIGNAL(errorReceived(int, QString)), this, SIGNAL(errorReceived(int, QString))); 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); 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); LOG("Retrieving chat history" << chatId << fromMessageId << offset << limit << onlyLocal);
QVariantMap requestObject; QVariantMap requestObject;

View file

@ -120,7 +120,7 @@ public:
Q_INVOKABLE void closeChat(const QString &chatId); Q_INVOKABLE void closeChat(const QString &chatId);
Q_INVOKABLE void joinChat(const QString &chatId); Q_INVOKABLE void joinChat(const QString &chatId);
Q_INVOKABLE void leaveChat(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 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 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"); 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 supergroupFullInfoUpdated(const QString &groupId, const QVariantMap &groupFullInfo);
void userProfilePhotosReceived(const QString &extra, const QVariantList &photos, int totalPhotos); void userProfilePhotosReceived(const QString &extra, const QVariantList &photos, int totalPhotos);
void chatPermissionsUpdated(const QString &chatId, const QVariantMap &permissions); void chatPermissionsUpdated(const QString &chatId, const QVariantMap &permissions);
void chatPhotoUpdated(qlonglong chatId, const QVariantMap &photo);
void chatTitleUpdated(const QString &chatId, const QString &title); void chatTitleUpdated(const QString &chatId, const QString &title);
void usersReceived(const QString &extra, const QVariantList &userIds, int totalUsers); void usersReceived(const QString &extra, const QVariantList &userIds, int totalUsers);
void errorReceived(const int code, const QString &message); void errorReceived(const int code, const QString &message);