Edit message seems to work

This commit is contained in:
Sebastian J. Wolf 2020-09-19 20:33:51 +02:00
parent c2492efa0f
commit b78a0f8731
9 changed files with 88 additions and 3 deletions

View file

@ -109,6 +109,11 @@ Page {
function getMessageStatusText(message, listItemIndex, lastReadSentIndex) {
var messageStatusSuffix = "";
if (message.edit_date > 0) {
messageStatusSuffix += " - " + qsTr("edited");
}
if (chatPage.myUserId === message.sender_user_id) {
messageStatusSuffix += "  "
if (listItemIndex <= lastReadSentIndex) {
@ -582,6 +587,13 @@ Page {
console.log("[ChatModel] Messages in this chat were read, new last read: " + lastReadSentIndex + ", updating description for index " + index + ", status: " + (index <= lastReadSentIndex));
messageDateText.text = getMessageStatusText(display, index, lastReadSentIndex);
}
onMessageUpdated: {
if (index === modelIndex) {
console.log("[ChatModel] This message was updated, index " + index + ", updating content...");
messageDateText.text = getMessageStatusText(display, index, chatView.lastReadSentIndex);
messageText.text = Emoji.emojify(Functions.getMessageText(display, false), font.pixelSize);
}
}
}
Text {
@ -721,7 +733,11 @@ Page {
}
EnterKey.onClicked: {
if (tdLibWrapper.getSendByEnter()) {
tdLibWrapper.sendTextMessage(chatInformation.id, newMessageTextField.text, newMessageColumn.replyToMessageId);
if (newMessageColumn.editMessageId !== "0") {
tdLibWrapper.editMessageText(chatInformation.id, newMessageColumn.editMessageId, newMessageTextField.text);
} else {
tdLibWrapper.sendTextMessage(chatInformation.id, newMessageTextField.text, newMessageColumn.replyToMessageId);
}
newMessageTextField.text = "";
newMessageTextField.focus = false;
}
@ -760,7 +776,11 @@ Page {
anchors.horizontalCenter: parent.horizontalCenter
enabled: false
onClicked: {
tdLibWrapper.sendTextMessage(chatInformation.id, newMessageTextField.text, newMessageColumn.replyToMessageId);
if (newMessageColumn.editMessageId !== "0") {
tdLibWrapper.editMessageText(chatInformation.id, newMessageColumn.editMessageId, newMessageTextField.text);
} else {
tdLibWrapper.sendTextMessage(chatInformation.id, newMessageTextField.text, newMessageColumn.replyToMessageId);
}
newMessageTextField.text = "";
newMessageTextField.focus = false;
}

View file

@ -34,6 +34,7 @@ ChatModel::ChatModel(TDLibWrapper *tdLibWrapper)
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(messageContentUpdated(QString, QString, QVariantMap)), this, SLOT(handleMessageContentUpdated(QString, QString, QVariantMap)));
}
ChatModel::~ChatModel()
@ -223,6 +224,24 @@ void ChatModel::handleChatNotificationSettingsUpdated(const QString &chatId, con
}
}
void ChatModel::handleMessageContentUpdated(const QString &chatId, const QString &messageId, const QVariantMap &newContent)
{
qDebug() << "[ChatModel] Message content updated" << chatId << messageId;
if (chatId == this->chatId && this->messageIndexMap.contains(messageId)) {
this->messagesMutex.lock();
qDebug() << "[ChatModel] 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;
this->messagesMutex.unlock();
emit messageUpdated(messageIndex);
emit dataChanged(index(messageIndex), index(messageIndex));
}
}
void ChatModel::insertMessages()
{
if (this->messages.isEmpty()) {

View file

@ -48,6 +48,7 @@ signals:
void unreadCountUpdated(const int &unreadCount, const QString &lastReadInboxMessageId);
void lastReadSentMessageUpdated(const int &lastReadSentIndex);
void notificationSettingsUpdated();
void messageUpdated(const int &modelIndex);
public slots:
void handleMessagesReceived(const QVariantList &messages);
@ -56,6 +57,7 @@ public slots:
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 handleMessageContentUpdated(const QString &chatId, const QString &messageId, const QVariantMap &newContent);
private:

View file

@ -78,6 +78,7 @@ void TDLibReceiver::processReceivedDocument(const QJsonDocument &receivedJsonDoc
if (objectTypeName == "updateActiveNotifications") { this->processUpdateActiveNotifications(receivedInformation); }
if (objectTypeName == "updateNotificationGroup") { this->processUpdateNotificationGroup(receivedInformation); }
if (objectTypeName == "updateChatNotificationSettings") { this->processUpdateChatNotificationSettings(receivedInformation); }
if (objectTypeName == "updateMessageContent") { this->processUpdateMessageContent(receivedInformation); }
}
void TDLibReceiver::processUpdateOption(const QVariantMap &receivedInformation)
@ -266,3 +267,11 @@ void TDLibReceiver::processUpdateChatNotificationSettings(const QVariantMap &rec
qDebug() << "[TDLibReceiver] Received new notification settings for chat " << chatId;
emit chatNotificationSettingsUpdated(chatId, receivedInformation.value("notification_settings").toMap());
}
void TDLibReceiver::processUpdateMessageContent(const QVariantMap &receivedInformation)
{
QString chatId = receivedInformation.value("chat_id").toString();
QString messageId = receivedInformation.value("message_id").toString();
qDebug() << "[TDLibReceiver] Message content updated " << chatId << messageId;
emit messageContentUpdated(chatId, messageId, receivedInformation.value("new_content").toMap());
}

View file

@ -61,6 +61,7 @@ signals:
void notificationGroupUpdated(const QVariantMap notificationGroupUpdate);
void notificationUpdated(const QVariantMap updatedNotification);
void chatNotificationSettingsUpdated(const QString &chatId, const QVariantMap updatedChatNotificationSettings);
void messageContentUpdated(const QString &chatId, const QString &messageId, const QVariantMap &newContent);
private:
void *tdLibClient;
@ -93,6 +94,7 @@ private:
void processUpdateNotificationGroup(const QVariantMap &receivedInformation);
void processUpdateNotification(const QVariantMap &receivedInformation);
void processUpdateChatNotificationSettings(const QVariantMap &receivedInformation);
void processUpdateMessageContent(const QVariantMap &receivedInformation);
};
#endif // TDLIBRECEIVER_H

View file

@ -70,6 +70,7 @@ TDLibWrapper::TDLibWrapper(QObject *parent) : QObject(parent), settings("harbour
connect(this->tdLibReceiver, SIGNAL(notificationGroupUpdated(QVariantMap)), this, SLOT(handleUpdateNotificationGroup(QVariantMap)));
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)));
this->tdLibReceiver->start();
@ -259,6 +260,22 @@ void TDLibWrapper::setChatNotificationSettings(const QString &chatId, const QVar
this->sendRequest(requestObject);
}
void TDLibWrapper::editMessageText(const QString &chatId, const QString &messageId, const QString &message)
{
qDebug() << "[TDLibWrapper] Editiing message text " << chatId << messageId;
QVariantMap requestObject;
requestObject.insert("@type", "editMessageText");
requestObject.insert("chat_id", chatId);
requestObject.insert("message_id", messageId);
QVariantMap inputMessageContent;
inputMessageContent.insert("@type", "inputMessageText");
QVariantMap formattedText;
formattedText.insert("text", message);
inputMessageContent.insert("text", formattedText);
requestObject.insert("input_message_content", inputMessageContent);
this->sendRequest(requestObject);
}
QVariantMap TDLibWrapper::getUserInformation()
{
return this->userInformation;
@ -575,6 +592,11 @@ void TDLibWrapper::handleChatNotificationSettingsUpdated(const QString &chatId,
emit chatNotificationSettingsUpdated(chatId, chatNotificationSettings);
}
void TDLibWrapper::handleMessageContentUpdated(const QString &chatId, const QString &messageId, const QVariantMap &newContent)
{
emit messageContentUpdated(chatId, messageId, newContent);
}
void TDLibWrapper::setInitialParameters()
{
qDebug() << "[TDLibWrapper] Sending initial parameters to TD Lib";

View file

@ -94,6 +94,7 @@ public:
Q_INVOKABLE void getMessage(const QString &chatId, const QString &messageId);
Q_INVOKABLE void setOptionInteger(const QString &optionName, const int &optionValue);
Q_INVOKABLE void setChatNotificationSettings(const QString &chatId, const QVariantMap &notificationSettings);
Q_INVOKABLE void editMessageText(const QString &chatId, const QString &messageId, const QString &message);
signals:
void versionDetected(const QString &version);
@ -123,6 +124,7 @@ signals:
void notificationGroupUpdated(const QVariantMap notificationGroupUpdate);
void notificationUpdated(const QVariantMap updatedNotification);
void chatNotificationSettingsUpdated(const QString &chatId, const QVariantMap chatNotificationSettings);
void messageContentUpdated(const QString &chatId, const QString &messageId, const QVariantMap &newContent);
public slots:
void handleVersionDetected(const QString &version);
@ -150,6 +152,7 @@ public slots:
void handleUpdateNotificationGroup(const QVariantMap notificationGroupUpdate);
void handleUpdateNotification(const QVariantMap updatedNotification);
void handleChatNotificationSettingsUpdated(const QString &chatId, const QVariantMap &chatNotificationSettings);
void handleMessageContentUpdated(const QString &chatId, const QString &messageId, const QVariantMap &newContent);
private:
void *tdLibClient;

View file

@ -163,7 +163,11 @@
</message>
<message>
<source>Edit Message</source>
<translation>Nachricht ändern</translation>
<translation>Nachricht editieren</translation>
</message>
<message>
<source>edited</source>
<translation>editiert</translation>
</message>
</context>
<context>

View file

@ -165,6 +165,10 @@
<source>Edit Message</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>edited</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>CoverPage</name>