Skip to content
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
8 changes: 8 additions & 0 deletions Quotient/events/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,14 @@ class QUOTIENT_API Event {
explicit Event(const QJsonObject& json);

QJsonObject& editJson() { return _json; }

virtual void onContentChanged() {}
void editContentJson(auto visitor)
{
editSubobject(_json, ContentKey, visitor);
onContentChanged();
}

virtual void dumpTo(QDebug dbg) const;

private:
Expand Down
15 changes: 10 additions & 5 deletions Quotient/events/roommemberevent.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,17 @@ class QUOTIENT_API RoomMemberEvent

using KeyedStateEventBase::KeyedStateEventBase;

Membership membership() const { return content().membership; }
QUO_CONTENT_GETTER(Membership, membership)
// Membership membership() const { return content().membership; }
QString userId() const { return stateKey(); }
bool isDirect() const { return content().isDirect; }
std::optional<QString> newDisplayName() const { return content().displayName; }
std::optional<QUrl> newAvatarUrl() const { return content().avatarUrl; }
QString reason() const { return content().reason; }
QUO_CONTENT_GETTER(bool, isDirect)
// bool isDirect() const { return content().isDirect; }
QUO_CONTENT_GETTER_X(std::optional<QString>, newDisplayName, "displayname"_L1)
// std::optional<QString> newDisplayName() const { return content().displayName; }
QUO_CONTENT_GETTER_X(std::optional<QUrl>, newAvatarUrl, "avatar_url"_L1)
// std::optional<QUrl> newAvatarUrl() const { return content().avatarUrl; }
QUO_CONTENT_GETTER(QString, reason)
// QString reason() const { return content().reason; }
bool changesMembership() const;
bool isBan() const;
bool isUnban() const;
Expand Down
5 changes: 2 additions & 3 deletions Quotient/events/roommessageevent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,8 @@ QString RoomMessageEvent::fileNameToDownload() const

void RoomMessageEvent::updateFileSourceInfo(const FileSourceInfo& fsi)
{
editSubobject(editJson(), ContentKey, [&fsi](QJsonObject& contentJson) {
Quotient::fillJson(contentJson, { "url"_L1, "file"_L1 }, fsi);
});
editContentJson(
[&fsi](QJsonObject &contentJson) { fillJson(contentJson, {"url"_L1, "file"_L1}, fsi); });
}

QString rawMsgTypeForMimeType(const QMimeType& mimeType)
Expand Down
21 changes: 11 additions & 10 deletions Quotient/events/simplestateevents.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@

namespace Quotient {

#define DEFINE_SIMPLE_STATE_EVENT(Name_, TypeId_, ValueType_, GetterName_, JsonKey_) \
constexpr inline auto Name_##Key = JsonKey_##_L1; \
class QUOTIENT_API Name_ : public ::Quotient::KeylessStateEventBase< \
Name_, EventContent::SingleKeyValue<ValueType_, Name_##Key>> { \
public: \
using value_type = ValueType_; \
QUO_EVENT(Name_, TypeId_) \
using KeylessStateEventBase::KeylessStateEventBase; \
auto GetterName_() const { return content().value; } \
}; \
#define DEFINE_SIMPLE_STATE_EVENT(Name_, TypeId_, ValueType_, GetterName_, JsonKey_) \
constexpr inline auto Name_##Key = JsonKey_##_L1; \
class QUOTIENT_API Name_ : public ::Quotient::KeylessStateEventBase< \
Name_, EventContent::SingleKeyValue<ValueType_, Name_##Key>> \
{ \
public: \
using value_type = ValueType_; \
QUO_EVENT(Name_, TypeId_) \
using KeylessStateEventBase::KeylessStateEventBase; \
QUO_CONTENT_GETTER_X(ValueType_, GetterName_, Name_##Key) \
}; \
// End of macro

DEFINE_SIMPLE_STATE_EVENT(RoomNameEvent, "m.room.name", QString, name, "name")
Expand Down
52 changes: 19 additions & 33 deletions Quotient/events/stateevent.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,44 +63,30 @@ class EventTemplate<EventT, StateEvent, ContentT>
public:
using content_type = ContentT;

struct Prev {
explicit Prev() = default;
explicit Prev(const QJsonObject& unsignedJson)
: senderId(fromJson<QString>(unsignedJson["prev_sender"_L1]))
, content(fromJson<std::optional<ContentT>>(unsignedJson[PrevContentKey]))
{}

QString senderId;
std::optional<ContentT> content;
};

explicit EventTemplate(const QJsonObject& fullJson)
: StateEvent(fullJson)
, _content(fromJson<ContentT>(Event::contentJson()))
, _prev(unsignedJson())
{}
explicit EventTemplate(const QJsonObject &fullJson) : StateEvent(fullJson) {}

template <typename... ContentParamTs>
explicit EventTemplate(const QString& stateKey,
ContentParamTs&&... contentParams)
: StateEvent(EventT::TypeId, stateKey)
, _content { std::forward<ContentParamTs>(contentParams)... }
{
editJson().insert(ContentKey, toJson(_content));
}
explicit EventTemplate(const QString &stateKey, ContentParamTs &&...contentParams)
: StateEvent(EventT::TypeId, stateKey,
toJson(ContentT{std::forward<ContentParamTs>(contentParams)...}))
{}

const ContentT& content() const { return _content; }
ContentT content() const { return fromJson<ContentT>(Event::contentJson()); }

void editContent(auto&& visitor)
template <std::invocable<ContentT &> VisitorT>
void editContent(VisitorT &&visitor)
{
visitor(_content);
editJson()[ContentKey] = toJson(_content);
editContentJson([&visitor](QJsonObject &contentJson) mutable {
auto content = fromJson<ContentT>(contentJson);
std::invoke(std::forward<VisitorT>(visitor), content);
contentJson = toJson(content);
});
}
const std::optional<ContentT>& prevContent() const { return _prev.content; }
QString prevSenderId() const { return _prev.senderId; }

private:
ContentT _content;
Prev _prev;
std::optional<ContentT> prevContent() const
{
return unsignedPart<std::optional<ContentT>>(PrevContentKey);
}
QString prevSenderId() const { return unsignedPart<QString>("prev_sender"_L1); }
};

template <typename EventT, typename ContentT>
Expand Down
3 changes: 1 addition & 2 deletions Quotient/room.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3307,8 +3307,7 @@ Room::Change Room::Private::processStateEvent(const RoomEvent& curEvent,
},
[this, oldEvent](const JoinRulesEvent& evt) {
if (const auto* oldJRE = static_cast<const JoinRulesEvent*>(oldEvent);
oldJRE && oldJRE->content().joinRule != evt.content().joinRule
) {
oldJRE && oldJRE->joinRule() != evt.joinRule()) {
emit q->joinRuleChanged();
}
return Change::Other;
Expand Down