Debian Codegraph @

mtxclient/include/mtx/events.hpp

raw symbols SCIP
1 #pragma once
2
3 /// @file
4 /// @brief Basetypes for events. Content is defined elsewhere.
5
6 #if __has_include(<nlohmann/json_fwd.hpp>)
7 #include <nlohmann/json_fwd.hpp>
8 #else
9 #include <nlohmann/json.hpp>
10 #endif
11
12 #include "mtx/events/event_type.hpp"
13 #include "mtx/events/messages/image.hpp"
14 #include "mtx/events/redaction.hpp"
15
16 //! Top level namespace for mtxclient
17 namespace mtx {
18 namespace events {
19 //! The basic set of fields all events must have.
20 template<class Content>
21 struct Event
22 {
23 //! The type of event.
24 //! This *should* be namespaced similar to Java package
25 //! naming conventions e.g. 'com.example.subdomain.event.type'
26 EventType type;
27 //! Contains the fully-qualified ID of the user who sent this event.
28 std::string sender;
29
30 //! The fields in this object will vary depending on the type of event.
31 //! When interacting with the REST API, this is the HTTP body.
32 Content content;
33
34 template<class C>
35 friend void to_json(nlohmann::json &obj, const Event<C> &event);
36 template<class C>
37 friend void from_json(const nlohmann::json &obj, Event<C> &event);
38 };
39
40 //! Extension of the Event type for device events.
41 template<class Content>
42 struct DeviceEvent : public Event<Content>
43 {
44 template<class C>
45 friend void from_json(const nlohmann::json &obj, DeviceEvent<C> &event);
46 template<class C>
47 friend void to_json(nlohmann::json &obj, const DeviceEvent<C> &event);
48 };
49
50 //! Additional server provided data for this event.
51 struct UnsignedData
52 {
53 //! The time in milliseconds that has elapsed since the event was sent.
54 //! This field is generated by the local homeserver,
55 //! and may be incorrect if the local time on at least one
56 //! of the two servers is out of sync, which can cause the age to
57 //! either be negative or greater than it actually is.
58 uint64_t age = 0;
59 //! The client-supplied transaction ID, if the client
60 //! being given the event is the same one which sent it.
61 std::string transaction_id;
62 //! The previous sender of a state event.
63 std::string prev_sender;
64 //! The replaced state event.
65 std::string replaces_state;
66 //! The event ID that redacted this event.
67 std::string redacted_by;
68 //! The event that redacted this event.
69 std::optional<Event<mtx::events::msg::Redaction>> redacted_because;
70
71 friend void from_json(const nlohmann::json &obj, UnsignedData &data);
72 friend void to_json(nlohmann::json &obj, const UnsignedData &event);
73 };
74
75 template<class Content>
76 struct StrippedEvent : public Event<Content>
77 {
78 std::string state_key;
79
80 template<class C>
81 friend void from_json(const nlohmann::json &obj, StrippedEvent<C> &event);
82 template<class C>
83 friend void to_json(nlohmann::json &obj, const StrippedEvent<C> &event);
84 };
85
86 //! RoomEvent.
87 template<class Content>
88 struct RoomEvent : public Event<Content>
89 {
90 //! The globally unique event identifier.
91 std::string event_id;
92 //! The ID of the room associated with this event.
93 std::string room_id;
94 //! Timestamp in milliseconds on originating homeserver
95 //! when this event was sent.
96 uint64_t origin_server_ts;
97 // SPEC_BUG: The contents of unsigned_data are also present as top level keys.
98 //! Contains optional extra information about the event.
99 UnsignedData unsigned_data;
100
101 template<class C>
102 friend void from_json(const nlohmann::json &obj, RoomEvent<C> &event);
103 template<class C>
104 friend void to_json(nlohmann::json &obj, const RoomEvent<C> &event);
105 };
106
107 //! Extension of the RoomEvent.
108 template<class Content>
109 struct StateEvent : public RoomEvent<Content>
110 {
111 //! A unique key which defines the overwriting semantics
112 //! for this piece of room state.
113 std::string state_key;
114
115 template<class C>
116 friend void to_json(nlohmann::json &obj, const StateEvent<C> &event);
117 template<class C>
118 friend void from_json(const nlohmann::json &obj, StateEvent<C> &event);
119 };
120
121 //! Extension of the RoomEvent.
122 template<class Content>
123 struct RedactionEvent : public RoomEvent<Content>
124 {
125 //! The event id of the event that was redacted.
126 std::string redacts;
127
128 template<class C>
129 friend void to_json(nlohmann::json &obj, const RedactionEvent<C> &event);
130 template<class C>
131 friend void from_json(const nlohmann::json &obj, RedactionEvent<C> &event);
132 };
133
134 //! Extension of the RoomEvent.
135 template<class Content>
136 struct EncryptedEvent : public RoomEvent<Content>
137 {
138 template<class C>
139 friend void to_json(nlohmann::json &obj, const EncryptedEvent<C> &event);
140 template<class C>
141 friend void from_json(const nlohmann::json &obj, EncryptedEvent<C> &event);
142 };
143
144 enum class MessageType
145 {
146 // m.audio
147 Audio,
148 // m.emote
149 Emote,
150 // m.file
151 File,
152 // m.image
153 Image,
154 // m.location
155 Location,
156 // m.notice
157 Notice,
158 // m.text
159 Text,
160 // m.video
161 Video,
162 /// m.key.verification.request
163 KeyVerificationRequest,
164 // Any of Element's custom effect msgtypes
165 ElementEffect,
166 // Unrecognized message type
167 Unknown,
168
169 // A redacted message that should be parsed differently
170 Redacted,
171 // Malformed content
172 Invalid,
173 };
174
175 MessageType
176 getMessageType(const std::string &type);
177
178 MessageType
179 getMessageType(const nlohmann::json &obj);
180
181 struct Sticker : public RoomEvent<mtx::events::msg::StickerImage>
182 {};
183
184 /// @brief An ephemeral event like typing or read receipts
185 /// @sa Event
186 template<class Content>
187 struct EphemeralEvent
188 {
189 //! The fields in this object will vary depending on the type of event.
190 //! When interacting with the REST API, this is the HTTP body.
191 Content content;
192 //! The type of event.
193 //! This *should* be namespaced similar to Java package
194 //! naming conventions e.g. 'com.example.subdomain.event.type'
195 EventType type;
196 //! The room this was sent in. May not always be present.
197 std::string room_id;
198
199 template<class C>
200 friend void to_json(nlohmann::json &obj, const EphemeralEvent<C> &event);
201 template<class C>
202 friend void from_json(const nlohmann::json &obj, EphemeralEvent<C> &event);
203 };
204
205 /// @brief An account_data event like fully_read or tags.
206 /// @sa Event
207 template<class Content>
208 using AccountDataEvent = EphemeralEvent<Content>;
209
210 } // namespace events
211 } // namespace mtx