10 constexpr inline auto PrevContentKey =
"prev_content"_L1;
14 QUO_BASE_EVENT(StateEvent, RoomEvent,
"json.contains('state_key')")
16 static bool isValid(
const QJsonObject& fullJson)
18 return fullJson.contains(StateKeyKey);
27 static constexpr auto needsStateKey =
false;
29 explicit StateEvent(event_type_t type,
const QString& stateKey = {},
30 const QJsonObject& contentJson = {});
33 static QJsonObject basicJson(
const QString& matrixTypeId,
34 const QString& stateKey = {},
35 const QJsonObject& contentJson = {})
37 return { { TypeKey, matrixTypeId },
38 { StateKeyKey, stateKey },
39 { ContentKey, contentJson } };
42 QString replacedState()
const;
43 virtual bool repeatsState()
const;
46 explicit StateEvent(
const QJsonObject& json);
47 void dumpTo(QDebug dbg)
const override;
49 using StateEventPtr = event_ptr_tt<StateEvent>;
50 using StateEvents = EventsArray<StateEvent>;
58 using StateEventKey = std::pair<QString, QString>;
60 template <
typename EventT,
typename ContentT>
61 class EventTemplate<EventT, StateEvent, ContentT>
64 using content_type = ContentT;
67 explicit Prev() =
default;
68 explicit Prev(
const QJsonObject& unsignedJson)
69 : senderId(fromJson<QString>(unsignedJson[
"prev_sender"_L1]))
70 , content(fromJson<std::optional<ContentT>>(unsignedJson[PrevContentKey]))
74 std::optional<ContentT> content;
77 explicit EventTemplate(
const QJsonObject& fullJson)
78 : StateEvent(fullJson)
79 , _content(fromJson<ContentT>(Event::contentJson()))
80 , _prev(unsignedJson())
82 template <
typename... ContentParamTs>
83 explicit EventTemplate(
const QString& stateKey,
84 ContentParamTs&&... contentParams)
85 : StateEvent(EventT::TypeId, stateKey)
86 , _content { std::forward<ContentParamTs>(contentParams)... }
88 editJson().insert(ContentKey, toJson(_content));
91 const ContentT& content()
const {
return _content; }
93 template <
typename VisitorT>
94 void editContent(VisitorT&& visitor)
97 editJson()[ContentKey] = toJson(_content);
99 const std::optional<ContentT>& prevContent()
const {
return _prev.content; }
100 QString prevSenderId()
const {
return _prev.senderId; }
107 template <
typename EventT,
typename ContentT>
108 class KeyedStateEventBase
109 :
public EventTemplate<EventT, StateEvent, ContentT> {
111 static constexpr auto needsStateKey =
true;
113 using EventTemplate<EventT, StateEvent, ContentT>::EventTemplate;
116 template <
typename EvT>
117 concept Keyed_State_Event = EvT::needsStateKey;
119 template <
typename EventT,
typename ContentT>
120 class KeylessStateEventBase
121 :
public EventTemplate<EventT, StateEvent, ContentT> {
123 using base_type = EventTemplate<EventT, StateEvent, ContentT>;
126 template <
typename... ContentParamTs>
130 requires std::constructible_from<base_type, QString, ContentParamTs...>
131 explicit KeylessStateEventBase(ContentParamTs&&... contentParams)
132 : base_type(QString(), std::forward<ContentParamTs>(contentParams)...)
136 explicit KeylessStateEventBase(
const QJsonObject& fullJson)
137 : base_type(fullJson)
141 template <
typename EvT>
142 concept Keyless_State_Event = !EvT::needsStateKey;
145 Q_DECLARE_METATYPE(Quotient::StateEvent*)
146 Q_DECLARE_METATYPE(
const Quotient::StateEvent*)
150 struct std::hash<Quotient::StateEventKey> {
151 size_t operator()(
const Quotient::StateEventKey& k)
const
153 return qHash(k, QHashSeed::globalSeed());