From 64b6c8607e2f7cc105eaf53ea6edac6b014a92b2 Mon Sep 17 00:00:00 2001 From: "Sebastian J. Wolf" Date: Sun, 20 Sep 2020 00:13:42 +0200 Subject: [PATCH] Deleting messages seems to work... --- qml/pages/ChatPage.qml | 13 ++++++++++++- src/chatlistmodel.cpp | 6 ++++++ src/chatlistmodel.h | 1 + src/chatmodel.cpp | 24 ++++++++++++++++++++++++ src/chatmodel.h | 2 ++ src/tdlibreceiver.cpp | 9 +++++++++ src/tdlibreceiver.h | 2 ++ src/tdlibwrapper.cpp | 19 ++++++++++++++++++- src/tdlibwrapper.h | 3 +++ translations/harbour-fernschreiber-de.ts | 12 ++++++++++-- translations/harbour-fernschreiber.ts | 8 ++++++++ 11 files changed, 95 insertions(+), 4 deletions(-) diff --git a/qml/pages/ChatPage.qml b/qml/pages/ChatPage.qml index ce1c038..d9cd8ee 100644 --- a/qml/pages/ChatPage.qml +++ b/qml/pages/ChatPage.qml @@ -111,7 +111,7 @@ Page { var messageStatusSuffix = ""; if (message.edit_date > 0) { - messageStatusSuffix += " - " + qsTr("edited"); + messageStatusSuffix += " - " + qsTr("edited"); } if (chatPage.myUserId === message.sender_user_id) { @@ -409,6 +409,13 @@ Page { text: qsTr("Edit Message") visible: display.can_be_edited } + MenuItem { + onClicked: { + deleteMessageRemorseItem.execute(messageListItem, qsTr("Deleting message"), function() { tdLibWrapper.deleteMessages(chatInformation.id, [ display.id ]); } ); + } + text: qsTr("Delete Message") + visible: display.can_be_deleted_for_all_users + } } Connections { @@ -423,6 +430,10 @@ Page { } } + RemorseItem { + id: deleteMessageRemorseItem + } + Row { id: messageTextRow spacing: Theme.paddingSmall diff --git a/src/chatlistmodel.cpp b/src/chatlistmodel.cpp index 0af4467..22c1d54 100644 --- a/src/chatlistmodel.cpp +++ b/src/chatlistmodel.cpp @@ -69,6 +69,12 @@ void ChatListModel::enableDeltaUpdates() this->deltaUpdates = true; } +void ChatListModel::redrawModel() +{ + qDebug() << "[ChatListModel] Enforcing UI redraw..."; + layoutChanged(); +} + bool compareChats(const QVariant &chat1, const QVariant &chat2) { QVariantMap chatMap1 = chat1.toMap(); diff --git a/src/chatlistmodel.h b/src/chatlistmodel.h index 1aa7e96..be54536 100644 --- a/src/chatlistmodel.h +++ b/src/chatlistmodel.h @@ -37,6 +37,7 @@ public: virtual bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override; Q_INVOKABLE void enableDeltaUpdates(); + Q_INVOKABLE void redrawModel(); signals: void chatChanged(const QString &chatId); diff --git a/src/chatmodel.cpp b/src/chatmodel.cpp index 0445516..cd4c409 100644 --- a/src/chatmodel.cpp +++ b/src/chatmodel.cpp @@ -35,6 +35,7 @@ ChatModel::ChatModel(TDLibWrapper *tdLibWrapper) 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(messageContentUpdated(QString, QString, QVariantMap)), this, SLOT(handleMessageContentUpdated(QString, QString, QVariantMap))); + connect(this->tdLibWrapper, SIGNAL(messagesDeleted(QString, QVariantList)), this, SLOT(handleMessagesDeleted(QString, QVariantList))); } ChatModel::~ChatModel() @@ -242,6 +243,29 @@ void ChatModel::handleMessageContentUpdated(const QString &chatId, const QString } } +void ChatModel::handleMessagesDeleted(const QString &chatId, const QVariantList &messageIds) +{ + qDebug() << "[ChatModel] Messages were deleted in a chat" << chatId; + if (chatId == this->chatId) { + this->messagesMutex.lock(); + qDebug() << "[ChatModel] Messages in this chat were deleted..."; + QListIterator 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; + this->messages.removeAt(messageIndex); + this->calculateMessageIndexMap(); + endRemoveRows(); + } + } + this->messagesMutex.unlock(); + emit messagesDeleted(); + } +} + void ChatModel::insertMessages() { if (this->messages.isEmpty()) { diff --git a/src/chatmodel.h b/src/chatmodel.h index 78a1d8d..a345b6a 100644 --- a/src/chatmodel.h +++ b/src/chatmodel.h @@ -49,6 +49,7 @@ signals: void lastReadSentMessageUpdated(const int &lastReadSentIndex); void notificationSettingsUpdated(); void messageUpdated(const int &modelIndex); + void messagesDeleted(); public slots: void handleMessagesReceived(const QVariantList &messages); @@ -58,6 +59,7 @@ public slots: void handleMessageSendSucceeded(const QString &messageId, const QString &oldMessageId, const QVariantMap &message); void handleChatNotificationSettingsUpdated(const QString &chatId, const QVariantMap &chatNotificationSettings); void handleMessageContentUpdated(const QString &chatId, const QString &messageId, const QVariantMap &newContent); + void handleMessagesDeleted(const QString &chatId, const QVariantList &messageIds); private: diff --git a/src/tdlibreceiver.cpp b/src/tdlibreceiver.cpp index b878785..da6453c 100644 --- a/src/tdlibreceiver.cpp +++ b/src/tdlibreceiver.cpp @@ -79,6 +79,7 @@ void TDLibReceiver::processReceivedDocument(const QJsonDocument &receivedJsonDoc if (objectTypeName == "updateNotificationGroup") { this->processUpdateNotificationGroup(receivedInformation); } if (objectTypeName == "updateChatNotificationSettings") { this->processUpdateChatNotificationSettings(receivedInformation); } if (objectTypeName == "updateMessageContent") { this->processUpdateMessageContent(receivedInformation); } + if (objectTypeName == "updateDeleteMessages") { this->processUpdateDeleteMessages(receivedInformation); } } void TDLibReceiver::processUpdateOption(const QVariantMap &receivedInformation) @@ -275,3 +276,11 @@ void TDLibReceiver::processUpdateMessageContent(const QVariantMap &receivedInfor qDebug() << "[TDLibReceiver] Message content updated " << chatId << messageId; emit messageContentUpdated(chatId, messageId, receivedInformation.value("new_content").toMap()); } + +void TDLibReceiver::processUpdateDeleteMessages(const QVariantMap &receivedInformation) +{ + QString chatId = receivedInformation.value("chat_id").toString(); + QVariantList messageIds = receivedInformation.value("message_ids").toList(); + qDebug() << "[TDLibReceiver] Some messages were deleted " << chatId << messageIds; + emit messagesDeleted(chatId, messageIds); +} diff --git a/src/tdlibreceiver.h b/src/tdlibreceiver.h index 609bf32..eda9838 100644 --- a/src/tdlibreceiver.h +++ b/src/tdlibreceiver.h @@ -62,6 +62,7 @@ signals: void notificationUpdated(const QVariantMap updatedNotification); void chatNotificationSettingsUpdated(const QString &chatId, const QVariantMap updatedChatNotificationSettings); void messageContentUpdated(const QString &chatId, const QString &messageId, const QVariantMap &newContent); + void messagesDeleted(const QString &chatId, const QVariantList &messageIds); private: void *tdLibClient; @@ -95,6 +96,7 @@ private: void processUpdateNotification(const QVariantMap &receivedInformation); void processUpdateChatNotificationSettings(const QVariantMap &receivedInformation); void processUpdateMessageContent(const QVariantMap &receivedInformation); + void processUpdateDeleteMessages(const QVariantMap &receivedInformation); }; #endif // TDLIBRECEIVER_H diff --git a/src/tdlibwrapper.cpp b/src/tdlibwrapper.cpp index df59065..7be1e2f 100644 --- a/src/tdlibwrapper.cpp +++ b/src/tdlibwrapper.cpp @@ -71,6 +71,7 @@ TDLibWrapper::TDLibWrapper(QObject *parent) : QObject(parent), settings("harbour connect(this->tdLibReceiver, SIGNAL(notificationUpdated(QVariantMap)), this, SLOT(handleUpdateNotification(QVariantMap))); connect(this->tdLibReceiver, SIGNAL(chatNotificationSettingsUpdated(QString, QVariantMap)), this, SLOT(handleChatNotificationSettingsUpdated(QString, QVariantMap))); connect(this->tdLibReceiver, SIGNAL(messageContentUpdated(QString, QString, QVariantMap)), this, SLOT(handleMessageContentUpdated(QString, QString, QVariantMap))); + connect(this->tdLibReceiver, SIGNAL(messagesDeleted(QString, QVariantList)), this, SLOT(handleMessagesDeleted(QString, QVariantList))); this->tdLibReceiver->start(); @@ -262,7 +263,7 @@ void TDLibWrapper::setChatNotificationSettings(const QString &chatId, const QVar void TDLibWrapper::editMessageText(const QString &chatId, const QString &messageId, const QString &message) { - qDebug() << "[TDLibWrapper] Editiing message text " << chatId << messageId; + qDebug() << "[TDLibWrapper] Editing message text " << chatId << messageId; QVariantMap requestObject; requestObject.insert("@type", "editMessageText"); requestObject.insert("chat_id", chatId); @@ -276,6 +277,17 @@ void TDLibWrapper::editMessageText(const QString &chatId, const QString &message this->sendRequest(requestObject); } +void TDLibWrapper::deleteMessages(const QString &chatId, const QVariantList messageIds) +{ + qDebug() << "[TDLibWrapper] Deleting some messages " << chatId << messageIds; + QVariantMap requestObject; + requestObject.insert("@type", "deleteMessages"); + requestObject.insert("chat_id", chatId); + requestObject.insert("message_ids", messageIds); + requestObject.insert("revoke", true); + this->sendRequest(requestObject); +} + QVariantMap TDLibWrapper::getUserInformation() { return this->userInformation; @@ -597,6 +609,11 @@ void TDLibWrapper::handleMessageContentUpdated(const QString &chatId, const QStr emit messageContentUpdated(chatId, messageId, newContent); } +void TDLibWrapper::handleMessagesDeleted(const QString &chatId, const QVariantList &messageIds) +{ + emit messagesDeleted(chatId, messageIds); +} + void TDLibWrapper::setInitialParameters() { qDebug() << "[TDLibWrapper] Sending initial parameters to TD Lib"; diff --git a/src/tdlibwrapper.h b/src/tdlibwrapper.h index 57f5a5d..9f63969 100644 --- a/src/tdlibwrapper.h +++ b/src/tdlibwrapper.h @@ -95,6 +95,7 @@ public: Q_INVOKABLE void setOptionInteger(const QString &optionName, const int &optionValue); Q_INVOKABLE void setChatNotificationSettings(const QString &chatId, const QVariantMap ¬ificationSettings); Q_INVOKABLE void editMessageText(const QString &chatId, const QString &messageId, const QString &message); + Q_INVOKABLE void deleteMessages(const QString &chatId, const QVariantList messageIds); signals: void versionDetected(const QString &version); @@ -125,6 +126,7 @@ signals: void notificationUpdated(const QVariantMap updatedNotification); void chatNotificationSettingsUpdated(const QString &chatId, const QVariantMap chatNotificationSettings); void messageContentUpdated(const QString &chatId, const QString &messageId, const QVariantMap &newContent); + void messagesDeleted(const QString &chatId, const QVariantList &messageIds); public slots: void handleVersionDetected(const QString &version); @@ -153,6 +155,7 @@ public slots: void handleUpdateNotification(const QVariantMap updatedNotification); void handleChatNotificationSettingsUpdated(const QString &chatId, const QVariantMap &chatNotificationSettings); void handleMessageContentUpdated(const QString &chatId, const QString &messageId, const QVariantMap &newContent); + void handleMessagesDeleted(const QString &chatId, const QVariantList &messageIds); private: void *tdLibClient; diff --git a/translations/harbour-fernschreiber-de.ts b/translations/harbour-fernschreiber-de.ts index 3e904ce..8ab5d7c 100644 --- a/translations/harbour-fernschreiber-de.ts +++ b/translations/harbour-fernschreiber-de.ts @@ -163,11 +163,19 @@ Edit Message - Nachricht editieren + Nachricht bearbeiten edited - editiert + bearbeitet + + + Deleting message + Lösche Nachricht + + + Delete Message + Nachricht löschen diff --git a/translations/harbour-fernschreiber.ts b/translations/harbour-fernschreiber.ts index fe92d58..19cc0a3 100644 --- a/translations/harbour-fernschreiber.ts +++ b/translations/harbour-fernschreiber.ts @@ -169,6 +169,14 @@ edited + + Deleting message + + + + Delete Message + + CoverPage