1#include "mtx/responses/notifications.hpp"
2#include "mtx/responses/common.hpp"
3
4#include <nlohmann/json.hpp>
5
6using json = nlohmann::json;
7
8namespace mtx {
9namespace responses {
10
11void
12from_json(const json &obj, Notification &res)
13{
14 res.actions = obj.at(key: "actions").get<decltype(res.actions)>();
15 res.read = obj.at(key: "read").get<bool>();
16 res.room_id = obj.at(key: "room_id").get<std::string>();
17 res.ts = obj.at(key: "ts").get<uint64_t>();
18
19 if (obj.find(key: "profile_tag") != obj.end() && !obj.at(key: "profile_tag").is_null())
20 res.profile_tag = obj.at(key: "profile_tag").get<std::string>();
21
22 // HACK to work around the fact that there isn't
23 // a method to parse a timeline event from a json object.
24 //
25 // TODO: Create method that retrieves a TimelineEvents variant from a json object.
26 // Ideally with an optional type to indicate failure.
27 std::vector<events::collections::TimelineEvents> tmp;
28 tmp.reserve(n: 1);
29
30 json arr;
31 arr.push_back(val: obj.at(key: "event"));
32
33 utils::parse_timeline_events(events: arr, container&: tmp);
34
35 if (!tmp.empty())
36 res.event = tmp.at(n: 0);
37}
38
39void
40to_json(json &obj, const Notification &res)
41{
42 obj["actions"] = res.actions;
43 obj["read"] = res.read;
44 obj["room_id"] = res.room_id;
45 obj["ts"] = res.ts;
46
47 // HACK to work around the fact that there isn't
48 // a method to parse a timeline event from a json object.
49 //
50 // TODO: Create method that retrieves a TimelineEvents variant from a json object.
51 // Ideally with an optional type to indicate failure.
52 std::vector<events::collections::TimelineEvents> tmp;
53 tmp.reserve(n: 1);
54
55 json arr;
56 tmp.push_back(x: res.event);
57
58 utils::compose_timeline_events(events&: arr, container: tmp);
59
60 if (!tmp.empty()) {
61 obj["event"] = arr;
62 }
63
64 if (!res.profile_tag.empty()) {
65 obj["profile_tag"] = res.profile_tag;
66 }
67}
68
69void
70from_json(const json &obj, Notifications &res)
71{
72 // res.next_token = obj.at("next_token").get<std::string>();
73 res.notifications = obj.at(key: "notifications").get<std::vector<Notification>>();
74}
75
76void
77to_json(json &obj, const Notifications &notif)
78{
79 obj["notifications"] = notif.notifications;
80}
81} // namespace responses
82} // namespace mtx
83