Skip to content
Snippets Groups Projects
Verified Commit b56083df authored by Joe Donofry's avatar Joe Donofry
Browse files

Add abilty to translate timeline events to json

parent 37df8236
No related branches found
No related tags found
No related merge requests found
......@@ -43,6 +43,165 @@ using TimelineEvents = std::vector<mtx::events::collections::TimelineEven
using StateEvents = std::vector<mtx::events::collections::StateEvents>;
using StrippedEvents = std::vector<mtx::events::collections::StrippedEvents>;
namespace states = mtx::events::state;
namespace msgs = mtx::events::msg;
struct TimelineEventVisitor : public boost::static_visitor<json>
{
json operator()(const events::StateEvent<states::Aliases> &aliasEv) const
{
json j;
mtx::events::to_json(j, aliasEv);
return j;
}
json operator()(const events::StateEvent<states::Avatar> &avatarEv) const
{
json j;
mtx::events::to_json(j, avatarEv);
return j;
};
json operator()(const events::StateEvent<states::CanonicalAlias> &aliasEv) const
{
json j;
mtx::events::to_json(j, aliasEv);
return j;
};
json operator()(const events::StateEvent<states::Create> &createEv) const
{
json j;
mtx::events::to_json(j, createEv);
return j;
};
json operator()(const events::StateEvent<states::Encryption> &encEv) const
{
json j;
mtx::events::to_json(j, encEv);
return j;
};
json operator()(const events::StateEvent<states::GuestAccess> &guestEv) const
{
json j;
mtx::events::to_json(j, guestEv);
return j;
};
json operator()(const events::StateEvent<states::HistoryVisibility> &histEv) const
{
json j;
mtx::events::to_json(j, histEv);
return j;
};
json operator()(const events::StateEvent<states::JoinRules> &joinEv) const
{
json j;
mtx::events::to_json(j, joinEv);
return j;
};
json operator()(const events::StateEvent<states::Member> &membEv) const
{
json j;
mtx::events::to_json(j, membEv);
return j;
};
json operator()(const events::StateEvent<states::Name> &nameEv) const
{
json j;
mtx::events::to_json(j, nameEv);
return j;
};
json operator()(const events::StateEvent<states::PinnedEvents> &pinEv) const
{
json j;
mtx::events::to_json(j, pinEv);
return j;
};
json operator()(const events::StateEvent<states::PowerLevels> &powEv) const
{
json j;
mtx::events::to_json(j, powEv);
return j;
};
json operator()(const events::StateEvent<states::Tombstone> &tombEv) const
{
json j;
mtx::events::to_json(j, tombEv);
return j;
};
json operator()(const events::StateEvent<states::Topic> &topicEv) const
{
json j;
mtx::events::to_json(j, topicEv);
return j;
};
json operator()(const events::EncryptedEvent<msgs::Encrypted> &encEv) const
{
json j;
mtx::events::to_json(j, encEv);
return j;
};
json operator()(const events::RedactionEvent<msgs::Redaction> &redEv) const
{
json j;
mtx::events::to_json(j, redEv);
return j;
};
json operator()(const events::Sticker &stickEv) const
{
json j;
mtx::events::to_json(j, stickEv);
return j;
};
json operator()(const events::RoomEvent<msgs::Redacted> &redEv) const
{
json j;
mtx::events::to_json(j, redEv);
return j;
};
json operator()(const events::RoomEvent<msgs::Audio> &audioEv) const
{
json j;
mtx::events::to_json(j, audioEv);
return j;
};
json operator()(const events::RoomEvent<msgs::Emote> &emoteEv) const
{
json j;
mtx::events::to_json(j, emoteEv);
return j;
};
json operator()(const events::RoomEvent<msgs::File> &fileEv) const
{
json j;
mtx::events::to_json(j, fileEv);
return j;
};
json operator()(const events::RoomEvent<msgs::Image> &imageEv) const
{
json j;
mtx::events::to_json(j, imageEv);
return j;
};
// TODO: json operator()(const events::RoomEvent<msgs::Location> &locEv) const { json j;
// mtx::events::to_json(j, locEv); return j;};
json operator()(const events::RoomEvent<msgs::Notice> &noticeEv) const
{
json j;
mtx::events::to_json(j, noticeEv);
return j;
};
json operator()(const events::RoomEvent<msgs::Text> &textEv) const
{
json j;
mtx::events::to_json(j, textEv);
return j;
};
json operator()(const events::RoomEvent<msgs::Video> &videoEv) const
{
json j;
mtx::events::to_json(j, videoEv);
return j;
};
};
void
log_error(json::exception &err, const json &event);
......@@ -52,6 +211,9 @@ log_error(std::string err, const json &event);
void
parse_room_account_data_events(const json &events, RoomAccountDataEvents &container);
void
compose_timeline_events(json &events, const TimelineEvents &container);
void
parse_timeline_events(const json &events, TimelineEvents &container);
......
......@@ -26,6 +26,8 @@ struct Notification
void
from_json(const nlohmann::json &obj, Notification &res);
void
to_json(nlohmann::json &obj, const Notification &res);
//! Response from the `GET /_matrix/client/r0/notifications` endpoint.
//
......@@ -44,5 +46,7 @@ struct Notifications
void
from_json(const nlohmann::json &obj, Notifications &res);
void
to_json(nlohmann::json &obj, const Notifications &res);
}
}
......@@ -104,6 +104,14 @@ parse_room_account_data_events(
}
}
void
compose_timeline_events(json &events,
const std::vector<mtx::events::collections::TimelineEvents> &container)
{
const auto c = container.at(0);
events = boost::apply_visitor(TimelineEventVisitor(), c);
}
void
parse_timeline_events(const json &events,
std::vector<mtx::events::collections::TimelineEvents> &container)
......
......@@ -34,11 +34,47 @@ from_json(const json &obj, Notification &res)
res.event = tmp.at(0);
}
void
to_json(json &obj, const Notification &res)
{
obj["actions"] = res.actions;
obj["read"] = res.read;
obj["room_id"] = res.room_id;
obj["ts"] = res.ts;
// HACK to work around the fact that there isn't
// a method to parse a timeline event from a json object.
//
// TODO: Create method that retrieves a TimelineEvents variant from a json object.
// Ideally with an optional type to indicate failure.
std::vector<events::collections::TimelineEvents> tmp;
tmp.reserve(1);
json arr;
tmp.push_back(res.event);
utils::compose_timeline_events(arr, tmp);
if (!tmp.empty()) {
obj["event"] = arr;
}
if (!res.profile_tag.empty()) {
obj["profile_tag"] = res.profile_tag;
}
}
void
from_json(const json &obj, Notifications &res)
{
// res.next_token = obj.at("next_token").get<std::string>();
res.notifications = obj.at("notifications").get<std::vector<Notification>>();
}
void
to_json(json &obj, const Notifications &notif)
{
obj["notifications"] = notif.notifications;
}
} // namespace responses
} // namespace mtx
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment