diff options
Diffstat (limited to 'events')
-rw-r--r-- | events/accountdataevents.h | 78 | ||||
-rw-r--r-- | events/directchatevent.cpp (renamed from events/tagevent.cpp) | 34 | ||||
-rw-r--r-- | events/directchatevent.h (renamed from events/tagevent.h) | 25 | ||||
-rw-r--r-- | events/event.cpp | 6 | ||||
-rw-r--r-- | events/event.h | 2 | ||||
-rw-r--r-- | events/receiptevent.cpp | 6 | ||||
-rw-r--r-- | events/receiptevent.h | 2 | ||||
-rw-r--r-- | events/roommemberevent.cpp | 1 | ||||
-rw-r--r-- | events/roommemberevent.h | 2 | ||||
-rw-r--r-- | events/simplestateevents.h | 29 |
10 files changed, 114 insertions, 71 deletions
diff --git a/events/accountdataevents.h b/events/accountdataevents.h new file mode 100644 index 00000000..f3ba27bb --- /dev/null +++ b/events/accountdataevents.h @@ -0,0 +1,78 @@ +#include <utility> + +/****************************************************************************** + * 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" +#include "eventcontent.h" + +namespace QMatrixClient +{ + static constexpr const char* FavouriteTag = "m.favourite"; + static constexpr const char* LowPriorityTag = "m.lowpriority"; + + struct TagRecord + { + TagRecord (QString order = {}) : order(std::move(order)) { } + explicit TagRecord(const QJsonValue& jv) + : order(jv.toObject().value("order").toString()) + { } + + QString order; + + bool operator==(const TagRecord& other) const + { return order == other.order; } + bool operator!=(const TagRecord& other) const + { return !operator==(other); } + }; + + inline QJsonValue toJson(const TagRecord& rec) + { + return QJsonObject {{ QStringLiteral("order"), rec.order }}; + } + + using TagsMap = QHash<QString, TagRecord>; + +#define DEFINE_SIMPLE_EVENT(_Name, _TypeId, _EnumType, _ContentType, _ContentKey) \ + class _Name : public Event \ + { \ + public: \ + static constexpr const char* TypeId = _TypeId; \ + static const char* typeId() { return TypeId; } \ + explicit _Name(const QJsonObject& obj) \ + : Event((_EnumType), obj) \ + , _content(contentJson(), QStringLiteral(#_ContentKey)) \ + { } \ + template <typename... Ts> \ + explicit _Name(Ts&&... contentArgs) \ + : Event(_EnumType) \ + , _content(QStringLiteral(#_ContentKey), \ + std::forward<Ts>(contentArgs)...) \ + { } \ + const _ContentType& _ContentKey() const { return _content.value; } \ + QJsonObject toJson() const { return _content.toJson(); } \ + protected: \ + EventContent::SimpleContent<_ContentType> _content; \ + }; + + DEFINE_SIMPLE_EVENT(TagEvent, "m.tag", EventType::Tag, TagsMap, tags) + DEFINE_SIMPLE_EVENT(ReadMarkerEvent, "m.fully_read", EventType::ReadMarker, + QString, event_id) +} diff --git a/events/tagevent.cpp b/events/directchatevent.cpp index c6297003..7049d967 100644 --- a/events/tagevent.cpp +++ b/events/directchatevent.cpp @@ -16,35 +16,21 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "tagevent.h" +#include "directchatevent.h" + +#include "converters.h" using namespace QMatrixClient; -TagRecord::TagRecord(const QJsonObject& json) - : order(json.value("order").toString()) +DirectChatEvent::DirectChatEvent(const QJsonObject& obj) + : Event(Type::DirectChat, obj) { } -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 +QMultiHash<QString, QString> DirectChatEvent::usersToDirectChats() 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())); + QMultiHash<QString, QString> result; + for (auto it = contentJson().begin(); it != contentJson().end(); ++it) + for (auto roomIdValue: it.value().toArray()) + result.insert(it.key(), roomIdValue.toString()); return result; } - -QJsonObject TagEvent::tagsObject() const -{ - return contentJson().value("tags").toObject(); -} diff --git a/events/tagevent.h b/events/directchatevent.h index 44a7e49a..2b0ad0a0 100644 --- a/events/tagevent.h +++ b/events/directchatevent.h @@ -22,30 +22,13 @@ 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 + class DirectChatEvent : 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; + explicit DirectChatEvent(const QJsonObject& obj); - static constexpr const char * TypeId = "m.tag"; + QMultiHash<QString, QString> usersToDirectChats() const; - protected: - QJsonObject tagsObject() const; + static constexpr const char * TypeId = "m.direct"; }; } diff --git a/events/event.cpp b/events/event.cpp index 74a2c3d7..8ddf3945 100644 --- a/events/event.cpp +++ b/events/event.cpp @@ -24,7 +24,8 @@ #include "roomavatarevent.h" #include "typingevent.h" #include "receiptevent.h" -#include "tagevent.h" +#include "accountdataevents.h" +#include "directchatevent.h" #include "redactionevent.h" #include "logging.h" @@ -88,7 +89,8 @@ EventPtr _impl::doMakeEvent<Event>(const QJsonObject& obj) return EventPtr(move(e)); return EventPtr { makeIfMatches<Event, - TypingEvent, ReceiptEvent, TagEvent>(obj, obj["type"].toString()) }; + TypingEvent, ReceiptEvent, TagEvent, ReadMarkerEvent, DirectChatEvent>( + obj, obj["type"].toString()) }; } RoomEvent::RoomEvent(Event::Type type) : Event(type) { } diff --git a/events/event.h b/events/event.h index f0ca2d15..eccfec41 100644 --- a/events/event.h +++ b/events/event.h @@ -45,7 +45,7 @@ namespace QMatrixClient enum class Type : quint16 { Unknown = 0, - Typing, Receipt, Tag, DirectChat, + Typing, Receipt, Tag, DirectChat, ReadMarker, RoomEventBase = 0x1000, RoomMessage = RoomEventBase + 1, RoomEncryptedMessage, Redaction, diff --git a/events/receiptevent.cpp b/events/receiptevent.cpp index 3c4d34ee..7555db82 100644 --- a/events/receiptevent.cpp +++ b/events/receiptevent.cpp @@ -66,11 +66,5 @@ ReceiptEvent::ReceiptEvent(const QJsonObject& obj) } _eventsWithReceipts.push_back({eventIt.key(), std::move(receipts)}); } - static const auto UnreadMsgsKey = - QStringLiteral("x-qmatrixclient.unread_messages"); - if (contents.contains(UnreadMsgsKey)) - _unreadMessages = contents["x-qmatrixclient.unread_messages"].toBool(); - else - _unreadMessages = obj["x-qmatrixclient.unread_messages"].toBool(); } diff --git a/events/receiptevent.h b/events/receiptevent.h index 92dace82..5b99ae3f 100644 --- a/events/receiptevent.h +++ b/events/receiptevent.h @@ -41,12 +41,10 @@ namespace QMatrixClient EventsWithReceipts eventsWithReceipts() const { return _eventsWithReceipts; } - bool unreadMessages() const { return _unreadMessages; } static constexpr const char* const TypeId = "m.receipt"; private: EventsWithReceipts _eventsWithReceipts; - bool _unreadMessages; // Spec extension for caching purposes }; } // namespace QMatrixClient diff --git a/events/roommemberevent.cpp b/events/roommemberevent.cpp index a9e301a4..76b003c2 100644 --- a/events/roommemberevent.cpp +++ b/events/roommemberevent.cpp @@ -51,6 +51,7 @@ namespace QMatrixClient MemberEventContent::MemberEventContent(const QJsonObject& json) : membership(fromJson<MembershipType>(json["membership"])) + , isDirect(json["is_direct"].toBool()) , displayName(json["displayname"].toString()) , avatarUrl(json["avatar_url"].toString()) { } diff --git a/events/roommemberevent.h b/events/roommemberevent.h index b9ff0d70..89b970c9 100644 --- a/events/roommemberevent.h +++ b/events/roommemberevent.h @@ -38,6 +38,7 @@ namespace QMatrixClient explicit MemberEventContent(const QJsonObject& json); MembershipType membership; + bool isDirect = false; QString displayName; QUrl avatarUrl; @@ -66,6 +67,7 @@ namespace QMatrixClient MembershipType membership() const { return content().membership; } QString userId() const { return originalJsonObject().value("state_key").toString(); } + bool isDirect() const { return content().isDirect; } QString displayName() const { return content().displayName; } QUrl avatarUrl() const { return content().avatarUrl; } diff --git a/events/simplestateevents.h b/events/simplestateevents.h index d5841bdc..6b0cd51a 100644 --- a/events/simplestateevents.h +++ b/events/simplestateevents.h @@ -19,36 +19,35 @@ #pragma once #include "event.h" - #include "eventcontent.h" namespace QMatrixClient { -#define DECLARE_SIMPLE_STATE_EVENT(_Name, _TypeId, _EnumType, _ContentType, _ContentKey) \ +#define DEFINE_SIMPLE_STATE_EVENT(_Name, _TypeId, _EnumType, _ContentType, _ContentKey) \ class _Name \ : public StateEvent<EventContent::SimpleContent<_ContentType>> \ { \ public: \ static constexpr const char* TypeId = _TypeId; \ explicit _Name(const QJsonObject& obj) \ - : StateEvent(_EnumType, obj, #_ContentKey) \ + : StateEvent(_EnumType, obj, QStringLiteral(#_ContentKey)) \ { } \ template <typename T> \ explicit _Name(T&& value) \ - : StateEvent(_EnumType, #_ContentKey, \ + : StateEvent(_EnumType, QStringLiteral(#_ContentKey), \ std::forward<T>(value)) \ { } \ - _ContentType _ContentKey() const { return content().value; } \ + const _ContentType& _ContentKey() const { return content().value; } \ }; - DECLARE_SIMPLE_STATE_EVENT(RoomNameEvent, "m.room.name", - Event::Type::RoomName, QString, name) - DECLARE_SIMPLE_STATE_EVENT(RoomAliasesEvent, "m.room.aliases", - Event::Type::RoomAliases, QStringList, aliases) - DECLARE_SIMPLE_STATE_EVENT(RoomCanonicalAliasEvent, "m.room.canonical_alias", - Event::Type::RoomCanonicalAlias, QString, alias) - DECLARE_SIMPLE_STATE_EVENT(RoomTopicEvent, "m.room.topic", - Event::Type::RoomTopic, QString, topic) - DECLARE_SIMPLE_STATE_EVENT(EncryptionEvent, "m.room.encryption", - Event::Type::RoomEncryption, QString, algorithm) + DEFINE_SIMPLE_STATE_EVENT(RoomNameEvent, "m.room.name", + Event::Type::RoomName, QString, name) + DEFINE_SIMPLE_STATE_EVENT(RoomAliasesEvent, "m.room.aliases", + Event::Type::RoomAliases, QStringList, aliases) + DEFINE_SIMPLE_STATE_EVENT(RoomCanonicalAliasEvent, "m.room.canonical_alias", + Event::Type::RoomCanonicalAlias, QString, alias) + DEFINE_SIMPLE_STATE_EVENT(RoomTopicEvent, "m.room.topic", + Event::Type::RoomTopic, QString, topic) + DEFINE_SIMPLE_STATE_EVENT(EncryptionEvent, "m.room.encryption", + Event::Type::RoomEncryption, QString, algorithm) } // namespace QMatrixClient |