diff --git a/CMakeLists.txt b/CMakeLists.txt index 98b6548..4ff29e3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project (mastodon-cpp - VERSION 0.13.2 + VERSION 0.13.3 LANGUAGES CXX ) diff --git a/src/easy/all.hpp b/src/easy/all.hpp index 57e8ba8..f66f904 100644 --- a/src/easy/all.hpp +++ b/src/easy/all.hpp @@ -35,6 +35,7 @@ #include "easy/entities/results.hpp" #include "easy/entities/status.hpp" #include "easy/entities/tag.hpp" + #include "easy/entities/pushsubscription.hpp" #else #include #include @@ -52,6 +53,7 @@ #include #include #include + #include #endif #endif // MASTODON_CPP_EASY_ALL_HPP diff --git a/src/easy/easy.hpp b/src/easy/easy.hpp index 307ce6b..93b4548 100644 --- a/src/easy/easy.hpp +++ b/src/easy/easy.hpp @@ -108,6 +108,13 @@ public: */ typedef std::pair stream_event; + /*! + * @brief Map of 'notification type' and 'push is requested or not' + * + * Used in PushSubscription::alerts(). + */ + typedef std::map alertmap; + /*! * @brief Class to hold the `Link`-header. * @@ -358,6 +365,7 @@ public: class Results; class Status; class Tag; + class PushSubscription; protected: inline static const string strtime diff --git a/src/easy/entities/pushsubscription.cpp b/src/easy/entities/pushsubscription.cpp new file mode 100644 index 0000000..11f5199 --- /dev/null +++ b/src/easy/entities/pushsubscription.cpp @@ -0,0 +1,89 @@ +/* This file is part of mastodon-cpp. + * Copyright © 2018 tastytea + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "pushsubscription.hpp" + +using namespace Mastodon; +using PushSubscription = Easy::PushSubscription; + +PushSubscription::PushSubscription(const string &json) +: Entity(json) +{} + +const uint_fast64_t PushSubscription::id() const +{ + return stouint64(get_string("id")); +} + +PushSubscription::PushSubscription() +: Entity() +{} + +const string PushSubscription::endpoint() const +{ + return get_string("endpoint"); +} + +const string PushSubscription::server_key() const +{ + return get_string("server_key"); +} + +const Easy::alertmap PushSubscription::alerts() const +{ + alertmap alerts; + const Json::Value node = get("alerts"); + for (auto it = node.begin(); it != node.end(); ++it) + { + const string &str = it.name(); + Easy::notification_type type; + if (str.compare("mention") == 0) + { + type = notification_type::Mention; + } + else if (str.compare("reblog") == 0) + { + type = notification_type::Reblog; + } + else if (str.compare("favourite") == 0) + { + type = notification_type::Favourite; + } + else if (str.compare("follow") == 0) + { + type = notification_type::Follow; + } + else + { + type = notification_type::Undefined; + } + + alerts.insert({{ type, s_to_b(it->asString()) }}); + } + return alerts; +} + +const bool PushSubscription::s_to_b(const string &str) const +{ + if (str.compare("true") == 0) + { + return true; + } + else + { + return false; + } +} diff --git a/src/easy/entities/pushsubscription.hpp b/src/easy/entities/pushsubscription.hpp new file mode 100644 index 0000000..671bb58 --- /dev/null +++ b/src/easy/entities/pushsubscription.hpp @@ -0,0 +1,88 @@ +/* This file is part of mastodon-cpp. + * Copyright © 2018 tastytea + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef MASTODON_CPP_EASY_PUSHSUBSCRIPTION_HPP +#define MASTODON_CPP_EASY_PUSHSUBSCRIPTION_HPP + +#include +#include +#include + +// If we are compiling mastodon-cpp, use another include path +#ifdef MASTODON_CPP + #include "mastodon-cpp.hpp" + #include "easy/easy.hpp" +#else + #include + #include +#endif + +using std::string; +using std::uint_fast64_t; + +namespace Mastodon +{ + /*! + * @brief Class to hold push subscriptions. + */ + class Easy::PushSubscription : public Easy::Entity + { + public: + /*! + * @brief Constructs an PushSubscription object from a JSON string. + * + * @param json JSON string + */ + explicit PushSubscription(const string &json); + + /*! + * @brief Constructs an empty PushSubscription object. + */ + PushSubscription(); + + /*! + * @brief Returns push subscription ID + */ + const uint_fast64_t id() const; + + /*! + * @brief Returns the endpoint URL + */ + const string endpoint() const; + + /*! + * @brief Returns the server public key for signature verification + */ + const string server_key() const; + + // TODO: Look up what the data looks like + /*! + * @brief Returns a map of 'notification event type' and + * 'push is requested or not' + */ + const Easy::alertmap alerts() const; + + protected: + /*! + * @brief Converts string to bool + * + * @return `true` if str is equal to "true", `false` otherwise + */ + const bool s_to_b(const string &str) const; +}; +} + +#endif // MASTODON_CPP_EASY_PUSHSUBSCRIPTION_HPP diff --git a/src/easy/entity.cpp b/src/easy/entity.cpp index 348eaba..741dbf8 100644 --- a/src/easy/entity.cpp +++ b/src/easy/entity.cpp @@ -69,6 +69,7 @@ const Json::Value Easy::Entity::to_object() const Easy::Entity::Entity() : _valid(false) +, _was_set(false) {} const bool Easy::Entity::valid() const