diff --git a/Quotient/room.cpp b/Quotient/room.cpp index bca9abb83..cf18238b1 100644 --- a/Quotient/room.cpp +++ b/Quotient/room.cpp @@ -52,7 +52,6 @@ #include "events/roomcanonicalaliasevent.h" #include "events/roomcreateevent.h" #include "events/roommemberevent.h" -#include "events/roommessageevent.h" #include "events/roompowerlevelsevent.h" #include "events/roomtombstoneevent.h" #include "events/simplestateevents.h" @@ -294,6 +293,25 @@ class Q_DECL_HIDDEN Room::Private { const PendingEventItem& sendEvent(RoomEventPtr&& event); + // template + template + QString postAllText(const QString& plainText, + std::optional html, + std::optional relatesTo) + { + static_assert(type == MessageEventType::Text || + type == MessageEventType::Emote || + type == MessageEventType::Notice , + "MessageEvent type is not a text message" + ); + + std::unique_ptr content = nullptr; + if (html) { + content = std::make_unique(*html, u"text/html"_s); + } + return q->post(plainText, type, std::move(content), relatesTo)->transactionId(); + } + QString doPostFile(event_ptr_tt fileEvent, const QUrl& localUrl); PendingEvents::iterator addAsPending(RoomEventPtr&& event); @@ -2122,37 +2140,19 @@ void Room::discardMessage(const QString& txnId) emit pendingEventDiscarded(); } -QString Room::postEmote(const QString& plainText, std::optional html, std::optional relatesTo) +QString Room::postText(const QString& plainText, std::optional html, std::optional relatesTo) { - std::unique_ptr content = nullptr; - if (html) { - content = std::make_unique(*html, u"text/html"_s); - } - - return post(plainText, MessageEventType::Emote, std::move(content), relatesTo)->transactionId(); + return d->postAllText(plainText, html, relatesTo); } -QString Room::postNotice(const QString& plainText, std::optional html, std::optional relatesTo) -{ - std::unique_ptr content = nullptr; - if (html) { - content = std::make_unique(*html, u"text/html"_s); - } - - return post(plainText, MessageEventType::Notice, std::move(content), relatesTo)->transactionId(); -} - -QString Room::postPlainText(const QString& plainText, std::optional relatesTo) +QString Room::postEmote(const QString& plainText, std::optional html, std::optional relatesTo) { - return post(plainText, MessageEventType::Text, nullptr, relatesTo)->transactionId(); + return d->postAllText(plainText, html, relatesTo); } -QString Room::postHtmlText(const QString& plainText, const QString& html, std::optional relatesTo) +QString Room::postNotice(const QString& plainText, std::optional html, std::optional relatesTo) { - return post(plainText, MessageEventType::Text, - std::make_unique(html, u"text/html"_s), - relatesTo) - ->transactionId(); + return d->postAllText(plainText, html, relatesTo); } QString Room::postReaction(const QString& eventId, const QString& key) diff --git a/Quotient/room.h b/Quotient/room.h index e13f20b78..d5f53527e 100644 --- a/Quotient/room.h +++ b/Quotient/room.h @@ -721,27 +721,24 @@ class QUOTIENT_API Room : public QObject { return post(makeEvent(std::forward(args)...)); } - /// Send a plain text message - QString postPlainText(const QString& plainText, std::optional relatesTo = std::nullopt); + //! Send a text message + QString postText(const QString& plainText, std::optional html = std::nullopt, std::optional relatesTo = std::nullopt); - /// Send a rich text message - QString postHtmlText(const QString& plainText, const QString& html, std::optional relatesTo = std::nullopt); - - /// Send a m.emote message + //! Send a m.emote message QString postEmote(const QString& plainText, std::optional html = std::nullopt, std::optional relatesTo = std::nullopt); - /// Send an m.notice message + //! Send an m.notice message QString postNotice(const QString& plainText, std::optional html = std::nullopt, std::optional relatesTo = std::nullopt); - /// Send a file with the given content + //! Send a file with the given content QString postFile(const QString& plainText, std::unique_ptr fileContent, std::optional relatesTo = std::nullopt); - /// Send the given Json as a message + //! Send the given Json as a message QString postJson(const QString& matrixType, const QJsonObject& eventContent); - /// Send a reaction on a given event with a given key + //! Send a reaction on a given event with a given key QString postReaction(const QString& eventId, const QString& key); PendingEventItem::future_type whenMessageMerged(QString txnId) const; diff --git a/quotest/quotest.cpp b/quotest/quotest.cpp index 36aefc7e6..21dfd17f1 100644 --- a/quotest/quotest.cpp +++ b/quotest/quotest.cpp @@ -173,7 +173,7 @@ void TestSuite::finishTest(const TestToken& token, bool condition, } else { clog << item << " FAILED at " << file << ":" << line << endl; if (targetRoom) - targetRoom->postPlainText(origin % ": "_L1 % QString::fromUtf8(item) % " FAILED at "_L1 + targetRoom->postText(origin % ": "_L1 % QString::fromUtf8(item) % " FAILED at "_L1 % QString::fromUtf8(file) % ", line "_L1 % QString::number(line)); } @@ -363,7 +363,7 @@ TEST_IMPL(loadMembers) TEST_IMPL(sendMessage) { - auto txnId = targetRoom->postPlainText("Hello, "_L1 % origin % " is here"_L1); + auto txnId = targetRoom->postText("Hello, "_L1 % origin % " is here"_L1); if (!validatePendingEvent(txnId)) { clog << "Invalid pending event right after submitting" << endl; FAIL_TEST(); @@ -462,7 +462,7 @@ TEST_IMPL(sendFile) if (id != txnId) return false; - targetRoom->postPlainText(origin % ": File upload failed: "_L1 % error); + targetRoom->postText(origin % ": File upload failed: "_L1 % error); tf->deleteLater(); FAIL_TEST(); }); @@ -922,7 +922,7 @@ void TestManager::conclude() htmlReport += "
Did not finish:"_L1 + QString::fromUtf8(dnfList); } - auto txnId = room->postHtmlText(plainReport, htmlReport); + auto txnId = room->postText(plainReport, htmlReport); // Now just wait until all the pending events reach the server connectUntil(room, &Room::messageSent, this, [this, txnId, room, plainReport] {