Skip to content

Commit

Permalink
Support event relations on all post message functions
Browse files Browse the repository at this point in the history
This is a backwards-compatible version of #806 for 0.9.x.
  • Loading branch information
KitsuneRal committed Jan 16, 2025
1 parent 5d682d9 commit 8ff75c3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
9 changes: 8 additions & 1 deletion Quotient/room.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2238,6 +2238,13 @@ QString Room::Private::doPostFile(event_ptr_tt<RoomMessageEvent> fileEvent, cons

QString Room::postFile(const QString& plainText,
std::unique_ptr<EventContent::FileContentBase> fileContent)
{
return postFile(plainText, std::move(fileContent), std::nullopt);
}

QString Room::postFile(const QString& plainText,
std::unique_ptr<EventContent::FileContentBase> fileContent,
std::optional<EventRelation> relatesTo)
{
Q_ASSERT(fileContent != nullptr);
const auto url = fileContent->url();
Expand All @@ -2247,7 +2254,7 @@ QString Room::postFile(const QString& plainText,

return d->doPostFile(makeEvent<RoomMessageEvent>(plainText,
RoomMessageEvent::rawMsgTypeForFile(localFile),
std::move(fileContent)),
std::move(fileContent), relatesTo),
url);
}

Expand Down
28 changes: 28 additions & 0 deletions Quotient/room.h
Original file line number Diff line number Diff line change
Expand Up @@ -726,8 +726,31 @@ class QUOTIENT_API Room : public QObject {
return post(makeEvent<EvT>(std::forward<ArgTs>(args)...));
}

//! \brief Send a text type message
//!
//! This means MessageEventType Text, Emote or Notice.
template<MessageEventType type = MessageEventType::Text>
QString postText(const QString& plainText,
const std::optional<QString>& html = std::nullopt,
const std::optional<EventRelation>& relatesTo = std::nullopt)
{
static_assert(type == MessageEventType::Text || type == MessageEventType::Emote
|| type == MessageEventType::Notice,
"MessageEvent type is not a text message");

return post<RoomMessageEvent>(
plainText, type,
html ? std::make_unique<EventContent::TextContent>(*html, u"text/html"_s)
: nullptr,
relatesTo)
->transactionId();
}

QString postFile(const QString& plainText,
std::unique_ptr<EventContent::FileContentBase> fileContent);
QString postFile(const QString& plainText,
std::unique_ptr<EventContent::FileContentBase> fileContent,
std::optional<EventRelation> relatesTo);

PendingEventItem::future_type whenMessageMerged(QString txnId) const;

Expand All @@ -754,11 +777,16 @@ public Q_SLOTS:
/** Check whether the room should be upgraded */
void checkVersion();

[[deprecated("Use postText() instead")]]
QString postMessage(const QString& plainText, MessageEventType type);
[[deprecated("Use postText() instead")]]
QString postPlainText(const QString& plainText);
[[deprecated("Use postText() instead")]]
QString postHtmlMessage(const QString& plainText, const QString& html,
MessageEventType type = MessageEventType::Text);
[[deprecated("Use postText() instead")]]
QString postHtmlText(const QString& plainText, const QString& html);

/// Send a reaction on a given event with a given key
QString postReaction(const QString& eventId, const QString& key);

Expand Down
18 changes: 9 additions & 9 deletions quotest/quotest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,14 @@ void TestSuite::finishTest(const TestToken& token, bool condition, std::source_l
if (condition) {
clog << item << " successful" << endl;
if (targetRoom)
targetRoom->postMessage(origin % ": "_L1 % QString::fromUtf8(item) % " successful"_L1,
MessageEventType::Notice);
targetRoom->postText<MessageEventType::Notice>(
origin % ": "_L1 % QString::fromUtf8(item) % " successful"_L1);
} else {
clog << item << " FAILED at " << loc.file_name() << ":" << loc.line() << endl;
if (targetRoom)
targetRoom->postPlainText(origin % ": "_L1 % QString::fromUtf8(item) % " FAILED at "_L1
% QString::fromUtf8(loc.file_name()) % ", line "_L1
% QString::number(loc.line()));
targetRoom->postText(origin % ": "_L1 % QString::fromUtf8(item) % " FAILED at "_L1
% QString::fromUtf8(loc.file_name()) % ", line "_L1
% QString::number(loc.line()));
}

emit finishedItem(item, condition);
Expand Down Expand Up @@ -380,7 +380,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<RoomMessageEvent>(txnId)) {
clog << "Invalid pending event right after submitting" << endl;
FAIL_TEST();
Expand Down Expand Up @@ -478,7 +478,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();
});
Expand Down Expand Up @@ -906,7 +906,7 @@ TEST_IMPL(visitResources)

TEST_IMPL(thread)
{
auto rootTxnId = targetRoom->postPlainText("Threadroot"_L1);
auto rootTxnId = targetRoom->postText("Threadroot"_L1);
connect(targetRoom, &Room::pendingEventAboutToMerge, this, [this, thisTest, rootTxnId](Quotient::RoomEvent* rootEvt) {
if (rootEvt->transactionId() == rootTxnId) {
const auto relation = EventRelation::replyInThread(rootEvt->id(), true, rootEvt->id());
Expand Down Expand Up @@ -976,7 +976,7 @@ void TestManager::conclude()
htmlReport += "<br><strong>Did not finish:</strong>"_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] {
const auto& pendingEvents = room->pendingEvents();
Expand Down

0 comments on commit 8ff75c3

Please sign in to comment.