Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support event relations on all post message functions #806

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 4 additions & 30 deletions Quotient/room.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2121,29 +2121,6 @@ void Room::discardMessage(const QString& txnId)
emit pendingEventDiscarded();
}

QString Room::postMessage(const QString& plainText, MessageEventType type)
{
return post<RoomMessageEvent>(plainText, type)->transactionId();
}

QString Room::postPlainText(const QString& plainText)
{
return postMessage(plainText, MessageEventType::Text);
}

QString Room::postHtmlMessage(const QString& plainText, const QString& html,
MessageEventType type)
{
return post<RoomMessageEvent>(plainText, type,
std::make_unique<EventContent::TextContent>(html, u"text/html"_s))
->transactionId();
}

QString Room::postHtmlText(const QString& plainText, const QString& html)
{
return postHtmlMessage(plainText, html);
}

QString Room::postReaction(const QString& eventId, const QString& key)
{
return post<ReactionEvent>(eventId, key)->transactionId();
Expand Down Expand Up @@ -2198,7 +2175,8 @@ QString Room::Private::doPostFile(event_ptr_tt<RoomMessageEvent> fileEvent, cons
}

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

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

QString Room::postEvent(RoomEvent* event)
{
return d->sendEvent(RoomEventPtr(event))->transactionId();
}

const PendingEventItem& Room::post(RoomEventPtr event)
{
return d->sendEvent(std::move(event));
Expand Down
48 changes: 30 additions & 18 deletions Quotient/room.h
Original file line number Diff line number Diff line change
Expand Up @@ -721,8 +721,37 @@ 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"
);

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

//! Send a file with the given content
QString postFile(const QString& plainText,
std::unique_ptr<EventContent::FileContentBase> fileContent);
std::unique_ptr<EventContent::FileContentBase> fileContent,
std::optional<EventRelation> relatesTo = std::nullopt);

//! 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
QString postReaction(const QString& eventId, const QString& key);

PendingEventItem::future_type whenMessageMerged(QString txnId) const;

Expand All @@ -749,23 +778,6 @@ public Q_SLOTS:
/** Check whether the room should be upgraded */
void checkVersion();

QString postMessage(const QString& plainText, MessageEventType type);
QString postPlainText(const QString& plainText);
QString postHtmlMessage(const QString& plainText, const QString& html,
MessageEventType type = MessageEventType::Text);
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);

/** Post a pre-created room message event
*
* Takes ownership of the event, deleting it once the matching one
* arrives with the sync
* \return transaction id associated with the event.
*/
[[deprecated("Use post() instead")]]
QString postEvent(RoomEvent* event);
QString postJson(const QString& matrixType, const QJsonObject& eventContent);
QString retryMessage(const QString& txnId);
void discardMessage(const QString& txnId);

Expand Down
11 changes: 5 additions & 6 deletions quotest/quotest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,11 @@ void TestSuite::finishTest(const TestToken& token, bool condition,
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 " << 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));
}

Expand Down Expand Up @@ -364,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<RoomMessageEvent>(txnId)) {
clog << "Invalid pending event right after submitting" << endl;
FAIL_TEST();
Expand Down Expand Up @@ -463,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();
});
Expand Down Expand Up @@ -923,7 +922,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] {
Expand Down