diff options
Diffstat (limited to 'events')
-rw-r--r-- | events/event.cpp | 38 | ||||
-rw-r--r-- | events/event.h | 26 | ||||
-rw-r--r-- | events/receiptevent.cpp | 4 | ||||
-rw-r--r-- | events/receiptevent.h | 2 | ||||
-rw-r--r-- | events/roommemberevent.cpp | 10 | ||||
-rw-r--r-- | events/roommemberevent.h | 18 | ||||
-rw-r--r-- | events/tagevent.cpp | 50 | ||||
-rw-r--r-- | events/tagevent.h | 51 |
8 files changed, 167 insertions, 32 deletions
diff --git a/events/event.cpp b/events/event.cpp index c7345a13..74a2c3d7 100644 --- a/events/event.cpp +++ b/events/event.cpp @@ -24,6 +24,7 @@ #include "roomavatarevent.h" #include "typingevent.h" #include "receiptevent.h" +#include "tagevent.h" #include "redactionevent.h" #include "logging.h" @@ -44,6 +45,11 @@ Event::Event(Type type, const QJsonObject& rep) Event::~Event() = default; +QString Event::jsonType() const +{ + return originalJsonObject().value("type").toString(); +} + QByteArray Event::originalJson() const { return QJsonDocument(_originalJson).toJson(); @@ -82,17 +88,18 @@ EventPtr _impl::doMakeEvent<Event>(const QJsonObject& obj) return EventPtr(move(e)); return EventPtr { makeIfMatches<Event, - TypingEvent, ReceiptEvent>(obj, obj["type"].toString()) }; + TypingEvent, ReceiptEvent, TagEvent>(obj, obj["type"].toString()) }; } RoomEvent::RoomEvent(Event::Type type) : Event(type) { } RoomEvent::RoomEvent(Type type, const QJsonObject& rep) - : Event(type, rep), _id(rep["event_id"].toString()) - , _roomId(rep["room_id"].toString()) - , _senderId(rep["sender"].toString()) - , _serverTimestamp( - QMatrixClient::fromJson<QDateTime>(rep["origin_server_ts"])) + : Event(type, rep) + , _id(rep["event_id"].toString()) +// , _roomId(rep["room_id"].toString()) +// , _senderId(rep["sender"].toString()) +// , _serverTimestamp( +// QMatrixClient::fromJson<QDateTime>(rep["origin_server_ts"])) { // if (_id.isEmpty()) // { @@ -113,7 +120,8 @@ RoomEvent::RoomEvent(Type type, const QJsonObject& rep) auto redaction = unsignedData.value("redacted_because"); if (redaction.isObject()) { - _redactedBecause.reset(new RedactionEvent(redaction.toObject())); + _redactedBecause = + std::make_unique<RedactionEvent>(redaction.toObject()); return; } @@ -124,6 +132,22 @@ RoomEvent::RoomEvent(Type type, const QJsonObject& rep) RoomEvent::~RoomEvent() = default; // Let the smart pointer do its job +QDateTime RoomEvent::timestamp() const +{ + return QMatrixClient::fromJson<QDateTime>( + originalJsonObject().value("origin_server_ts")); +} + +QString RoomEvent::roomId() const +{ + return originalJsonObject().value("room_id").toString(); +} + +QString RoomEvent::senderId() const +{ + return originalJsonObject().value("sender").toString(); +} + QString RoomEvent::redactionReason() const { return isRedacted() ? _redactedBecause->reason() : QString{}; diff --git a/events/event.h b/events/event.h index b5a4d94e..f0ca2d15 100644 --- a/events/event.h +++ b/events/event.h @@ -45,7 +45,7 @@ namespace QMatrixClient enum class Type : quint16 { Unknown = 0, - Typing, Receipt, + Typing, Receipt, Tag, DirectChat, RoomEventBase = 0x1000, RoomMessage = RoomEventBase + 1, RoomEncryptedMessage, Redaction, @@ -63,6 +63,7 @@ namespace QMatrixClient virtual ~Event(); Type type() const { return _type; } + QString jsonType() const; bool isStateEvent() const { return (quint16(_type) & 0x1800) == 0x1800; @@ -76,7 +77,6 @@ namespace QMatrixClient // (and in most cases it will be a combination of other fields // instead of "content" field). - protected: const QJsonObject contentJson() const; private: @@ -100,7 +100,7 @@ namespace QMatrixClient { auto e = _impl::doMakeEvent<EventT>(obj); if (!e) - e.reset(new EventT(EventType::Unknown, obj)); + e = std::make_unique<EventT>(EventType::Unknown, obj); return e; } @@ -167,10 +167,10 @@ namespace QMatrixClient RoomEvent(Type type, const QJsonObject& rep); ~RoomEvent(); - const QString& id() const { return _id; } - const QDateTime& timestamp() const { return _serverTimestamp; } - const QString& roomId() const { return _roomId; } - const QString& senderId() const { return _senderId; } + QString id() const { return _id; } + QDateTime timestamp() const; + QString roomId() const; + QString senderId() const; bool isRedacted() const { return bool(_redactedBecause); } const RedactionEvent* redactedBecause() const { @@ -202,9 +202,9 @@ namespace QMatrixClient private: QString _id; - QString _roomId; - QString _senderId; - QDateTime _serverTimestamp; +// QString _roomId; +// QString _senderId; +// QDateTime _serverTimestamp; event_ptr_tt<RedactionEvent> _redactedBecause; QString _txnId; }; @@ -296,10 +296,10 @@ namespace QMatrixClient QJsonObject toJson() const { return _content.toJson(); } - ContentT content() const { return _content; } + const ContentT& content() const { return _content; } /** @deprecated Use prevContent instead */ - ContentT* prev_content() const { return prevContent(); } - ContentT* prevContent() const + const ContentT* prev_content() const { return prevContent(); } + const ContentT* prevContent() const { return _prev ? &_prev->content : nullptr; } QString prevSenderId() const { return _prev ? _prev->senderId : ""; } diff --git a/events/receiptevent.cpp b/events/receiptevent.cpp index e30fe4e4..3c4d34ee 100644 --- a/events/receiptevent.cpp +++ b/events/receiptevent.cpp @@ -43,10 +43,10 @@ using namespace QMatrixClient; ReceiptEvent::ReceiptEvent(const QJsonObject& obj) : Event(Type::Receipt, obj) { - Q_ASSERT(obj["type"].toString() == jsonType); + Q_ASSERT(obj["type"].toString() == TypeId); const QJsonObject contents = contentJson(); - _eventsWithReceipts.reserve(static_cast<size_t>(contents.size())); + _eventsWithReceipts.reserve(contents.size()); for( auto eventIt = contents.begin(); eventIt != contents.end(); ++eventIt ) { if (eventIt.key().isEmpty()) diff --git a/events/receiptevent.h b/events/receiptevent.h index 9494c7c6..92dace82 100644 --- a/events/receiptevent.h +++ b/events/receiptevent.h @@ -48,7 +48,5 @@ namespace QMatrixClient private: EventsWithReceipts _eventsWithReceipts; bool _unreadMessages; // Spec extension for caching purposes - - static constexpr const char * jsonType = "m.receipt"; }; } // namespace QMatrixClient diff --git a/events/roommemberevent.cpp b/events/roommemberevent.cpp index 76df5f2e..a9e301a4 100644 --- a/events/roommemberevent.cpp +++ b/events/roommemberevent.cpp @@ -44,7 +44,7 @@ namespace QMatrixClient return MembershipType(it - membershipStrings.begin()); qCWarning(EVENTS) << "Unknown MembershipType: " << membershipString; - return MembershipType::Join; + return MembershipType::Undefined; } }; } @@ -58,7 +58,11 @@ MemberEventContent::MemberEventContent(const QJsonObject& json) void MemberEventContent::fillJson(QJsonObject* o) const { Q_ASSERT(o); - o->insert("membership", membershipStrings[membership]); + Q_ASSERT_X(membership != MembershipType::Undefined, __FUNCTION__, + "The key 'membership' must be explicit in MemberEventContent"); + if (membership != MembershipType::Undefined) + o->insert("membership", membershipStrings[membership]); o->insert("displayname", displayName); - o->insert("avatar_url", avatarUrl.toString()); + if (avatarUrl.isValid()) + o->insert("avatar_url", avatarUrl.toString()); } diff --git a/events/roommemberevent.h b/events/roommemberevent.h index d0c63f15..b9ff0d70 100644 --- a/events/roommemberevent.h +++ b/events/roommemberevent.h @@ -29,9 +29,13 @@ namespace QMatrixClient class MemberEventContent: public EventContent::Base { public: - enum MembershipType : size_t {Invite = 0, Join, Knock, Leave, Ban}; + enum MembershipType : size_t { Invite = 0, Join, Knock, Leave, Ban, + Undefined }; - MemberEventContent(const QJsonObject& json); + explicit MemberEventContent(MembershipType mt = MembershipType::Join) + : membership(mt) + { } + explicit MemberEventContent(const QJsonObject& json); MembershipType membership; QString displayName; @@ -51,18 +55,22 @@ namespace QMatrixClient using MembershipType = MemberEventContent::MembershipType; + RoomMemberEvent(MemberEventContent&& c) + : StateEvent(Type::RoomMember, c) + { } explicit RoomMemberEvent(const QJsonObject& obj) : StateEvent(Type::RoomMember, obj) - , _userId(obj["state_key"].toString()) +// , _userId(obj["state_key"].toString()) { } MembershipType membership() const { return content().membership; } - QString userId() const { return _userId; } + QString userId() const + { return originalJsonObject().value("state_key").toString(); } QString displayName() const { return content().displayName; } QUrl avatarUrl() const { return content().avatarUrl; } private: - QString _userId; +// QString _userId; REGISTER_ENUM(MembershipType) }; } // namespace QMatrixClient diff --git a/events/tagevent.cpp b/events/tagevent.cpp new file mode 100644 index 00000000..c6297003 --- /dev/null +++ b/events/tagevent.cpp @@ -0,0 +1,50 @@ +/****************************************************************************** + * Copyright (C) 2018 Kitsune Ral <kitsune-ral@users.sf.net> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "tagevent.h" + +using namespace QMatrixClient; + +TagRecord::TagRecord(const QJsonObject& json) + : order(json.value("order").toString()) +{ } + +TagEvent::TagEvent(const QJsonObject& obj) + : Event(Type::Tag, obj) +{ + Q_ASSERT(obj["type"].toString() == TypeId); +} + +QStringList TagEvent::tagNames() const +{ + return tagsObject().keys(); +} + +QHash<QString, TagRecord> TagEvent::tags() const +{ + QHash<QString, TagRecord> result; + auto allTags { tagsObject() }; + for (auto it = allTags.begin(); it != allTags.end(); ++ it) + result.insert(it.key(), TagRecord(it.value().toObject())); + return result; +} + +QJsonObject TagEvent::tagsObject() const +{ + return contentJson().value("tags").toObject(); +} diff --git a/events/tagevent.h b/events/tagevent.h new file mode 100644 index 00000000..44a7e49a --- /dev/null +++ b/events/tagevent.h @@ -0,0 +1,51 @@ +/****************************************************************************** + * Copyright (C) 2018 Kitsune Ral <kitsune-ral@users.sf.net> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#pragma once + +#include "event.h" + +namespace QMatrixClient +{ + static constexpr const char* FavouriteTag = "m.favourite"; + static constexpr const char* LowPriorityTag = "m.lowpriority"; + + struct TagRecord + { + explicit TagRecord(const QJsonObject& json = {}); + + QString order; + }; + + class TagEvent : public Event + { + public: + explicit TagEvent(const QJsonObject& obj); + + /** Get the list of tag names */ + QStringList tagNames() const; + + /** Get the list of tags along with information on each */ + QHash<QString, TagRecord> tags() const; + + static constexpr const char * TypeId = "m.tag"; + + protected: + QJsonObject tagsObject() const; + }; +} |