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
18namespace mtx {
19namespace events {
20//! The basic set of fields all events must have.
21template<class Content>
22struct 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.
41template<class Content>
42struct 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.
53struct 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
77template<class Content>
78struct 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.
89template<class Content>
90struct 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.
110template<class Content>
111struct 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.
124template<class Content>
125struct 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.
137template<class Content>
138struct 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
146enum 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
172MessageType
173getMessageType(const std::string &type);
174
175MessageType
176getMessageType(const nlohmann::json &obj);
177
178struct Sticker : public RoomEvent<mtx::events::msg::StickerImage>
179{};
180
181/// @brief An ephemeral event like typing or read receipts
182/// @sa Event
183template<class Content>
184struct 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
204template<class Content>
205using AccountDataEvent = EphemeralEvent<Content>;
206
207} // namespace events
208} // namespace mtx
209