1#include <nlohmann/json.hpp>
2#include <string>
3
4#include "mtx/events/messages/file.hpp"
5
6using json = nlohmann::json;
7
8namespace common = mtx::common;
9
10namespace mtx {
11namespace events {
12namespace msg {
13
14void
15from_json(const json &obj, File &content)
16{
17 content.body = obj.at(key: "body").get<std::string>();
18 content.msgtype = obj.at(key: "msgtype").get<std::string>();
19
20 if (obj.find(key: "url") != obj.end())
21 content.url = obj.at(key: "url").get<std::string>();
22
23 if (obj.find(key: "filename") != obj.end())
24 content.filename = obj.at(key: "filename").get<std::string>();
25
26 if (obj.find(key: "info") != obj.end())
27 content.info = obj.at(key: "info").get<common::FileInfo>();
28
29 if (obj.find(key: "file") != obj.end())
30 content.file = obj.at(key: "file").get<crypto::EncryptedFile>();
31
32 content.relations = common::parse_relations(obj);
33}
34
35void
36to_json(json &obj, const File &content)
37{
38 obj["msgtype"] = "m.file";
39 obj["body"] = content.body;
40
41 if (!content.filename.empty())
42 obj["filename"] = content.filename;
43 obj["info"] = content.info;
44
45 if (content.file)
46 obj["file"] = content.file.value();
47 else
48 obj["url"] = content.url;
49
50 common::apply_relations(obj, relations: content.relations);
51}
52
53} // namespace msg
54} // namespace events
55} // namespace mtx
56