Debian Codegraph @

mtxclient/lib/structs/events/messages/file.cpp

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