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