diff --git a/imports/NeoChat/Component/ChatBox/ChatBar.qml b/imports/NeoChat/Component/ChatBox/ChatBar.qml index 707216550..19dfa080d 100644 --- a/imports/NeoChat/Component/ChatBox/ChatBar.qml +++ b/imports/NeoChat/Component/ChatBox/ChatBar.qml @@ -339,6 +339,7 @@ ToolBar { function postMessage() { checkForFancyEffectsReason(); + if (ChatBoxHelper.hasAttachment) { // send attachment but don't reset the text actionsHandler.postMessage("", ChatBoxHelper.attachmentPath, @@ -347,8 +348,16 @@ ToolBar { messageSent(); return; } - actionsHandler.postMessage(inputField.text.trim(), ChatBoxHelper.attachmentPath, - ChatBoxHelper.replyEventId, ChatBoxHelper.editEventId, userAutocompleted); + + const re = /^s\/([^\/]*)\/([^\/]*)/; + if (Config.allowQuickEdit && re.test(inputField.text)) { + // send edited messages + actionsHandler.postEdit(inputField.text); + } else { + // send normal message + actionsHandler.postMessage(inputField.text.trim(), ChatBoxHelper.attachmentPath, + ChatBoxHelper.replyEventId, ChatBoxHelper.editEventId, userAutocompleted); + } currentRoom.markAllMessagesAsRead(); inputField.clear(); inputField.text = Qt.binding(function() { diff --git a/imports/NeoChat/Page/SettingsPage.qml b/imports/NeoChat/Page/SettingsPage.qml index 580be4caa..386b60cf1 100644 --- a/imports/NeoChat/Page/SettingsPage.qml +++ b/imports/NeoChat/Page/SettingsPage.qml @@ -76,5 +76,13 @@ Kirigami.ScrollablePage { Config.save() } } + QQC2.CheckBox { + text: i18n("Use s/text/replacement syntax to edit your last message") + checked: Config.allowQuickEdit + onToggled: { + Config.allowQuickEdit = checked + Config.save() + } + } } } diff --git a/src/actionshandler.cpp b/src/actionshandler.cpp index 4c66e792e..43fbeb862 100644 --- a/src/actionshandler.cpp +++ b/src/actionshandler.cpp @@ -2,8 +2,10 @@ // SPDX-License-Identifier: GPL-3.0-or-later #include "actionshandler.h" +#include "controller.h" -#include "csapi/joining.h" +#include +#include #include #include @@ -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(&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(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, diff --git a/src/actionshandler.h b/src/actionshandler.h index acd4af9e5..fe2e35ba0 100644 --- a/src/actionshandler.h +++ b/src/actionshandler.h @@ -58,6 +58,12 @@ public Q_SLOTS: void postMessage(const QString &text, const QString &attachementPath, const QString &replyEventId, const QString &editEventId, const QVariantMap &usernames); + /// \brief Send edit instructions (.e.g s/hallo/hello/) + /// + /// This will automatically edit the last message posted and send the sed + /// instruction to IRC. + void postEdit(const QString &text); + private: Connection *m_connection = nullptr; NeoChatRoom *m_room = nullptr; diff --git a/src/neochatconfig.kcfg b/src/neochatconfig.kcfg index 07b32dd0d..aa22e0bad 100644 --- a/src/neochatconfig.kcfg +++ b/src/neochatconfig.kcfg @@ -30,6 +30,10 @@ true + + + false +