aboutsummaryrefslogtreecommitdiff
path: root/lib/events/stateevent.h
diff options
context:
space:
mode:
authorAndres Salomon <dilinger@queued.net>2021-01-18 04:00:14 -0500
committerAndres Salomon <dilinger@queued.net>2021-01-18 04:00:14 -0500
commit09eb39236666e81d5da014acea011dcd74d0999b (patch)
tree52876d96be71be1a39d5d935c1295a51995e8949 /lib/events/stateevent.h
parentf1788ee27f33e9339334e0d79bde9a27d9ce2e44 (diff)
parenta4e78956f105875625b572d8b98459ffa86fafe5 (diff)
downloadlibquotient-09eb39236666e81d5da014acea011dcd74d0999b.tar.gz
libquotient-09eb39236666e81d5da014acea011dcd74d0999b.zip
Update upstream source from tag 'upstream/0.6.4'
Update to upstream version '0.6.4' with Debian dir aa8705fd74743e79c043bc9e3e425d5064404cfe
Diffstat (limited to 'lib/events/stateevent.h')
-rw-r--r--lib/events/stateevent.h222
1 files changed, 116 insertions, 106 deletions
diff --git a/lib/events/stateevent.h b/lib/events/stateevent.h
index 3f54f7bf..710b4271 100644
--- a/lib/events/stateevent.h
+++ b/lib/events/stateevent.h
@@ -1,121 +1,131 @@
/******************************************************************************
-* 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
-*/
+ * 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 "roomevent.h"
-namespace QMatrixClient {
- class StateEventBase: public RoomEvent
- {
- public:
- using factory_t = EventFactory<StateEventBase>;
+namespace Quotient {
+
+/// Make a minimal correct Matrix state event JSON
+template <typename StrT>
+inline QJsonObject basicStateEventJson(StrT matrixType,
+ const QJsonObject& content,
+ const QString& stateKey = {})
+{
+ return { { TypeKey, std::forward<StrT>(matrixType) },
+ { StateKeyKey, stateKey },
+ { ContentKey, content } };
+}
+
+class StateEventBase : public RoomEvent {
+public:
+ using factory_t = EventFactory<StateEventBase>;
- using RoomEvent::RoomEvent;
- ~StateEventBase() override = default;
+ StateEventBase(Type type, const QJsonObject& json) : RoomEvent(type, json)
+ {}
+ StateEventBase(Type type, event_mtype_t matrixType,
+ const QString& stateKey = {},
+ const QJsonObject& contentJson = {});
+ ~StateEventBase() override = default;
- bool isStateEvent() const override { return true; }
- QString replacedState() const;
- void dumpTo(QDebug dbg) const override;
+ bool isStateEvent() const override { return true; }
+ QString replacedState() const;
+ void dumpTo(QDebug dbg) const override;
+
+ virtual bool repeatsState() const;
+};
+using StateEventPtr = event_ptr_tt<StateEventBase>;
+using StateEvents = EventsArray<StateEventBase>;
+
+template <>
+inline bool is<StateEventBase>(const Event& e)
+{
+ return e.isStateEvent();
+}
- virtual bool repeatsState() const;
- };
- using StateEventPtr = event_ptr_tt<StateEventBase>;
- using StateEvents = EventsArray<StateEventBase>;
+/**
+ * A combination of event type and state key uniquely identifies a piece
+ * of state in Matrix.
+ * \sa
+ * https://matrix.org/docs/spec/client_server/unstable.html#types-of-room-events
+ */
+using StateEventKey = QPair<QString, QString>;
- template <>
- inline bool is<StateEventBase>(const Event& e) { return e.isStateEvent(); }
+template <typename ContentT>
+struct Prev {
+ template <typename... ContentParamTs>
+ explicit Prev(const QJsonObject& unsignedJson,
+ ContentParamTs&&... contentParams)
+ : senderId(unsignedJson.value("prev_sender"_ls).toString())
+ , content(unsignedJson.value(PrevContentKeyL).toObject(),
+ std::forward<ContentParamTs>(contentParams)...)
+ {}
- /**
- * A combination of event type and state key uniquely identifies a piece
- * of state in Matrix.
- * \sa https://matrix.org/docs/spec/client_server/unstable.html#types-of-room-events
- */
- using StateEventKey = QPair<QString, QString>;
+ QString senderId;
+ ContentT content;
+};
- template <typename ContentT>
- struct Prev
+template <typename ContentT>
+class StateEvent : public StateEventBase {
+public:
+ using content_type = ContentT;
+
+ template <typename... ContentParamTs>
+ explicit StateEvent(Type type, const QJsonObject& fullJson,
+ ContentParamTs&&... contentParams)
+ : StateEventBase(type, fullJson)
+ , _content(contentJson(), std::forward<ContentParamTs>(contentParams)...)
{
- template <typename... ContentParamTs>
- explicit Prev(const QJsonObject& unsignedJson,
- ContentParamTs&&... contentParams)
- : senderId(unsignedJson.value("prev_sender"_ls).toString())
- , content(unsignedJson.value(PrevContentKeyL).toObject(),
- std::forward<ContentParamTs>(contentParams)...)
- { }
-
- QString senderId;
- ContentT content;
- };
-
- template <typename ContentT>
- class StateEvent: public StateEventBase
+ const auto& unsignedData = unsignedJson();
+ if (unsignedData.contains(PrevContentKeyL))
+ _prev = std::make_unique<Prev<ContentT>>(
+ unsignedData, std::forward<ContentParamTs>(contentParams)...);
+ }
+ template <typename... ContentParamTs>
+ explicit StateEvent(Type type, event_mtype_t matrixType,
+ const QString& stateKey,
+ ContentParamTs&&... contentParams)
+ : StateEventBase(type, matrixType, stateKey)
+ , _content(std::forward<ContentParamTs>(contentParams)...)
{
- public:
- using content_type = ContentT;
-
- template <typename... ContentParamTs>
- explicit StateEvent(Type type, const QJsonObject& fullJson,
- ContentParamTs&&... contentParams)
- : StateEventBase(type, fullJson)
- , _content(contentJson(),
- std::forward<ContentParamTs>(contentParams)...)
- {
- const auto& unsignedData = unsignedJson();
- if (unsignedData.contains(PrevContentKeyL))
- _prev = std::make_unique<Prev<ContentT>>(unsignedData,
- std::forward<ContentParamTs>(contentParams)...);
- }
- template <typename... ContentParamTs>
- explicit StateEvent(Type type, event_mtype_t matrixType,
- ContentParamTs&&... contentParams)
- : StateEventBase(type, matrixType)
- , _content(std::forward<ContentParamTs>(contentParams)...)
- {
- editJson().insert(ContentKey, _content.toJson());
- }
-
- const ContentT& content() const { return _content; }
- template <typename VisitorT>
- void editContent(VisitorT&& visitor)
- {
- visitor(_content);
- editJson()[ContentKeyL] = _content.toJson();
- }
- [[deprecated("Use prevContent instead")]]
- const ContentT* prev_content() const { return prevContent(); }
- const ContentT* prevContent() const
- { return _prev ? &_prev->content : nullptr; }
- QString prevSenderId() const
- { return _prev ? _prev->senderId : QString(); }
-
- private:
- ContentT _content;
- std::unique_ptr<Prev<ContentT>> _prev;
- };
-} // namespace QMatrixClient
-
-namespace std {
- template <> struct hash<QMatrixClient::StateEventKey>
+ editJson().insert(ContentKey, _content.toJson());
+ }
+
+ const ContentT& content() const { return _content; }
+ template <typename VisitorT>
+ void editContent(VisitorT&& visitor)
{
- size_t operator()(const QMatrixClient::StateEventKey& k) const Q_DECL_NOEXCEPT
- {
- return qHash(k);
- }
- };
-}
+ visitor(_content);
+ editJson()[ContentKeyL] = _content.toJson();
+ }
+ [[deprecated("Use prevContent instead")]] const ContentT* prev_content() const
+ {
+ return prevContent();
+ }
+ const ContentT* prevContent() const
+ {
+ return _prev ? &_prev->content : nullptr;
+ }
+ QString prevSenderId() const { return _prev ? _prev->senderId : QString(); }
+
+private:
+ ContentT _content;
+ std::unique_ptr<Prev<ContentT>> _prev;
+};
+} // namespace Quotient