diff options
-rw-r--r-- | events/roommemberevent.cpp | 43 | ||||
-rw-r--r-- | events/roommemberevent.h | 41 |
2 files changed, 59 insertions, 25 deletions
diff --git a/events/roommemberevent.cpp b/events/roommemberevent.cpp index 19f116d2..40e14c39 100644 --- a/events/roommemberevent.cpp +++ b/events/roommemberevent.cpp @@ -22,21 +22,38 @@ using namespace QMatrixClient; -static const auto membershipStrings = - { "invite", "join", "knock", "leave", "ban" }; +static const std::array<const char*, 5> membershipStrings = + { { "invite", "join", "knock", "leave", "ban" } }; -RoomMemberEvent::RoomMemberEvent(const QJsonObject& obj) - : RoomEvent(Type::RoomMember, obj), _userId(obj["state_key"].toString()) +namespace QMatrixClient { - const auto contentObj = contentJson(); - _displayName = contentObj["displayname"].toString(); - _avatarUrl = contentObj["avatar_url"].toString(); - QString membershipString = contentObj["membership"].toString(); - for (auto it = membershipStrings.begin(); it != membershipStrings.end(); ++it) - if (membershipString == *it) + template <> + struct FromJson<MembershipType> + { + MembershipType operator()(const QJsonValue& jv) const { - _membership = MembershipType(it - membershipStrings.begin()); - return; + const auto membershipString = jv.toString(); + for (auto it = membershipStrings.begin(); + it != membershipStrings.end(); ++it) + if (membershipString == *it) + return MembershipType(it - membershipStrings.begin()); + + qCWarning(EVENTS) << "Unknown MembershipType: " << membershipString; + return MembershipType::Join; } - qCWarning(EVENTS) << "Unknown MembershipType: " << membershipString; + }; +} + +MemberEventContent::MemberEventContent(const QJsonObject& json) + : membership(fromJson<MembershipType>(json["membership"])) + , displayName(json["displayname"].toString()) + , avatarUrl(json["avatar_url"].toString()) +{ } + +void MemberEventContent::fillJson(QJsonObject* o) const +{ + Q_ASSERT(o); + o->insert("membership", membershipStrings[membership]); + o->insert("displayname", displayName); + o->insert("avatar_url", avatarUrl.toString()); } diff --git a/events/roommemberevent.h b/events/roommemberevent.h index 358af257..d0c63f15 100644 --- a/events/roommemberevent.h +++ b/events/roommemberevent.h @@ -20,32 +20,49 @@ #include "event.h" +#include "eventcontent.h" + #include <QtCore/QUrl> namespace QMatrixClient { - class RoomMemberEvent: public RoomEvent + class MemberEventContent: public EventContent::Base + { + public: + enum MembershipType : size_t {Invite = 0, Join, Knock, Leave, Ban}; + + MemberEventContent(const QJsonObject& json); + + MembershipType membership; + QString displayName; + QUrl avatarUrl; + + protected: + void fillJson(QJsonObject* o) const override; + }; + + using MembershipType = MemberEventContent::MembershipType; + + class RoomMemberEvent: public StateEvent<MemberEventContent> { Q_GADGET public: static constexpr const char* TypeId = "m.room.member"; - enum MembershipType : int {Invite = 0, Join, Knock, Leave, Ban}; + using MembershipType = MemberEventContent::MembershipType; - explicit RoomMemberEvent(const QJsonObject& obj); + explicit RoomMemberEvent(const QJsonObject& obj) + : StateEvent(Type::RoomMember, obj) + , _userId(obj["state_key"].toString()) + { } - MembershipType membership() const { return _membership; } - const QString& userId() const { return _userId; } - const QString& displayName() const { return _displayName; } - const QUrl& avatarUrl() const { return _avatarUrl; } + MembershipType membership() const { return content().membership; } + QString userId() const { return _userId; } + QString displayName() const { return content().displayName; } + QUrl avatarUrl() const { return content().avatarUrl; } private: - MembershipType _membership; QString _userId; - QString _displayName; - QUrl _avatarUrl; - REGISTER_ENUM(MembershipType) }; - using MembershipType = RoomMemberEvent::MembershipType; } // namespace QMatrixClient |