diff --git a/src/easy/easy.cpp b/src/easy/easy.cpp index f71ea02..247f9e4 100644 --- a/src/easy/easy.cpp +++ b/src/easy/easy.cpp @@ -17,6 +17,7 @@ #include #include // get_time #include +#include #include "easy.hpp" #include "macros.hpp" @@ -85,6 +86,34 @@ const void Easy::Entity::from_string(const string &json) } } +const std::vector + Easy::parse_stream(const std::string &streamdata) +{ + string stream = streamdata; + std::regex reevent("event: (update|notification|delete)\ndata: (.*)\n"); + std::smatch match; + std::vector vec = {}; + + while (std::regex_search(stream, match, reevent)) + { + const string &event = match[1].str(); + const string &data = match[2].str(); + event_type type = event_type::Undefined; + + if (event.compare("update") == 0) + type = event_type::Update; + else if (event.compare("notification") == 0) + type = event_type::Notification; + else if (event.compare("delete") == 0) + type = event_type::Delete; + + vec.push_back(stream_event(type, data)); + stream = match.suffix().str(); + } + + return vec; +} + const Json::Value Easy::Entity::to_object() const { return _tree; diff --git a/src/easy/easy.hpp b/src/easy/easy.hpp index 21d0290..c1fbd87 100644 --- a/src/easy/easy.hpp +++ b/src/easy/easy.hpp @@ -21,6 +21,7 @@ #include #include #include +#include #include // If we are compiling mastodon-cpp, use another include path @@ -42,6 +43,17 @@ namespace Mastodon class Easy : public API { public: + /*! + * @brief Describes the event type + */ + enum class event_type + { + Update, + Notification, + Delete, + Undefined + }; + /*! * @brief Describes visibility of toots. * @@ -92,6 +104,8 @@ public: unknown }; + typedef std::pair stream_event; + /*! * @brief Constructs a new Easy object. * @@ -112,6 +126,16 @@ public: */ static const std::vector json_array_to_vector(const string &json); + /*! + * @brief Split stream into a vector of events + * + * @param streamdata Data from get_stream() + * + * @return vector of stream events + */ + static const std::vector + parse_stream(const std::string &streamdata); + /*! * @brief Base class for entities. */