aboutsummaryrefslogtreecommitdiff
path: root/lib/events
diff options
context:
space:
mode:
Diffstat (limited to 'lib/events')
-rw-r--r--lib/events/roomcreateevent.cpp19
-rw-r--r--lib/events/roommemberevent.cpp19
2 files changed, 10 insertions, 28 deletions
diff --git a/lib/events/roomcreateevent.cpp b/lib/events/roomcreateevent.cpp
index bb6de648..3b5024d5 100644
--- a/lib/events/roomcreateevent.cpp
+++ b/lib/events/roomcreateevent.cpp
@@ -6,20 +6,11 @@
using namespace Quotient;
template <>
-struct Quotient::JsonConverter<RoomType> {
- static RoomType load(const QJsonValue& jv)
- {
- const auto& roomTypeString = jv.toString();
- for (auto it = RoomTypeStrings.begin(); it != RoomTypeStrings.end();
- ++it)
- if (roomTypeString == *it)
- return RoomType(it - RoomTypeStrings.begin());
-
- if (!roomTypeString.isEmpty())
- qCWarning(EVENTS) << "Unknown Room Type: " << roomTypeString;
- return RoomType::Undefined;
- }
-};
+RoomType Quotient::fromJson(const QJsonValue& jv)
+{
+ return enumFromJsonString(jv.toString(), RoomTypeStrings,
+ RoomType::Undefined);
+}
bool RoomCreateEvent::isFederated() const
{
diff --git a/lib/events/roommemberevent.cpp b/lib/events/roommemberevent.cpp
index c3be0e00..953ff8ae 100644
--- a/lib/events/roommemberevent.cpp
+++ b/lib/events/roommemberevent.cpp
@@ -11,18 +11,10 @@ template <>
struct JsonConverter<Membership> {
static Membership load(const QJsonValue& jv)
{
- const auto& ms = jv.toString();
- if (ms.isEmpty())
- {
- qCWarning(EVENTS) << "Empty membership state";
- return Membership::Invalid;
- }
- const auto it =
- std::find(MembershipStrings.begin(), MembershipStrings.end(), ms);
- if (it != MembershipStrings.end())
- return Membership(1U << (it - MembershipStrings.begin()));
-
- qCWarning(EVENTS) << "Unknown Membership value: " << ms;
+ if (const auto& ms = jv.toString(); !ms.isEmpty())
+ return flagFromJsonString<Membership>(ms, MembershipStrings);
+
+ qCWarning(EVENTS) << "Empty membership state";
return Membership::Invalid;
}
};
@@ -46,8 +38,7 @@ QJsonObject MemberEventContent::toJson() const
QJsonObject o;
if (membership != Membership::Invalid)
o.insert(QStringLiteral("membership"),
- MembershipStrings[qCountTrailingZeroBits(
- std::underlying_type_t<Membership>(membership))]);
+ flagToJsonString(membership, MembershipStrings));
if (displayName)
o.insert(QStringLiteral("displayname"), *displayName);
if (avatarUrl && avatarUrl->isValid())