From b3dfec420489f82f0f513be02405e5268efa2760 Mon Sep 17 00:00:00 2001 From: tastytea Date: Tue, 12 Jun 2018 03:05:12 +0200 Subject: [PATCH] Added Easy::Account::fields() Returns the metadata fields introduced in Mastodon 2.4.0. --- CMakeLists.txt | 2 +- examples/example16_account_fields.cpp | 45 +++++++++++++++++++++++++++ src/easy/entities/account.cpp | 18 +++++++++++ src/easy/entities/account.hpp | 16 ++++++++++ src/mastodon-cpp.hpp | 1 + 5 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 examples/example16_account_fields.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 3ff5c43..56fddce 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project (mastodon-cpp - VERSION 0.16.0 + VERSION 0.16.1 LANGUAGES CXX ) diff --git a/examples/example16_account_fields.cpp b/examples/example16_account_fields.cpp new file mode 100644 index 0000000..bc549f8 --- /dev/null +++ b/examples/example16_account_fields.cpp @@ -0,0 +1,45 @@ +/* This file is part of mastodon-cpp. + * Get fields from own account. + */ + +#include +#include +#include +#include +#ifdef MASTODON_CPP + #include "mastodon-cpp.hpp" + #include "easy/all.hpp" +#else + #include + #include +#endif + +using std::cout; +using Mastodon::API; +using Mastodon::Easy; + +int main(int argc, char *argv[]) +{ + if (argc < 3) + { + std::cerr << "usage: " << argv[0] << " \n"; + return 1; + } + + Easy masto(argv[1], argv[2]); + std::string answer; + std::uint16_t ret; + ret = masto.get(API::v1::accounts_verify_credentials, answer); + + cout << "Return code: " << ret << '\n'; + + Easy::Account account(answer); + std::vector fields(account.fields()); + + for (const auto &field : fields) + { + cout << "Name: " << field.first << "\nValue: " << field.second << "\n\n"; + } + + return 0; +} diff --git a/src/easy/entities/account.cpp b/src/easy/entities/account.cpp index cc8083c..447b74a 100644 --- a/src/easy/entities/account.cpp +++ b/src/easy/entities/account.cpp @@ -58,6 +58,24 @@ const string Account::display_name() const return get_string("display_name"); } +const std::vector Account::fields() const +{ + const Json::Value &node = get("fields"); + + if (node.isArray()) + { + std::vector vec; + for (const Json::Value &value : node) + { + vec.push_back(Account::fields_pair(value["name"].asString(), + value["value"].asString())); + } + return vec; + } + + return {}; +} + const std::uint_fast64_t Account::followers_count() const { return get_uint64("followers_count"); diff --git a/src/easy/entities/account.hpp b/src/easy/entities/account.hpp index b554aca..ce0f1b0 100644 --- a/src/easy/entities/account.hpp +++ b/src/easy/entities/account.hpp @@ -20,6 +20,8 @@ #include #include #include +#include +#include // If we are compiling mastodon-cpp, use another include path #ifdef MASTODON_CPP @@ -42,6 +44,13 @@ namespace Mastodon class Easy::Account : public Easy::Entity { public: + /*! + * @brief Describes a field. Format: name, value + * + * @since 0.16.1 + */ + using fields_pair = std::pair; + /*! * @brief Constructs an Account object from a JSON string. * @@ -87,6 +96,13 @@ namespace Mastodon */ const string display_name() const; + /*! + * @brief Returns metadata fields + * + * @since 0.16.1 + */ + const std::vector fields() const; + /*! * @brief Returns number of followers */ diff --git a/src/mastodon-cpp.hpp b/src/mastodon-cpp.hpp index 35149be..e69c020 100644 --- a/src/mastodon-cpp.hpp +++ b/src/mastodon-cpp.hpp @@ -46,6 +46,7 @@ using std::string; * @example example13_easy_stream.cpp * @example example14_easy_treeview.cpp * @example example15_proxy.cpp + * @example example16_account_fields.cpp */ namespace Mastodon