-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathevents_impl.hpp
296 lines (236 loc) · 8.45 KB
/
events_impl.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include "events.hpp"
#include <nlohmann/json.hpp>
#include "mtx/events/encrypted.hpp"
#include "mtx/events/unknown.hpp"
namespace mtx::events {
namespace detail {
template<typename, typename = void>
struct can_edit : std::false_type
{};
template<typename Content>
struct can_edit<Content, std::void_t<decltype(Content::relations)>>
: std::is_same<decltype(Content::relations), mtx::common::Relations>
{};
}
template<class Content>
[[gnu::used, gnu::retain]] void
to_json(nlohmann::json &obj, const Event<Content> &event)
{
obj["content"] = event.content;
obj["sender"] = event.sender;
if constexpr (std::is_same_v<Unknown, Content>)
obj["type"] = event.content.type;
else
obj["type"] = ::mtx::events::to_string(event.type);
}
template<class Content>
[[gnu::used, gnu::retain]] void
from_json(const nlohmann::json &obj, Event<Content> &event)
{
if (!std::is_same_v<Content, mtx::events::msg::Encrypted> &&
obj.at("content").contains("m.new_content")) {
auto new_content = obj.at("content").at("m.new_content");
if (obj.at("content").contains("m.relates_to"))
new_content["m.relates_to"] = obj.at("content").at("m.relates_to");
if (obj.at("content").at("m.new_content").contains("m.relates_to"))
new_content["m.new_content"]["m.relates_to"] =
obj.at("content").at("m.new_content").at("m.relates_to");
if (obj.at("content").contains("im.nheko.relations.v1.relations"))
new_content["im.nheko.relations.v1.relations"] =
obj.at("content").at("im.nheko.relations.v1.relations");
event.content = new_content.get<Content>();
} else if (obj.at("content").is_object()) {
event.content = obj.at("content").get<Content>();
} else {
event.content = {};
}
auto type = obj.at("type").get<std::string>();
if (type.size() > 255) {
throw std::out_of_range("Type exceeds 255 bytes");
}
event.type = getEventType(type);
event.sender = obj.value("sender", "");
if (event.sender.size() > 255) {
throw std::out_of_range("Sender exceeds 255 bytes");
}
if constexpr (std::is_same_v<Unknown, Content>)
event.content.type = obj.at("type").get<std::string>();
}
template<class Content>
[[gnu::used, gnu::retain]] void
from_json(const nlohmann::json &obj, DeviceEvent<Content> &event)
{
Event<Content> &base = event;
from_json(obj, base);
if (event.sender.empty()) {
throw std::out_of_range("Empty sender in to_device event.");
}
}
template<class Content>
[[gnu::used, gnu::retain]] void
to_json(nlohmann::json &obj, const DeviceEvent<Content> &event)
{
Event<Content> base_event = event;
to_json(obj, base_event);
}
void
from_json(const nlohmann::json &obj, UnsignedData &data)
{
if (auto field = obj.find("age"); field != obj.end())
data.age = field->get<uint64_t>();
if (auto field = obj.find("transaction_id"); field != obj.end())
data.transaction_id = field->get<std::string>();
if (auto field = obj.find("prev_sender"); field != obj.end())
data.prev_sender = field->get<std::string>();
// This field is non-standard, but otherwise we need to be able to nest arbitrary event contents
// here, which currently doesn't work with how we type our event variants...
if (auto field = obj.find("replaces_state"); field != obj.end())
data.replaces_state = field->get<std::string>();
if (auto field = obj.find("redacted_by"); field != obj.end())
data.redacted_by = field->get<std::string>();
if (auto field = obj.find("redacted_because"); field != obj.end())
data.redacted_because = field->get<Event<mtx::events::msg::Redaction>>();
}
void
to_json(nlohmann::json &obj, const UnsignedData &event)
{
if (!event.prev_sender.empty())
obj["prev_sender"] = event.prev_sender;
if (!event.transaction_id.empty())
obj["transaction_id"] = event.transaction_id;
if (!event.replaces_state.empty())
obj["replaces_state"] = event.replaces_state;
if (event.age != 0)
obj["age"] = event.age;
if (!event.redacted_by.empty())
obj["redacted_by"] = event.redacted_by;
if (event.redacted_because)
obj["redacted_because"] = *event.redacted_because;
}
template<class Content>
[[gnu::used, gnu::retain]] void
from_json(const nlohmann::json &obj, StrippedEvent<Content> &event)
{
Event<Content> &base = event;
from_json(obj, base);
event.state_key = obj.at("state_key").get<std::string>();
if (event.state_key.size() > 255) {
throw std::out_of_range("State key exceeds 255 bytes");
}
}
template<class Content>
[[gnu::used, gnu::retain]] void
to_json(nlohmann::json &obj, const StrippedEvent<Content> &event)
{
Event<Content> base_event = event;
to_json(obj, base_event);
obj["state_key"] = event.state_key;
}
template<class Content>
[[gnu::used, gnu::retain]] void
from_json(const nlohmann::json &obj, RoomEvent<Content> &event)
{
Event<Content> &base = event;
from_json(obj, base);
event.event_id = obj.at("event_id").get<std::string>();
if (event.event_id.size() > 255) {
throw std::out_of_range("Event id exceeds 255 bytes");
}
event.origin_server_ts = obj.at("origin_server_ts").get<uint64_t>();
// Not present in the state array returned by /sync.
if (auto field = obj.find("room_id"); field != obj.end())
event.room_id = field->get<std::string>();
if (event.room_id.size() > 255) {
throw std::out_of_range("Room id exceeds 255 bytes");
}
if (auto field = obj.find("unsigned"); field != obj.end())
event.unsigned_data = field->get<UnsignedData>();
}
template<class Content>
[[gnu::used, gnu::retain]] void
to_json(nlohmann::json &obj, const RoomEvent<Content> &event)
{
Event<Content> base_event = event;
to_json(obj, base_event);
if (!event.room_id.empty())
obj["room_id"] = event.room_id;
obj["event_id"] = event.event_id;
obj["unsigned"] = event.unsigned_data;
obj["origin_server_ts"] = event.origin_server_ts;
}
template<class Content>
[[gnu::used, gnu::retain]] void
to_json(nlohmann::json &obj, const StateEvent<Content> &event)
{
RoomEvent<Content> base_event = event;
to_json(obj, base_event);
obj["state_key"] = event.state_key;
}
template<class Content>
[[gnu::used, gnu::retain]] void
from_json(const nlohmann::json &obj, StateEvent<Content> &event)
{
RoomEvent<Content> &base = event;
from_json(obj, base);
event.state_key = obj.at("state_key").get<std::string>();
if (event.state_key.size() > 255) {
throw std::out_of_range("State key exceeds 255 bytes");
}
}
template<class Content>
[[gnu::used, gnu::retain]] void
to_json(nlohmann::json &obj, const RedactionEvent<Content> &event)
{
RoomEvent<Content> base_event = event;
to_json(obj, base_event);
obj["redacts"] = event.redacts;
}
template<class Content>
[[gnu::used, gnu::retain]] void
from_json(const nlohmann::json &obj, RedactionEvent<Content> &event)
{
RoomEvent<Content> &base = event;
from_json(obj, base);
event.redacts = obj.at("redacts").get<std::string>();
}
template<class Content>
[[gnu::used, gnu::retain]] void
to_json(nlohmann::json &obj, const EncryptedEvent<Content> &event)
{
RoomEvent<Content> base_event = event;
to_json(obj, base_event);
}
template<class Content>
[[gnu::used, gnu::retain]] void
from_json(const nlohmann::json &obj, EncryptedEvent<Content> &event)
{
RoomEvent<Content> &base = event;
from_json(obj, base);
}
template<class Content>
[[gnu::used, gnu::retain]] void
to_json(nlohmann::json &obj, const EphemeralEvent<Content> &event)
{
obj["content"] = event.content;
if constexpr (std::is_same_v<Unknown, Content>)
obj["type"] = event.content.type;
else
obj["type"] = ::mtx::events::to_string(event.type);
if (!event.room_id.empty())
obj["room_id"] = event.room_id;
}
template<class Content>
[[gnu::used, gnu::retain]] void
from_json(const nlohmann::json &obj, EphemeralEvent<Content> &event)
{
event.content = obj.at("content").get<Content>();
event.type = getEventType(obj.at("type").get<std::string>());
if constexpr (std::is_same_v<Unknown, Content>)
event.content.type = obj.at("type").get<std::string>();
if (obj.contains("room_id"))
event.room_id = obj.at("room_id").get<std::string>();
if (event.room_id.size() > 255) {
throw std::out_of_range("Room id exceeds 255 bytes");
}
}
}