Skip to content

Commit

Permalink
Make all the functions non-callable from QML and remove postMessage b…
Browse files Browse the repository at this point in the history
…ecause we probably shouldn't allow someone to send a message with an arbritary type but not the correct content. This is replaced by postEmote and postNotice the two types that are probably valid from the original postMessage function.
  • Loading branch information
nvrWhere committed Nov 19, 2024
1 parent eecba51 commit 1b217c5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 36 deletions.
37 changes: 21 additions & 16 deletions Quotient/room.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#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"
Expand Down Expand Up @@ -2121,28 +2122,37 @@ void Room::discardMessage(const QString& txnId)
emit pendingEventDiscarded();
}

QString Room::postMessage(const QString& plainText, MessageEventType type, std::optional<EventRelation> relatesTo)
QString Room::postEmote(const QString& plainText, std::optional<const QString> html, std::optional<EventRelation> relatesTo)
{
return post<RoomMessageEvent>(plainText, type, nullptr, relatesTo)->transactionId();
std::unique_ptr<EventContent::TextContent> content = nullptr;
if (html) {
content = std::make_unique<EventContent::TextContent>(*html, u"text/html"_s);
}

return post<RoomMessageEvent>(plainText, MessageEventType::Emote, std::move(content), relatesTo)->transactionId();
}

QString Room::postPlainText(const QString& plainText, std::optional<EventRelation> relatesTo)
QString Room::postNotice(const QString& plainText, std::optional<const QString> html, std::optional<EventRelation> relatesTo)
{
return postMessage(plainText, MessageEventType::Text, relatesTo);
std::unique_ptr<EventContent::TextContent> content = nullptr;
if (html) {
content = std::make_unique<EventContent::TextContent>(*html, u"text/html"_s);
}

return post<RoomMessageEvent>(plainText, MessageEventType::Notice, std::move(content), relatesTo)->transactionId();
}

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

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

QString Room::postReaction(const QString& eventId, const QString& key)
Expand Down Expand Up @@ -2215,11 +2225,6 @@ QString Room::postFile(const QString& plainText,
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
37 changes: 19 additions & 18 deletions Quotient/room.h
Original file line number Diff line number Diff line change
Expand Up @@ -721,10 +721,29 @@ class QUOTIENT_API Room : public QObject {
return post(makeEvent<EvT>(std::forward<ArgTs>(args)...));
}

/// Send a plain text message
QString postPlainText(const QString& plainText, std::optional<EventRelation> relatesTo = std::nullopt);

/// Send a rich text message
QString postHtmlText(const QString& plainText, const QString& html, std::optional<EventRelation> relatesTo = std::nullopt);

/// Send a m.emote message
QString postEmote(const QString& plainText, std::optional<const QString> html = std::nullopt, std::optional<EventRelation> relatesTo = std::nullopt);

/// Send an m.notice message
QString postNotice(const QString& plainText, std::optional<const QString> html = std::nullopt, std::optional<EventRelation> relatesTo = std::nullopt);

/// Send a file with the given content
QString postFile(const QString& plainText,
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;

//! Send a request to update the room state with the given event
Expand All @@ -750,24 +769,6 @@ public Q_SLOTS:
/** Check whether the room should be upgraded */
void checkVersion();

QString postMessage(const QString& plainText, MessageEventType type, std::optional<EventRelation> relatesTo = std::nullopt);
QString postPlainText(const QString& plainText, std::optional<EventRelation> relatesTo = std::nullopt);
QString postHtmlMessage(const QString& plainText, const QString& html,
MessageEventType type = MessageEventType::Text,
std::optional<EventRelation> relatesTo = std::nullopt);
QString postHtmlText(const QString& plainText, const QString& html, std::optional<EventRelation> relatesTo = std::nullopt);
/// 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
3 changes: 1 addition & 2 deletions quotest/quotest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,7 @@ 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->postNotice(origin % ": "_L1 % QString::fromUtf8(item) % " successful"_L1);
} else {
clog << item << " FAILED at " << file << ":" << line << endl;
if (targetRoom)
Expand Down

0 comments on commit 1b217c5

Please sign in to comment.