diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/events/event.h | 21 | ||||
-rw-r--r-- | lib/events/roomevent.cpp | 10 | ||||
-rw-r--r-- | lib/events/stateevent.cpp | 10 |
3 files changed, 21 insertions, 20 deletions
diff --git a/lib/events/event.h b/lib/events/event.h index 733fadd8..e45fecca 100644 --- a/lib/events/event.h +++ b/lib/events/event.h @@ -221,18 +221,11 @@ public: // different types, we're implementing it per-event type. const QJsonObject contentJson() const; - const QJsonObject unsignedJson() const; - template <typename T = QJsonValue> - const T contentPart(const QString& key) const + template <typename T = QJsonValue, typename KeyT> + const T contentPart(KeyT&& key) const { - return fromJson<T>(contentJson()[key]); - } - - template <typename T = QJsonValue> - const T contentPart(QLatin1String key) const - { - return fromJson<T>(contentJson()[key]); + return fromJson<T>(contentJson()[std::forward<KeyT>(key)]); } template <typename T> @@ -242,6 +235,14 @@ public: return contentPart<T>(key); } + const QJsonObject unsignedJson() const; + + template <typename T = QJsonValue, typename KeyT> + const T unsignedPart(KeyT&& key) const + { + return fromJson<T>(unsignedJson()[std::forward<KeyT>(key)]); + } + friend QDebug operator<<(QDebug dbg, const Event& e) { QDebugStateSaver _dss { dbg }; diff --git a/lib/events/roomevent.cpp b/lib/events/roomevent.cpp index 4fec9d2b..fb921af6 100644 --- a/lib/events/roomevent.cpp +++ b/lib/events/roomevent.cpp @@ -19,7 +19,7 @@ RoomEvent::RoomEvent(Type type, event_mtype_t matrixType, RoomEvent::RoomEvent(Type type, const QJsonObject& json) : Event(type, json) { - if (const auto redaction = unsignedJson()[RedactedCauseKeyL]; + if (const auto redaction = unsignedPart(RedactedCauseKeyL); redaction.isObject()) _redactedBecause = makeEvent<RedactionEvent>(redaction.toObject()); } @@ -45,14 +45,14 @@ QString RoomEvent::senderId() const bool RoomEvent::isReplaced() const { - return unsignedJson()["m.relations"_ls].toObject().contains("m.replace"); + return unsignedPart<QJsonObject>("m.relations"_ls).contains("m.replace"); } QString RoomEvent::replacedBy() const { // clang-format off - return unsignedJson()["m.relations"_ls].toObject() - .value("m.replace").toObject() + return unsignedPart<QJsonObject>("m.relations"_ls) + .value("m.replace"_ls).toObject() .value(EventIdKeyL).toString(); // clang-format on } @@ -64,7 +64,7 @@ QString RoomEvent::redactionReason() const QString RoomEvent::transactionId() const { - return unsignedJson()["transaction_id"_ls].toString(); + return unsignedPart<QString>("transaction_id"_ls); } QString RoomEvent::stateKey() const diff --git a/lib/events/stateevent.cpp b/lib/events/stateevent.cpp index 42fc9054..efe011a0 100644 --- a/lib/events/stateevent.cpp +++ b/lib/events/stateevent.cpp @@ -28,22 +28,22 @@ StateEventBase::StateEventBase(Event::Type type, event_mtype_t matrixType, bool StateEventBase::repeatsState() const { - const auto prevContentJson = unsignedJson().value(PrevContentKeyL); + const auto prevContentJson = unsignedPart(PrevContentKeyL); return fullJson().value(ContentKeyL) == prevContentJson; } QString StateEventBase::replacedState() const { - return unsignedJson().value("replaces_state"_ls).toString(); + return unsignedPart<QString>("replaces_state"_ls); } void StateEventBase::dumpTo(QDebug dbg) const { if (!stateKey().isEmpty()) dbg << '<' << stateKey() << "> "; - if (unsignedJson().contains(PrevContentKeyL)) - dbg << QJsonDocument(unsignedJson()[PrevContentKeyL].toObject()) - .toJson(QJsonDocument::Compact) + if (const auto prevContentJson = unsignedPart<QJsonObject>(PrevContentKeyL); + !prevContentJson.isEmpty()) + dbg << QJsonDocument(prevContentJson).toJson(QJsonDocument::Compact) << " -> "; RoomEvent::dumpTo(dbg); } |