From e19bdc2feb7f2b3ec57487e31c7445431f66a0c7 Mon Sep 17 00:00:00 2001 From: tastytea Date: Wed, 21 Mar 2018 16:45:52 +0100 Subject: [PATCH 01/12] added Easy::Account() --- .gitignore | 1 + CMakeLists.txt | 21 +++- src/easy/account.cpp | 252 +++++++++++++++++++++++++++++++++++++++++++ src/easy/easy.cpp | 27 +++++ src/easy/easy.hpp | 181 +++++++++++++++++++++++++++++++ src/macros.hpp | 2 +- 6 files changed, 479 insertions(+), 5 deletions(-) create mode 100644 src/easy/account.cpp create mode 100644 src/easy/easy.cpp create mode 100644 src/easy/easy.hpp diff --git a/.gitignore b/.gitignore index 40fa305..57797e0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /build/ /doc/ /update_gh-pages.sh +/src/examples/example99* diff --git a/CMakeLists.txt b/CMakeLists.txt index f1a61fb..cb25364 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project (mastodon-cpp - VERSION 0.6.6 + VERSION 0.7.0 LANGUAGES CXX ) @@ -9,6 +9,7 @@ include(GNUInstallDirs) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC") set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall") +set(CXX_EXTENSIONS OFF) set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD_REQUIRED ON) @@ -26,13 +27,25 @@ if(CMAKE_BUILD_TYPE STREQUAL "Debug") endif() # Library -file(GLOB sources src/*.cpp src/*.hpp src/api/*.cpp) +if(WITHOUT_EASY) + file(GLOB sources src/*.cpp src/api/*.cpp) +else() + file(GLOB sources src/*.cpp src/api/*.cpp src/easy/*.cpp) +endif() add_library(mastodon-cpp SHARED ${sources}) set_target_properties(mastodon-cpp PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION ${mastodon-cpp_VERSION_MAJOR} ) -target_link_libraries(mastodon-cpp curlpp) +if(WITHOUT_EASY) + target_link_libraries(mastodon-cpp curlpp) + install(FILES src/mastodon-cpp.hpp + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/mastodon-cpp) +else() + target_link_libraries(mastodon-cpp curlpp jsoncpp) + install(FILES src/mastodon-cpp.hpp src/easy/easy.hpp + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/mastodon-cpp) +endif() install(TARGETS mastodon-cpp LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) install(FILES ${PROJECT_SOURCE_DIR}/src/mastodon-cpp.hpp DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) @@ -54,7 +67,7 @@ if(WITH_EXAMPLES) foreach(src ${sources_examples}) get_filename_component(bin ${src} NAME_WE) add_executable(${bin} ${src}) - target_link_libraries(${bin} -lpthread -ljsoncpp mastodon-cpp) + target_link_libraries(${bin} pthread jsoncpp mastodon-cpp) endforeach() endif() diff --git a/src/easy/account.cpp b/src/easy/account.cpp new file mode 100644 index 0000000..bdfc479 --- /dev/null +++ b/src/easy/account.cpp @@ -0,0 +1,252 @@ +/* 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 +#include +#include +#include +#include +#include // get_time +#include +#include +#include "easy.hpp" +#include "macros.hpp" + +using namespace Mastodon; +using Account = Easy::Account; +using std::string; + +Account::Account(const string &json) +: _valid(false) +{ + std::stringstream ss(json); + ss >> _tree; + + if (_tree.isNull()) + { + std::cerr << "ERROR: Could not build Account from JSON string\n"; + ttdebug << "String was: " << json << '\n'; + } + else + { + _valid = true; + } +} + +const bool Account::valid() const +{ + return _valid; +} + +const string Account::acct() const +{ + if (_tree["acct"].isString()) + { + return _tree["acct"].asString(); + } + + ttdebug << "Could not get account data: acct\n"; + return ""; +} + +const string Account::avatar() const +{ + if (_tree["avatar"].isString()) + { + return _tree["avatar"].asString(); + } + + ttdebug << "Could not get account data: avatar\n"; + return ""; +} + +const string Account::avatar_static() const +{ + if (_tree["avatar_static"].isString()) + { + return _tree["avatar_static"].asString(); + } + + ttdebug << "Could not get account data: avatar_static\n"; + return ""; +} + +const std::chrono::system_clock::time_point Account::created_at() const +{ + if (_tree["created_at"].isString()) + { + std::stringstream sstime(_tree["created_at"].asString()); + struct std::tm tm = {0}; + sstime >> std::get_time(&tm, "%Y-%m-%dT%T"); + std::time_t time = timegm(&tm); + return std::chrono::system_clock::from_time_t(time); + } + + ttdebug << "Could not get account data: created_at\n"; + // Return clocks epoch + return std::chrono::system_clock::time_point(); +} + +const string Account::display_name() const +{ + if (_tree["display_name"].isString()) + { + return _tree["display_name"].asString(); + } + + ttdebug << "Could not get account data: display_name\n"; + return ""; +} + +const std::uint64_t Account::followers_count() const +{ + if (_tree["followers_count"].isUInt64()) + { + return _tree["followers_count"].asUInt64(); + } + + ttdebug << "Could not get account data: followers_count\n"; + return 0; +} + +const std::uint64_t Account::following_count() const +{ + if (_tree["following_count"].isUInt64()) + { + return _tree["following_count"].asUInt64(); + } + + ttdebug << "Could not get account data: following_count\n"; + return 0; +} + +const string Account::header() const +{ + if (_tree["header"].isString()) + { + return _tree["header"].asString(); + } + + ttdebug << "Could not get account data: header\n"; + return ""; +} + +const string Account::header_static() const +{ + if (_tree["header_static"].isString()) + { + return _tree["header_static"].asString(); + } + + ttdebug << "Could not get account data: header_static\n"; + return ""; +} + +const std::uint64_t Account::id() const +{ + if (_tree["id"].isUInt64()) + { + return _tree["id"].asUInt64(); + } + + ttdebug << "Could not get account data: id\n"; + return 0; +} + +const bool Account::locked() const +{ + if (_tree["locked"].isBool()) + { + return _tree["locked"].asBool(); + } + + ttdebug << "Could not get account data: locked\n"; + return false; +} + +const string Account::note() const +{ + if (_tree["note"].isString()) + { + return _tree["note"].asString(); + } + + ttdebug << "Could not get account data: note\n"; + return ""; +} + +const string Account::note_plain() const +{ + if (_tree["source"]["note"].isString()) + { + return _tree["source"]["note"].asString(); + } + + ttdebug << "Could not get account data: note_plain\n"; + return ""; +} + +const Easy::visibility Account::privacy() const +{ + if (_tree["source"]["privacy"].isString()) + { + const string strprivacy = _tree["source"]["privacy"].asString(); + if (strprivacy.compare("public")) + return visibility::Public; + else if (strprivacy.compare("unlisted")) + return visibility::Unlisted; + else if (strprivacy.compare("private")) + return visibility::Private; + else if (strprivacy.compare("direct")) + return visibility::Direct; + } + + ttdebug << "Could not get account data: privacy\n"; + return visibility::Undefined; +} + +const bool Account::sensitive() const +{ + if (_tree["source"]["sensitive"].isBool()) + { + return _tree["source"]["sensitive"].asBool(); + } + + ttdebug << "Could not get account data: sensitive\n"; + return false; +} + +const std::uint64_t Account::statuses_count() const +{ + if (_tree["statuses_count"].isUInt64()) + { + return _tree["statuses_count"].asUInt64(); + } + + ttdebug << "Could not get account data: statuses_count\n"; + return 0; +} + +const string Account::username() const +{ + if (_tree["username"].isString()) + { + return _tree["username"].asString(); + } + + ttdebug << "Could not get account data: username\n"; + return ""; +} diff --git a/src/easy/easy.cpp b/src/easy/easy.cpp new file mode 100644 index 0000000..1ee446c --- /dev/null +++ b/src/easy/easy.cpp @@ -0,0 +1,27 @@ +/* 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 +#include "easy.hpp" + +using namespace Mastodon; +using std::string; + +Easy::Easy(const string &instance, const string &access_token) +: API(instance, access_token) +{ + // +} diff --git a/src/easy/easy.hpp b/src/easy/easy.hpp new file mode 100644 index 0000000..8dc4308 --- /dev/null +++ b/src/easy/easy.hpp @@ -0,0 +1,181 @@ +/* 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_EASY_CPP_HPP +#define MASTODON_EASY_CPP_HPP + +#include +#include +#include +#include +#include "mastodon-cpp.hpp" + +using std::string; +using std::uint16_t; + +namespace Mastodon +{ +/*! + * @brief Child of Mastodon::API with abstract methods. + */ +class Easy : public API +{ +public: + /*! + * @brief Describes visibility of toots. + * + * The names begin with a capital letter because some of them + * are reserved keywords when written in all-lowercase. + */ + enum class visibility + { + Direct, + Private, + Unlisted, + Public, + Undefined + }; + + /*! + * @brief Constructs a new Easy object. + * + * To register your application, leave access_token blank and call + * register_app1() and register_app2(). + * + * @param instance The hostname of your instance + * @param access_token The access token + */ + explicit Easy(const string &instance, const string &access_token); + + /*! + * @brief Class to hold accounts. + */ + class Account + { + public: + /*! + * @brief Constructs an account object from a JSON string. + * + * @param json JSON string + */ + Account(const string &json); + + /*! + * @brief Returns true if the account holds valid data + */ + const bool valid() const; + + /*! + * @brief Returns username + * + * `username` for users on the same instance, `user@hostname` + * for users on other instances. + */ + const string acct() const; + + /*! + * @brief Returns URL of avatar + */ + const string avatar() const; + + /*! + * @brief Returns URL of static avatar + */ + const string avatar_static() const; + + /*! + * @brief Returns time of creation + */ + const std::chrono::system_clock::time_point created_at() const; + + /*! + * @brief Returns display name + */ + const string display_name() const; + + /*! + * @brief Returns number of followers + */ + const std::uint64_t followers_count() const; + + /*! + * @brief Returns number of people this account follows + */ + const std::uint64_t following_count() const; + + /*! + * @brief Returns URL of header image + */ + const string header() const; + + /*! + * @brief Returns URL of static header image + */ + const string header_static() const; + + /*! + * @brief Returns account-ID + */ + const std::uint64_t id() const; + + /*! + * @brief Returns true if the account is locked + */ + const bool locked() const; + + /*! + * @brief Returns note + */ + const string note() const; + + /*! + * @brief Returns plaintext version of note + */ + const string note_plain() const; + + /*! + * @brief Returns default privacy of new toots + */ + const visibility privacy() const; + + /*! + * @brief Returns if media is amrked as sensitive by default + */ + const bool sensitive() const; + + /*! + * @brief Returns number of statuses + */ + const std::uint64_t statuses_count() const; + + /*! + * @brief Returns URL of the profile + */ + const string url() const; + + /*! + * @brief Returns username (without @hostname) + */ + const string username() const; + + private: + Json::Value _tree; + bool _valid; + }; +}; +} + +#endif // MASTODON_EASY_CPP_HPP diff --git a/src/macros.hpp b/src/macros.hpp index 50cb294..29ef058 100644 --- a/src/macros.hpp +++ b/src/macros.hpp @@ -17,6 +17,7 @@ #ifndef MACROS_HPP #define MACROS_HPP +#include #ifdef DEBUG #define ttdebug std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] DEBUG: " @@ -24,5 +25,4 @@ #define ttdebug false && std::cerr #endif - #endif // MACROS_HPP From a4a46d089e2735eeeb45cc5fe929b7d4d26f585e Mon Sep 17 00:00:00 2001 From: tastytea Date: Wed, 21 Mar 2018 17:08:33 +0100 Subject: [PATCH 02/12] Updated header location in examples, easy.hpp --- .gitignore | 1 + CMakeLists.txt | 18 +++++++++++------- README.md | 9 +++++++-- src/easy/easy.hpp | 2 +- src/examples/example01_dump_json.cpp | 2 +- src/examples/example02_parse_account.cpp | 2 +- src/examples/example03_mastocron.cpp | 2 +- src/examples/example04_update_credentials.cpp | 2 +- src/examples/example05_follow_unfollow.cpp | 2 +- src/examples/example06_toot_delete-toot.cpp | 2 +- src/examples/example07_register_app.cpp | 2 +- src/examples/example08_rate_limiting.cpp | 2 +- src/examples/example09_streaming_api.cpp | 2 +- src/examples/example10_simplify.cpp | 2 +- src/examples/example11_post_media.cpp | 2 +- 15 files changed, 31 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index 57797e0..ecc66e3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /build/ /doc/ /update_gh-pages.sh +/src/mastodon-cpp /src/examples/example99* diff --git a/CMakeLists.txt b/CMakeLists.txt index cb25364..f7d5fa5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project (mastodon-cpp - VERSION 0.7.0 + VERSION 0.7.1 LANGUAGES CXX ) @@ -26,6 +26,10 @@ if(CMAKE_BUILD_TYPE STREQUAL "Debug") add_definitions(-DDEBUG=1) endif() +if(WITHOUT_EASY) + add_definitions(-DWITHOUT_EASY=1) +endif() + # Library if(WITHOUT_EASY) file(GLOB sources src/*.cpp src/api/*.cpp) @@ -39,16 +43,16 @@ set_target_properties(mastodon-cpp PROPERTIES ) if(WITHOUT_EASY) target_link_libraries(mastodon-cpp curlpp) - install(FILES src/mastodon-cpp.hpp - DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/mastodon-cpp) else() target_link_libraries(mastodon-cpp curlpp jsoncpp) - install(FILES src/mastodon-cpp.hpp src/easy/easy.hpp - DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/mastodon-cpp) endif() install(TARGETS mastodon-cpp LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) -install(FILES ${PROJECT_SOURCE_DIR}/src/mastodon-cpp.hpp - DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) +install(FILES src/mastodon-cpp.hpp + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/mastodon-cpp) +if(NOT WITHOUT_EASY) + install(FILES src/easy/easy.hpp + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/mastodon-cpp) +endif() # Documentation if(WITH_DOC) diff --git a/README.md b/README.md index 26764a4..9028417 100644 --- a/README.md +++ b/README.md @@ -9,12 +9,16 @@ The HTML reference can be generated with `build_doc.sh`, if doxygen is installed Or just look in `src/mastodon-cpp.hpp`. It is also available at [tastytea.github.io/mastodon-cpp/](https://tastytea.github.io/mastodon-cpp/docs/classMastodon_1_1API.html). There are [examples](https://github.com/tastytea/mastodon-cpp/tree/master/src/examples) in `src/examples/`. +## Upgrading from below 0.7.0 + +The header location has changed. It is now in `mastodon-cpp/`. + ## Most basic example ```C++ #include #include -#include +#include int main() { @@ -85,8 +89,8 @@ To use the DEB package on stretch, you will need [libcurlpp0](https://packages.d * [libcurl](https://curl.haxx.se/) (tested: 7.58.0/7.35.0) * [curlpp](http://www.curlpp.org/) (tested: 0.8.1/0.7.3) * Optional + * Easy interface & Examples: [jsoncpp](https://github.com/open-source-parsers/jsoncpp) (tested: 1.8.1 / 1.7.2) * Documentation: [doxygen](https://www.stack.nl/~dimitri/doxygen/) (tested: 1.8.13) - * Examples: [jsoncpp](https://github.com/open-source-parsers/jsoncpp) (tested: 1.8.1 / 1.7.2) * DEB package: [dpkg](https://packages.qa.debian.org/dpkg) (tested: 1.19.0.5) * RPM package: [rpm](http://www.rpm.org) (tested: 4.11.0.1) @@ -112,6 +116,7 @@ Download the current release at [GitHub](https://github.com/tastytea/mastodon-cp cmake options: * `-DCMAKE_BUILD_TYPE=Debug` for a debug build + * `-DWITHOUT_EASY=ON` to not build the Easy abstractions and to get rid of the jsoncpp-dependency (not recommended) * `-DWITH_EXAMPLES=ON` if you want to compile the examples * `-DWITH_TESTS=ON` if you want to compile the tests * `-DWITH_DOC=ON` if you want to compile the HTML reference diff --git a/src/easy/easy.hpp b/src/easy/easy.hpp index 8dc4308..3cf06f0 100644 --- a/src/easy/easy.hpp +++ b/src/easy/easy.hpp @@ -21,7 +21,7 @@ #include #include #include -#include "mastodon-cpp.hpp" +#include "mastodon-cpp/mastodon-cpp.hpp" using std::string; using std::uint16_t; diff --git a/src/examples/example01_dump_json.cpp b/src/examples/example01_dump_json.cpp index 40d7db4..32ffdec 100644 --- a/src/examples/example01_dump_json.cpp +++ b/src/examples/example01_dump_json.cpp @@ -7,7 +7,7 @@ #include #include #include -#include "../mastodon-cpp.hpp" +#include "mastodon-cpp/mastodon-cpp.hpp" using Mastodon::API; diff --git a/src/examples/example02_parse_account.cpp b/src/examples/example02_parse_account.cpp index 7cc9064..78d27db 100644 --- a/src/examples/example02_parse_account.cpp +++ b/src/examples/example02_parse_account.cpp @@ -8,7 +8,7 @@ #include #include #include -#include "../mastodon-cpp.hpp" +#include "mastodon-cpp/mastodon-cpp.hpp" using Mastodon::API; using std::cout; diff --git a/src/examples/example03_mastocron.cpp b/src/examples/example03_mastocron.cpp index 21cfe44..e2c8108 100644 --- a/src/examples/example03_mastocron.cpp +++ b/src/examples/example03_mastocron.cpp @@ -11,7 +11,7 @@ #include #include #include -#include "../mastodon-cpp.hpp" +#include "mastodon-cpp/mastodon-cpp.hpp" using Mastodon::API; using std::cout; diff --git a/src/examples/example04_update_credentials.cpp b/src/examples/example04_update_credentials.cpp index 5c16fbe..0ae4991 100644 --- a/src/examples/example04_update_credentials.cpp +++ b/src/examples/example04_update_credentials.cpp @@ -6,7 +6,7 @@ #include #include #include -#include "../mastodon-cpp.hpp" +#include "mastodon-cpp/mastodon-cpp.hpp" using Mastodon::API; diff --git a/src/examples/example05_follow_unfollow.cpp b/src/examples/example05_follow_unfollow.cpp index 0415e1a..05d7b95 100644 --- a/src/examples/example05_follow_unfollow.cpp +++ b/src/examples/example05_follow_unfollow.cpp @@ -8,7 +8,7 @@ #include #include #include -#include "../mastodon-cpp.hpp" +#include "mastodon-cpp/mastodon-cpp.hpp" using Mastodon::API; diff --git a/src/examples/example06_toot_delete-toot.cpp b/src/examples/example06_toot_delete-toot.cpp index ef15012..5fda1a1 100644 --- a/src/examples/example06_toot_delete-toot.cpp +++ b/src/examples/example06_toot_delete-toot.cpp @@ -8,7 +8,7 @@ #include #include #include -#include "../mastodon-cpp.hpp" +#include "mastodon-cpp/mastodon-cpp.hpp" using Mastodon::API; diff --git a/src/examples/example07_register_app.cpp b/src/examples/example07_register_app.cpp index fcfe855..1302900 100644 --- a/src/examples/example07_register_app.cpp +++ b/src/examples/example07_register_app.cpp @@ -7,7 +7,7 @@ #include #include #include -#include "../mastodon-cpp.hpp" +#include "mastodon-cpp/mastodon-cpp.hpp" using Mastodon::API; diff --git a/src/examples/example08_rate_limiting.cpp b/src/examples/example08_rate_limiting.cpp index 1aeeedb..2e90d6d 100644 --- a/src/examples/example08_rate_limiting.cpp +++ b/src/examples/example08_rate_limiting.cpp @@ -7,7 +7,7 @@ #include #include #include -#include "../mastodon-cpp.hpp" +#include "mastodon-cpp/mastodon-cpp.hpp" using Mastodon::API; diff --git a/src/examples/example09_streaming_api.cpp b/src/examples/example09_streaming_api.cpp index f05372e..127e38d 100644 --- a/src/examples/example09_streaming_api.cpp +++ b/src/examples/example09_streaming_api.cpp @@ -9,7 +9,7 @@ #include #include #include -#include "../mastodon-cpp.hpp" +#include "mastodon-cpp/mastodon-cpp.hpp" using Mastodon::API; diff --git a/src/examples/example10_simplify.cpp b/src/examples/example10_simplify.cpp index 1cd9195..02b33da 100644 --- a/src/examples/example10_simplify.cpp +++ b/src/examples/example10_simplify.cpp @@ -6,7 +6,7 @@ #include #include #include -#include "../mastodon-cpp.hpp" +#include "mastodon-cpp/mastodon-cpp.hpp" using Mastodon::API; diff --git a/src/examples/example11_post_media.cpp b/src/examples/example11_post_media.cpp index b93dd43..f9df024 100644 --- a/src/examples/example11_post_media.cpp +++ b/src/examples/example11_post_media.cpp @@ -8,7 +8,7 @@ #include #include #include -#include "../mastodon-cpp.hpp" +#include "mastodon-cpp/mastodon-cpp.hpp" using Mastodon::API; From 968339a0c0d8b7fd4db2bcc08e9dedcab15a02bd Mon Sep 17 00:00:00 2001 From: tastytea Date: Wed, 21 Mar 2018 17:51:17 +0100 Subject: [PATCH 03/12] updated ebuilds --- packages/gentoo/mastodon-cpp-0.0.0.ebuild | 2 +- packages/gentoo/mastodon-cpp-9999.ebuild | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/gentoo/mastodon-cpp-0.0.0.ebuild b/packages/gentoo/mastodon-cpp-0.0.0.ebuild index 437a9af..87be06a 100644 --- a/packages/gentoo/mastodon-cpp-0.0.0.ebuild +++ b/packages/gentoo/mastodon-cpp-0.0.0.ebuild @@ -9,7 +9,7 @@ SLOT="0" KEYWORDS="" IUSE="doc debug examples" RDEPEND=">=dev-cpp/curlpp-0.7.3 - examples? ( >=dev-libs/boost-1.63.0 )" + >=dev-libs/jsoncpp-1.8.1" DEPEND=">=dev-util/cmake-3.9.6 doc? ( >=app-doc/doxygen-1.8.13-r1 ) ${RDEPEND}" diff --git a/packages/gentoo/mastodon-cpp-9999.ebuild b/packages/gentoo/mastodon-cpp-9999.ebuild index 906b019..5d4d275 100644 --- a/packages/gentoo/mastodon-cpp-9999.ebuild +++ b/packages/gentoo/mastodon-cpp-9999.ebuild @@ -13,7 +13,7 @@ SLOT="0" KEYWORDS="" IUSE="doc debug examples" RDEPEND=">=dev-cpp/curlpp-0.8.1 - examples? ( >=dev-libs/boost-1.63.0 )" + >=dev-libs/jsoncpp-1.8.1" DEPEND=">=dev-util/cmake-3.9.6 doc? ( >=app-doc/doxygen-1.8.13-r1 ) ${RDEPEND}" From 6cb9589cc0af352ce1432fbdc014c0f16c02dc53 Mon Sep 17 00:00:00 2001 From: tastytea Date: Wed, 21 Mar 2018 20:12:45 +0100 Subject: [PATCH 04/12] fixed header searching --- .gitignore | 1 - CMakeLists.txt | 3 + src/easy/easy.hpp | 71 +++++++++++++++++-- src/examples/example01_dump_json.cpp | 6 +- src/examples/example02_parse_account.cpp | 6 +- src/examples/example03_mastocron.cpp | 6 +- src/examples/example04_update_credentials.cpp | 6 +- src/examples/example05_follow_unfollow.cpp | 6 +- src/examples/example06_toot_delete-toot.cpp | 6 +- src/examples/example07_register_app.cpp | 6 +- src/examples/example08_rate_limiting.cpp | 6 +- src/examples/example09_streaming_api.cpp | 6 +- src/examples/example10_simplify.cpp | 6 +- src/examples/example11_post_media.cpp | 6 +- 14 files changed, 123 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index ecc66e3..57797e0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ /build/ /doc/ /update_gh-pages.sh -/src/mastodon-cpp /src/examples/example99* diff --git a/CMakeLists.txt b/CMakeLists.txt index f7d5fa5..4152d6c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,9 @@ configure_file ( "${PROJECT_BINARY_DIR}/version.hpp" ) +# Announce that we are compiling mastodon-cpp (used in easy.hpp and examples) +add_definitions(-DMASTODON_CPP=1) + if(CMAKE_BUILD_TYPE STREQUAL "Debug") add_definitions(-DDEBUG=1) endif() diff --git a/src/easy/easy.hpp b/src/easy/easy.hpp index 3cf06f0..2b3ea39 100644 --- a/src/easy/easy.hpp +++ b/src/easy/easy.hpp @@ -21,10 +21,16 @@ #include #include #include -#include "mastodon-cpp/mastodon-cpp.hpp" +// If we are compiling mastodon-cpp, use another include path +#ifdef MASTODON_CPP + #include "mastodon-cpp.hpp" +#else + #include +#endif using std::string; using std::uint16_t; +using std::uint64_t; namespace Mastodon { @@ -49,6 +55,17 @@ public: Undefined }; + /*! + * @brief Describes the attachment type + */ + enum class attachment_type + { + image, + video, + gifv, + unknown + }; + /*! * @brief Constructs a new Easy object. * @@ -71,7 +88,7 @@ public: * * @param json JSON string */ - Account(const string &json); + explicit Account(const string &json); /*! * @brief Returns true if the account holds valid data @@ -109,12 +126,12 @@ public: /*! * @brief Returns number of followers */ - const std::uint64_t followers_count() const; + const uint64_t followers_count() const; /*! * @brief Returns number of people this account follows */ - const std::uint64_t following_count() const; + const uint64_t following_count() const; /*! * @brief Returns URL of header image @@ -129,13 +146,24 @@ public: /*! * @brief Returns account-ID */ - const std::uint64_t id() const; + const uint64_t id() const; /*! * @brief Returns true if the account is locked */ const bool locked() const; + /*! + * @brief Returns true if the account has been moved + */ + const bool has_moved() const; + + /*! + * @brief If the owner decided to switch accounts, new account is in + * this attribute + */ + const Account moved() const; + /*! * @brief Returns note */ @@ -159,7 +187,7 @@ public: /*! * @brief Returns number of statuses */ - const std::uint64_t statuses_count() const; + const uint64_t statuses_count() const; /*! * @brief Returns URL of the profile @@ -175,6 +203,37 @@ public: Json::Value _tree; bool _valid; }; + + /*! + * @brief Class to hold attachments + */ + class Attachment + { + public: + /*! + * @brief Constructs an attachment object from a JSON string. + * + * @param json JSON string + */ + explicit Attachment(const string &json); + + /*! + * @brief Returns true if the attachment holds valid data + */ + const bool valid() const; + + const uint64_t id() const; + const attachment_type type() const; + const string url() const; + const string remote_url() const; + const string preview_url() const; + const string text_url() const; + const string description() const; + + private: + Json::Value _tree; + bool _valid; + }; }; } diff --git a/src/examples/example01_dump_json.cpp b/src/examples/example01_dump_json.cpp index 32ffdec..7d29e3a 100644 --- a/src/examples/example01_dump_json.cpp +++ b/src/examples/example01_dump_json.cpp @@ -7,7 +7,11 @@ #include #include #include -#include "mastodon-cpp/mastodon-cpp.hpp" +#ifdef MASTODON_CPP + #include "mastodon-cpp.hpp" +#else + #include +#endif using Mastodon::API; diff --git a/src/examples/example02_parse_account.cpp b/src/examples/example02_parse_account.cpp index 78d27db..9ecfcc4 100644 --- a/src/examples/example02_parse_account.cpp +++ b/src/examples/example02_parse_account.cpp @@ -8,7 +8,11 @@ #include #include #include -#include "mastodon-cpp/mastodon-cpp.hpp" +#ifdef MASTODON_CPP + #include "mastodon-cpp.hpp" +#else + #include +#endif using Mastodon::API; using std::cout; diff --git a/src/examples/example03_mastocron.cpp b/src/examples/example03_mastocron.cpp index e2c8108..613114f 100644 --- a/src/examples/example03_mastocron.cpp +++ b/src/examples/example03_mastocron.cpp @@ -11,7 +11,11 @@ #include #include #include -#include "mastodon-cpp/mastodon-cpp.hpp" +#ifdef MASTODON_CPP + #include "mastodon-cpp.hpp" +#else + #include +#endif using Mastodon::API; using std::cout; diff --git a/src/examples/example04_update_credentials.cpp b/src/examples/example04_update_credentials.cpp index 0ae4991..7278e9a 100644 --- a/src/examples/example04_update_credentials.cpp +++ b/src/examples/example04_update_credentials.cpp @@ -6,7 +6,11 @@ #include #include #include -#include "mastodon-cpp/mastodon-cpp.hpp" +#ifdef MASTODON_CPP + #include "mastodon-cpp.hpp" +#else + #include +#endif using Mastodon::API; diff --git a/src/examples/example05_follow_unfollow.cpp b/src/examples/example05_follow_unfollow.cpp index 05d7b95..b2d2b95 100644 --- a/src/examples/example05_follow_unfollow.cpp +++ b/src/examples/example05_follow_unfollow.cpp @@ -8,7 +8,11 @@ #include #include #include -#include "mastodon-cpp/mastodon-cpp.hpp" +#ifdef MASTODON_CPP + #include "mastodon-cpp.hpp" +#else + #include +#endif using Mastodon::API; diff --git a/src/examples/example06_toot_delete-toot.cpp b/src/examples/example06_toot_delete-toot.cpp index 5fda1a1..6361b49 100644 --- a/src/examples/example06_toot_delete-toot.cpp +++ b/src/examples/example06_toot_delete-toot.cpp @@ -8,7 +8,11 @@ #include #include #include -#include "mastodon-cpp/mastodon-cpp.hpp" +#ifdef MASTODON_CPP + #include "mastodon-cpp.hpp" +#else + #include +#endif using Mastodon::API; diff --git a/src/examples/example07_register_app.cpp b/src/examples/example07_register_app.cpp index 1302900..a96e464 100644 --- a/src/examples/example07_register_app.cpp +++ b/src/examples/example07_register_app.cpp @@ -7,7 +7,11 @@ #include #include #include -#include "mastodon-cpp/mastodon-cpp.hpp" +#ifdef MASTODON_CPP + #include "mastodon-cpp.hpp" +#else + #include +#endif using Mastodon::API; diff --git a/src/examples/example08_rate_limiting.cpp b/src/examples/example08_rate_limiting.cpp index 2e90d6d..1255ccc 100644 --- a/src/examples/example08_rate_limiting.cpp +++ b/src/examples/example08_rate_limiting.cpp @@ -7,7 +7,11 @@ #include #include #include -#include "mastodon-cpp/mastodon-cpp.hpp" +#ifdef MASTODON_CPP + #include "mastodon-cpp.hpp" +#else + #include +#endif using Mastodon::API; diff --git a/src/examples/example09_streaming_api.cpp b/src/examples/example09_streaming_api.cpp index 127e38d..13de09e 100644 --- a/src/examples/example09_streaming_api.cpp +++ b/src/examples/example09_streaming_api.cpp @@ -9,7 +9,11 @@ #include #include #include -#include "mastodon-cpp/mastodon-cpp.hpp" +#ifdef MASTODON_CPP + #include "mastodon-cpp.hpp" +#else + #include +#endif using Mastodon::API; diff --git a/src/examples/example10_simplify.cpp b/src/examples/example10_simplify.cpp index 02b33da..baf4a53 100644 --- a/src/examples/example10_simplify.cpp +++ b/src/examples/example10_simplify.cpp @@ -6,7 +6,11 @@ #include #include #include -#include "mastodon-cpp/mastodon-cpp.hpp" +#ifdef MASTODON_CPP + #include "mastodon-cpp.hpp" +#else + #include +#endif using Mastodon::API; diff --git a/src/examples/example11_post_media.cpp b/src/examples/example11_post_media.cpp index f9df024..0948ef3 100644 --- a/src/examples/example11_post_media.cpp +++ b/src/examples/example11_post_media.cpp @@ -8,7 +8,11 @@ #include #include #include -#include "mastodon-cpp/mastodon-cpp.hpp" +#ifdef MASTODON_CPP + #include "mastodon-cpp.hpp" +#else + #include +#endif using Mastodon::API; From 92e60eb7c27ad8754321980a95e9c886201e5839 Mon Sep 17 00:00:00 2001 From: tastytea Date: Wed, 21 Mar 2018 20:29:33 +0100 Subject: [PATCH 05/12] added Attachment class --- CMakeLists.txt | 2 +- src/easy/account.cpp | 24 +++++++- src/easy/attachment.cpp | 128 ++++++++++++++++++++++++++++++++++++++++ src/easy/easy.cpp | 2 +- src/easy/easy.hpp | 41 ++++++++++--- 5 files changed, 187 insertions(+), 10 deletions(-) create mode 100644 src/easy/attachment.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 4152d6c..73fdaa3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project (mastodon-cpp - VERSION 0.7.1 + VERSION 0.7.2 LANGUAGES CXX ) diff --git a/src/easy/account.cpp b/src/easy/account.cpp index bdfc479..38d75e2 100644 --- a/src/easy/account.cpp +++ b/src/easy/account.cpp @@ -1,6 +1,6 @@ /* 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. @@ -177,6 +177,28 @@ const bool Account::locked() const return false; } +const bool Account::has_moved() const +{ + if (_tree["moved"].isObject()) + { + return true; + } + + return false; +} + +const Account Account::moved() const +{ + if (has_moved()) + { + // TODO: Find an account with this node and test + return Account(_tree["moved"].toStyledString()); + } + + ttdebug << "Could not get account data: moved\n"; + return Account(""); +} + const string Account::note() const { if (_tree["note"].isString()) diff --git a/src/easy/attachment.cpp b/src/easy/attachment.cpp new file mode 100644 index 0000000..296c9d2 --- /dev/null +++ b/src/easy/attachment.cpp @@ -0,0 +1,128 @@ +/* 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 +#include +#include +#include "easy.hpp" +#include "macros.hpp" + +using namespace Mastodon; +using Attachment = Easy::Attachment; +using std::string; + +Attachment::Attachment(const string &json) +{ + std::stringstream ss(json); + ss >> _tree; + + if (_tree.isNull()) + { + std::cerr << "ERROR: Could not build Attachment from JSON string\n"; + ttdebug << "String was: " << json << '\n'; + } + else + { + _valid = true; + } +} + +const bool Attachment::valid() const +{ + return _valid; +} + +const string Attachment::description() const +{ + if (_tree["description"].isString()) + { + return _tree["description"].asString(); + } + + ttdebug << "Could not get attachment data: description\n"; + return ""; +} + +const std::uint64_t Attachment::id() const +{ + if (_tree["id"].isUInt64()) + { + return _tree["id"].asUInt64(); + } + + ttdebug << "Could not get attachment data: id\n"; + return 0; +} + +const string Attachment::preview_url() const +{ + if (_tree["preview_url"].isString()) + { + return _tree["preview_url"].asString(); + } + + ttdebug << "Could not get attachment data: preview_url\n"; + return ""; +} + +const string Attachment::remote_url() const +{ + if (_tree["remote_url"].isString()) + { + return _tree["remote_url"].asString(); + } + + ttdebug << "Could not get attachment data: remote_url\n"; + return ""; +} + +const string Attachment::text_url() const +{ + if (_tree["text_url"].isString()) + { + return _tree["text_url"].asString(); + } + + ttdebug << "Could not get attachment data: text_url\n"; + return ""; +} + +const Easy::attachment_type Attachment::type() const +{ + const string strtype = _tree["type"].asString(); + if (strtype.compare("image")) + return attachment_type::image; + else if (strtype.compare("video")) + return attachment_type::video; + else if (strtype.compare("gifv")) + return attachment_type::gifv; + else if (strtype.compare("unknown")) + return attachment_type::unknown; + + ttdebug << "Could not get account data: type\n"; + return attachment_type::unknown; +} + +const string Attachment::url() const +{ + if (_tree["url"].isString()) + { + return _tree["url"].asString(); + } + + ttdebug << "Could not get attachment data: url\n"; + return ""; +} diff --git a/src/easy/easy.cpp b/src/easy/easy.cpp index 1ee446c..91672cb 100644 --- a/src/easy/easy.cpp +++ b/src/easy/easy.cpp @@ -1,6 +1,6 @@ /* 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. diff --git a/src/easy/easy.hpp b/src/easy/easy.hpp index 2b3ea39..e545b49 100644 --- a/src/easy/easy.hpp +++ b/src/easy/easy.hpp @@ -180,7 +180,7 @@ public: const visibility privacy() const; /*! - * @brief Returns if media is amrked as sensitive by default + * @brief Returns if media is marked as sensitive by default */ const bool sensitive() const; @@ -222,14 +222,41 @@ public: */ const bool valid() const; - const uint64_t id() const; - const attachment_type type() const; - const string url() const; - const string remote_url() const; - const string preview_url() const; - const string text_url() const; + /*! + * @brief Returns the image description + */ const string description() const; + /*! + * @brief Returns the ID of the attachment + */ + const uint64_t id() const; + + /*! + * @brief Returns the URL of the preview image + */ + const string preview_url() const; + + /*! + * @brief Returns the remote URL of the original image + */ + const string remote_url() const; + + /*! + * @brief Returns shorter URL for the image + */ + const string text_url() const; + + /*! + * @brief Returns attachment type + */ + const attachment_type type() const; + + /*! + * @brief Returns URL of the locally hosted version of the image + */ + const string url() const; + private: Json::Value _tree; bool _valid; From 29581f92fc6c911b51c9bbce00fd989d8ab6f5c2 Mon Sep 17 00:00:00 2001 From: tastytea Date: Wed, 21 Mar 2018 23:27:10 +0100 Subject: [PATCH 06/12] moved examples and tests to root directory --- .gitignore | 2 +- CMakeLists.txt | 4 ++-- {src/examples => examples}/example01_dump_json.cpp | 0 {src/examples => examples}/example02_parse_account.cpp | 0 {src/examples => examples}/example03_mastocron.cpp | 0 {src/examples => examples}/example04_update_credentials.cpp | 0 {src/examples => examples}/example05_follow_unfollow.cpp | 0 {src/examples => examples}/example06_toot_delete-toot.cpp | 0 {src/examples => examples}/example07_register_app.cpp | 0 {src/examples => examples}/example08_rate_limiting.cpp | 0 {src/examples => examples}/example09_streaming_api.cpp | 0 {src/examples => examples}/example10_simplify.cpp | 0 {src/examples => examples}/example11_post_media.cpp | 0 packages/gentoo/mastodon-cpp-0.0.0.ebuild | 2 +- packages/gentoo/mastodon-cpp-9999.ebuild | 2 +- {src/tests => tests}/test_00_library_is_loadable.cpp | 2 +- {src/tests => tests}/test_01_get_instance.cpp | 2 +- 17 files changed, 7 insertions(+), 7 deletions(-) rename {src/examples => examples}/example01_dump_json.cpp (100%) rename {src/examples => examples}/example02_parse_account.cpp (100%) rename {src/examples => examples}/example03_mastocron.cpp (100%) rename {src/examples => examples}/example04_update_credentials.cpp (100%) rename {src/examples => examples}/example05_follow_unfollow.cpp (100%) rename {src/examples => examples}/example06_toot_delete-toot.cpp (100%) rename {src/examples => examples}/example07_register_app.cpp (100%) rename {src/examples => examples}/example08_rate_limiting.cpp (100%) rename {src/examples => examples}/example09_streaming_api.cpp (100%) rename {src/examples => examples}/example10_simplify.cpp (100%) rename {src/examples => examples}/example11_post_media.cpp (100%) rename {src/tests => tests}/test_00_library_is_loadable.cpp (78%) rename {src/tests => tests}/test_01_get_instance.cpp (93%) diff --git a/.gitignore b/.gitignore index 57797e0..97496bd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ /build/ /doc/ /update_gh-pages.sh -/src/examples/example99* +/examples/example99* diff --git a/CMakeLists.txt b/CMakeLists.txt index 73fdaa3..f320c96 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -70,7 +70,7 @@ endif() # Examples if(WITH_EXAMPLES) - file(GLOB sources_examples src/examples/*.cpp) + file(GLOB sources_examples examples/*.cpp) foreach(src ${sources_examples}) get_filename_component(bin ${src} NAME_WE) add_executable(${bin} ${src}) @@ -81,7 +81,7 @@ endif() # Tests if(WITH_TESTS) include(CTest) - file(GLOB sources_tests src/tests/test_*.cpp) + file(GLOB sources_tests tests/test_*.cpp) foreach(src ${sources_tests}) get_filename_component(bin ${src} NAME_WE) add_executable(${bin} ${src}) diff --git a/src/examples/example01_dump_json.cpp b/examples/example01_dump_json.cpp similarity index 100% rename from src/examples/example01_dump_json.cpp rename to examples/example01_dump_json.cpp diff --git a/src/examples/example02_parse_account.cpp b/examples/example02_parse_account.cpp similarity index 100% rename from src/examples/example02_parse_account.cpp rename to examples/example02_parse_account.cpp diff --git a/src/examples/example03_mastocron.cpp b/examples/example03_mastocron.cpp similarity index 100% rename from src/examples/example03_mastocron.cpp rename to examples/example03_mastocron.cpp diff --git a/src/examples/example04_update_credentials.cpp b/examples/example04_update_credentials.cpp similarity index 100% rename from src/examples/example04_update_credentials.cpp rename to examples/example04_update_credentials.cpp diff --git a/src/examples/example05_follow_unfollow.cpp b/examples/example05_follow_unfollow.cpp similarity index 100% rename from src/examples/example05_follow_unfollow.cpp rename to examples/example05_follow_unfollow.cpp diff --git a/src/examples/example06_toot_delete-toot.cpp b/examples/example06_toot_delete-toot.cpp similarity index 100% rename from src/examples/example06_toot_delete-toot.cpp rename to examples/example06_toot_delete-toot.cpp diff --git a/src/examples/example07_register_app.cpp b/examples/example07_register_app.cpp similarity index 100% rename from src/examples/example07_register_app.cpp rename to examples/example07_register_app.cpp diff --git a/src/examples/example08_rate_limiting.cpp b/examples/example08_rate_limiting.cpp similarity index 100% rename from src/examples/example08_rate_limiting.cpp rename to examples/example08_rate_limiting.cpp diff --git a/src/examples/example09_streaming_api.cpp b/examples/example09_streaming_api.cpp similarity index 100% rename from src/examples/example09_streaming_api.cpp rename to examples/example09_streaming_api.cpp diff --git a/src/examples/example10_simplify.cpp b/examples/example10_simplify.cpp similarity index 100% rename from src/examples/example10_simplify.cpp rename to examples/example10_simplify.cpp diff --git a/src/examples/example11_post_media.cpp b/examples/example11_post_media.cpp similarity index 100% rename from src/examples/example11_post_media.cpp rename to examples/example11_post_media.cpp diff --git a/packages/gentoo/mastodon-cpp-0.0.0.ebuild b/packages/gentoo/mastodon-cpp-0.0.0.ebuild index 87be06a..d76bc2b 100644 --- a/packages/gentoo/mastodon-cpp-0.0.0.ebuild +++ b/packages/gentoo/mastodon-cpp-0.0.0.ebuild @@ -44,7 +44,7 @@ src_install() { if use examples; then docinto examples - for file in src/examples/*.cpp; do + for file in examples/*.cpp; do dodoc ${file} done fi diff --git a/packages/gentoo/mastodon-cpp-9999.ebuild b/packages/gentoo/mastodon-cpp-9999.ebuild index 5d4d275..b28db04 100644 --- a/packages/gentoo/mastodon-cpp-9999.ebuild +++ b/packages/gentoo/mastodon-cpp-9999.ebuild @@ -52,7 +52,7 @@ src_install() { if use examples; then docinto examples - for file in src/examples/*.cpp; do + for file in examples/*.cpp; do dodoc ${file} done fi diff --git a/src/tests/test_00_library_is_loadable.cpp b/tests/test_00_library_is_loadable.cpp similarity index 78% rename from src/tests/test_00_library_is_loadable.cpp rename to tests/test_00_library_is_loadable.cpp index 8565086..8ce4269 100644 --- a/src/tests/test_00_library_is_loadable.cpp +++ b/tests/test_00_library_is_loadable.cpp @@ -1,7 +1,7 @@ /* This file is part of mastodon-cpp. */ -#include "../mastodon-cpp.hpp" +#include "mastodon-cpp.hpp" int main(int argc, char *argv[]) { diff --git a/src/tests/test_01_get_instance.cpp b/tests/test_01_get_instance.cpp similarity index 93% rename from src/tests/test_01_get_instance.cpp rename to tests/test_01_get_instance.cpp index b43b72c..9d88fbb 100644 --- a/src/tests/test_01_get_instance.cpp +++ b/tests/test_01_get_instance.cpp @@ -3,7 +3,7 @@ #include #include -#include "../mastodon-cpp.hpp" +#include "mastodon-cpp.hpp" int main(int argc, char *argv[]) { From c72715a63de39ceae2ac36a27c20fa01ceece452 Mon Sep 17 00:00:00 2001 From: tastytea Date: Wed, 21 Mar 2018 23:28:48 +0100 Subject: [PATCH 07/12] Added meta info to attachment class --- src/easy/account.cpp | 2 +- src/easy/attachment.cpp | 105 ++++++++++++++++++++++++++++++++++++++++ src/easy/easy.hpp | 93 ++++++++++++++++++++++++++++++++++- 3 files changed, 197 insertions(+), 3 deletions(-) diff --git a/src/easy/account.cpp b/src/easy/account.cpp index 38d75e2..183b169 100644 --- a/src/easy/account.cpp +++ b/src/easy/account.cpp @@ -191,7 +191,7 @@ const Account Account::moved() const { if (has_moved()) { - // TODO: Find an account with this node and test + // TODO: Find an account with moved-node and test return Account(_tree["moved"].toStyledString()); } diff --git a/src/easy/attachment.cpp b/src/easy/attachment.cpp index 296c9d2..4a6dce2 100644 --- a/src/easy/attachment.cpp +++ b/src/easy/attachment.cpp @@ -16,6 +16,7 @@ #include #include +#include #include #include "easy.hpp" #include "macros.hpp" @@ -25,6 +26,7 @@ using Attachment = Easy::Attachment; using std::string; Attachment::Attachment(const string &json) +: _valid(false) { std::stringstream ss(json); ss >> _tree; @@ -45,6 +47,28 @@ const bool Attachment::valid() const return _valid; } +const double Attachment::aspect() const +{ + if (_tree["meta"]["original"]["aspect"].isDouble()) + { + return _tree["meta"]["original"]["aspect"].asDouble(); + } + + ttdebug << "Could not get attachment data: aspect\n"; + return 0; +} + +const double Attachment::aspect_small() const +{ + if (_tree["meta"]["small"]["aspect"].isDouble()) + { + return _tree["meta"]["small"]["aspect"].asDouble(); + } + + ttdebug << "Could not get attachment data: aspect_small\n"; + return 0; +} + const string Attachment::description() const { if (_tree["description"].isString()) @@ -56,6 +80,43 @@ const string Attachment::description() const return ""; } +const std::array Attachment::focus() const +{ + if (_tree["meta"]["focus"]["x"].isUInt64()) + { + return + { + _tree["meta"]["focus"]["x"].asUInt64(), + _tree["meta"]["focus"]["y"].asUInt64() + }; + } + + ttdebug << "Could not get attachment data: focus\n"; + return {}; +} + +const uint64_t Attachment::height() const +{ + if (_tree["meta"]["original"]["height"].isDouble()) + { + return _tree["meta"]["original"]["height"].asDouble(); + } + + ttdebug << "Could not get attachment data: height\n"; + return 0; +} + +const uint64_t Attachment::height_small() const +{ + if (_tree["meta"]["small"]["height"].isDouble()) + { + return _tree["meta"]["small"]["height"].asDouble(); + } + + ttdebug << "Could not get attachment data: height_small\n"; + return 0; +} + const std::uint64_t Attachment::id() const { if (_tree["id"].isUInt64()) @@ -89,6 +150,28 @@ const string Attachment::remote_url() const return ""; } +const string Attachment::size() const +{ + if (_tree["meta"]["original"]["size"].isString()) + { + return _tree["meta"]["original"]["size"].asString(); + } + + ttdebug << "Could not get attachment data: size\n"; + return ""; +} + +const string Attachment::size_small() const +{ + if (_tree["meta"]["original"]["size"].isString()) + { + return _tree["meta"]["original"]["size"].asString(); + } + + ttdebug << "Could not get attachment data: size_small\n"; + return ""; +} + const string Attachment::text_url() const { if (_tree["text_url"].isString()) @@ -126,3 +209,25 @@ const string Attachment::url() const ttdebug << "Could not get attachment data: url\n"; return ""; } + +const uint64_t Attachment::width() const +{ + if (_tree["meta"]["original"]["width"].isDouble()) + { + return _tree["meta"]["original"]["width"].asDouble(); + } + + ttdebug << "Could not get attachment data: width\n"; + return 0; +} + +const uint64_t Attachment::width_small() const +{ + if (_tree["meta"]["small"]["width"].isDouble()) + { + return _tree["meta"]["small"]["width"].asDouble(); + } + + ttdebug << "Could not get attachment data: width_small\n"; + return 0; +} diff --git a/src/easy/easy.hpp b/src/easy/easy.hpp index e545b49..8b11874 100644 --- a/src/easy/easy.hpp +++ b/src/easy/easy.hpp @@ -20,6 +20,7 @@ #include #include #include +#include #include // If we are compiling mastodon-cpp, use another include path #ifdef MASTODON_CPP @@ -84,7 +85,7 @@ public: { public: /*! - * @brief Constructs an account object from a JSON string. + * @brief Constructs an Account object from a JSON string. * * @param json JSON string */ @@ -211,7 +212,7 @@ public: { public: /*! - * @brief Constructs an attachment object from a JSON string. + * @brief Constructs an Attachment object from a JSON string. * * @param json JSON string */ @@ -222,11 +223,37 @@ public: */ const bool valid() const; + /*! + * @brief Aspect of original image + */ + const double aspect() const; + + /*! + * @brief Aspect of preview image + */ + const double aspect_small() const; + /*! * @brief Returns the image description */ const string description() const; + /*! + * @brief Returns the focus point (x, y) + */ + // TODO: find attachment with focus + const std::array focus() const; + + /*! + * @brief Returns the height of the original image + */ + const uint64_t height() const; + + /*! + * @brief Returns the height of the preview image + */ + const uint64_t height_small() const; + /*! * @brief Returns the ID of the attachment */ @@ -242,6 +269,16 @@ public: */ const string remote_url() const; + /*! + * @brief Returns the size of the original image + */ + const string size() const; + + /*! + * @brief Returns the size of the preview image + */ + const string size_small() const; + /*! * @brief Returns shorter URL for the image */ @@ -257,6 +294,58 @@ public: */ const string url() const; + /*! + * @brief Returns the width of the original image + */ + const uint64_t width() const; + + /*! + * @brief Returns the width of the preview image + */ + const uint64_t width_small() const; + + + // TODO: find an attachment with framerate, duration or bitrate set + // const uint16_t framerate() const; + // const std::chrono::seconds duration() const; + // const uint64_t bitrate() const; + + private: + Json::Value _tree; + bool _valid; + }; + + /*! + * @brief Class to hold cards + */ + class Card + { + public: + /*! + * @brief Constructs a Card object from a JSON string. + * + * @param json JSON string + */ + explicit Card(const string &json); + + /*! + * @brief Returns true if the card holds valid data + */ + const bool valid() const; + + const string url() const; + const string title() const; + const string description() const; + const string image() const; + const string type() const; + const string author_name() const; + const string author_url() const; + const string provider_name() const; + const string provider_url() const; + const string html() const; + const string width() const; + const string height() const; + private: Json::Value _tree; bool _valid; From 15a8ee5b69c5397302b6b9d7c9f42bf090a305bc Mon Sep 17 00:00:00 2001 From: tastytea Date: Thu, 22 Mar 2018 00:33:36 +0100 Subject: [PATCH 08/12] Split easy.hpp into several files --- CMakeLists.txt | 6 +- src/easy/account.cpp | 22 +--- src/easy/account.hpp | 158 +++++++++++++++++++++++ src/easy/all.hpp | 32 +++++ src/easy/attachment.cpp | 26 +--- src/easy/attachment.hpp | 142 +++++++++++++++++++++ src/easy/easy.cpp | 24 ++++ src/easy/easy.hpp | 274 ++-------------------------------------- 8 files changed, 377 insertions(+), 307 deletions(-) create mode 100644 src/easy/account.hpp create mode 100644 src/easy/all.hpp create mode 100644 src/easy/attachment.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index f320c96..95425aa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project (mastodon-cpp - VERSION 0.7.2 + VERSION 0.7.3 LANGUAGES CXX ) @@ -53,8 +53,12 @@ install(TARGETS mastodon-cpp LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) install(FILES src/mastodon-cpp.hpp DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/mastodon-cpp) if(NOT WITHOUT_EASY) + file(GLOB easy_header src/easy/*.hpp) + list(FILTER easy_header EXCLUDE REGEX "easy\.hpp$") install(FILES src/easy/easy.hpp DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/mastodon-cpp) + install(FILES ${easy_header} + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/mastodon-cpp/easy) endif() # Documentation diff --git a/src/easy/account.cpp b/src/easy/account.cpp index 183b169..caf5981 100644 --- a/src/easy/account.cpp +++ b/src/easy/account.cpp @@ -22,7 +22,7 @@ #include // get_time #include #include -#include "easy.hpp" +#include "account.hpp" #include "macros.hpp" using namespace Mastodon; @@ -30,25 +30,9 @@ using Account = Easy::Account; using std::string; Account::Account(const string &json) -: _valid(false) +: Entity(json) { - std::stringstream ss(json); - ss >> _tree; - - if (_tree.isNull()) - { - std::cerr << "ERROR: Could not build Account from JSON string\n"; - ttdebug << "String was: " << json << '\n'; - } - else - { - _valid = true; - } -} - -const bool Account::valid() const -{ - return _valid; + // } const string Account::acct() const diff --git a/src/easy/account.hpp b/src/easy/account.hpp new file mode 100644 index 0000000..3883b67 --- /dev/null +++ b/src/easy/account.hpp @@ -0,0 +1,158 @@ +/* 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_ACCOUNT_HPP +#define MASTODON_CPP_EASY_ACCOUNT_HPP + +#include +#include +#include + +// If we are compiling mastodon-cpp, use another include path +#ifdef MASTODON_CPP + #include "mastodon-cpp.hpp" + #include "easy.hpp" +#else + #include + #include +#endif + +using std::string; +using std::uint16_t; +using std::uint64_t; + +namespace Mastodon +{ + /*! + * @brief Class to hold accounts. + */ + class Easy::Account : public Easy::Entity + { + public: + /*! + * @brief Constructs an Account object from a JSON string. + * + * @param json JSON string + */ + explicit Account(const string &json); + + /*! + * @brief Returns username + * + * `username` for users on the same instance, `user@hostname` + * for users on other instances. + */ + const string acct() const; + + /*! + * @brief Returns URL of avatar + */ + const string avatar() const; + + /*! + * @brief Returns URL of static avatar + */ + const string avatar_static() const; + + /*! + * @brief Returns time of creation + */ + const std::chrono::system_clock::time_point created_at() const; + + /*! + * @brief Returns display name + */ + const string display_name() const; + + /*! + * @brief Returns number of followers + */ + const uint64_t followers_count() const; + + /*! + * @brief Returns number of people this account follows + */ + const uint64_t following_count() const; + + /*! + * @brief Returns URL of header image + */ + const string header() const; + + /*! + * @brief Returns URL of static header image + */ + const string header_static() const; + + /*! + * @brief Returns account-ID + */ + const uint64_t id() const; + + /*! + * @brief Returns true if the account is locked + */ + const bool locked() const; + + /*! + * @brief Returns true if the account has been moved + */ + const bool has_moved() const; + + /*! + * @brief If the owner decided to switch accounts, new account is in + * this attribute + */ + const Account moved() const; + + /*! + * @brief Returns note + */ + const string note() const; + + /*! + * @brief Returns plaintext version of note + */ + const string note_plain() const; + + /*! + * @brief Returns default privacy of new toots + */ + const visibility privacy() const; + + /*! + * @brief Returns if media is marked as sensitive by default + */ + const bool sensitive() const; + + /*! + * @brief Returns number of statuses + */ + const uint64_t statuses_count() const; + + /*! + * @brief Returns URL of the profile + */ + const string url() const; + + /*! + * @brief Returns username (without @hostname) + */ + const string username() const; +}; +} + +#endif // MASTODON_CPP_EASY_ACCOUNT_HPP diff --git a/src/easy/all.hpp b/src/easy/all.hpp new file mode 100644 index 0000000..1540b34 --- /dev/null +++ b/src/easy/all.hpp @@ -0,0 +1,32 @@ +/* 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_ALL_HPP +#define MASTODON_CPP_EASY_ALL_HPP + +#ifdef MASTODON_CPP + #include "easy.hpp" + #include "easy/account.hpp" + #include "easy/attachment.hpp" + //#include "easy/card.hpp" +#else + #include + #include + #include + //#include +#endif + +#endif // MASTODON_CPP_EASY_ALL_HPP diff --git a/src/easy/attachment.cpp b/src/easy/attachment.cpp index 4a6dce2..8299071 100644 --- a/src/easy/attachment.cpp +++ b/src/easy/attachment.cpp @@ -18,7 +18,7 @@ #include #include #include -#include "easy.hpp" +#include "attachment.hpp" #include "macros.hpp" using namespace Mastodon; @@ -26,25 +26,9 @@ using Attachment = Easy::Attachment; using std::string; Attachment::Attachment(const string &json) -: _valid(false) +: Entity(json) { - std::stringstream ss(json); - ss >> _tree; - - if (_tree.isNull()) - { - std::cerr << "ERROR: Could not build Attachment from JSON string\n"; - ttdebug << "String was: " << json << '\n'; - } - else - { - _valid = true; - } -} - -const bool Attachment::valid() const -{ - return _valid; + // } const double Attachment::aspect() const @@ -85,10 +69,10 @@ const std::array Attachment::focus() const if (_tree["meta"]["focus"]["x"].isUInt64()) { return - { + {{ _tree["meta"]["focus"]["x"].asUInt64(), _tree["meta"]["focus"]["y"].asUInt64() - }; + }}; } ttdebug << "Could not get attachment data: focus\n"; diff --git a/src/easy/attachment.hpp b/src/easy/attachment.hpp new file mode 100644 index 0000000..cf200e5 --- /dev/null +++ b/src/easy/attachment.hpp @@ -0,0 +1,142 @@ +/* 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_ATTACHMENT_HPP +#define MASTODON_CPP_EASY_ATTACHMENT_HPP + +#include +#include +#include +#include + +// If we are compiling mastodon-cpp, use another include path +#ifdef MASTODON_CPP + #include "mastodon-cpp.hpp" + #include "easy.hpp" +#else + #include + #include +#endif + +using std::string; +using std::uint16_t; +using std::uint64_t; + +namespace Mastodon +{ + /*! + * @brief Class to hold attachments + */ + class Easy::Attachment : public Easy::Entity + { + public: + /*! + * @brief Constructs an Attachment object from a JSON string. + * + * @param json JSON string + */ + explicit Attachment(const string &json); + + /*! + * @brief Aspect of original image + */ + const double aspect() const; + + /*! + * @brief Aspect of preview image + */ + const double aspect_small() const; + + /*! + * @brief Returns the image description + */ + const string description() const; + + /*! + * @brief Returns the focus point (x, y) + */ + // TODO: find attachment with focus + const std::array focus() const; + + /*! + * @brief Returns the height of the original image + */ + const uint64_t height() const; + + /*! + * @brief Returns the height of the preview image + */ + const uint64_t height_small() const; + + /*! + * @brief Returns the ID of the attachment + */ + const uint64_t id() const; + + /*! + * @brief Returns the URL of the preview image + */ + const string preview_url() const; + + /*! + * @brief Returns the remote URL of the original image + */ + const string remote_url() const; + + /*! + * @brief Returns the size of the original image + */ + const string size() const; + + /*! + * @brief Returns the size of the preview image + */ + const string size_small() const; + + /*! + * @brief Returns shorter URL for the image + */ + const string text_url() const; + + /*! + * @brief Returns attachment type + */ + const attachment_type type() const; + + /*! + * @brief Returns URL of the locally hosted version of the image + */ + const string url() const; + + /*! + * @brief Returns the width of the original image + */ + const uint64_t width() const; + + /*! + * @brief Returns the width of the preview image + */ + const uint64_t width_small() const; + + + // TODO: find an attachment with framerate, duration or bitrate set + // const uint16_t framerate() const; + // const std::chrono::seconds duration() const; + // const uint64_t bitrate() const; + }; +} + +#endif // MASTODON_CPP_EASY_ATTACHMENT_HPP diff --git a/src/easy/easy.cpp b/src/easy/easy.cpp index 91672cb..aa114cf 100644 --- a/src/easy/easy.cpp +++ b/src/easy/easy.cpp @@ -15,7 +15,9 @@ */ #include +#include #include "easy.hpp" +#include "macros.hpp" using namespace Mastodon; using std::string; @@ -25,3 +27,25 @@ Easy::Easy(const string &instance, const string &access_token) { // } + +Easy::Entity::Entity(const string &json) +: _valid(false) +{ + std::stringstream ss(json); + ss >> _tree; + + if (_tree.isNull()) + { + std::cerr << "ERROR: Could not build Entity from JSON string\n"; + ttdebug << "String was: " << json << '\n'; + } + else + { + _valid = true; + } +} + +const bool Easy::Entity::valid() const +{ + return _valid; +} diff --git a/src/easy/easy.hpp b/src/easy/easy.hpp index 8b11874..a0b290c 100644 --- a/src/easy/easy.hpp +++ b/src/easy/easy.hpp @@ -18,10 +18,8 @@ #define MASTODON_EASY_CPP_HPP #include -#include -#include -#include #include + // If we are compiling mastodon-cpp, use another include path #ifdef MASTODON_CPP #include "mastodon-cpp.hpp" @@ -30,8 +28,6 @@ #endif using std::string; -using std::uint16_t; -using std::uint64_t; namespace Mastodon { @@ -78,278 +74,24 @@ public: */ explicit Easy(const string &instance, const string &access_token); - /*! - * @brief Class to hold accounts. - */ - class Account + class Entity { public: - /*! - * @brief Constructs an Account object from a JSON string. - * - * @param json JSON string - */ - explicit Account(const string &json); + explicit Entity(const string &json); /*! - * @brief Returns true if the account holds valid data + * @brief Returns true if the Entity holds valid data */ const bool valid() const; - /*! - * @brief Returns username - * - * `username` for users on the same instance, `user@hostname` - * for users on other instances. - */ - const string acct() const; - - /*! - * @brief Returns URL of avatar - */ - const string avatar() const; - - /*! - * @brief Returns URL of static avatar - */ - const string avatar_static() const; - - /*! - * @brief Returns time of creation - */ - const std::chrono::system_clock::time_point created_at() const; - - /*! - * @brief Returns display name - */ - const string display_name() const; - - /*! - * @brief Returns number of followers - */ - const uint64_t followers_count() const; - - /*! - * @brief Returns number of people this account follows - */ - const uint64_t following_count() const; - - /*! - * @brief Returns URL of header image - */ - const string header() const; - - /*! - * @brief Returns URL of static header image - */ - const string header_static() const; - - /*! - * @brief Returns account-ID - */ - const uint64_t id() const; - - /*! - * @brief Returns true if the account is locked - */ - const bool locked() const; - - /*! - * @brief Returns true if the account has been moved - */ - const bool has_moved() const; - - /*! - * @brief If the owner decided to switch accounts, new account is in - * this attribute - */ - const Account moved() const; - - /*! - * @brief Returns note - */ - const string note() const; - - /*! - * @brief Returns plaintext version of note - */ - const string note_plain() const; - - /*! - * @brief Returns default privacy of new toots - */ - const visibility privacy() const; - - /*! - * @brief Returns if media is marked as sensitive by default - */ - const bool sensitive() const; - - /*! - * @brief Returns number of statuses - */ - const uint64_t statuses_count() const; - - /*! - * @brief Returns URL of the profile - */ - const string url() const; - - /*! - * @brief Returns username (without @hostname) - */ - const string username() const; - - private: + protected: Json::Value _tree; bool _valid; }; - /*! - * @brief Class to hold attachments - */ - class Attachment - { - public: - /*! - * @brief Constructs an Attachment object from a JSON string. - * - * @param json JSON string - */ - explicit Attachment(const string &json); - - /*! - * @brief Returns true if the attachment holds valid data - */ - const bool valid() const; - - /*! - * @brief Aspect of original image - */ - const double aspect() const; - - /*! - * @brief Aspect of preview image - */ - const double aspect_small() const; - - /*! - * @brief Returns the image description - */ - const string description() const; - - /*! - * @brief Returns the focus point (x, y) - */ - // TODO: find attachment with focus - const std::array focus() const; - - /*! - * @brief Returns the height of the original image - */ - const uint64_t height() const; - - /*! - * @brief Returns the height of the preview image - */ - const uint64_t height_small() const; - - /*! - * @brief Returns the ID of the attachment - */ - const uint64_t id() const; - - /*! - * @brief Returns the URL of the preview image - */ - const string preview_url() const; - - /*! - * @brief Returns the remote URL of the original image - */ - const string remote_url() const; - - /*! - * @brief Returns the size of the original image - */ - const string size() const; - - /*! - * @brief Returns the size of the preview image - */ - const string size_small() const; - - /*! - * @brief Returns shorter URL for the image - */ - const string text_url() const; - - /*! - * @brief Returns attachment type - */ - const attachment_type type() const; - - /*! - * @brief Returns URL of the locally hosted version of the image - */ - const string url() const; - - /*! - * @brief Returns the width of the original image - */ - const uint64_t width() const; - - /*! - * @brief Returns the width of the preview image - */ - const uint64_t width_small() const; - - - // TODO: find an attachment with framerate, duration or bitrate set - // const uint16_t framerate() const; - // const std::chrono::seconds duration() const; - // const uint64_t bitrate() const; - - private: - Json::Value _tree; - bool _valid; - }; - - /*! - * @brief Class to hold cards - */ - class Card - { - public: - /*! - * @brief Constructs a Card object from a JSON string. - * - * @param json JSON string - */ - explicit Card(const string &json); - - /*! - * @brief Returns true if the card holds valid data - */ - const bool valid() const; - - const string url() const; - const string title() const; - const string description() const; - const string image() const; - const string type() const; - const string author_name() const; - const string author_url() const; - const string provider_name() const; - const string provider_url() const; - const string html() const; - const string width() const; - const string height() const; - - private: - Json::Value _tree; - bool _valid; - }; + class Account; + class Attachment; + class Card; }; } From d9416448a49dc0057a478b640e41b5e6311370ed Mon Sep 17 00:00:00 2001 From: tastytea Date: Fri, 23 Mar 2018 00:53:41 +0100 Subject: [PATCH 09/12] fix documentation generation --- Doxyfile | 4 ++-- src/easy/easy.hpp | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/Doxyfile b/Doxyfile index 3c18141..2f36d41 100644 --- a/Doxyfile +++ b/Doxyfile @@ -1,7 +1,7 @@ DOXYFILE_ENCODING = UTF-8 PROJECT_NAME = "mastodon-cpp" PROJECT_NUMBER = 0.0.0 -INPUT = README.md src/mastodon-cpp.hpp +INPUT = README.md src/ src/easy/ USE_MDFILE_AS_MAINPAGE = README.md CREATE_SUBDIRS = NO ALLOW_UNICODE_NAMES = YES @@ -77,7 +77,7 @@ WARN_FORMAT = "$file:$line: $text" INPUT_ENCODING = UTF-8 RECURSIVE = NO EXCLUDE_SYMLINKS = NO -EXAMPLE_PATH = src/examples +EXAMPLE_PATH = examples EXAMPLE_RECURSIVE = YES FILTER_SOURCE_FILES = NO SOURCE_BROWSER = NO diff --git a/src/easy/easy.hpp b/src/easy/easy.hpp index a0b290c..d964919 100644 --- a/src/easy/easy.hpp +++ b/src/easy/easy.hpp @@ -74,6 +74,9 @@ public: */ explicit Easy(const string &instance, const string &access_token); + /*! + * @brief Base class for entities. + */ class Entity { public: From 312ae6b5dea24c674bed29a91fdf4552365e1169 Mon Sep 17 00:00:00 2001 From: tastytea Date: Sun, 25 Mar 2018 19:43:48 +0200 Subject: [PATCH 10/12] added Easy::Card --- CMakeLists.txt | 2 +- src/easy/account.cpp | 1 + src/easy/all.hpp | 4 +- src/easy/attachment.cpp | 1 + src/easy/card.cpp | 169 ++++++++++++++++++++++++++++++++++++++++ src/easy/card.hpp | 113 +++++++++++++++++++++++++++ src/easy/easy.hpp | 12 +++ 7 files changed, 299 insertions(+), 3 deletions(-) create mode 100644 src/easy/card.cpp create mode 100644 src/easy/card.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 95425aa..cfc9d80 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project (mastodon-cpp - VERSION 0.7.3 + VERSION 0.7.4 LANGUAGES CXX ) diff --git a/src/easy/account.cpp b/src/easy/account.cpp index caf5981..f8ae569 100644 --- a/src/easy/account.cpp +++ b/src/easy/account.cpp @@ -28,6 +28,7 @@ using namespace Mastodon; using Account = Easy::Account; using std::string; +using std::uint64_t; Account::Account(const string &json) : Entity(json) diff --git a/src/easy/all.hpp b/src/easy/all.hpp index 1540b34..9e77182 100644 --- a/src/easy/all.hpp +++ b/src/easy/all.hpp @@ -21,12 +21,12 @@ #include "easy.hpp" #include "easy/account.hpp" #include "easy/attachment.hpp" - //#include "easy/card.hpp" + #include "easy/card.hpp" #else #include #include #include - //#include + #include #endif #endif // MASTODON_CPP_EASY_ALL_HPP diff --git a/src/easy/attachment.cpp b/src/easy/attachment.cpp index 8299071..704fe0d 100644 --- a/src/easy/attachment.cpp +++ b/src/easy/attachment.cpp @@ -24,6 +24,7 @@ using namespace Mastodon; using Attachment = Easy::Attachment; using std::string; +using std::uint64_t; Attachment::Attachment(const string &json) : Entity(json) diff --git a/src/easy/card.cpp b/src/easy/card.cpp new file mode 100644 index 0000000..5dc0421 --- /dev/null +++ b/src/easy/card.cpp @@ -0,0 +1,169 @@ +/* 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 +#include +#include +#include "card.hpp" +#include "macros.hpp" + +using namespace Mastodon; +using Card = Easy::Card; +using std::string; +using std::uint64_t; + +Card::Card(const string &json) +: Entity(json) +{ + // +} + +const string Card::author_name() const +{ + if (_tree["author_name"].isString()) + { + return _tree["author_name"].asString(); + } + + ttdebug << "Could not get attachment data: author_name\n"; + return ""; +} + +const string Card::author_url() const +{ + if (_tree["author_url"].isString()) + { + return _tree["author_url"].asString(); + } + + ttdebug << "Could not get attachment data: author_url\n"; + return ""; +} + +const string Card::description() const +{ + if (_tree["description"].isString()) + { + return _tree["description"].asString(); + } + + ttdebug << "Could not get attachment data: description\n"; + return ""; +} + +const uint64_t Card::height() const +{ + if (_tree["height"].isUInt64()) + { + return _tree["height"].asUInt64(); + } + + ttdebug << "Could not get attachment data: height\n"; + return 0; +} + +const string Card::html() const +{ + if (_tree["html"].isString()) + { + return _tree["html"].asString(); + } + + ttdebug << "Could not get attachment data: html\n"; + return ""; +} + +const string Card::image() const +{ + if (_tree["image"].isString()) + { + return _tree["image"].asString(); + } + + ttdebug << "Could not get attachment data: image\n"; + return ""; +} + +const string Card::provider_name() const +{ + if (_tree["provider_name"].isString()) + { + return _tree["provider_name"].asString(); + } + + ttdebug << "Could not get attachment data: provider_name\n"; + return ""; +} + +const string Card::provider_url() const +{ + if (_tree["provider_url"].isString()) + { + return _tree["provider_url"].asString(); + } + + ttdebug << "Could not get attachment data: provider_url\n"; + return ""; +} + +const string Card::title() const +{ + if (_tree["title"].isString()) + { + return _tree["title"].asString(); + } + + ttdebug << "Could not get attachment data: title\n"; + return ""; +} + +const Easy::card_type Card::type() const +{ + const string strtype = _tree["type"].asString(); + if (strtype.compare("link")) + return card_type::link; + else if (strtype.compare("photo")) + return card_type::photo; + else if (strtype.compare("video")) + return card_type::video; + else if (strtype.compare("rich")) + return card_type::rich; + + ttdebug << "Could not get account data: type\n"; + return card_type::unknown; +} + +const string Card::url() const +{ + if (_tree["url"].isString()) + { + return _tree["url"].asString(); + } + + ttdebug << "Could not get attachment data: url\n"; + return ""; +} + +const uint64_t Card::width() const +{ + if (_tree["width"].isUInt64()) + { + return _tree["width"].asUInt64(); + } + + ttdebug << "Could not get attachment data: width\n"; + return 0; +} diff --git a/src/easy/card.hpp b/src/easy/card.hpp new file mode 100644 index 0000000..88f5d8f --- /dev/null +++ b/src/easy/card.hpp @@ -0,0 +1,113 @@ +/* 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_CARD_HPP +#define MASTODON_CPP_EASY_CARD_HPP + +#include +#include + +// If we are compiling mastodon-cpp, use another include path +#ifdef MASTODON_CPP + #include "mastodon-cpp.hpp" + #include "easy.hpp" +#else + #include + #include +#endif + +using std::string; +using std::uint16_t; +using std::uint64_t; + +namespace Mastodon +{ + /*! + * @brief Class to hold cards + */ + class Easy::Card : public Easy::Entity + { + public: + /*! + * @brief Constructs a Card object from a JSON string. + * + * @param json JSON string + */ + explicit Card(const string &json); + + /*! + * @brief Returns the name of the author + */ + const string author_name() const; + + /*! + * @brief Returns the URL of the author + */ + const string author_url() const; + + /*! + * @brief Returns the description + */ + const string description() const; + + /*! + * @brief Returns the height of the card + */ + const uint64_t height() const; + + /*! + * @brief Returns the HTML + */ + const string html() const; + + /*! + * @brief Returns the URL of the image associated with the card + */ + const string image() const; + + /*! + * @brief Returns the name of the provider + */ + const string provider_name() const; + + /*! + * @brief Returns the URL of the provider + */ + const string provider_url() const; + + /*! + * @brief Returns the title + */ + const string title() const; + + /*! + * @brief Returns the type of the card + */ + const Easy::card_type type() const; + + /*! + * @brief Returns the URL associated with the card + */ + const string url() const; + + /*! + * @brief Returns the width of the card + */ + const uint64_t width() const; + }; +} + +#endif // MASTODON_CPP_EASY_CARD_HPP diff --git a/src/easy/easy.hpp b/src/easy/easy.hpp index d964919..96ae5fe 100644 --- a/src/easy/easy.hpp +++ b/src/easy/easy.hpp @@ -63,6 +63,18 @@ public: unknown }; + /*! + * @brief Describes the card type + */ + enum class card_type + { + link, + photo, + video, + rich, + unknown + }; + /*! * @brief Constructs a new Easy object. * From 5598a7fb3d9d11a145d2a814d86ea23afdb64b37 Mon Sep 17 00:00:00 2001 From: tastytea Date: Sun, 25 Mar 2018 22:46:38 +0200 Subject: [PATCH 11/12] refactored Easy::Entity and children --- CMakeLists.txt | 2 +- src/easy/account.cpp | 170 +++++++--------------------------------- src/easy/account.hpp | 1 + src/easy/attachment.cpp | 144 +++++++--------------------------- src/easy/card.cpp | 107 +++++-------------------- src/easy/easy.cpp | 113 ++++++++++++++++++++++++++ src/easy/easy.hpp | 54 ++++++++++++- 7 files changed, 247 insertions(+), 344 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cfc9d80..d307e26 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project (mastodon-cpp - VERSION 0.7.4 + VERSION 0.7.5 LANGUAGES CXX ) diff --git a/src/easy/account.cpp b/src/easy/account.cpp index f8ae569..1b8d675 100644 --- a/src/easy/account.cpp +++ b/src/easy/account.cpp @@ -18,9 +18,6 @@ #include #include #include -#include -#include // get_time -#include #include #include "account.hpp" #include "macros.hpp" @@ -29,142 +26,70 @@ using namespace Mastodon; using Account = Easy::Account; using std::string; using std::uint64_t; +using std::chrono::system_clock; Account::Account(const string &json) : Entity(json) -{ - // -} +{} const string Account::acct() const { - if (_tree["acct"].isString()) - { - return _tree["acct"].asString(); - } - - ttdebug << "Could not get account data: acct\n"; - return ""; + return get_string("acct"); } const string Account::avatar() const { - if (_tree["avatar"].isString()) - { - return _tree["avatar"].asString(); - } - - ttdebug << "Could not get account data: avatar\n"; - return ""; + return get_string("avatar"); } const string Account::avatar_static() const { - if (_tree["avatar_static"].isString()) - { - return _tree["avatar_static"].asString(); - } - - ttdebug << "Could not get account data: avatar_static\n"; - return ""; + return get_string("avatar_static"); } -const std::chrono::system_clock::time_point Account::created_at() const +const system_clock::time_point Account::created_at() const { - if (_tree["created_at"].isString()) - { - std::stringstream sstime(_tree["created_at"].asString()); - struct std::tm tm = {0}; - sstime >> std::get_time(&tm, "%Y-%m-%dT%T"); - std::time_t time = timegm(&tm); - return std::chrono::system_clock::from_time_t(time); - } - - ttdebug << "Could not get account data: created_at\n"; - // Return clocks epoch - return std::chrono::system_clock::time_point(); + return get_time_point("created_at"); } const string Account::display_name() const { - if (_tree["display_name"].isString()) - { - return _tree["display_name"].asString(); - } - - ttdebug << "Could not get account data: display_name\n"; - return ""; + return get_string("display_name"); } const std::uint64_t Account::followers_count() const { - if (_tree["followers_count"].isUInt64()) - { - return _tree["followers_count"].asUInt64(); - } - - ttdebug << "Could not get account data: followers_count\n"; - return 0; + return get_uint64("followers_count"); } const std::uint64_t Account::following_count() const { - if (_tree["following_count"].isUInt64()) - { - return _tree["following_count"].asUInt64(); - } - - ttdebug << "Could not get account data: following_count\n"; - return 0; + return get_uint64("following_count"); } const string Account::header() const { - if (_tree["header"].isString()) - { - return _tree["header"].asString(); - } - - ttdebug << "Could not get account data: header\n"; - return ""; + return get_string("header"); } const string Account::header_static() const { - if (_tree["header_static"].isString()) - { - return _tree["header_static"].asString(); - } - - ttdebug << "Could not get account data: header_static\n"; - return ""; + return get_string("header_static"); } const std::uint64_t Account::id() const { - if (_tree["id"].isUInt64()) - { - return _tree["id"].asUInt64(); - } - - ttdebug << "Could not get account data: id\n"; - return 0; + return get_uint64("id"); } const bool Account::locked() const { - if (_tree["locked"].isBool()) - { - return _tree["locked"].asBool(); - } - - ttdebug << "Could not get account data: locked\n"; - return false; + return get_bool("locked"); } const bool Account::has_moved() const { - if (_tree["moved"].isObject()) + if (get("moved").isObject()) { return true; } @@ -177,83 +102,48 @@ const Account Account::moved() const if (has_moved()) { // TODO: Find an account with moved-node and test - return Account(_tree["moved"].toStyledString()); + return Account(get("moved").toStyledString()); } - ttdebug << "Could not get account data: moved\n"; return Account(""); } const string Account::note() const { - if (_tree["note"].isString()) - { - return _tree["note"].asString(); - } - - ttdebug << "Could not get account data: note\n"; - return ""; + return get_string("source.note"); } const string Account::note_plain() const { - if (_tree["source"]["note"].isString()) - { - return _tree["source"]["note"].asString(); - } - - ttdebug << "Could not get account data: note_plain\n"; - return ""; + return get_string("source.note"); } const Easy::visibility Account::privacy() const { - if (_tree["source"]["privacy"].isString()) - { - const string strprivacy = _tree["source"]["privacy"].asString(); - if (strprivacy.compare("public")) - return visibility::Public; - else if (strprivacy.compare("unlisted")) - return visibility::Unlisted; - else if (strprivacy.compare("private")) - return visibility::Private; - else if (strprivacy.compare("direct")) - return visibility::Direct; - } + const string strprivacy = get_string("source.privacy"); + if (strprivacy.compare("public") == 0) + return visibility::Public; + else if (strprivacy.compare("unlisted") == 0) + return visibility::Unlisted; + else if (strprivacy.compare("private") == 0) + return visibility::Private; + else if (strprivacy.compare("direct") == 0) + return visibility::Direct; - ttdebug << "Could not get account data: privacy\n"; return visibility::Undefined; } const bool Account::sensitive() const { - if (_tree["source"]["sensitive"].isBool()) - { - return _tree["source"]["sensitive"].asBool(); - } - - ttdebug << "Could not get account data: sensitive\n"; - return false; + return get_bool("source.sensitive"); } const std::uint64_t Account::statuses_count() const { - if (_tree["statuses_count"].isUInt64()) - { - return _tree["statuses_count"].asUInt64(); - } - - ttdebug << "Could not get account data: statuses_count\n"; - return 0; + return get_uint64("statuses_count"); } const string Account::username() const { - if (_tree["username"].isString()) - { - return _tree["username"].asString(); - } - - ttdebug << "Could not get account data: username\n"; - return ""; + return get_string("username"); } diff --git a/src/easy/account.hpp b/src/easy/account.hpp index 3883b67..f2b9992 100644 --- a/src/easy/account.hpp +++ b/src/easy/account.hpp @@ -20,6 +20,7 @@ #include #include #include +#include // If we are compiling mastodon-cpp, use another include path #ifdef MASTODON_CPP diff --git a/src/easy/attachment.cpp b/src/easy/attachment.cpp index 704fe0d..36fbb76 100644 --- a/src/easy/attachment.cpp +++ b/src/easy/attachment.cpp @@ -28,191 +28,105 @@ using std::uint64_t; Attachment::Attachment(const string &json) : Entity(json) -{ - // -} +{} const double Attachment::aspect() const { - if (_tree["meta"]["original"]["aspect"].isDouble()) - { - return _tree["meta"]["original"]["aspect"].asDouble(); - } - - ttdebug << "Could not get attachment data: aspect\n"; - return 0; + return get_double("meta.original.aspect"); } const double Attachment::aspect_small() const { - if (_tree["meta"]["small"]["aspect"].isDouble()) - { - return _tree["meta"]["small"]["aspect"].asDouble(); - } - - ttdebug << "Could not get attachment data: aspect_small\n"; - return 0; + return get_double("meta.small.aspect"); } const string Attachment::description() const { - if (_tree["description"].isString()) - { - return _tree["description"].asString(); - } - - ttdebug << "Could not get attachment data: description\n"; - return ""; + return get_string("description"); } const std::array Attachment::focus() const { - if (_tree["meta"]["focus"]["x"].isUInt64()) + const Json::Value x = get("meta.focus.x"); + const Json::Value y = get("meta.focus.y"); + if (x.isUInt64() && y.isUInt64()) { return {{ - _tree["meta"]["focus"]["x"].asUInt64(), - _tree["meta"]["focus"]["y"].asUInt64() + x.asUInt64(), + y.asUInt64() }}; } - ttdebug << "Could not get attachment data: focus\n"; return {}; } const uint64_t Attachment::height() const { - if (_tree["meta"]["original"]["height"].isDouble()) - { - return _tree["meta"]["original"]["height"].asDouble(); - } - - ttdebug << "Could not get attachment data: height\n"; - return 0; + return get_uint64("meta.original.height"); } const uint64_t Attachment::height_small() const { - if (_tree["meta"]["small"]["height"].isDouble()) - { - return _tree["meta"]["small"]["height"].asDouble(); - } - - ttdebug << "Could not get attachment data: height_small\n"; - return 0; + return get_uint64("meta.small.height"); } const std::uint64_t Attachment::id() const { - if (_tree["id"].isUInt64()) - { - return _tree["id"].asUInt64(); - } - - ttdebug << "Could not get attachment data: id\n"; - return 0; + return get_uint64("id"); } const string Attachment::preview_url() const { - if (_tree["preview_url"].isString()) - { - return _tree["preview_url"].asString(); - } - - ttdebug << "Could not get attachment data: preview_url\n"; - return ""; + return get_string("preview_url"); } const string Attachment::remote_url() const { - if (_tree["remote_url"].isString()) - { - return _tree["remote_url"].asString(); - } - - ttdebug << "Could not get attachment data: remote_url\n"; - return ""; + return get_string("remote_url"); } const string Attachment::size() const { - if (_tree["meta"]["original"]["size"].isString()) - { - return _tree["meta"]["original"]["size"].asString(); - } - - ttdebug << "Could not get attachment data: size\n"; - return ""; + return get_string("meta.original.size"); } const string Attachment::size_small() const { - if (_tree["meta"]["original"]["size"].isString()) - { - return _tree["meta"]["original"]["size"].asString(); - } - - ttdebug << "Could not get attachment data: size_small\n"; - return ""; + return get_string("meta.small.size"); } const string Attachment::text_url() const { - if (_tree["text_url"].isString()) - { - return _tree["text_url"].asString(); - } - - ttdebug << "Could not get attachment data: text_url\n"; - return ""; + return get_string("text_url"); } const Easy::attachment_type Attachment::type() const { - const string strtype = _tree["type"].asString(); - if (strtype.compare("image")) - return attachment_type::image; - else if (strtype.compare("video")) - return attachment_type::video; - else if (strtype.compare("gifv")) - return attachment_type::gifv; - else if (strtype.compare("unknown")) - return attachment_type::unknown; + const string strtype = get_string("type"); + if (strtype.compare("image") == 0) + return attachment_type::image; + else if (strtype.compare("video") == 0) + return attachment_type::video; + else if (strtype.compare("gifv") == 0) + return attachment_type::gifv; + else if (strtype.compare("unknown") == 0) + return attachment_type::unknown; - ttdebug << "Could not get account data: type\n"; return attachment_type::unknown; } const string Attachment::url() const { - if (_tree["url"].isString()) - { - return _tree["url"].asString(); - } - - ttdebug << "Could not get attachment data: url\n"; - return ""; + return get_string("url"); } const uint64_t Attachment::width() const { - if (_tree["meta"]["original"]["width"].isDouble()) - { - return _tree["meta"]["original"]["width"].asDouble(); - } - - ttdebug << "Could not get attachment data: width\n"; - return 0; + return get_uint64("meta.original.width"); } const uint64_t Attachment::width_small() const { - if (_tree["meta"]["small"]["width"].isDouble()) - { - return _tree["meta"]["small"]["width"].asDouble(); - } - - ttdebug << "Could not get attachment data: width_small\n"; - return 0; + return get_uint64("meta.small.width"); } diff --git a/src/easy/card.cpp b/src/easy/card.cpp index 5dc0421..23bdcda 100644 --- a/src/easy/card.cpp +++ b/src/easy/card.cpp @@ -33,137 +33,70 @@ Card::Card(const string &json) const string Card::author_name() const { - if (_tree["author_name"].isString()) - { - return _tree["author_name"].asString(); - } - - ttdebug << "Could not get attachment data: author_name\n"; - return ""; + return get_string("author_name"); } const string Card::author_url() const { - if (_tree["author_url"].isString()) - { - return _tree["author_url"].asString(); - } - - ttdebug << "Could not get attachment data: author_url\n"; - return ""; + return get_string("author_url"); } const string Card::description() const { - if (_tree["description"].isString()) - { - return _tree["description"].asString(); - } - - ttdebug << "Could not get attachment data: description\n"; - return ""; + return get_string("description"); } const uint64_t Card::height() const { - if (_tree["height"].isUInt64()) - { - return _tree["height"].asUInt64(); - } - - ttdebug << "Could not get attachment data: height\n"; - return 0; + return get_uint64("height"); } const string Card::html() const { - if (_tree["html"].isString()) - { - return _tree["html"].asString(); - } - - ttdebug << "Could not get attachment data: html\n"; - return ""; + return get_string("html"); } const string Card::image() const { - if (_tree["image"].isString()) - { - return _tree["image"].asString(); - } - - ttdebug << "Could not get attachment data: image\n"; - return ""; + return get_string("image"); } const string Card::provider_name() const { - if (_tree["provider_name"].isString()) - { - return _tree["provider_name"].asString(); - } - - ttdebug << "Could not get attachment data: provider_name\n"; - return ""; + return get_string("provider_name"); } const string Card::provider_url() const { - if (_tree["provider_url"].isString()) - { - return _tree["provider_url"].asString(); - } - - ttdebug << "Could not get attachment data: provider_url\n"; - return ""; + return get_string("provider_url"); } const string Card::title() const { - if (_tree["title"].isString()) - { - return _tree["title"].asString(); - } - - ttdebug << "Could not get attachment data: title\n"; - return ""; + return get_string("title"); } const Easy::card_type Card::type() const { - const string strtype = _tree["type"].asString(); - if (strtype.compare("link")) - return card_type::link; - else if (strtype.compare("photo")) - return card_type::photo; - else if (strtype.compare("video")) - return card_type::video; - else if (strtype.compare("rich")) - return card_type::rich; + const string strtype = get_string("type"); + if (strtype.compare("link") == 0) + return card_type::link; + else if (strtype.compare("photo") == 0) + return card_type::photo; + else if (strtype.compare("video") == 0) + return card_type::video; + else if (strtype.compare("rich") == 0) + return card_type::rich; - ttdebug << "Could not get account data: type\n"; return card_type::unknown; } const string Card::url() const { - if (_tree["url"].isString()) - { - return _tree["url"].asString(); - } - - ttdebug << "Could not get attachment data: url\n"; - return ""; + return get_string("url"); } const uint64_t Card::width() const { - if (_tree["width"].isUInt64()) - { - return _tree["width"].asUInt64(); - } - - ttdebug << "Could not get attachment data: width\n"; - return 0; + return get_uint64("width"); } diff --git a/src/easy/easy.cpp b/src/easy/easy.cpp index aa114cf..7b675ab 100644 --- a/src/easy/easy.cpp +++ b/src/easy/easy.cpp @@ -16,11 +16,19 @@ #include #include +#include +#include +#include +#include // get_time +#include +#include #include "easy.hpp" #include "macros.hpp" using namespace Mastodon; using std::string; +using std::uint64_t; +using std::chrono::system_clock; Easy::Easy(const string &instance, const string &access_token) : API(instance, access_token) @@ -49,3 +57,108 @@ const bool Easy::Entity::valid() const { return _valid; } + +const Json::Value Easy::Entity::get(const string &key) const +{ + const Json::Value *node; + if (key.find('.') == std::string::npos) + { + node = &_tree[key]; + } + else + { + // If dots in key, we have to walk through the tree + std::size_t pos = 0; + string current_key = key; + node = &_tree; + while ((pos = current_key.find('.')) != std::string::npos) + { + try + { + node = &(*node)[current_key.substr(0, pos)]; + current_key = current_key.substr(pos + 1); + } + catch (const Json::LogicError &e) + { + ttdebug << e.what() << '\n'; + goto error; + } + } + node = &(*node)[current_key]; + } + + if (!node->isNull()) + { + return *node; + } + + error: + ttdebug << "Could not get data: " << key << '\n'; + return Json::Value(); +} + +const string Easy::Entity::get_string(const string &key) const +{ + const Json::Value node = get(key); + + if (node.isString()) + { + return node.asString(); + } + + return ""; +} + +const uint64_t Easy::Entity::get_uint64(const string &key) const +{ + const Json::Value node = get(key); + + if (node.isUInt64()) + { + return node.asUInt64(); + } + + return 0; +} + +const double Easy::Entity::get_double(const string &key) const +{ + const Json::Value node = get(key); + + if (node.isDouble()) + { + return node.asDouble(); + } + + return 0.0; +} + +const bool Easy::Entity::get_bool(const string &key) const +{ + const Json::Value node = get(key); + + if (node.isBool()) + { + return node.asBool(); + } + + return false; +} + +const system_clock::time_point + Easy::Entity::get_time_point(const string &key) const +{ + const Json::Value node = get(key); + + if (node.isString()) + { + std::stringstream sstime(node.asString()); + struct std::tm tm = {0}; + sstime >> std::get_time(&tm, "%Y-%m-%dT%T"); + std::time_t time = timegm(&tm); + return system_clock::from_time_t(time); + } + + // Return clocks epoch + return system_clock::time_point(); +} diff --git a/src/easy/easy.hpp b/src/easy/easy.hpp index 96ae5fe..419b235 100644 --- a/src/easy/easy.hpp +++ b/src/easy/easy.hpp @@ -18,6 +18,8 @@ #define MASTODON_EASY_CPP_HPP #include +#include +#include #include // If we are compiling mastodon-cpp, use another include path @@ -28,6 +30,8 @@ #endif using std::string; +using std::uint64_t; +using std::chrono::system_clock; namespace Mastodon { @@ -92,13 +96,61 @@ public: class Entity { public: - explicit Entity(const string &json); + /*! + * @brief Constructs an Entity object from a JSON string. + * + * @param json JSON string + */ + Entity(const string &json); /*! * @brief Returns true if the Entity holds valid data */ const bool valid() const; + /*! + * @brief Returns the value of node as Json::Value + * + * Returns an empty object on error. + */ + const Json::Value get(const string &key) const; + + /*! + * @brief Returns the value of node as std::string + * + * returns "" on error. + */ + const string get_string(const string &key) const; + + /*! + * @brief Returns the value of node as std::uint64_t + * + * Returns 0 on error. + */ + const uint64_t get_uint64(const string &key) const; + + /*! + * @brief Returns the value of node as double + * + * Returns 0.0 on error. + */ + const double get_double(const string &key) const; + + // TODO: Investigate if uint8_t would be better + /*! + * @brief Returns the value of node as bool + * + * Returns false on error. + */ + const bool get_bool(const string &key) const; + + /*! + * @brief Returns the value of node as time_point + * + * Returns clocks epoch on error. + */ + const system_clock::time_point get_time_point(const string &key) const; + protected: Json::Value _tree; bool _valid; From dd5a6905334fdb53bc46ecb527afc3ecde4cae59 Mon Sep 17 00:00:00 2001 From: tastytea Date: Mon, 26 Mar 2018 01:14:59 +0200 Subject: [PATCH 12/12] Updated documentation --- README.md | 22 +++++++++++----------- src/mastodon-cpp.hpp | 8 +++++--- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 9028417..33fa7bd 100644 --- a/README.md +++ b/README.md @@ -6,12 +6,12 @@ The library takes care of the network stuff. You submit a query and get the raw # Usage The HTML reference can be generated with `build_doc.sh`, if doxygen is installed. -Or just look in `src/mastodon-cpp.hpp`. It is also available at [tastytea.github.io/mastodon-cpp/](https://tastytea.github.io/mastodon-cpp/docs/classMastodon_1_1API.html). -There are [examples](https://github.com/tastytea/mastodon-cpp/tree/master/src/examples) in `src/examples/`. +It is also available at [tastytea.github.io/mastodon-cpp/](https://tastytea.github.io/mastodon-cpp/docs/annotated.html). +There are [examples](https://github.com/tastytea/mastodon-cpp/tree/master/examples) in `examples/`. ## Upgrading from below 0.7.0 -The header location has changed. It is now in `mastodon-cpp/`. +The header location has changed. They are now in `mastodon-cpp/`. ## Most basic example @@ -31,7 +31,7 @@ int main() ## Compiling your project -After you did a `make install`, a project consisting of one file can be compiled as follows: +A project consisting of one file can be compiled as follows: g++ -std=c++14 -lmastodon-cpp example.cpp @@ -68,7 +68,7 @@ packages for the package managers of Gentoo, Debian and Red Hat. ### Gentoo -Put the ebuild into your [local overlay](https://wiki.gentoo.org/wiki/Custom_repository) and run `ebuild \ manifest`. +Put the ebuild into your [local overlay](https://wiki.gentoo.org/wiki/Custom_repository) and run `ebuild manifest`. Install with `emerge mastodon-cpp`. ### DEB and RPM @@ -83,11 +83,11 @@ To use the DEB package on stretch, you will need [libcurlpp0](https://packages.d ### Dependencies -* Tested OS: GNU/Linux -* C++ compiler (tested: gcc 6.4/5.4, clang 5.0) +* Tested OS: Linux +* C++ compiler (tested: gcc 6.4 / 5.4, clang 5.0) * [cmake](https://cmake.org/) (tested: 3.9.6) -* [libcurl](https://curl.haxx.se/) (tested: 7.58.0/7.35.0) -* [curlpp](http://www.curlpp.org/) (tested: 0.8.1/0.7.3) +* [libcurl](https://curl.haxx.se/) (tested: 7.58.0 / 7.35.0) +* [curlpp](http://www.curlpp.org/) (tested: 0.8.1 / 0.7.3) * Optional * Easy interface & Examples: [jsoncpp](https://github.com/open-source-parsers/jsoncpp) (tested: 1.8.1 / 1.7.2) * Documentation: [doxygen](https://www.stack.nl/~dimitri/doxygen/) (tested: 1.8.13) @@ -124,13 +124,13 @@ cmake options: * `-DWITH_RPM=ON` if you want to be able to generate an rpm-package You can run the tests with `ctest ..` inside the build directory. -To install, run `make install` +To install, run `make install`. ### Packages #### Gentoo -Put the ebuild in `packages/gentoo` into your [local overlay](https://wiki.gentoo.org/wiki/Custom_repository) and rename it to match the desired version or use the live-ebuild (`mastodon-cpp-9999.ebuild`) to install the development version. +Put the ebuild from `packages/gentoo` into your [local overlay](https://wiki.gentoo.org/wiki/Custom_repository) and rename it to match the desired version or use the live-ebuild (`mastodon-cpp-9999.ebuild`) to install the development version. #### DEB and RPM diff --git a/src/mastodon-cpp.hpp b/src/mastodon-cpp.hpp index 3b781f9..83bacd9 100644 --- a/src/mastodon-cpp.hpp +++ b/src/mastodon-cpp.hpp @@ -41,10 +41,12 @@ namespace Mastodon { /*! - * @brief Class for the Mastodon API. All input is expected to be UTF-8. - * Binary data must be base64-encoded or a filename. - * + * @brief Class for the Mastodon API. + * + * All input is expected to be UTF-8. Binary data must be + * base64-encoded or a filename. * It appears that media attachements can not be base64 encoded. + * * @section error Error codes * mastodon-cpp will never use error codes below 11, except 0. * | Code | Explanation |