Implement quick edit

This commit is contained in:
Carl Schwan
2021-05-06 16:14:38 +00:00
parent 7b1c5f5aab
commit 0a51c845e6
5 changed files with 62 additions and 3 deletions

View File

@@ -2,8 +2,10 @@
// SPDX-License-Identifier: GPL-3.0-or-later
#include "actionshandler.h"
#include "controller.h"
#include "csapi/joining.h"
#include <csapi/joining.h>
#include <events/roommessageevent.h>
#include <KLocalizedString>
#include <QDebug>
@@ -56,6 +58,36 @@ void ActionsHandler::setConnection(Connection *connection)
Q_EMIT connectionChanged();
}
void ActionsHandler::postEdit(const QString &text)
{
const auto localId = Controller::instance().activeConnection()->userId();
for (auto it = m_room->messageEvents().crbegin(); it != m_room->messageEvents().crend(); ++it) {
const auto &evt = **it;
if (const auto event = eventCast<const RoomMessageEvent>(&evt)) {
if (event->senderId() == localId && event->hasTextContent()) {
static QRegularExpression re("^s/([^/]*)/([^/]*)");
auto match = re.match(text);
if (!match.hasMatch()) {
// should not happen but still make sure to send the message normally
// just in case.
postMessage(text, QString(), QString(), QString(), QVariantMap());
}
const QString regex = match.captured(1);
const QString replacement = match.captured(2);
QString originalString;
if (event->content()) {
originalString = static_cast<const Quotient::EventContent::TextContent *>(event->content())->body;
} else {
originalString = event->plainBody();
}
m_room->postHtmlMessage(text, originalString.replace(regex, replacement), event->msgtype(), "", event->id());
return;
}
}
}
}
void ActionsHandler::postMessage(const QString &text,
const QString &attachementPath,
const QString &replyEventId,