From e19bdc2feb7f2b3ec57487e31c7445431f66a0c7 Mon Sep 17 00:00:00 2001 From: tastytea Date: Wed, 21 Mar 2018 16:45:52 +0100 Subject: [PATCH 01/63] 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/63] 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/63] 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/63] 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/63] 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/63] 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/63] 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/63] 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/63] 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/63] 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/63] 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/63] 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 | From dae9ac1fc28b3df1156230fc67a3636ad0af4f57 Mon Sep 17 00:00:00 2001 From: tastytea Date: Wed, 21 Mar 2018 16:45:52 +0100 Subject: [PATCH 13/63] 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 f7eafdaf95ea539bfade97c35970654813a0db47 Mon Sep 17 00:00:00 2001 From: tastytea Date: Wed, 21 Mar 2018 17:08:33 +0100 Subject: [PATCH 14/63] 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 57641d0e4aa9924807c88b8893a22ae631295eef Mon Sep 17 00:00:00 2001 From: tastytea Date: Wed, 21 Mar 2018 17:51:17 +0100 Subject: [PATCH 15/63] 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 a1c1257c2dd029e2953faeb50238097133d726e1 Mon Sep 17 00:00:00 2001 From: tastytea Date: Wed, 21 Mar 2018 20:12:45 +0100 Subject: [PATCH 16/63] 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 d2398008e894366e25128dcd5cae9f908e944bdb Mon Sep 17 00:00:00 2001 From: tastytea Date: Wed, 21 Mar 2018 20:29:33 +0100 Subject: [PATCH 17/63] 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 55ea84c27c46b6daa6140c0db17959d4d40c586e Mon Sep 17 00:00:00 2001 From: tastytea Date: Wed, 21 Mar 2018 23:27:10 +0100 Subject: [PATCH 18/63] 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 0223b9e854347f2e20fcfb2a45ffe1ab8636c119 Mon Sep 17 00:00:00 2001 From: tastytea Date: Wed, 21 Mar 2018 23:28:48 +0100 Subject: [PATCH 19/63] 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 bcc5c304e919b6d5348b76cab1174f61229b499b Mon Sep 17 00:00:00 2001 From: tastytea Date: Thu, 22 Mar 2018 00:33:36 +0100 Subject: [PATCH 20/63] 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 4ce6a5e03aaadc4ab0d1e6086d56d1f9cd28d86e Mon Sep 17 00:00:00 2001 From: tastytea Date: Fri, 23 Mar 2018 00:53:41 +0100 Subject: [PATCH 21/63] 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 6e9113292ed74b4965c4691d6776c1f311bf0838 Mon Sep 17 00:00:00 2001 From: tastytea Date: Sun, 25 Mar 2018 19:43:48 +0200 Subject: [PATCH 22/63] 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 b6bb41e93acc07913d660a29eb00a99e0c8ed944 Mon Sep 17 00:00:00 2001 From: tastytea Date: Sun, 25 Mar 2018 22:46:38 +0200 Subject: [PATCH 23/63] 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 5c30e40259f96a154f80f9832a0f25c7fe235e78 Mon Sep 17 00:00:00 2001 From: tastytea Date: Mon, 26 Mar 2018 01:14:59 +0200 Subject: [PATCH 24/63] 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 a9fae60..40ac08c 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 | From 328e06c2b50d85d51382f8ef3986799e4720261e Mon Sep 17 00:00:00 2001 From: tastytea Date: Fri, 30 Mar 2018 18:41:28 +0200 Subject: [PATCH 25/63] Added Easy::Emoji --- CMakeLists.txt | 2 +- src/easy/all.hpp | 4 +++ src/easy/easy.hpp | 2 ++ src/easy/emoji.cpp | 47 +++++++++++++++++++++++++++++++++ src/easy/emoji.hpp | 65 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 src/easy/emoji.cpp create mode 100644 src/easy/emoji.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index d307e26..4eeebf6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project (mastodon-cpp - VERSION 0.7.5 + VERSION 0.7.6 LANGUAGES CXX ) diff --git a/src/easy/all.hpp b/src/easy/all.hpp index 9e77182..bedf5b6 100644 --- a/src/easy/all.hpp +++ b/src/easy/all.hpp @@ -22,11 +22,15 @@ #include "easy/account.hpp" #include "easy/attachment.hpp" #include "easy/card.hpp" + //#include "easy/context.hpp" + #include "easy/emoji.hpp" #else #include #include #include #include + //#include + #include #endif #endif // MASTODON_CPP_EASY_ALL_HPP diff --git a/src/easy/easy.hpp b/src/easy/easy.hpp index 419b235..5f83f3f 100644 --- a/src/easy/easy.hpp +++ b/src/easy/easy.hpp @@ -159,6 +159,8 @@ public: class Account; class Attachment; class Card; + class Context; + class Emoji; }; } diff --git a/src/easy/emoji.cpp b/src/easy/emoji.cpp new file mode 100644 index 0000000..60e7e6d --- /dev/null +++ b/src/easy/emoji.cpp @@ -0,0 +1,47 @@ +/* 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 "emoji.hpp" +#include "macros.hpp" + +using namespace Mastodon; +using Emoji = Easy::Emoji; +using std::string; +using std::uint64_t; + +Emoji::Emoji(const string &json) +: Entity(json) +{ + // +} + +const string Emoji::shortcode() const +{ + return get_string("shortcode"); +} + +const string Emoji::static_url() const +{ + return get_string("static_url"); +} + +const string Emoji::url() const +{ + return get_string("url"); +} diff --git a/src/easy/emoji.hpp b/src/easy/emoji.hpp new file mode 100644 index 0000000..168b182 --- /dev/null +++ b/src/easy/emoji.hpp @@ -0,0 +1,65 @@ +/* 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 + +// 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; + +namespace Mastodon +{ + /*! + * @brief Class to hold cards + */ + class Easy::Emoji : public Easy::Entity + { + public: + /*! + * @brief Constructs a Emoji object from a JSON string. + * + * @param json JSON string + */ + explicit Emoji(const string &json); + + /*! + * @brief Returns the shortcode of the emoji + */ + const string shortcode() const; + + /*! + * @brief Returns the URL to the emoji static image + */ + const string static_url() const; + + /*! + * @brief Returns the URL to the emoji image + */ + const string url() const; + }; +} + +#endif // MASTODON_CPP_EASY_CARD_HPP From 1a0a02d619e8a5fb2100b8ff872e7cfb05cdd9ab Mon Sep 17 00:00:00 2001 From: tastytea Date: Fri, 30 Mar 2018 19:01:09 +0200 Subject: [PATCH 26/63] Added error() to all entities --- src/easy/easy.cpp | 20 ++++++++++++++++---- src/easy/easy.hpp | 13 ++++++++++++- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/easy/easy.cpp b/src/easy/easy.cpp index 7b675ab..4cf74c9 100644 --- a/src/easy/easy.cpp +++ b/src/easy/easy.cpp @@ -32,9 +32,7 @@ using std::chrono::system_clock; Easy::Easy(const string &instance, const string &access_token) : API(instance, access_token) -{ - // -} +{} Easy::Entity::Entity(const string &json) : _valid(false) @@ -44,7 +42,12 @@ Easy::Entity::Entity(const string &json) if (_tree.isNull()) { - std::cerr << "ERROR: Could not build Entity from JSON string\n"; + ttdebug << "ERROR: JSON string holds no object\n"; + ttdebug << "String was: " << json << '\n'; + } + else if (_tree["error"].isString()) + { + ttdebug << "ERROR: Server returned an error\n"; ttdebug << "String was: " << json << '\n'; } else @@ -53,11 +56,20 @@ Easy::Entity::Entity(const string &json) } } +Easy::Entity::Entity() +: _valid(false) +{} + const bool Easy::Entity::valid() const { return _valid; } +const string Easy::Entity::error() const +{ + return get_string("error"); +} + const Json::Value Easy::Entity::get(const string &key) const { const Json::Value *node; diff --git a/src/easy/easy.hpp b/src/easy/easy.hpp index 5f83f3f..a0f3d27 100644 --- a/src/easy/easy.hpp +++ b/src/easy/easy.hpp @@ -101,13 +101,23 @@ public: * * @param json JSON string */ - Entity(const string &json); + explicit Entity(const string &json); + + /*! + * @brief Constructs an empty Entity object. + */ + explicit Entity(); /*! * @brief Returns true if the Entity holds valid data */ const bool valid() const; + /*! + * @brief Returns error string + */ + const string error() const; + /*! * @brief Returns the value of node as Json::Value * @@ -161,6 +171,7 @@ public: class Card; class Context; class Emoji; + class Instance; }; } From 18aabc454f9035714bc56b2e0489c94dd727c334 Mon Sep 17 00:00:00 2001 From: tastytea Date: Fri, 30 Mar 2018 20:08:47 +0200 Subject: [PATCH 27/63] Added Easy::Instance --- CMakeLists.txt | 2 +- src/easy/all.hpp | 2 + src/easy/easy.cpp | 18 ++++++++ src/easy/easy.hpp | 10 ++++- src/easy/instance.cpp | 83 ++++++++++++++++++++++++++++++++++ src/easy/instance.hpp | 101 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 214 insertions(+), 2 deletions(-) create mode 100644 src/easy/instance.cpp create mode 100644 src/easy/instance.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 4eeebf6..beb5dc7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project (mastodon-cpp - VERSION 0.7.6 + VERSION 0.7.7 LANGUAGES CXX ) diff --git a/src/easy/all.hpp b/src/easy/all.hpp index bedf5b6..378435e 100644 --- a/src/easy/all.hpp +++ b/src/easy/all.hpp @@ -24,10 +24,12 @@ #include "easy/card.hpp" //#include "easy/context.hpp" #include "easy/emoji.hpp" + #include "easy/instance.hpp" #else #include #include #include + #include #include //#include #include diff --git a/src/easy/easy.cpp b/src/easy/easy.cpp index 4cf74c9..2d0970c 100644 --- a/src/easy/easy.cpp +++ b/src/easy/easy.cpp @@ -21,6 +21,7 @@ #include #include // get_time #include +#include #include #include "easy.hpp" #include "macros.hpp" @@ -174,3 +175,20 @@ const system_clock::time_point // Return clocks epoch return system_clock::time_point(); } + +const std::vector Easy::Entity::get_vector(const string &key) const +{ + const Json::Value node = get(key); + + if (node.isArray()) + { + std::vector vec; + for (const Json::Value &value : node) + { + vec.push_back(value.asString()); + } + return vec; + } + + return {}; +} diff --git a/src/easy/easy.hpp b/src/easy/easy.hpp index a0f3d27..4c42013 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 @@ -106,7 +107,7 @@ public: /*! * @brief Constructs an empty Entity object. */ - explicit Entity(); + Entity(); /*! * @brief Returns true if the Entity holds valid data @@ -161,6 +162,13 @@ public: */ const system_clock::time_point get_time_point(const string &key) const; + /*! + * @brief Returns the value of node as vector + * + * Returns false on error. + */ + const std::vector get_vector(const string &key) const; + protected: Json::Value _tree; bool _valid; diff --git a/src/easy/instance.cpp b/src/easy/instance.cpp new file mode 100644 index 0000000..1bd7e9c --- /dev/null +++ b/src/easy/instance.cpp @@ -0,0 +1,83 @@ +/* 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 "instance.hpp" +#include "account.hpp" +#include "macros.hpp" + +using namespace Mastodon; +using Instance = Easy::Instance; +using std::string; +using std::uint64_t; + +Instance::Instance(const string &json) +: Entity(json) +{} + +Instance::Instance() +: Entity() +{} + +const Easy::Account Instance::contact_account() const +{ + const Json::Value node = _tree["contact_account"]; + if (node.isObject()) + { + return Easy::Account(node.toStyledString()); + } + + ttdebug << "Could not get data: contact_account\n"; + return Easy::Account(""); +} + +const string Instance::description() const +{ + return get_string("description"); +} + +const string Instance::email() const +{ + return get_string("email"); +} + +const std::vector Instance::languages() const +{ + return get_vector("languages"); +} + +const string Instance::title() const +{ + return get_string("title"); +} + +const string Instance::uri() const +{ + return get_string("uri"); +} + +const string Instance::version() const +{ + return get_string("version"); +} + +const std::vector Instance::urls() const +{ + return get_vector("urls"); +} diff --git a/src/easy/instance.hpp b/src/easy/instance.hpp new file mode 100644 index 0000000..b5b6897 --- /dev/null +++ b/src/easy/instance.hpp @@ -0,0 +1,101 @@ +/* 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_INSTANCE_HPP +#define MASTODON_CPP_EASY_INSTANCE_HPP + +#include +#include + +// If we are compiling mastodon-cpp, use another include path +#ifdef MASTODON_CPP + #include "mastodon-cpp.hpp" + #include "easy.hpp" + #include "account.hpp" +#else + #include + #include + #include +#endif + +using std::string; + +namespace Mastodon +{ + /*! + * @brief Class to hold cards + */ + class Easy::Instance : public Easy::Entity + { + public: + /*! + * @brief Constructs an Instance object from a JSON string. + * + * @param json JSON string + */ + explicit Instance(const string &json); + + /*! + * @brief Constructs an empty Instance object. + */ + Instance(); + + /*! + * @brief Returns the Account of the admin or another contact person + */ + const Account contact_account() const; + + /*! + * @brief Returns the description of the instance + */ + const string description() const; + + /*! + * @brief Returns the email address which can be used to contact the + * instance administrator + */ + const string email() const; + + /*! + * @brief Returns a vector of ISO 6391 language codes the instance has + * chosen to advertise + */ + const std::vector languages() const; + + /*! + * @brief Returns the title of the instance + */ + const string title() const; + + /*! + * @brief Returns the URI of the instance + */ + const string uri() const; + + /*! + * @brief Returns the version used by the instance + */ + const string version() const; + + // TODO: Find out what Instance.urls is about + /*! + * @brief Returns the a vector of URLs for the streaming API (?) + */ + const std::vector urls() const; + }; +} + +#endif // MASTODON_CPP_EASY_INSTANCE_HPP From 69740568e0a2ca32ac1518d34ce09ba77375e5fa Mon Sep 17 00:00:00 2001 From: tastytea Date: Fri, 30 Mar 2018 20:16:31 +0200 Subject: [PATCH 28/63] Added constructors for empty Entities --- src/easy/account.cpp | 6 +++++- src/easy/account.hpp | 5 +++++ src/easy/attachment.cpp | 4 ++++ src/easy/attachment.hpp | 5 +++++ src/easy/card.cpp | 8 +++++--- src/easy/card.hpp | 5 +++++ src/easy/emoji.cpp | 8 +++++--- src/easy/emoji.hpp | 13 +++++++++---- src/easy/instance.cpp | 2 +- 9 files changed, 44 insertions(+), 12 deletions(-) diff --git a/src/easy/account.cpp b/src/easy/account.cpp index 1b8d675..229188a 100644 --- a/src/easy/account.cpp +++ b/src/easy/account.cpp @@ -32,6 +32,10 @@ Account::Account(const string &json) : Entity(json) {} +Account::Account() +: Entity() +{} + const string Account::acct() const { return get_string("acct"); @@ -105,7 +109,7 @@ const Account Account::moved() const return Account(get("moved").toStyledString()); } - return Account(""); + return Account(); } const string Account::note() const diff --git a/src/easy/account.hpp b/src/easy/account.hpp index f2b9992..fecbc5d 100644 --- a/src/easy/account.hpp +++ b/src/easy/account.hpp @@ -50,6 +50,11 @@ namespace Mastodon */ explicit Account(const string &json); + /*! + * @brief Constructs an empty Account object. + */ + Account(); + /*! * @brief Returns username * diff --git a/src/easy/attachment.cpp b/src/easy/attachment.cpp index 36fbb76..ece9766 100644 --- a/src/easy/attachment.cpp +++ b/src/easy/attachment.cpp @@ -30,6 +30,10 @@ Attachment::Attachment(const string &json) : Entity(json) {} +Attachment::Attachment() +: Entity() +{} + const double Attachment::aspect() const { return get_double("meta.original.aspect"); diff --git a/src/easy/attachment.hpp b/src/easy/attachment.hpp index cf200e5..3f39d1f 100644 --- a/src/easy/attachment.hpp +++ b/src/easy/attachment.hpp @@ -50,6 +50,11 @@ namespace Mastodon */ explicit Attachment(const string &json); + /*! + * @brief Constructs an empty Attachment object. + */ + Attachment(); + /*! * @brief Aspect of original image */ diff --git a/src/easy/card.cpp b/src/easy/card.cpp index 23bdcda..b3418b9 100644 --- a/src/easy/card.cpp +++ b/src/easy/card.cpp @@ -27,9 +27,11 @@ using std::uint64_t; Card::Card(const string &json) : Entity(json) -{ - // -} +{} + +Card::Card() +: Entity() +{} const string Card::author_name() const { diff --git a/src/easy/card.hpp b/src/easy/card.hpp index 88f5d8f..ed0b3a3 100644 --- a/src/easy/card.hpp +++ b/src/easy/card.hpp @@ -48,6 +48,11 @@ namespace Mastodon */ explicit Card(const string &json); + /*! + * @brief Constructs an empty Card object. + */ + Card(); + /*! * @brief Returns the name of the author */ diff --git a/src/easy/emoji.cpp b/src/easy/emoji.cpp index 60e7e6d..ea91df9 100644 --- a/src/easy/emoji.cpp +++ b/src/easy/emoji.cpp @@ -27,9 +27,11 @@ using std::uint64_t; Emoji::Emoji(const string &json) : Entity(json) -{ - // -} +{} + +Emoji::Emoji() +: Entity() +{} const string Emoji::shortcode() const { diff --git a/src/easy/emoji.hpp b/src/easy/emoji.hpp index 168b182..cf5d793 100644 --- a/src/easy/emoji.hpp +++ b/src/easy/emoji.hpp @@ -14,8 +14,8 @@ * along with this program. If not, see . */ -#ifndef MASTODON_CPP_EASY_CARD_HPP -#define MASTODON_CPP_EASY_CARD_HPP +#ifndef MASTODON_CPP_EASY_EMOJI_HPP +#define MASTODON_CPP_EASY_EMOJI_HPP #include @@ -39,12 +39,17 @@ namespace Mastodon { public: /*! - * @brief Constructs a Emoji object from a JSON string. + * @brief Constructs an Emoji object from a JSON string. * * @param json JSON string */ explicit Emoji(const string &json); + /*! + * @brief Constructs an empty Emoji object. + */ + Emoji(); + /*! * @brief Returns the shortcode of the emoji */ @@ -62,4 +67,4 @@ namespace Mastodon }; } -#endif // MASTODON_CPP_EASY_CARD_HPP +#endif // MASTODON_CPP_EASY_EMOJI_HPP diff --git a/src/easy/instance.cpp b/src/easy/instance.cpp index 1bd7e9c..f269a71 100644 --- a/src/easy/instance.cpp +++ b/src/easy/instance.cpp @@ -44,7 +44,7 @@ const Easy::Account Instance::contact_account() const } ttdebug << "Could not get data: contact_account\n"; - return Easy::Account(""); + return Easy::Account(); } const string Instance::description() const From a89656caf14ec5ebce07b867f0793bf4217c8c25 Mon Sep 17 00:00:00 2001 From: tastytea Date: Fri, 30 Mar 2018 20:22:45 +0200 Subject: [PATCH 29/63] Added Easy::Context --- src/easy/context.cpp | 33 ++++++++++++++++++++++ src/easy/context.hpp | 66 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 src/easy/context.cpp create mode 100644 src/easy/context.hpp diff --git a/src/easy/context.cpp b/src/easy/context.cpp new file mode 100644 index 0000000..04e359d --- /dev/null +++ b/src/easy/context.cpp @@ -0,0 +1,33 @@ +/* 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 "context.hpp" +#include "macros.hpp" + +using namespace Mastodon; +using Context = Easy::Context; +using std::string; +using std::uint64_t; + +Context::Context(const string &json) +: Entity(json) +{} + +Context::Context() +: Entity() +{} diff --git a/src/easy/context.hpp b/src/easy/context.hpp new file mode 100644 index 0000000..f986b79 --- /dev/null +++ b/src/easy/context.hpp @@ -0,0 +1,66 @@ +/* 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_CONTEXT_HPP +#define MASTODON_CPP_EASY_CONTEXT_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; + +namespace Mastodon +{ + /*! + * @brief Class to hold cards + */ + class Easy::Context : public Easy::Entity + { + public: + /*! + * @brief Constructs a Context object from a JSON string. + * + * @param json JSON string + */ + explicit Context(const string &json); + + /*! + * @brief Constructs an empty Context object. + */ + Context(); + + // /*! + // * @brief Returns the ancestors of the Status as vector of Statuses + // */ + // const std::vector ancestors() const; + + // /*! + // * @brief Returns the descendants of the Status as vector of Statuses + // */ + // const std::vector descendants() const; + }; +} + +#endif // MASTODON_CPP_EASY_CONTEXT_HPP From 6a8fb144ad37cefbd9bcb21f548976b261b705e5 Mon Sep 17 00:00:00 2001 From: tastytea Date: Fri, 30 Mar 2018 20:30:29 +0200 Subject: [PATCH 30/63] Added Entity::List --- CMakeLists.txt | 2 +- src/easy/list.cpp | 43 ++++++++++++++++++++++++++++++ src/easy/list.hpp | 66 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 src/easy/list.cpp create mode 100644 src/easy/list.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index beb5dc7..bd99e65 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project (mastodon-cpp - VERSION 0.7.7 + VERSION 0.7.8 LANGUAGES CXX ) diff --git a/src/easy/list.cpp b/src/easy/list.cpp new file mode 100644 index 0000000..3fc6ed8 --- /dev/null +++ b/src/easy/list.cpp @@ -0,0 +1,43 @@ +/* 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 "list.hpp" +#include "macros.hpp" + +using namespace Mastodon; +using List = Easy::List; +using std::string; +using std::uint64_t; + +List::List(const string &json) +: Entity(json) +{} + +List::List() +: Entity() +{} + +const uint64_t List::id() const +{ + return get_uint64("id"); +} + +const string List::title() const +{ + return get_string("title"); +} diff --git a/src/easy/list.hpp b/src/easy/list.hpp new file mode 100644 index 0000000..49d8e6b --- /dev/null +++ b/src/easy/list.hpp @@ -0,0 +1,66 @@ +/* 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_LIST_HPP +#define MASTODON_CPP_EASY_LIST_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; + +namespace Mastodon +{ + /*! + * @brief Class to hold lists + */ + class Easy::List : public Easy::Entity + { + public: + /*! + * @brief Constructs a List object from a JSON string. + * + * @param json JSON string + */ + explicit List(const string &json); + + /*! + * @brief Constructs an empty List object. + */ + List(); + + /*! + * @brief Returns list-ID + */ + const uint64_t id() const; + + /*! + * @brief Returns title + */ + const string title() const; + }; +} + +#endif // MASTODON_CPP_EASY_LIST_HPP From 4a1b454ba11027e823ac9bcbc4369968a2f95b6c Mon Sep 17 00:00:00 2001 From: tastytea Date: Fri, 30 Mar 2018 20:31:30 +0200 Subject: [PATCH 31/63] Removed unneccessary using-statements, fixed documentation --- src/easy/account.hpp | 1 - src/easy/all.hpp | 2 ++ src/easy/card.hpp | 1 - src/easy/context.hpp | 2 +- src/easy/easy.hpp | 1 + src/easy/emoji.hpp | 2 +- src/easy/instance.cpp | 1 - src/easy/instance.hpp | 2 +- 8 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/easy/account.hpp b/src/easy/account.hpp index fecbc5d..703d7dc 100644 --- a/src/easy/account.hpp +++ b/src/easy/account.hpp @@ -32,7 +32,6 @@ #endif using std::string; -using std::uint16_t; using std::uint64_t; namespace Mastodon diff --git a/src/easy/all.hpp b/src/easy/all.hpp index 378435e..535e3f2 100644 --- a/src/easy/all.hpp +++ b/src/easy/all.hpp @@ -25,6 +25,7 @@ //#include "easy/context.hpp" #include "easy/emoji.hpp" #include "easy/instance.hpp" + #include "easy/list.hpp" #else #include #include @@ -33,6 +34,7 @@ #include //#include #include + #include "easy/list.hpp" #endif #endif // MASTODON_CPP_EASY_ALL_HPP diff --git a/src/easy/card.hpp b/src/easy/card.hpp index ed0b3a3..40bf49c 100644 --- a/src/easy/card.hpp +++ b/src/easy/card.hpp @@ -30,7 +30,6 @@ #endif using std::string; -using std::uint16_t; using std::uint64_t; namespace Mastodon diff --git a/src/easy/context.hpp b/src/easy/context.hpp index f986b79..466cf42 100644 --- a/src/easy/context.hpp +++ b/src/easy/context.hpp @@ -34,7 +34,7 @@ using std::string; namespace Mastodon { /*! - * @brief Class to hold cards + * @brief Class to hold contexts */ class Easy::Context : public Easy::Entity { diff --git a/src/easy/easy.hpp b/src/easy/easy.hpp index 4c42013..2f36729 100644 --- a/src/easy/easy.hpp +++ b/src/easy/easy.hpp @@ -180,6 +180,7 @@ public: class Context; class Emoji; class Instance; + class List; }; } diff --git a/src/easy/emoji.hpp b/src/easy/emoji.hpp index cf5d793..afd0a68 100644 --- a/src/easy/emoji.hpp +++ b/src/easy/emoji.hpp @@ -33,7 +33,7 @@ using std::string; namespace Mastodon { /*! - * @brief Class to hold cards + * @brief Class to hold emojis */ class Easy::Emoji : public Easy::Entity { diff --git a/src/easy/instance.cpp b/src/easy/instance.cpp index f269a71..d29f32d 100644 --- a/src/easy/instance.cpp +++ b/src/easy/instance.cpp @@ -25,7 +25,6 @@ using namespace Mastodon; using Instance = Easy::Instance; using std::string; -using std::uint64_t; Instance::Instance(const string &json) : Entity(json) diff --git a/src/easy/instance.hpp b/src/easy/instance.hpp index b5b6897..2fa16ca 100644 --- a/src/easy/instance.hpp +++ b/src/easy/instance.hpp @@ -36,7 +36,7 @@ using std::string; namespace Mastodon { /*! - * @brief Class to hold cards + * @brief Class to hold instances */ class Easy::Instance : public Easy::Entity { From fa03cb7ed2b567bfcf31ecfafef1bb24dec9802d Mon Sep 17 00:00:00 2001 From: tastytea Date: Fri, 30 Mar 2018 23:51:09 +0200 Subject: [PATCH 32/63] Added Easy::Mention, small fixes --- src/easy/account.hpp | 3 +- src/easy/all.hpp | 6 +++- src/easy/easy.hpp | 14 ++++++++ src/easy/list.hpp | 2 ++ src/easy/mention.cpp | 52 +++++++++++++++++++++++++++++ src/easy/mention.hpp | 78 ++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 153 insertions(+), 2 deletions(-) create mode 100644 src/easy/mention.cpp create mode 100644 src/easy/mention.hpp diff --git a/src/easy/account.hpp b/src/easy/account.hpp index 703d7dc..9b617d8 100644 --- a/src/easy/account.hpp +++ b/src/easy/account.hpp @@ -33,6 +33,7 @@ using std::string; using std::uint64_t; +using std::chrono::system_clock; namespace Mastodon { @@ -75,7 +76,7 @@ namespace Mastodon /*! * @brief Returns time of creation */ - const std::chrono::system_clock::time_point created_at() const; + const system_clock::time_point created_at() const; /*! * @brief Returns display name diff --git a/src/easy/all.hpp b/src/easy/all.hpp index 535e3f2..e32a10f 100644 --- a/src/easy/all.hpp +++ b/src/easy/all.hpp @@ -26,6 +26,8 @@ #include "easy/emoji.hpp" #include "easy/instance.hpp" #include "easy/list.hpp" + #include "easy/mention.hpp" + //#include "easy/notification.hpp" #else #include #include @@ -34,7 +36,9 @@ #include //#include #include - #include "easy/list.hpp" + #include + #include + //#include #endif #endif // MASTODON_CPP_EASY_ALL_HPP diff --git a/src/easy/easy.hpp b/src/easy/easy.hpp index 2f36729..233b0f8 100644 --- a/src/easy/easy.hpp +++ b/src/easy/easy.hpp @@ -80,6 +80,18 @@ public: unknown }; + /*! + * @brief Describes the notification type + */ + enum class notification_type + { + mention, + reblog, + favourite, + follow, + unknown + }; + /*! * @brief Constructs a new Easy object. * @@ -181,6 +193,8 @@ public: class Emoji; class Instance; class List; + class Mention; + class Notification; }; } diff --git a/src/easy/list.hpp b/src/easy/list.hpp index 49d8e6b..2e8a5e3 100644 --- a/src/easy/list.hpp +++ b/src/easy/list.hpp @@ -19,6 +19,7 @@ #include #include +#include // If we are compiling mastodon-cpp, use another include path #ifdef MASTODON_CPP @@ -30,6 +31,7 @@ #endif using std::string; +using std::uint64_t; namespace Mastodon { diff --git a/src/easy/mention.cpp b/src/easy/mention.cpp new file mode 100644 index 0000000..aaeba8a --- /dev/null +++ b/src/easy/mention.cpp @@ -0,0 +1,52 @@ +/* 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 "mention.hpp" +#include "macros.hpp" + +using namespace Mastodon; +using Mention = Easy::Mention; +using std::string; +using std::uint64_t; + +Mention::Mention(const string &json) +: Entity(json) +{} + +Mention::Mention() +: Entity() +{} + +const string Mention::acct() const +{ + return get_string("acct"); +} + +const uint64_t Mention::id() const +{ + return get_uint64("id"); +} + +const string Mention::url() const +{ + return get_string("url"); +} + +const string Mention::username() const +{ + return get_string("username"); +} diff --git a/src/easy/mention.hpp b/src/easy/mention.hpp new file mode 100644 index 0000000..95cd7dc --- /dev/null +++ b/src/easy/mention.hpp @@ -0,0 +1,78 @@ +/* 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_MENTION_HPP +#define MASTODON_CPP_EASY_MENTION_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::uint64_t; +using std::chrono::system_clock; + +namespace Mastodon +{ + /*! + * @brief Class to hold mentions + */ + class Easy::Mention : public Easy::Entity + { + public: + /*! + * @brief Constructs a Mention object from a JSON string. + * + * @param json JSON string + */ + explicit Mention(const string &json); + + /*! + * @brief Constructs an empty Mention object. + */ + Mention(); + + /*! + * @brief Returns acct + */ + const string acct() const; + + /*! + * @brief Returns account ID + */ + const uint64_t id() const; + + /*! + * @brief Returns the URL of user's profile + */ + const string url() const; + + /*! + * @brief Returns the username of the account + */ + const string username() const; + }; +} + +#endif // MASTODON_CPP_EASY_MENTION_HPP From 2f7da8a99a5d8605243aabcfb09cf843be8eeb01 Mon Sep 17 00:00:00 2001 From: tastytea Date: Sat, 31 Mar 2018 00:00:20 +0200 Subject: [PATCH 33/63] Addedd Easy::Notification (without status() for now) --- CMakeLists.txt | 2 +- src/easy/notification.cpp | 73 ++++++++++++++++++++++++++++++++ src/easy/notification.hpp | 87 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 161 insertions(+), 1 deletion(-) create mode 100644 src/easy/notification.cpp create mode 100644 src/easy/notification.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index bd99e65..9eb853b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project (mastodon-cpp - VERSION 0.7.8 + VERSION 0.7.9 LANGUAGES CXX ) diff --git a/src/easy/notification.cpp b/src/easy/notification.cpp new file mode 100644 index 0000000..7d6878d --- /dev/null +++ b/src/easy/notification.cpp @@ -0,0 +1,73 @@ +/* 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 "notification.hpp" +#include "macros.hpp" + +using namespace Mastodon; +using Notification = Easy::Notification; +using std::string; +using std::uint64_t; + +Notification::Notification(const string &json) +: Entity(json) +{} + +Notification::Notification() +: Entity() +{} + +const Easy::Account Notification::account() const +{ + const Json::Value node = _tree["account"]; + if (node.isObject()) + { + return Easy::Account(node.toStyledString()); + } + + ttdebug << "Could not get data: account\n"; + return Easy::Account(); +} + +const system_clock::time_point Notification::created_at() const +{ + return get_time_point("created_at"); +} + +const uint64_t Notification::id() const +{ + return get_uint64("id"); +} + +// const Status Notification::status() const +// { +// // +// } + +const Easy::notification_type Notification::type() const +{ + const string strtype = get_string("type"); + if (strtype.compare("mention") == 0) + return notification_type::mention; + else if (strtype.compare("reblog") == 0) + return notification_type::reblog; + else if (strtype.compare("favourite") == 0) + return notification_type::favourite; + else if (strtype.compare("follow") == 0) + return notification_type::follow; + + return notification_type::unknown; +} diff --git a/src/easy/notification.hpp b/src/easy/notification.hpp new file mode 100644 index 0000000..2eda6ba --- /dev/null +++ b/src/easy/notification.hpp @@ -0,0 +1,87 @@ +/* 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_NOTIFICATION_HPP +#define MASTODON_CPP_EASY_NOTIFICATION_HPP + +#include +#include +#include + +// If we are compiling mastodon-cpp, use another include path +#ifdef MASTODON_CPP + #include "mastodon-cpp.hpp" + #include "easy.hpp" + #include "account.hpp" +#else + #include + #include + #include +#endif + +using std::string; +using std::uint64_t; +using std::chrono::system_clock; + +namespace Mastodon +{ + /*! + * @brief Class to hold notifications + */ + class Easy::Notification : public Easy::Entity + { + public: + /*! + * @brief Constructs a Notification object from a JSON string. + * + * @param json JSON string + */ + explicit Notification(const string &json); + + /*! + * @brief Constructs an empty Notification object. + */ + Notification(); + + /*! + * @brief Returns the Account sending the notification to the user + */ + const Account account() const; + + /*! + * @brief Returns time of creation + */ + const system_clock::time_point created_at() const; + + /*! + * @brief Returns notification ID + */ + const uint64_t id() const; + + /*! + * @brief Returns the Status associated with the notification, if + * applicable + */ + // const Status status() const; + + /*! + * @brief Returns notification type + */ + const Easy::notification_type type() const; + }; +} + +#endif // MASTODON_CPP_EASY_NOTIFICATION_HPP From d6a208857fb2311f2dd5fcff86654cc8aff141c3 Mon Sep 17 00:00:00 2001 From: tastytea Date: Sat, 31 Mar 2018 00:12:34 +0200 Subject: [PATCH 34/63] removed duplicate includes and such --- src/easy/account.cpp | 9 +-------- src/easy/all.hpp | 3 ++- src/easy/attachment.cpp | 6 +----- src/easy/card.cpp | 6 +----- src/easy/context.cpp | 5 ----- src/easy/easy.cpp | 6 ------ src/easy/emoji.cpp | 5 ----- src/easy/instance.cpp | 5 ----- src/easy/list.cpp | 3 --- src/easy/mention.cpp | 4 ---- src/easy/notification.cpp | 2 -- 11 files changed, 5 insertions(+), 49 deletions(-) diff --git a/src/easy/account.cpp b/src/easy/account.cpp index 229188a..0838703 100644 --- a/src/easy/account.cpp +++ b/src/easy/account.cpp @@ -14,19 +14,11 @@ * along with this program. If not, see . */ -#include -#include -#include -#include -#include #include "account.hpp" #include "macros.hpp" 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) @@ -134,6 +126,7 @@ const Easy::visibility Account::privacy() const else if (strprivacy.compare("direct") == 0) return visibility::Direct; + ttdebug << "Could not get data: source.privacy\n"; return visibility::Undefined; } diff --git a/src/easy/all.hpp b/src/easy/all.hpp index e32a10f..68a40e4 100644 --- a/src/easy/all.hpp +++ b/src/easy/all.hpp @@ -17,6 +17,7 @@ #ifndef MASTODON_CPP_EASY_ALL_HPP #define MASTODON_CPP_EASY_ALL_HPP +// If we are compiling mastodon-cpp, use another include path #ifdef MASTODON_CPP #include "easy.hpp" #include "easy/account.hpp" @@ -32,10 +33,10 @@ #include #include #include - #include #include //#include #include + #include #include #include //#include diff --git a/src/easy/attachment.cpp b/src/easy/attachment.cpp index ece9766..7ce1c9d 100644 --- a/src/easy/attachment.cpp +++ b/src/easy/attachment.cpp @@ -14,17 +14,12 @@ * along with this program. If not, see . */ -#include #include -#include -#include #include "attachment.hpp" #include "macros.hpp" using namespace Mastodon; using Attachment = Easy::Attachment; -using std::string; -using std::uint64_t; Attachment::Attachment(const string &json) : Entity(json) @@ -117,6 +112,7 @@ const Easy::attachment_type Attachment::type() const else if (strtype.compare("unknown") == 0) return attachment_type::unknown; + ttdebug << "Could not get data: type\n"; return attachment_type::unknown; } diff --git a/src/easy/card.cpp b/src/easy/card.cpp index b3418b9..86f0df4 100644 --- a/src/easy/card.cpp +++ b/src/easy/card.cpp @@ -14,16 +14,11 @@ * 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) @@ -90,6 +85,7 @@ const Easy::card_type Card::type() const else if (strtype.compare("rich") == 0) return card_type::rich; + ttdebug << "Could not get data: type\n"; return card_type::unknown; } diff --git a/src/easy/context.cpp b/src/easy/context.cpp index 04e359d..d206c1d 100644 --- a/src/easy/context.cpp +++ b/src/easy/context.cpp @@ -14,15 +14,10 @@ * along with this program. If not, see . */ -#include -#include #include "context.hpp" -#include "macros.hpp" using namespace Mastodon; using Context = Easy::Context; -using std::string; -using std::uint64_t; Context::Context(const string &json) : Entity(json) diff --git a/src/easy/easy.cpp b/src/easy/easy.cpp index 2d0970c..6d5d4ee 100644 --- a/src/easy/easy.cpp +++ b/src/easy/easy.cpp @@ -14,15 +14,9 @@ * along with this program. If not, see . */ -#include -#include -#include -#include #include #include // get_time #include -#include -#include #include "easy.hpp" #include "macros.hpp" diff --git a/src/easy/emoji.cpp b/src/easy/emoji.cpp index ea91df9..3782b02 100644 --- a/src/easy/emoji.cpp +++ b/src/easy/emoji.cpp @@ -14,16 +14,11 @@ * along with this program. If not, see . */ -#include -#include -#include #include "emoji.hpp" #include "macros.hpp" using namespace Mastodon; using Emoji = Easy::Emoji; -using std::string; -using std::uint64_t; Emoji::Emoji(const string &json) : Entity(json) diff --git a/src/easy/instance.cpp b/src/easy/instance.cpp index d29f32d..95c6eb8 100644 --- a/src/easy/instance.cpp +++ b/src/easy/instance.cpp @@ -14,17 +14,12 @@ * along with this program. If not, see . */ -#include -#include -#include -#include #include "instance.hpp" #include "account.hpp" #include "macros.hpp" using namespace Mastodon; using Instance = Easy::Instance; -using std::string; Instance::Instance(const string &json) : Entity(json) diff --git a/src/easy/list.cpp b/src/easy/list.cpp index 3fc6ed8..338e08b 100644 --- a/src/easy/list.cpp +++ b/src/easy/list.cpp @@ -14,10 +14,7 @@ * along with this program. If not, see . */ -#include -#include #include "list.hpp" -#include "macros.hpp" using namespace Mastodon; using List = Easy::List; diff --git a/src/easy/mention.cpp b/src/easy/mention.cpp index aaeba8a..65bf152 100644 --- a/src/easy/mention.cpp +++ b/src/easy/mention.cpp @@ -14,14 +14,10 @@ * along with this program. If not, see . */ -#include #include "mention.hpp" -#include "macros.hpp" using namespace Mastodon; using Mention = Easy::Mention; -using std::string; -using std::uint64_t; Mention::Mention(const string &json) : Entity(json) diff --git a/src/easy/notification.cpp b/src/easy/notification.cpp index 7d6878d..77dce58 100644 --- a/src/easy/notification.cpp +++ b/src/easy/notification.cpp @@ -19,8 +19,6 @@ using namespace Mastodon; using Notification = Easy::Notification; -using std::string; -using std::uint64_t; Notification::Notification(const string &json) : Entity(json) From d4708e538f538386d7fa08e73eaea1f59769f0fd Mon Sep 17 00:00:00 2001 From: tastytea Date: Sat, 31 Mar 2018 02:54:00 +0200 Subject: [PATCH 35/63] Added Easy::Relationship --- CMakeLists.txt | 2 +- src/easy/all.hpp | 2 + src/easy/easy.hpp | 19 ++++---- src/easy/relationship.cpp | 68 ++++++++++++++++++++++++++++ src/easy/relationship.hpp | 95 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 176 insertions(+), 10 deletions(-) create mode 100644 src/easy/relationship.cpp create mode 100644 src/easy/relationship.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 9eb853b..ced92f9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project (mastodon-cpp - VERSION 0.7.9 + VERSION 0.7.10 LANGUAGES CXX ) diff --git a/src/easy/all.hpp b/src/easy/all.hpp index 68a40e4..339f8cc 100644 --- a/src/easy/all.hpp +++ b/src/easy/all.hpp @@ -29,6 +29,7 @@ #include "easy/list.hpp" #include "easy/mention.hpp" //#include "easy/notification.hpp" + #include "easy/relationship.hpp" #else #include #include @@ -40,6 +41,7 @@ #include #include //#include + #include #endif #endif // MASTODON_CPP_EASY_ALL_HPP diff --git a/src/easy/easy.hpp b/src/easy/easy.hpp index 233b0f8..89ae4f7 100644 --- a/src/easy/easy.hpp +++ b/src/easy/easy.hpp @@ -131,29 +131,30 @@ public: */ const string error() const; + protected: /*! - * @brief Returns the value of node as Json::Value - * + * @brief Returns the value of key 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 + * @brief Returns the value of key as std::string * * returns "" on error. */ const string get_string(const string &key) const; /*! - * @brief Returns the value of node as std::uint64_t + * @brief Returns the value of key 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 + * @brief Returns the value of key as double * * Returns 0.0 on error. */ @@ -161,27 +162,26 @@ public: // TODO: Investigate if uint8_t would be better /*! - * @brief Returns the value of node as bool + * @brief Returns the value of key as bool * * Returns false on error. */ const bool get_bool(const string &key) const; /*! - * @brief Returns the value of node as time_point + * @brief Returns the value of key as time_point * * Returns clocks epoch on error. */ const system_clock::time_point get_time_point(const string &key) const; /*! - * @brief Returns the value of node as vector + * @brief Returns the value of key as vector * * Returns false on error. */ const std::vector get_vector(const string &key) const; - protected: Json::Value _tree; bool _valid; }; @@ -195,6 +195,7 @@ public: class List; class Mention; class Notification; + class Relationship; }; } diff --git a/src/easy/relationship.cpp b/src/easy/relationship.cpp new file mode 100644 index 0000000..77b3a68 --- /dev/null +++ b/src/easy/relationship.cpp @@ -0,0 +1,68 @@ +/* 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 "relationship.hpp" + +using namespace Mastodon; +using Relationship = Easy::Relationship; + +Relationship::Relationship(const string &json) +: Entity(json) +{} + +Relationship::Relationship() +: Entity() +{} + +const bool Relationship::blocking() const +{ + return get_bool("blocking"); +} + +const bool Relationship::domain_blocking() const +{ + return get_bool("domain_blocking"); +} + +const bool Relationship::followed_by() const +{ + return get_bool("followed_by"); +} + +const bool Relationship::following() const +{ + return get_bool("following"); +} + +const uint64_t Relationship::id() const +{ + return get_uint64("id"); +} + +const bool Relationship::muting() const +{ + return get_bool("muting"); +} + +const bool Relationship::muting_notifications() const +{ + return get_bool("muting_notifications"); +} + +const bool Relationship::requested() const +{ + return get_bool("requested"); +} diff --git a/src/easy/relationship.hpp b/src/easy/relationship.hpp new file mode 100644 index 0000000..85d5625 --- /dev/null +++ b/src/easy/relationship.hpp @@ -0,0 +1,95 @@ +/* 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_RELATIONSHIP_HPP +#define MASTODON_CPP_EASY_RELATIONSHIP_HPP + +#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::uint64_t; + +namespace Mastodon +{ + /*! + * @brief Class to hold relationships + */ + class Easy::Relationship : public Easy::Entity + { + public: + /*! + * @brief Constructs a Relationship object from a JSON string. + * + * @param json JSON string + */ + explicit Relationship(const string &json); + + /*! + * @brief Constructs an empty Relationship object. + */ + Relationship(); + + /*! + * @brief Returns true if the user is blocking the account + */ + const bool blocking() const; + + /*! + * @brief Returns true if the user is blocking the account's domain + */ + const bool domain_blocking() const; + + /*! + * @brief Returns true if the user is being followed by the account + */ + const bool followed_by() const; + + /*! + * @brief Returns true if the user is being following the account + */ + const bool following() const; + + /*! + * @brief Returns the target account ID + */ + const uint64_t id() const; + + /*! + * @brief Returns true if the user is muting the account + */ + const bool muting() const; + + /*! + * @brief Returns true if the user is also muting notifications + */ + const bool muting_notifications() const; + + /*! + * @brief Returns true if the user has requested to follow the account + */ + const bool requested() const; + }; +} + +#endif // MASTODON_CPP_EASY_RELATIONSHIP_HPP From 06ab515f179af6f07178af9cc66c5545a83aade2 Mon Sep 17 00:00:00 2001 From: tastytea Date: Sat, 31 Mar 2018 03:28:18 +0200 Subject: [PATCH 36/63] Add Easy::Report and Easy::Results (incomplete) --- CMakeLists.txt | 2 +- src/easy/all.hpp | 4 +++ src/easy/easy.hpp | 2 ++ src/easy/instance.cpp | 3 +- src/easy/relationship.hpp | 2 ++ src/easy/report.cpp | 39 +++++++++++++++++++++ src/easy/report.hpp | 68 +++++++++++++++++++++++++++++++++++ src/easy/results.cpp | 52 +++++++++++++++++++++++++++ src/easy/results.hpp | 74 +++++++++++++++++++++++++++++++++++++++ 9 files changed, 244 insertions(+), 2 deletions(-) create mode 100644 src/easy/report.cpp create mode 100644 src/easy/report.hpp create mode 100644 src/easy/results.cpp create mode 100644 src/easy/results.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index ced92f9..40af053 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project (mastodon-cpp - VERSION 0.7.10 + VERSION 0.7.11 LANGUAGES CXX ) diff --git a/src/easy/all.hpp b/src/easy/all.hpp index 339f8cc..36e566f 100644 --- a/src/easy/all.hpp +++ b/src/easy/all.hpp @@ -30,6 +30,8 @@ #include "easy/mention.hpp" //#include "easy/notification.hpp" #include "easy/relationship.hpp" + #include "easy/report.hpp" + //#include "easy/results.hpp" #else #include #include @@ -42,6 +44,8 @@ #include //#include #include + #include + //#include #endif #endif // MASTODON_CPP_EASY_ALL_HPP diff --git a/src/easy/easy.hpp b/src/easy/easy.hpp index 89ae4f7..e0caa88 100644 --- a/src/easy/easy.hpp +++ b/src/easy/easy.hpp @@ -196,6 +196,8 @@ public: class Mention; class Notification; class Relationship; + class Report; + class Results; }; } diff --git a/src/easy/instance.cpp b/src/easy/instance.cpp index 95c6eb8..c36f869 100644 --- a/src/easy/instance.cpp +++ b/src/easy/instance.cpp @@ -14,6 +14,7 @@ * along with this program. If not, see . */ +#include #include "instance.hpp" #include "account.hpp" #include "macros.hpp" @@ -31,7 +32,7 @@ Instance::Instance() const Easy::Account Instance::contact_account() const { - const Json::Value node = _tree["contact_account"]; + const Json::Value node = get("contact_account"); if (node.isObject()) { return Easy::Account(node.toStyledString()); diff --git a/src/easy/relationship.hpp b/src/easy/relationship.hpp index 85d5625..cb68afc 100644 --- a/src/easy/relationship.hpp +++ b/src/easy/relationship.hpp @@ -17,6 +17,7 @@ #ifndef MASTODON_CPP_EASY_RELATIONSHIP_HPP #define MASTODON_CPP_EASY_RELATIONSHIP_HPP +#include #include // If we are compiling mastodon-cpp, use another include path @@ -28,6 +29,7 @@ #include #endif +using std::string; using std::uint64_t; namespace Mastodon diff --git a/src/easy/report.cpp b/src/easy/report.cpp new file mode 100644 index 0000000..17f1551 --- /dev/null +++ b/src/easy/report.cpp @@ -0,0 +1,39 @@ +/* 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 "report.hpp" + +using namespace Mastodon; +using Report = Easy::Report; + +Report::Report(const string &json) +: Entity(json) +{} + +Report::Report() +: Entity() +{} + +const bool Report::action_taken() const +{ + return get_bool("action_taken"); +} + +const uint64_t Report::id() const +{ + return get_uint64("id"); +} + diff --git a/src/easy/report.hpp b/src/easy/report.hpp new file mode 100644 index 0000000..c18a596 --- /dev/null +++ b/src/easy/report.hpp @@ -0,0 +1,68 @@ +/* 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_REPORT_HPP +#define MASTODON_CPP_EASY_REPORT_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::uint64_t; + +namespace Mastodon +{ + /*! + * @brief Class to hold reports + */ + class Easy::Report : public Easy::Entity + { + public: + /*! + * @brief Constructs a Report object from a JSON string. + * + * @param json JSON string + */ + explicit Report(const string &json); + + /*! + * @brief Constructs an empty Report object. + */ + Report(); + + /*! + * @brief Returns true if an action was taken in response to the + * report + */ + const bool action_taken() const; + + /*! + * @brief Returns the ID of the report + */ + const uint64_t id() const; + }; +} + +#endif // MASTODON_CPP_EASY_REPORT_HPP diff --git a/src/easy/results.cpp b/src/easy/results.cpp new file mode 100644 index 0000000..59c5cea --- /dev/null +++ b/src/easy/results.cpp @@ -0,0 +1,52 @@ +/* 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 "results.hpp" +#include "macros.hpp" + +using namespace Mastodon; +using Results = Easy::Results; + +Results::Results(const string &json) +: Entity(json) +{} + +Results::Results() +: Entity() +{} + +const std::vector Results::accounts() const +{ + const Json::Value node = get("accounts"); + if (node.isArray()) + { + std::vector vec; + for (const Json::Value &value : node) + { + vec.push_back(Easy::Account(value.toStyledString())); + } + return vec; + } + + ttdebug << "Could not get data: accounts\n"; + return {}; +} + +const std::vector Results::hashtags() const +{ + return get_vector("hashtags"); +} diff --git a/src/easy/results.hpp b/src/easy/results.hpp new file mode 100644 index 0000000..b865a3e --- /dev/null +++ b/src/easy/results.hpp @@ -0,0 +1,74 @@ +/* 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_RESULTS_HPP +#define MASTODON_CPP_EASY_RESULTS_HPP + +#include +#include + +// If we are compiling mastodon-cpp, use another include path +#ifdef MASTODON_CPP + #include "mastodon-cpp.hpp" + #include "easy.hpp" + #include "account.hpp" +#else + #include + #include + #include +#endif + +using std::string; +using std::uint64_t; + +namespace Mastodon +{ + /*! + * @brief Class to hold results + */ + class Easy::Results : public Easy::Entity + { + public: + /*! + * @brief Constructs a Result object from a JSON string. + * + * @param json JSON string + */ + explicit Results(const string &json); + + /*! + * @brief Constructs an empty Results object. + */ + Results(); + + /*! + * @brief Returns an array of matched Accounts + */ + const std::vector accounts() const; + + /*! + * @brief Returns an array of matched Statuses + */ + // const std::vector statuses() const; + + /*! + * @brief Returns an array of matched hashtags + */ + const std::vector hashtags() const; + }; +} + +#endif // MASTODON_CPP_EASY_RESULTS_HPP From 7e2c2b0359706766239d520cd94a64082a502c50 Mon Sep 17 00:00:00 2001 From: tastytea Date: Sat, 31 Mar 2018 04:07:21 +0200 Subject: [PATCH 37/63] Added Easy::Application and Easy::Tag --- src/easy/account.hpp | 1 - src/easy/all.hpp | 6 ++++ src/easy/application.cpp | 38 +++++++++++++++++++++++ src/easy/application.hpp | 65 ++++++++++++++++++++++++++++++++++++++++ src/easy/easy.hpp | 3 ++ src/easy/results.hpp | 3 +- src/easy/tag.cpp | 38 +++++++++++++++++++++++ src/easy/tag.hpp | 65 ++++++++++++++++++++++++++++++++++++++++ 8 files changed, 216 insertions(+), 3 deletions(-) create mode 100644 src/easy/application.cpp create mode 100644 src/easy/application.hpp create mode 100644 src/easy/tag.cpp create mode 100644 src/easy/tag.hpp diff --git a/src/easy/account.hpp b/src/easy/account.hpp index 9b617d8..14a8059 100644 --- a/src/easy/account.hpp +++ b/src/easy/account.hpp @@ -20,7 +20,6 @@ #include #include #include -#include // If we are compiling mastodon-cpp, use another include path #ifdef MASTODON_CPP diff --git a/src/easy/all.hpp b/src/easy/all.hpp index 36e566f..3d5ce14 100644 --- a/src/easy/all.hpp +++ b/src/easy/all.hpp @@ -21,6 +21,7 @@ #ifdef MASTODON_CPP #include "easy.hpp" #include "easy/account.hpp" + #include "easy/application.hpp" #include "easy/attachment.hpp" #include "easy/card.hpp" //#include "easy/context.hpp" @@ -32,9 +33,12 @@ #include "easy/relationship.hpp" #include "easy/report.hpp" //#include "easy/results.hpp" + #include "easy/status.hpp" + #include "easy/tag.hpp" #else #include #include + #include #include #include //#include @@ -46,6 +50,8 @@ #include #include //#include + #include + #include #endif #endif // MASTODON_CPP_EASY_ALL_HPP diff --git a/src/easy/application.cpp b/src/easy/application.cpp new file mode 100644 index 0000000..00b919c --- /dev/null +++ b/src/easy/application.cpp @@ -0,0 +1,38 @@ +/* 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 "application.hpp" + +using namespace Mastodon; +using Application = Easy::Application; + +Application::Application(const string &json) +: Entity(json) +{} + +Application::Application() +: Entity() +{} + +const string Application::name() const +{ + get_string("name"); +} + +const string Application::website() const +{ + get_string("website"); +} diff --git a/src/easy/application.hpp b/src/easy/application.hpp new file mode 100644 index 0000000..6350a6c --- /dev/null +++ b/src/easy/application.hpp @@ -0,0 +1,65 @@ +/* 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_APPLICATION_HPP +#define MASTODON_CPP_EASY_APPLICATION_HPP + +#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; + +namespace Mastodon +{ + /*! + * @brief Class to hold applications. + */ + class Easy::Application : public Easy::Entity + { + public: + /*! + * @brief Constructs an Application object from a JSON string. + * + * @param json JSON string + */ + explicit Application(const string &json); + + /*! + * @brief Constructs an empty Application object. + */ + Application(); + + /*! + * @brief Returns the name of the application + */ + const string name() const; + + /*! + * @brief Returns the website of the application + */ + const string website() const; +}; +} + +#endif // MASTODON_CPP_EASY_APPLICATION_HPP diff --git a/src/easy/easy.hpp b/src/easy/easy.hpp index e0caa88..0cc0f9f 100644 --- a/src/easy/easy.hpp +++ b/src/easy/easy.hpp @@ -187,6 +187,7 @@ public: }; class Account; + class Application; class Attachment; class Card; class Context; @@ -198,6 +199,8 @@ public: class Relationship; class Report; class Results; + class Status; + class Tag; }; } diff --git a/src/easy/results.hpp b/src/easy/results.hpp index b865a3e..7c2b6a0 100644 --- a/src/easy/results.hpp +++ b/src/easy/results.hpp @@ -32,7 +32,6 @@ #endif using std::string; -using std::uint64_t; namespace Mastodon { @@ -43,7 +42,7 @@ namespace Mastodon { public: /*! - * @brief Constructs a Result object from a JSON string. + * @brief Constructs a Results object from a JSON string. * * @param json JSON string */ diff --git a/src/easy/tag.cpp b/src/easy/tag.cpp new file mode 100644 index 0000000..2e44d13 --- /dev/null +++ b/src/easy/tag.cpp @@ -0,0 +1,38 @@ +/* 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 "tag.hpp" + +using namespace Mastodon; +using Tag = Easy::Tag; + +Tag::Tag(const string &json) +: Entity(json) +{} + +Tag::Tag() +: Entity() +{} + +const string Tag::name() const +{ + get_string("name"); +} + +const string Tag::url() const +{ + get_string("url"); +} diff --git a/src/easy/tag.hpp b/src/easy/tag.hpp new file mode 100644 index 0000000..39447e5 --- /dev/null +++ b/src/easy/tag.hpp @@ -0,0 +1,65 @@ +/* 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_TAG_HPP +#define MASTODON_CPP_EASY_TAG_HPP + +#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; + +namespace Mastodon +{ + /*! + * @brief Class to hold tags. + */ + class Easy::Tag : public Easy::Entity + { + public: + /*! + * @brief Constructs an Tag object from a JSON string. + * + * @param json JSON string + */ + explicit Tag(const string &json); + + /*! + * @brief Constructs an empty Tag object. + */ + Tag(); + + /*! + * @brief Returns the name of the application + */ + const string name() const; + + /*! + * @brief Returns the URL of the application + */ + const string url() const; +}; +} + +#endif // MASTODON_CPP_EASY_TAG_HPP From d78c699955d57ce4d1899f4820b57e5750aaaa6f Mon Sep 17 00:00:00 2001 From: tastytea Date: Sat, 31 Mar 2018 04:29:55 +0200 Subject: [PATCH 38/63] Added Easy::Status --- CMakeLists.txt | 2 +- src/easy/account.cpp | 12 +- src/easy/account.hpp | 2 +- src/easy/application.cpp | 4 +- src/easy/easy.hpp | 2 +- src/easy/status.cpp | 230 +++++++++++++++++++++++++++++++++++++++ src/easy/status.hpp | 197 +++++++++++++++++++++++++++++++++ src/easy/tag.cpp | 4 +- 8 files changed, 440 insertions(+), 13 deletions(-) create mode 100644 src/easy/status.cpp create mode 100644 src/easy/status.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 40af053..ffefdd2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project (mastodon-cpp - VERSION 0.7.11 + VERSION 0.7.12 LANGUAGES CXX ) diff --git a/src/easy/account.cpp b/src/easy/account.cpp index 0838703..70320e7 100644 --- a/src/easy/account.cpp +++ b/src/easy/account.cpp @@ -114,20 +114,20 @@ const string Account::note_plain() const return get_string("source.note"); } -const Easy::visibility Account::privacy() const +const Easy::visibility_type Account::privacy() const { const string strprivacy = get_string("source.privacy"); if (strprivacy.compare("public") == 0) - return visibility::Public; + return visibility_type::Public; else if (strprivacy.compare("unlisted") == 0) - return visibility::Unlisted; + return visibility_type::Unlisted; else if (strprivacy.compare("private") == 0) - return visibility::Private; + return visibility_type::Private; else if (strprivacy.compare("direct") == 0) - return visibility::Direct; + return visibility_type::Direct; ttdebug << "Could not get data: source.privacy\n"; - return visibility::Undefined; + return visibility_type::Undefined; } const bool Account::sensitive() const diff --git a/src/easy/account.hpp b/src/easy/account.hpp index 14a8059..9727786 100644 --- a/src/easy/account.hpp +++ b/src/easy/account.hpp @@ -136,7 +136,7 @@ namespace Mastodon /*! * @brief Returns default privacy of new toots */ - const visibility privacy() const; + const visibility_type privacy() const; /*! * @brief Returns if media is marked as sensitive by default diff --git a/src/easy/application.cpp b/src/easy/application.cpp index 00b919c..bec917f 100644 --- a/src/easy/application.cpp +++ b/src/easy/application.cpp @@ -29,10 +29,10 @@ Application::Application() const string Application::name() const { - get_string("name"); + return get_string("name"); } const string Application::website() const { - get_string("website"); + return get_string("website"); } diff --git a/src/easy/easy.hpp b/src/easy/easy.hpp index 0cc0f9f..b3d0cf6 100644 --- a/src/easy/easy.hpp +++ b/src/easy/easy.hpp @@ -48,7 +48,7 @@ public: * The names begin with a capital letter because some of them * are reserved keywords when written in all-lowercase. */ - enum class visibility + enum class visibility_type { Direct, Private, diff --git a/src/easy/status.cpp b/src/easy/status.cpp new file mode 100644 index 0000000..58e62fb --- /dev/null +++ b/src/easy/status.cpp @@ -0,0 +1,230 @@ +/* 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 "status.hpp" +#include "macros.hpp" + +using namespace Mastodon; +using Status = Easy::Status; + +Status::Status(const string &json) +: Entity(json) +{} + +Status::Status() +: Entity() +{} + +const Easy::Account Status::account() const +{ + const Json::Value node = get("account"); + if (node.isObject()) + { + return Easy::Account(node.toStyledString()); + } + + ttdebug << "Could not get data: account\n"; + return Easy::Account(); +} + +const Easy::Application Status::application() const +{ + const Json::Value node = get("application"); + if (node.isObject()) + { + return Easy::Application(node.toStyledString()); + } + + ttdebug << "Could not get data: application\n"; + return Easy::Application(); +} + +const system_clock::time_point Status::created_at() const +{ + return get_time_point("created_at"); +} + +const string Status::content() const +{ + return get_string("content"); +} + +const std::vector Status::emojis() const +{ + const Json::Value node = get("emojis"); + if (node.isArray()) + { + std::vector vec; + for (const Json::Value &value : node) + { + vec.push_back(Easy::Emoji(value.toStyledString())); + } + return vec; + } + + ttdebug << "Could not get data: emojis\n"; + return {}; +} + +const bool Status::favourited() const +{ + return get_bool("favourited"); +} + +const uint64_t Status::favourites_count() const +{ + return get_uint64("favourites_count"); +} + +const uint64_t Status::id() const +{ + return get_uint64("id"); +} + +const uint64_t Status::in_reply_to_id() const +{ + return get_uint64("in_reply_to_id"); +} + +const uint64_t Status::in_reply_to_account_id() const +{ + return get_uint64("in_reply_to_account_id"); +} + +const string Status::language() const +{ + return get_string("language"); +} + +const std::vector Status::media_attachments() const +{ + const Json::Value node = get("media_attachments"); + if (node.isArray()) + { + std::vector vec; + for (const Json::Value &value : node) + { + vec.push_back(Easy::Attachment(value.toStyledString())); + } + return vec; + } + + ttdebug << "Could not get data: media_attachments\n"; + return {}; +} + +const std::vector Status::mentions() const +{ + const Json::Value node = get("mentions"); + if (node.isArray()) + { + std::vector vec; + for (const Json::Value &value : node) + { + vec.push_back(Easy::Mention(value.toStyledString())); + } + return vec; + } + + ttdebug << "Could not get data: mentions\n"; + return {}; +} + +const bool Status::muted() const +{ + return get_bool("muted"); +} + +const bool Status::pinned() const +{ + return get_bool("pinned"); +} + +const Easy::Status Status::reblog() const +{ + const Json::Value node = get("reblog"); + if (node.isObject()) + { + return Easy::Status(node.toStyledString()); + } + + ttdebug << "Could not get data: reblog\n"; + return Easy::Status(); +} + +const bool Status::reblogged() const +{ + return get_bool("reblogged"); +} + +const uint64_t Status::reblogs_count() const +{ + return get_uint64("reblogs_count"); +} + +const bool Status::sensitive() const +{ + return get_bool("sensitive"); +} + +const string Status::spoiler_text() const +{ + return get_string("spoiler_text"); +} + +const std::vector Status::tags() const +{ + const Json::Value node = get("tags"); + if (node.isArray()) + { + std::vector vec; + for (const Json::Value &value : node) + { + vec.push_back(Easy::Tag(value.toStyledString())); + } + return vec; + } + + ttdebug << "Could not get data: tags\n"; + return {}; +} + +const string Status::uri() const +{ + return get_string("uri"); +} + +const string Status::url() const +{ + return get_string("url"); +} + +const Easy::visibility_type Status::visibility() const +{ + const string strvisibility = get_string("visibility"); + if (strvisibility.compare("public") == 0) + return visibility_type::Public; + else if (strvisibility.compare("unlisted") == 0) + return visibility_type::Unlisted; + else if (strvisibility.compare("private") == 0) + return visibility_type::Private; + else if (strvisibility.compare("direct") == 0) + return visibility_type::Direct; + + ttdebug << "Could not get data: visibility\n"; + return visibility_type::Undefined; +} diff --git a/src/easy/status.hpp b/src/easy/status.hpp new file mode 100644 index 0000000..26b5ecb --- /dev/null +++ b/src/easy/status.hpp @@ -0,0 +1,197 @@ +/* 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_STATUS_HPP +#define MASTODON_CPP_EASY_STATUS_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" + #include "account.hpp" + #include "emoji.hpp" + #include "attachment.hpp" + #include "mention.hpp" + #include "tag.hpp" + #include "application.hpp" +#else + #include + #include + #include + #include + #include + #include + #include + #include +#endif + +using std::string; +using std::uint64_t; +using std::chrono::system_clock; + +namespace Mastodon +{ + /*! + * @brief Class to hold statuses + */ + class Easy::Status : public Easy::Entity + { + public: + /*! + * @brief Constructs a Status object from a JSON string. + * + * @param json JSON string + */ + explicit Status(const string &json); + + /*! + * @brief Constructs an empty Status object. + */ + Status(); + + /*! + * @brief Returns an array of matched accounts + */ + const Account account() const; + + /*! + * @brief Returns application from which the status was posted + */ + const Application application() const; + + /*! + * @brief Returns time of creation + */ + const system_clock::time_point created_at() const; + + /*! + * @brief Returns content of status + */ + const string content() const; + + /*! + * @brief Returns an array of emojis + */ + const std::vector emojis() const; + + /*! + * @brief Returns true if the user has favourited the status + */ + const bool favourited() const; + + /*! + * @brief Returns the number of favourites + */ + const uint64_t favourites_count() const; + + /*! + * @brief Returns the ID of the status + */ + const uint64_t id() const; + + /*! + * @brief Returns the ID of the status it replies to + */ + const uint64_t in_reply_to_id() const; + + /*! + * @brief Returns the ID of the account it replies to + */ + const uint64_t in_reply_to_account_id() const; + + /*! + * @brief Returns the language of the status + */ + const string language() const; + + /*! + * @brief Returns the attachments + */ + const std::vector media_attachments() const; + + /*! + * @brief Returns the mentions + */ + const std::vector mentions() const; + + /*! + * @brief Returns true if the user muted the conversation + */ + const bool muted() const; + + /*! + * @brief Returns true if the status is pinned + */ + const bool pinned() const; + + /*! + * @brief Returns the reblogged Status + */ + const Status reblog() const; + + /*! + * @brief Returns true if the user has reblogged the status + */ + const bool reblogged() const; + + /*! + * @brief Returns the number of reblogs for the status + */ + const uint64_t reblogs_count() const; + + /*! + * @brief Returns true if the attachments should be hidden by default + */ + const bool sensitive() const; + + /*! + * @brief Returns the spoiler text + */ + const string spoiler_text() const; + + /*! + * @brief Returns the tags + */ + const std::vector tags() const; + + /*! + * @brief Returns the Fediverse-unique resource ID + */ + const string uri() const; + + /*! + * @brief Returns the URL to the status page + */ + const string url() const; + + /*! + * @brief Returns the visibility of the status + */ + const visibility_type visibility() const; + + /*! + * @brief Returns the + */ + + }; +} + +#endif // MASTODON_CPP_EASY_STATUS_HPP diff --git a/src/easy/tag.cpp b/src/easy/tag.cpp index 2e44d13..4ab6402 100644 --- a/src/easy/tag.cpp +++ b/src/easy/tag.cpp @@ -29,10 +29,10 @@ Tag::Tag() const string Tag::name() const { - get_string("name"); + return get_string("name"); } const string Tag::url() const { - get_string("url"); + return get_string("url"); } From e4d2b1759e1df504fe221f7b6aadd4e9413a0ccb Mon Sep 17 00:00:00 2001 From: tastytea Date: Sat, 31 Mar 2018 04:37:36 +0200 Subject: [PATCH 39/63] Completed most of the incomplete Entitys (Attachment is still incomplete) --- CMakeLists.txt | 2 +- src/easy/all.hpp | 12 ++++++------ src/easy/context.cpp | 35 +++++++++++++++++++++++++++++++++++ src/easy/context.hpp | 18 ++++++++++-------- src/easy/notification.cpp | 15 +++++++++++---- src/easy/notification.hpp | 4 +++- src/easy/results.cpp | 17 +++++++++++++++++ src/easy/results.hpp | 4 +++- 8 files changed, 86 insertions(+), 21 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ffefdd2..a7a786a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project (mastodon-cpp - VERSION 0.7.12 + VERSION 0.7.13 LANGUAGES CXX ) diff --git a/src/easy/all.hpp b/src/easy/all.hpp index 3d5ce14..17d14b0 100644 --- a/src/easy/all.hpp +++ b/src/easy/all.hpp @@ -24,15 +24,15 @@ #include "easy/application.hpp" #include "easy/attachment.hpp" #include "easy/card.hpp" - //#include "easy/context.hpp" + #include "easy/context.hpp" #include "easy/emoji.hpp" #include "easy/instance.hpp" #include "easy/list.hpp" #include "easy/mention.hpp" - //#include "easy/notification.hpp" + #include "easy/notification.hpp" #include "easy/relationship.hpp" #include "easy/report.hpp" - //#include "easy/results.hpp" + #include "easy/results.hpp" #include "easy/status.hpp" #include "easy/tag.hpp" #else @@ -41,15 +41,15 @@ #include #include #include - //#include + #include #include #include #include #include - //#include + #include #include #include - //#include + #include #include #include #endif diff --git a/src/easy/context.cpp b/src/easy/context.cpp index d206c1d..9f521a7 100644 --- a/src/easy/context.cpp +++ b/src/easy/context.cpp @@ -15,6 +15,7 @@ */ #include "context.hpp" +#include "macros.hpp" using namespace Mastodon; using Context = Easy::Context; @@ -26,3 +27,37 @@ Context::Context(const string &json) Context::Context() : Entity() {} + +const std::vector Context::ancestors() const +{ + const Json::Value node = get("ancestors"); + if (node.isArray()) + { + std::vector vec; + for (const Json::Value &value : node) + { + vec.push_back(Easy::Status(value.toStyledString())); + } + return vec; + } + + ttdebug << "Could not get data: ancestors\n"; + return {}; +} + +const std::vector Context::descendants() const +{ + const Json::Value node = get("descendants"); + if (node.isArray()) + { + std::vector vec; + for (const Json::Value &value : node) + { + vec.push_back(Easy::Status(value.toStyledString())); + } + return vec; + } + + ttdebug << "Could not get data: descendants\n"; + return {}; +} diff --git a/src/easy/context.hpp b/src/easy/context.hpp index 466cf42..37fa469 100644 --- a/src/easy/context.hpp +++ b/src/easy/context.hpp @@ -24,9 +24,11 @@ #ifdef MASTODON_CPP #include "mastodon-cpp.hpp" #include "easy.hpp" + #include "status.hpp" #else #include #include + #include #endif using std::string; @@ -51,15 +53,15 @@ namespace Mastodon */ Context(); - // /*! - // * @brief Returns the ancestors of the Status as vector of Statuses - // */ - // const std::vector ancestors() const; + /*! + * @brief Returns the ancestors of the Status as vector of Statuses + */ + const std::vector ancestors() const; - // /*! - // * @brief Returns the descendants of the Status as vector of Statuses - // */ - // const std::vector descendants() const; + /*! + * @brief Returns the descendants of the Status as vector of Statuses + */ + const std::vector descendants() const; }; } diff --git a/src/easy/notification.cpp b/src/easy/notification.cpp index 77dce58..2bd4e24 100644 --- a/src/easy/notification.cpp +++ b/src/easy/notification.cpp @@ -50,10 +50,17 @@ const uint64_t Notification::id() const return get_uint64("id"); } -// const Status Notification::status() const -// { -// // -// } +const Easy::Status Notification::status() const +{ + const Json::Value node = get("restatusblog"); + if (node.isObject()) + { + return Easy::Status(node.toStyledString()); + } + + ttdebug << "Could not get data: status\n"; + return Easy::Status(); +} const Easy::notification_type Notification::type() const { diff --git a/src/easy/notification.hpp b/src/easy/notification.hpp index 2eda6ba..ab5ff44 100644 --- a/src/easy/notification.hpp +++ b/src/easy/notification.hpp @@ -26,10 +26,12 @@ #include "mastodon-cpp.hpp" #include "easy.hpp" #include "account.hpp" + #include "status.hpp" #else #include #include #include + #include #endif using std::string; @@ -75,7 +77,7 @@ namespace Mastodon * @brief Returns the Status associated with the notification, if * applicable */ - // const Status status() const; + const Status status() const; /*! * @brief Returns notification type diff --git a/src/easy/results.cpp b/src/easy/results.cpp index 59c5cea..966231b 100644 --- a/src/easy/results.cpp +++ b/src/easy/results.cpp @@ -46,6 +46,23 @@ const std::vector Results::accounts() const return {}; } +const std::vector Results::statuses() const +{ + const Json::Value node = get("statuses"); + if (node.isArray()) + { + std::vector vec; + for (const Json::Value &value : node) + { + vec.push_back(Easy::Status(value.toStyledString())); + } + return vec; + } + + ttdebug << "Could not get data: statuses\n"; + return {}; +} + const std::vector Results::hashtags() const { return get_vector("hashtags"); diff --git a/src/easy/results.hpp b/src/easy/results.hpp index 7c2b6a0..81859e2 100644 --- a/src/easy/results.hpp +++ b/src/easy/results.hpp @@ -25,10 +25,12 @@ #include "mastodon-cpp.hpp" #include "easy.hpp" #include "account.hpp" + #include "status.hpp" #else #include #include #include + #include #endif using std::string; @@ -61,7 +63,7 @@ namespace Mastodon /*! * @brief Returns an array of matched Statuses */ - // const std::vector statuses() const; + const std::vector statuses() const; /*! * @brief Returns an array of matched hashtags From 168a4926d1c1b1c72d61ff79aa2ad9aa268b8b2f Mon Sep 17 00:00:00 2001 From: tastytea Date: Sat, 31 Mar 2018 05:02:03 +0200 Subject: [PATCH 40/63] Added Account::url() (forgot it before) --- src/easy/account.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/easy/account.cpp b/src/easy/account.cpp index 70320e7..07f71ad 100644 --- a/src/easy/account.cpp +++ b/src/easy/account.cpp @@ -140,6 +140,11 @@ const std::uint64_t Account::statuses_count() const return get_uint64("statuses_count"); } +const string Account::url() const +{ + return get_string("url"); +} + const string Account::username() const { return get_string("username"); From c1174c0061809025073a699ce77a60f6110c77a7 Mon Sep 17 00:00:00 2001 From: tastytea Date: Sat, 31 Mar 2018 06:52:22 +0200 Subject: [PATCH 41/63] Entity: Added check for single-object arrays --- src/easy/easy.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/easy/easy.cpp b/src/easy/easy.cpp index 6d5d4ee..477d25e 100644 --- a/src/easy/easy.cpp +++ b/src/easy/easy.cpp @@ -30,17 +30,25 @@ Easy::Easy(const string &instance, const string &access_token) {} Easy::Entity::Entity(const string &json) -: _valid(false) +: _tree(Json::nullValue) +, _valid(false) { std::stringstream ss(json); ss >> _tree; + // If the JSON is a single object encapsulated in an array, + // transform it into an object. If the JSON string is [], transform to null + if (_tree.type() == Json::ValueType::arrayValue && _tree.size() <= 1) + { + _tree = _tree[0]; + } + if (_tree.isNull()) { ttdebug << "ERROR: JSON string holds no object\n"; ttdebug << "String was: " << json << '\n'; } - else if (_tree["error"].isString()) + else if (!_tree["error"].isNull()) { ttdebug << "ERROR: Server returned an error\n"; ttdebug << "String was: " << json << '\n'; From f8530d09e8cc423bbf09d6e10371b003796df369 Mon Sep 17 00:00:00 2001 From: tastytea Date: Sat, 31 Mar 2018 19:29:03 +0200 Subject: [PATCH 42/63] Added type conversion for IDs (string->uint64) --- CMakeLists.txt | 2 +- src/easy/account.cpp | 2 +- src/easy/attachment.cpp | 2 +- src/easy/list.cpp | 2 +- src/easy/mention.cpp | 2 +- src/easy/notification.cpp | 2 +- src/easy/relationship.cpp | 2 +- src/easy/report.cpp | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a7a786a..b1e9334 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project (mastodon-cpp - VERSION 0.7.13 + VERSION 0.7.14 LANGUAGES CXX ) diff --git a/src/easy/account.cpp b/src/easy/account.cpp index 07f71ad..bf9b44a 100644 --- a/src/easy/account.cpp +++ b/src/easy/account.cpp @@ -75,7 +75,7 @@ const string Account::header_static() const const std::uint64_t Account::id() const { - return get_uint64("id"); + return std::stoull(get_string("id")); } const bool Account::locked() const diff --git a/src/easy/attachment.cpp b/src/easy/attachment.cpp index 7ce1c9d..e89a43d 100644 --- a/src/easy/attachment.cpp +++ b/src/easy/attachment.cpp @@ -72,7 +72,7 @@ const uint64_t Attachment::height_small() const const std::uint64_t Attachment::id() const { - return get_uint64("id"); + return std::stoull(get_string("id")); } const string Attachment::preview_url() const diff --git a/src/easy/list.cpp b/src/easy/list.cpp index 338e08b..a148a35 100644 --- a/src/easy/list.cpp +++ b/src/easy/list.cpp @@ -31,7 +31,7 @@ List::List() const uint64_t List::id() const { - return get_uint64("id"); + return std::stoull(get_string("id")); } const string List::title() const diff --git a/src/easy/mention.cpp b/src/easy/mention.cpp index 65bf152..fe8be65 100644 --- a/src/easy/mention.cpp +++ b/src/easy/mention.cpp @@ -34,7 +34,7 @@ const string Mention::acct() const const uint64_t Mention::id() const { - return get_uint64("id"); + return std::stoull(get_string("id")); } const string Mention::url() const diff --git a/src/easy/notification.cpp b/src/easy/notification.cpp index 2bd4e24..856ad87 100644 --- a/src/easy/notification.cpp +++ b/src/easy/notification.cpp @@ -47,7 +47,7 @@ const system_clock::time_point Notification::created_at() const const uint64_t Notification::id() const { - return get_uint64("id"); + return std::stoull(get_string("id")); } const Easy::Status Notification::status() const diff --git a/src/easy/relationship.cpp b/src/easy/relationship.cpp index 77b3a68..a80a5a4 100644 --- a/src/easy/relationship.cpp +++ b/src/easy/relationship.cpp @@ -49,7 +49,7 @@ const bool Relationship::following() const const uint64_t Relationship::id() const { - return get_uint64("id"); + return std::stoull(get_string("id")); } const bool Relationship::muting() const diff --git a/src/easy/report.cpp b/src/easy/report.cpp index 17f1551..84f0e3d 100644 --- a/src/easy/report.cpp +++ b/src/easy/report.cpp @@ -34,6 +34,6 @@ const bool Report::action_taken() const const uint64_t Report::id() const { - return get_uint64("id"); + return std::stoull(get_string("id")); } From d37f1aefb72127a57dd198c03192b720596635b3 Mon Sep 17 00:00:00 2001 From: tastytea Date: Sat, 31 Mar 2018 19:43:15 +0200 Subject: [PATCH 43/63] Changed uint64_t to uint_fast64_t --- CMakeLists.txt | 2 +- src/easy/account.cpp | 8 ++++---- src/easy/account.hpp | 10 +++++----- src/easy/attachment.cpp | 12 ++++++------ src/easy/attachment.hpp | 16 ++++++++-------- src/easy/card.cpp | 4 ++-- src/easy/card.hpp | 6 +++--- src/easy/easy.cpp | 4 ++-- src/easy/easy.hpp | 6 +++--- src/easy/list.cpp | 4 ++-- src/easy/list.hpp | 4 ++-- src/easy/mention.cpp | 2 +- src/easy/mention.hpp | 4 ++-- src/easy/notification.cpp | 2 +- src/easy/notification.hpp | 4 ++-- src/easy/relationship.cpp | 2 +- src/easy/relationship.hpp | 4 ++-- src/easy/report.cpp | 2 +- src/easy/report.hpp | 4 ++-- src/easy/status.cpp | 10 +++++----- src/easy/status.hpp | 12 ++++++------ 21 files changed, 61 insertions(+), 61 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b1e9334..e8e1daa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project (mastodon-cpp - VERSION 0.7.14 + VERSION 0.7.15 LANGUAGES CXX ) diff --git a/src/easy/account.cpp b/src/easy/account.cpp index bf9b44a..92cde18 100644 --- a/src/easy/account.cpp +++ b/src/easy/account.cpp @@ -53,12 +53,12 @@ const string Account::display_name() const return get_string("display_name"); } -const std::uint64_t Account::followers_count() const +const std::uint_fast64_t Account::followers_count() const { return get_uint64("followers_count"); } -const std::uint64_t Account::following_count() const +const std::uint_fast64_t Account::following_count() const { return get_uint64("following_count"); } @@ -73,7 +73,7 @@ const string Account::header_static() const return get_string("header_static"); } -const std::uint64_t Account::id() const +const std::uint_fast64_t Account::id() const { return std::stoull(get_string("id")); } @@ -135,7 +135,7 @@ const bool Account::sensitive() const return get_bool("source.sensitive"); } -const std::uint64_t Account::statuses_count() const +const std::uint_fast64_t Account::statuses_count() const { return get_uint64("statuses_count"); } diff --git a/src/easy/account.hpp b/src/easy/account.hpp index 9727786..30559bc 100644 --- a/src/easy/account.hpp +++ b/src/easy/account.hpp @@ -31,7 +31,7 @@ #endif using std::string; -using std::uint64_t; +using std::uint_fast64_t; using std::chrono::system_clock; namespace Mastodon @@ -85,12 +85,12 @@ namespace Mastodon /*! * @brief Returns number of followers */ - const uint64_t followers_count() const; + const uint_fast64_t followers_count() const; /*! * @brief Returns number of people this account follows */ - const uint64_t following_count() const; + const uint_fast64_t following_count() const; /*! * @brief Returns URL of header image @@ -105,7 +105,7 @@ namespace Mastodon /*! * @brief Returns account-ID */ - const uint64_t id() const; + const uint_fast64_t id() const; /*! * @brief Returns true if the account is locked @@ -146,7 +146,7 @@ namespace Mastodon /*! * @brief Returns number of statuses */ - const uint64_t statuses_count() const; + const uint_fast64_t statuses_count() const; /*! * @brief Returns URL of the profile diff --git a/src/easy/attachment.cpp b/src/easy/attachment.cpp index e89a43d..461d527 100644 --- a/src/easy/attachment.cpp +++ b/src/easy/attachment.cpp @@ -44,7 +44,7 @@ const string Attachment::description() const return get_string("description"); } -const std::array Attachment::focus() const +const std::array Attachment::focus() const { const Json::Value x = get("meta.focus.x"); const Json::Value y = get("meta.focus.y"); @@ -60,17 +60,17 @@ const std::array Attachment::focus() const return {}; } -const uint64_t Attachment::height() const +const uint_fast64_t Attachment::height() const { return get_uint64("meta.original.height"); } -const uint64_t Attachment::height_small() const +const uint_fast64_t Attachment::height_small() const { return get_uint64("meta.small.height"); } -const std::uint64_t Attachment::id() const +const std::uint_fast64_t Attachment::id() const { return std::stoull(get_string("id")); } @@ -121,12 +121,12 @@ const string Attachment::url() const return get_string("url"); } -const uint64_t Attachment::width() const +const uint_fast64_t Attachment::width() const { return get_uint64("meta.original.width"); } -const uint64_t Attachment::width_small() const +const uint_fast64_t Attachment::width_small() const { return get_uint64("meta.small.width"); } diff --git a/src/easy/attachment.hpp b/src/easy/attachment.hpp index 3f39d1f..a6b4524 100644 --- a/src/easy/attachment.hpp +++ b/src/easy/attachment.hpp @@ -33,7 +33,7 @@ using std::string; using std::uint16_t; -using std::uint64_t; +using std::uint_fast64_t; namespace Mastodon { @@ -74,22 +74,22 @@ namespace Mastodon * @brief Returns the focus point (x, y) */ // TODO: find attachment with focus - const std::array focus() const; + const std::array focus() const; /*! * @brief Returns the height of the original image */ - const uint64_t height() const; + const uint_fast64_t height() const; /*! * @brief Returns the height of the preview image */ - const uint64_t height_small() const; + const uint_fast64_t height_small() const; /*! * @brief Returns the ID of the attachment */ - const uint64_t id() const; + const uint_fast64_t id() const; /*! * @brief Returns the URL of the preview image @@ -129,18 +129,18 @@ namespace Mastodon /*! * @brief Returns the width of the original image */ - const uint64_t width() const; + const uint_fast64_t width() const; /*! * @brief Returns the width of the preview image */ - const uint64_t width_small() const; + const uint_fast64_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; + // const uint_fast64_t bitrate() const; }; } diff --git a/src/easy/card.cpp b/src/easy/card.cpp index 86f0df4..306baaa 100644 --- a/src/easy/card.cpp +++ b/src/easy/card.cpp @@ -43,7 +43,7 @@ const string Card::description() const return get_string("description"); } -const uint64_t Card::height() const +const uint_fast64_t Card::height() const { return get_uint64("height"); } @@ -94,7 +94,7 @@ const string Card::url() const return get_string("url"); } -const uint64_t Card::width() const +const uint_fast64_t Card::width() const { return get_uint64("width"); } diff --git a/src/easy/card.hpp b/src/easy/card.hpp index 40bf49c..5b5036e 100644 --- a/src/easy/card.hpp +++ b/src/easy/card.hpp @@ -30,7 +30,7 @@ #endif using std::string; -using std::uint64_t; +using std::uint_fast64_t; namespace Mastodon { @@ -70,7 +70,7 @@ namespace Mastodon /*! * @brief Returns the height of the card */ - const uint64_t height() const; + const uint_fast64_t height() const; /*! * @brief Returns the HTML @@ -110,7 +110,7 @@ namespace Mastodon /*! * @brief Returns the width of the card */ - const uint64_t width() const; + const uint_fast64_t width() const; }; } diff --git a/src/easy/easy.cpp b/src/easy/easy.cpp index 477d25e..43d4668 100644 --- a/src/easy/easy.cpp +++ b/src/easy/easy.cpp @@ -22,7 +22,7 @@ using namespace Mastodon; using std::string; -using std::uint64_t; +using std::uint_fast64_t; using std::chrono::system_clock; Easy::Easy(const string &instance, const string &access_token) @@ -124,7 +124,7 @@ const string Easy::Entity::get_string(const string &key) const return ""; } -const uint64_t Easy::Entity::get_uint64(const string &key) const +const uint_fast64_t Easy::Entity::get_uint64(const string &key) const { const Json::Value node = get(key); diff --git a/src/easy/easy.hpp b/src/easy/easy.hpp index b3d0cf6..e57cd14 100644 --- a/src/easy/easy.hpp +++ b/src/easy/easy.hpp @@ -31,7 +31,7 @@ #endif using std::string; -using std::uint64_t; +using std::uint_fast64_t; using std::chrono::system_clock; namespace Mastodon @@ -147,11 +147,11 @@ public: const string get_string(const string &key) const; /*! - * @brief Returns the value of key as std::uint64_t + * @brief Returns the value of key as std::uint_fast64_t * * Returns 0 on error. */ - const uint64_t get_uint64(const string &key) const; + const uint_fast64_t get_uint64(const string &key) const; /*! * @brief Returns the value of key as double diff --git a/src/easy/list.cpp b/src/easy/list.cpp index a148a35..9b0e413 100644 --- a/src/easy/list.cpp +++ b/src/easy/list.cpp @@ -19,7 +19,7 @@ using namespace Mastodon; using List = Easy::List; using std::string; -using std::uint64_t; +using std::uint_fast64_t; List::List(const string &json) : Entity(json) @@ -29,7 +29,7 @@ List::List() : Entity() {} -const uint64_t List::id() const +const uint_fast64_t List::id() const { return std::stoull(get_string("id")); } diff --git a/src/easy/list.hpp b/src/easy/list.hpp index 2e8a5e3..654cbda 100644 --- a/src/easy/list.hpp +++ b/src/easy/list.hpp @@ -31,7 +31,7 @@ #endif using std::string; -using std::uint64_t; +using std::uint_fast64_t; namespace Mastodon { @@ -56,7 +56,7 @@ namespace Mastodon /*! * @brief Returns list-ID */ - const uint64_t id() const; + const uint_fast64_t id() const; /*! * @brief Returns title diff --git a/src/easy/mention.cpp b/src/easy/mention.cpp index fe8be65..ba90b58 100644 --- a/src/easy/mention.cpp +++ b/src/easy/mention.cpp @@ -32,7 +32,7 @@ const string Mention::acct() const return get_string("acct"); } -const uint64_t Mention::id() const +const uint_fast64_t Mention::id() const { return std::stoull(get_string("id")); } diff --git a/src/easy/mention.hpp b/src/easy/mention.hpp index 95cd7dc..9865b16 100644 --- a/src/easy/mention.hpp +++ b/src/easy/mention.hpp @@ -30,7 +30,7 @@ #endif using std::string; -using std::uint64_t; +using std::uint_fast64_t; using std::chrono::system_clock; namespace Mastodon @@ -61,7 +61,7 @@ namespace Mastodon /*! * @brief Returns account ID */ - const uint64_t id() const; + const uint_fast64_t id() const; /*! * @brief Returns the URL of user's profile diff --git a/src/easy/notification.cpp b/src/easy/notification.cpp index 856ad87..af5234b 100644 --- a/src/easy/notification.cpp +++ b/src/easy/notification.cpp @@ -45,7 +45,7 @@ const system_clock::time_point Notification::created_at() const return get_time_point("created_at"); } -const uint64_t Notification::id() const +const uint_fast64_t Notification::id() const { return std::stoull(get_string("id")); } diff --git a/src/easy/notification.hpp b/src/easy/notification.hpp index ab5ff44..c9db8b7 100644 --- a/src/easy/notification.hpp +++ b/src/easy/notification.hpp @@ -35,7 +35,7 @@ #endif using std::string; -using std::uint64_t; +using std::uint_fast64_t; using std::chrono::system_clock; namespace Mastodon @@ -71,7 +71,7 @@ namespace Mastodon /*! * @brief Returns notification ID */ - const uint64_t id() const; + const uint_fast64_t id() const; /*! * @brief Returns the Status associated with the notification, if diff --git a/src/easy/relationship.cpp b/src/easy/relationship.cpp index a80a5a4..45a7b9a 100644 --- a/src/easy/relationship.cpp +++ b/src/easy/relationship.cpp @@ -47,7 +47,7 @@ const bool Relationship::following() const return get_bool("following"); } -const uint64_t Relationship::id() const +const uint_fast64_t Relationship::id() const { return std::stoull(get_string("id")); } diff --git a/src/easy/relationship.hpp b/src/easy/relationship.hpp index cb68afc..72402c7 100644 --- a/src/easy/relationship.hpp +++ b/src/easy/relationship.hpp @@ -30,7 +30,7 @@ #endif using std::string; -using std::uint64_t; +using std::uint_fast64_t; namespace Mastodon { @@ -75,7 +75,7 @@ namespace Mastodon /*! * @brief Returns the target account ID */ - const uint64_t id() const; + const uint_fast64_t id() const; /*! * @brief Returns true if the user is muting the account diff --git a/src/easy/report.cpp b/src/easy/report.cpp index 84f0e3d..3fe79fe 100644 --- a/src/easy/report.cpp +++ b/src/easy/report.cpp @@ -32,7 +32,7 @@ const bool Report::action_taken() const return get_bool("action_taken"); } -const uint64_t Report::id() const +const uint_fast64_t Report::id() const { return std::stoull(get_string("id")); } diff --git a/src/easy/report.hpp b/src/easy/report.hpp index c18a596..93905cb 100644 --- a/src/easy/report.hpp +++ b/src/easy/report.hpp @@ -30,7 +30,7 @@ #endif using std::string; -using std::uint64_t; +using std::uint_fast64_t; namespace Mastodon { @@ -61,7 +61,7 @@ namespace Mastodon /*! * @brief Returns the ID of the report */ - const uint64_t id() const; + const uint_fast64_t id() const; }; } diff --git a/src/easy/status.cpp b/src/easy/status.cpp index 58e62fb..4e723bf 100644 --- a/src/easy/status.cpp +++ b/src/easy/status.cpp @@ -85,22 +85,22 @@ const bool Status::favourited() const return get_bool("favourited"); } -const uint64_t Status::favourites_count() const +const uint_fast64_t Status::favourites_count() const { return get_uint64("favourites_count"); } -const uint64_t Status::id() const +const uint_fast64_t Status::id() const { return get_uint64("id"); } -const uint64_t Status::in_reply_to_id() const +const uint_fast64_t Status::in_reply_to_id() const { return get_uint64("in_reply_to_id"); } -const uint64_t Status::in_reply_to_account_id() const +const uint_fast64_t Status::in_reply_to_account_id() const { return get_uint64("in_reply_to_account_id"); } @@ -171,7 +171,7 @@ const bool Status::reblogged() const return get_bool("reblogged"); } -const uint64_t Status::reblogs_count() const +const uint_fast64_t Status::reblogs_count() const { return get_uint64("reblogs_count"); } diff --git a/src/easy/status.hpp b/src/easy/status.hpp index 26b5ecb..e842a86 100644 --- a/src/easy/status.hpp +++ b/src/easy/status.hpp @@ -44,7 +44,7 @@ #endif using std::string; -using std::uint64_t; +using std::uint_fast64_t; using std::chrono::system_clock; namespace Mastodon @@ -100,22 +100,22 @@ namespace Mastodon /*! * @brief Returns the number of favourites */ - const uint64_t favourites_count() const; + const uint_fast64_t favourites_count() const; /*! * @brief Returns the ID of the status */ - const uint64_t id() const; + const uint_fast64_t id() const; /*! * @brief Returns the ID of the status it replies to */ - const uint64_t in_reply_to_id() const; + const uint_fast64_t in_reply_to_id() const; /*! * @brief Returns the ID of the account it replies to */ - const uint64_t in_reply_to_account_id() const; + const uint_fast64_t in_reply_to_account_id() const; /*! * @brief Returns the language of the status @@ -155,7 +155,7 @@ namespace Mastodon /*! * @brief Returns the number of reblogs for the status */ - const uint64_t reblogs_count() const; + const uint_fast64_t reblogs_count() const; /*! * @brief Returns true if the attachments should be hidden by default From 3d2c636d541108582dda1cd4ee9148d8a8762c95 Mon Sep 17 00:00:00 2001 From: tastytea Date: Sat, 31 Mar 2018 20:03:31 +0200 Subject: [PATCH 44/63] Added Easy::Entity::from_string() and Easy::json_array_to_vector() --- CMakeLists.txt | 2 +- src/easy/easy.cpp | 26 ++++++++++++++++++++++++++ src/easy/easy.hpp | 17 +++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e8e1daa..0c63173 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project (mastodon-cpp - VERSION 0.7.15 + VERSION 0.7.16 LANGUAGES CXX ) diff --git a/src/easy/easy.cpp b/src/easy/easy.cpp index 43d4668..ce1c09c 100644 --- a/src/easy/easy.cpp +++ b/src/easy/easy.cpp @@ -29,9 +29,35 @@ Easy::Easy(const string &instance, const string &access_token) : API(instance, access_token) {} +const std::vector Easy::json_array_to_vector(const string &json) +{ + Json::Value json_array; + std::stringstream ss(json); + ss >> json_array; + + if (json_array.isArray()) + { + std::vector vec; + for (const Json::Value &value : json_array) + { + vec.push_back(value.toStyledString()); + } + return vec; + } + + ttdebug << "ERROR: JSON string holds no array\n"; + ttdebug << "String was: " << json << '\n'; + return {}; +} + Easy::Entity::Entity(const string &json) : _tree(Json::nullValue) , _valid(false) +{ + from_string(json); +} + +const void Easy::Entity::from_string(const string &json) { std::stringstream ss(json); ss >> _tree; diff --git a/src/easy/easy.hpp b/src/easy/easy.hpp index e57cd14..478553b 100644 --- a/src/easy/easy.hpp +++ b/src/easy/easy.hpp @@ -103,6 +103,15 @@ public: */ explicit Easy(const string &instance, const string &access_token); + /*! + * @brief Turns an JSON array into a vector of strings + * + * @param json JSON string holding the array + * + * @return vector of strings or an empty vector on error + */ + static const std::vector json_array_to_vector(const string &json); + /*! * @brief Base class for entities. */ @@ -121,6 +130,14 @@ public: */ Entity(); + /*! + * @brief Replaces the Entity object with a new one from a JSON + * string. + * + * @param json JSON string + */ + const void from_string(const string &json); + /*! * @brief Returns true if the Entity holds valid data */ From 233646668e877564d1aaa4fc9e642b0ba5374bb6 Mon Sep 17 00:00:00 2001 From: tastytea Date: Sun, 1 Apr 2018 00:14:55 +0200 Subject: [PATCH 45/63] fixed Easy::Status::id() --- src/easy/status.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/easy/status.cpp b/src/easy/status.cpp index 4e723bf..61dc232 100644 --- a/src/easy/status.cpp +++ b/src/easy/status.cpp @@ -92,7 +92,7 @@ const uint_fast64_t Status::favourites_count() const const uint_fast64_t Status::id() const { - return get_uint64("id"); + return std::stoull(get_string("id")); } const uint_fast64_t Status::in_reply_to_id() const From 67da294d1dcb6a6a87f3f70db19a942609d33c9a Mon Sep 17 00:00:00 2001 From: tastytea Date: Sun, 1 Apr 2018 00:15:35 +0200 Subject: [PATCH 46/63] Added Easy::Entity::to_object() --- CMakeLists.txt | 2 +- src/easy/easy.cpp | 5 +++++ src/easy/easy.hpp | 10 ++++++++-- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0c63173..477aaa4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project (mastodon-cpp - VERSION 0.7.16 + VERSION 0.7.17 LANGUAGES CXX ) diff --git a/src/easy/easy.cpp b/src/easy/easy.cpp index ce1c09c..f71ea02 100644 --- a/src/easy/easy.cpp +++ b/src/easy/easy.cpp @@ -85,6 +85,11 @@ const void Easy::Entity::from_string(const string &json) } } +const Json::Value Easy::Entity::to_object() const +{ + return _tree; +} + Easy::Entity::Entity() : _valid(false) {} diff --git a/src/easy/easy.hpp b/src/easy/easy.hpp index 478553b..3d7fad8 100644 --- a/src/easy/easy.hpp +++ b/src/easy/easy.hpp @@ -131,13 +131,19 @@ public: Entity(); /*! - * @brief Replaces the Entity object with a new one from a JSON - * string. + * @brief Replaces the Entity with a new one from a JSON string. * * @param json JSON string */ const void from_string(const string &json); + /*! + * @brief Returns the JSON object of the Entity + * + * @return JSON object + */ + const Json::Value to_object() const; + /*! * @brief Returns true if the Entity holds valid data */ From 168f55a7912fb104fb3b7cc94fc82b0c383f7daa Mon Sep 17 00:00:00 2001 From: tastytea Date: Sun, 1 Apr 2018 01:50:26 +0200 Subject: [PATCH 47/63] Added bitrate(), duration() and framerate() to Easy::Attachment --- CMakeLists.txt | 2 +- README.md | 2 +- src/easy/attachment.cpp | 41 +++++++++++++++++++++++++++++++++++++---- src/easy/attachment.hpp | 22 +++++++++++++++------- 4 files changed, 54 insertions(+), 13 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 477aaa4..d1e540a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project (mastodon-cpp - VERSION 0.7.17 + VERSION 0.7.18 LANGUAGES CXX ) diff --git a/README.md b/README.md index 33fa7bd..1f0f37b 100644 --- a/README.md +++ b/README.md @@ -143,7 +143,7 @@ Run `make package` from the build directory to generate a tar.gz archive. # Status of implementation -Feature complete as of Mastodon 2.2.0 +Feature complete as of Mastodon 2.3.0 * [x] GET /api/v1/accounts/:id * [x] GET /api/v1/accounts/verify_credentials diff --git a/src/easy/attachment.cpp b/src/easy/attachment.cpp index 461d527..2895b6a 100644 --- a/src/easy/attachment.cpp +++ b/src/easy/attachment.cpp @@ -39,27 +39,60 @@ const double Attachment::aspect_small() const return get_double("meta.small.aspect"); } +const uint_fast64_t Attachment::bitrate() const +{ + return get_uint64("meta.original.bitrate"); +} + const string Attachment::description() const { return get_string("description"); } -const std::array Attachment::focus() const +const std::chrono::duration Attachment::duration() const +{ + const double sec = get_double("meta.original.duration"); + + if (sec > 0.0) + { + return std::chrono::duration(sec); + } +} + +const std::array Attachment::focus() const { const Json::Value x = get("meta.focus.x"); const Json::Value y = get("meta.focus.y"); - if (x.isUInt64() && y.isUInt64()) + if (x.isDouble() && y.isDouble()) { return {{ - x.asUInt64(), - y.asUInt64() + x.asDouble(), + y.asDouble() }}; } return {}; } +const double Attachment::framerate() const +{ + string strframes = get_string("meta.original.frame_rate"); + + if (!strframes.empty()) + { + std::size_t pos = strframes.find('/'); + if (pos != std::string::npos) + { + std::uint_fast16_t frames = std::stoul(strframes.substr(0, pos)); + std::uint_fast16_t divider = std::stoul(strframes.substr(pos + 1)); + + return frames / divider; + } + } + +} + const uint_fast64_t Attachment::height() const { return get_uint64("meta.original.height"); diff --git a/src/easy/attachment.hpp b/src/easy/attachment.hpp index a6b4524..6414e51 100644 --- a/src/easy/attachment.hpp +++ b/src/easy/attachment.hpp @@ -32,7 +32,6 @@ #endif using std::string; -using std::uint16_t; using std::uint_fast64_t; namespace Mastodon @@ -65,16 +64,29 @@ namespace Mastodon */ const double aspect_small() const; + /*! + * @brief Returns the bitrate of a video + */ + const uint_fast64_t bitrate() const; /*! * @brief Returns the image description */ const string description() const; + /*! + * @brief Returns the duration of a video in seconds + */ + const std::chrono::duration duration() const; + /*! * @brief Returns the focus point (x, y) */ - // TODO: find attachment with focus - const std::array focus() const; + const std::array focus() const; + + /*! + * @brief Returns the framerate of a video in frames per second + */ + const double framerate() const; /*! * @brief Returns the height of the original image @@ -137,10 +149,6 @@ namespace Mastodon const uint_fast64_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 uint_fast64_t bitrate() const; }; } From 0c0495b5f165e4df2cd37c2a6ce6e5d962da103d Mon Sep 17 00:00:00 2001 From: tastytea Date: Sun, 1 Apr 2018 01:58:03 +0200 Subject: [PATCH 48/63] added Instance::streaming_api() --- CMakeLists.txt | 2 +- src/easy/easy.hpp | 2 +- src/easy/instance.cpp | 4 ++-- src/easy/instance.hpp | 5 ++--- 4 files changed, 6 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d1e540a..03d595e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project (mastodon-cpp - VERSION 0.7.18 + VERSION 0.7.19 LANGUAGES CXX ) diff --git a/src/easy/easy.hpp b/src/easy/easy.hpp index 3d7fad8..49b4bb7 100644 --- a/src/easy/easy.hpp +++ b/src/easy/easy.hpp @@ -183,7 +183,7 @@ public: */ const double get_double(const string &key) const; - // TODO: Investigate if uint8_t would be better + // TODO: Maybe an enum would be better? /*! * @brief Returns the value of key as bool * diff --git a/src/easy/instance.cpp b/src/easy/instance.cpp index c36f869..ef658f6 100644 --- a/src/easy/instance.cpp +++ b/src/easy/instance.cpp @@ -72,7 +72,7 @@ const string Instance::version() const return get_string("version"); } -const std::vector Instance::urls() const +const string Instance::streaming_api() const { - return get_vector("urls"); + return get_string("urls.streaming_api"); } diff --git a/src/easy/instance.hpp b/src/easy/instance.hpp index 2fa16ca..d80c418 100644 --- a/src/easy/instance.hpp +++ b/src/easy/instance.hpp @@ -90,11 +90,10 @@ namespace Mastodon */ const string version() const; - // TODO: Find out what Instance.urls is about /*! - * @brief Returns the a vector of URLs for the streaming API (?) + * @brief Returns the URL for the streaming API */ - const std::vector urls() const; + const string streaming_api() const; }; } From b436a7abe20e56346b2369690a1ca432833c80d7 Mon Sep 17 00:00:00 2001 From: tastytea Date: Sun, 1 Apr 2018 02:19:39 +0200 Subject: [PATCH 49/63] added example12 --- examples/example12_easy_laststatus.cpp | 125 +++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 examples/example12_easy_laststatus.cpp diff --git a/examples/example12_easy_laststatus.cpp b/examples/example12_easy_laststatus.cpp new file mode 100644 index 0000000..8c481ad --- /dev/null +++ b/examples/example12_easy_laststatus.cpp @@ -0,0 +1,125 @@ +/* This file is part of mastodon-cpp. + */ + +// Don't compile this if the Easy-interface is turned off +#ifndef WITHOUT_EASY + +#include +#include +#include + +// If we are compiling mastodon-cpp, use another include path +#ifdef MASTODON_CPP + #include "mastodon-cpp.hpp" + #include "easy/all.hpp" +#else + #include + // Include all headers in mastodon-cpp/easy/ + #include +#endif + +using Mastodon::API; +using Mastodon::Easy; +using std::cout; + +int main(int argc, char *argv[]) +{ + if (argc < 3) + { + std::cerr << "usage: " << argv[0] << " \n"; + return 1; + } + + Easy masto(argv[1], argv[2]); + std::string answer; + std::uint16_t ret; + + // Get own account in order to obtain account ID + ret = masto.get(API::v1::accounts_verify_credentials, answer); + if (ret == 0) + { + // Construct an Account object using the JSON string from the server + Easy::Account acc(answer); + + // Get last status + ret = masto.get(API::v1::accounts_id_statuses, + std::to_string(acc.id()), + {{ "limit", { "1" } }}, + answer); + if (ret == 0) + { + // answer contains an array with a single object. This works because + // Easy::Status will turn that into an object, but an array with + // more than 1 object will not work. + Easy::Status status(answer); + + // An Entitiy is valid if the JSON was not empty and contained no + // "error":-key + if (status.valid()) + { + if (!status.language().empty()) + cout << "Language: " << status.language() << '\n'; + if (!status.content().empty()) + cout << "Content: " + << status.content().substr(0, 200) << "…\n"; + if (!status.application().name().empty()) + cout << "Application used: " + << status.application().name() << '\n'; + cout << "ID: " << status.id() << '\n'; + + string acct; + string url; + std::vector attachments; + std::vector tags; + // If the status is a reblog, print the original author + if (status.reblog().valid()) + { + // status.reblog() is an Easy::Status + // status.reblog().account() is an Easy::Account + cout << "Original ID: " << status.reblog().id() << '\n'; + acct = status.reblog().account().acct(); + url = status.reblog().account().url(); + attachments = status.reblog().media_attachments(); + tags = status.reblog().tags(); + } + else + { + acct = status.account().acct(); + url = status.account().url(); + attachments = status.media_attachments(); + tags = status.tags(); + } + cout << "From: " << acct << " "; + cout << "(" << url << ")\n"; + + // List attachments, if any + for (const Easy::Attachment &attachment : attachments) + { + cout << "Attachment: " << attachment.text_url() + << " (" << attachment.size() << ")\n"; + } + + // List hashtags, if any + for (const Easy::Tag &tag : tags) + { + cout << "Hashtag: #" << tag.name() + << " (" << tag.url() << ")\n"; + } + } + + return 0; + } + } + + std::cout << answer << '\n'; + std::cerr << "Error code: " << ret << '\n'; + return ret; +} + +#else +int main() +{ + printf("mastodon-cpp was compiled without Easy support.\n"); + return 255; +} +#endif // WITHOUT_EASY From 1b7753424ba9592273b546c2fb367f39c621ec9c Mon Sep 17 00:00:00 2001 From: tastytea Date: Sun, 1 Apr 2018 02:20:10 +0200 Subject: [PATCH 50/63] documentation enhancement --- src/easy/attachment.hpp | 2 ++ src/easy/easy.hpp | 2 +- src/mastodon-cpp.hpp | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/easy/attachment.hpp b/src/easy/attachment.hpp index 6414e51..ae546e4 100644 --- a/src/easy/attachment.hpp +++ b/src/easy/attachment.hpp @@ -80,6 +80,8 @@ namespace Mastodon /*! * @brief Returns the focus point (x, y) + * + * Values are between -1.0 and 1.0. */ const std::array focus() const; diff --git a/src/easy/easy.hpp b/src/easy/easy.hpp index 49b4bb7..21d0290 100644 --- a/src/easy/easy.hpp +++ b/src/easy/easy.hpp @@ -104,7 +104,7 @@ public: explicit Easy(const string &instance, const string &access_token); /*! - * @brief Turns an JSON array into a vector of strings + * @brief Turns a JSON array into a vector of strings * * @param json JSON string holding the array * diff --git a/src/mastodon-cpp.hpp b/src/mastodon-cpp.hpp index 40ac08c..4b1b535 100644 --- a/src/mastodon-cpp.hpp +++ b/src/mastodon-cpp.hpp @@ -37,6 +37,7 @@ * @example example09_streaming_api.cpp * @example example10_simplify.cpp * @example example11_post_media.cpp + * @example example12_easy_laststatus.cpp */ namespace Mastodon { From 7f9e8aca0b370918bcce3275611d7372efbee079 Mon Sep 17 00:00:00 2001 From: tastytea Date: Sun, 1 Apr 2018 02:26:06 +0200 Subject: [PATCH 51/63] fixed missing default return value in Attachment::framerate() --- src/easy/attachment.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/easy/attachment.cpp b/src/easy/attachment.cpp index 2895b6a..50d2c48 100644 --- a/src/easy/attachment.cpp +++ b/src/easy/attachment.cpp @@ -91,6 +91,7 @@ const double Attachment::framerate() const } } + return 0.0; } const uint_fast64_t Attachment::height() const From 4baef545db69a7296f99c1a2ac14922f510eabb6 Mon Sep 17 00:00:00 2001 From: tastytea Date: Sun, 1 Apr 2018 03:08:22 +0200 Subject: [PATCH 52/63] Added Easy::parse_stream() --- src/easy/easy.cpp | 29 +++++++++++++++++++++++++++++ src/easy/easy.hpp | 24 ++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/src/easy/easy.cpp b/src/easy/easy.cpp index f71ea02..247f9e4 100644 --- a/src/easy/easy.cpp +++ b/src/easy/easy.cpp @@ -17,6 +17,7 @@ #include #include // get_time #include +#include #include "easy.hpp" #include "macros.hpp" @@ -85,6 +86,34 @@ const void Easy::Entity::from_string(const string &json) } } +const std::vector + Easy::parse_stream(const std::string &streamdata) +{ + string stream = streamdata; + std::regex reevent("event: (update|notification|delete)\ndata: (.*)\n"); + std::smatch match; + std::vector vec = {}; + + while (std::regex_search(stream, match, reevent)) + { + const string &event = match[1].str(); + const string &data = match[2].str(); + event_type type = event_type::Undefined; + + if (event.compare("update") == 0) + type = event_type::Update; + else if (event.compare("notification") == 0) + type = event_type::Notification; + else if (event.compare("delete") == 0) + type = event_type::Delete; + + vec.push_back(stream_event(type, data)); + stream = match.suffix().str(); + } + + return vec; +} + const Json::Value Easy::Entity::to_object() const { return _tree; diff --git a/src/easy/easy.hpp b/src/easy/easy.hpp index 21d0290..c1fbd87 100644 --- a/src/easy/easy.hpp +++ b/src/easy/easy.hpp @@ -21,6 +21,7 @@ #include #include #include +#include #include // If we are compiling mastodon-cpp, use another include path @@ -42,6 +43,17 @@ namespace Mastodon class Easy : public API { public: + /*! + * @brief Describes the event type + */ + enum class event_type + { + Update, + Notification, + Delete, + Undefined + }; + /*! * @brief Describes visibility of toots. * @@ -92,6 +104,8 @@ public: unknown }; + typedef std::pair stream_event; + /*! * @brief Constructs a new Easy object. * @@ -112,6 +126,16 @@ public: */ static const std::vector json_array_to_vector(const string &json); + /*! + * @brief Split stream into a vector of events + * + * @param streamdata Data from get_stream() + * + * @return vector of stream events + */ + static const std::vector + parse_stream(const std::string &streamdata); + /*! * @brief Base class for entities. */ From 445567744d63d35654e387e4a26090ed290d9fd8 Mon Sep 17 00:00:00 2001 From: tastytea Date: Sun, 1 Apr 2018 03:36:11 +0200 Subject: [PATCH 53/63] fixed headers, refactoring --- CMakeLists.txt | 2 +- src/easy/account.hpp | 4 +- src/easy/all.hpp | 4 +- src/easy/application.hpp | 4 +- src/easy/attachment.cpp | 5 +- src/easy/attachment.hpp | 4 +- src/easy/card.hpp | 4 +- src/easy/context.hpp | 8 +- src/easy/easy.cpp | 178 --------------------------------- src/easy/easy.hpp | 3 - src/easy/emoji.hpp | 4 +- src/easy/entity.cpp | 203 ++++++++++++++++++++++++++++++++++++++ src/easy/instance.hpp | 6 +- src/easy/list.hpp | 4 +- src/easy/mention.hpp | 4 +- src/easy/notification.hpp | 8 +- src/easy/relationship.hpp | 4 +- src/easy/report.hpp | 4 +- src/easy/results.hpp | 12 +-- src/easy/status.hpp | 28 +++--- src/easy/tag.hpp | 4 +- 21 files changed, 258 insertions(+), 239 deletions(-) create mode 100644 src/easy/entity.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 03d595e..6abd554 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project (mastodon-cpp - VERSION 0.7.19 + VERSION 0.7.20 LANGUAGES CXX ) diff --git a/src/easy/account.hpp b/src/easy/account.hpp index 30559bc..53c34e5 100644 --- a/src/easy/account.hpp +++ b/src/easy/account.hpp @@ -24,10 +24,10 @@ // If we are compiling mastodon-cpp, use another include path #ifdef MASTODON_CPP #include "mastodon-cpp.hpp" - #include "easy.hpp" + #include "easy/easy.hpp" #else #include - #include + #include #endif using std::string; diff --git a/src/easy/all.hpp b/src/easy/all.hpp index 17d14b0..eee9f28 100644 --- a/src/easy/all.hpp +++ b/src/easy/all.hpp @@ -19,7 +19,7 @@ // If we are compiling mastodon-cpp, use another include path #ifdef MASTODON_CPP - #include "easy.hpp" + #include "easy/easy.hpp" #include "easy/account.hpp" #include "easy/application.hpp" #include "easy/attachment.hpp" @@ -36,7 +36,7 @@ #include "easy/status.hpp" #include "easy/tag.hpp" #else - #include + #include #include #include #include diff --git a/src/easy/application.hpp b/src/easy/application.hpp index 6350a6c..f3017c6 100644 --- a/src/easy/application.hpp +++ b/src/easy/application.hpp @@ -22,10 +22,10 @@ // If we are compiling mastodon-cpp, use another include path #ifdef MASTODON_CPP #include "mastodon-cpp.hpp" - #include "easy.hpp" + #include "easy/easy.hpp" #else #include - #include + #include #endif using std::string; diff --git a/src/easy/attachment.cpp b/src/easy/attachment.cpp index 50d2c48..415c1c3 100644 --- a/src/easy/attachment.cpp +++ b/src/easy/attachment.cpp @@ -53,10 +53,7 @@ const std::chrono::duration Attachment::duration() const { const double sec = get_double("meta.original.duration"); - if (sec > 0.0) - { - return std::chrono::duration(sec); - } + return std::chrono::duration(sec); } const std::array Attachment::focus() const diff --git a/src/easy/attachment.hpp b/src/easy/attachment.hpp index ae546e4..299fb96 100644 --- a/src/easy/attachment.hpp +++ b/src/easy/attachment.hpp @@ -25,10 +25,10 @@ // If we are compiling mastodon-cpp, use another include path #ifdef MASTODON_CPP #include "mastodon-cpp.hpp" - #include "easy.hpp" + #include "easy/easy.hpp" #else #include - #include + #include #endif using std::string; diff --git a/src/easy/card.hpp b/src/easy/card.hpp index 5b5036e..1b63dc1 100644 --- a/src/easy/card.hpp +++ b/src/easy/card.hpp @@ -23,10 +23,10 @@ // If we are compiling mastodon-cpp, use another include path #ifdef MASTODON_CPP #include "mastodon-cpp.hpp" - #include "easy.hpp" + #include "easy/easy.hpp" #else #include - #include + #include #endif using std::string; diff --git a/src/easy/context.hpp b/src/easy/context.hpp index 37fa469..18fbad4 100644 --- a/src/easy/context.hpp +++ b/src/easy/context.hpp @@ -23,12 +23,12 @@ // If we are compiling mastodon-cpp, use another include path #ifdef MASTODON_CPP #include "mastodon-cpp.hpp" - #include "easy.hpp" - #include "status.hpp" + #include "easy/easy.hpp" + #include "easy/status.hpp" #else #include - #include - #include + #include + #include #endif using std::string; diff --git a/src/easy/easy.cpp b/src/easy/easy.cpp index 247f9e4..f456c4f 100644 --- a/src/easy/easy.cpp +++ b/src/easy/easy.cpp @@ -23,8 +23,6 @@ using namespace Mastodon; using std::string; -using std::uint_fast64_t; -using std::chrono::system_clock; Easy::Easy(const string &instance, const string &access_token) : API(instance, access_token) @@ -51,41 +49,6 @@ const std::vector Easy::json_array_to_vector(const string &json) return {}; } -Easy::Entity::Entity(const string &json) -: _tree(Json::nullValue) -, _valid(false) -{ - from_string(json); -} - -const void Easy::Entity::from_string(const string &json) -{ - std::stringstream ss(json); - ss >> _tree; - - // If the JSON is a single object encapsulated in an array, - // transform it into an object. If the JSON string is [], transform to null - if (_tree.type() == Json::ValueType::arrayValue && _tree.size() <= 1) - { - _tree = _tree[0]; - } - - if (_tree.isNull()) - { - ttdebug << "ERROR: JSON string holds no object\n"; - ttdebug << "String was: " << json << '\n'; - } - else if (!_tree["error"].isNull()) - { - ttdebug << "ERROR: Server returned an error\n"; - ttdebug << "String was: " << json << '\n'; - } - else - { - _valid = true; - } -} - const std::vector Easy::parse_stream(const std::string &streamdata) { @@ -113,144 +76,3 @@ const std::vector return vec; } - -const Json::Value Easy::Entity::to_object() const -{ - return _tree; -} - -Easy::Entity::Entity() -: _valid(false) -{} - -const bool Easy::Entity::valid() const -{ - return _valid; -} - -const string Easy::Entity::error() const -{ - return get_string("error"); -} - -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 uint_fast64_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(); -} - -const std::vector Easy::Entity::get_vector(const string &key) const -{ - const Json::Value node = get(key); - - if (node.isArray()) - { - std::vector vec; - for (const Json::Value &value : node) - { - vec.push_back(value.asString()); - } - return vec; - } - - return {}; -} diff --git a/src/easy/easy.hpp b/src/easy/easy.hpp index c1fbd87..360aa29 100644 --- a/src/easy/easy.hpp +++ b/src/easy/easy.hpp @@ -136,9 +136,6 @@ public: static const std::vector parse_stream(const std::string &streamdata); - /*! - * @brief Base class for entities. - */ class Entity { public: diff --git a/src/easy/emoji.hpp b/src/easy/emoji.hpp index afd0a68..45ab64f 100644 --- a/src/easy/emoji.hpp +++ b/src/easy/emoji.hpp @@ -22,10 +22,10 @@ // If we are compiling mastodon-cpp, use another include path #ifdef MASTODON_CPP #include "mastodon-cpp.hpp" - #include "easy.hpp" + #include "easy/easy.hpp" #else #include - #include + #include #endif using std::string; diff --git a/src/easy/entity.cpp b/src/easy/entity.cpp new file mode 100644 index 0000000..f164225 --- /dev/null +++ b/src/easy/entity.cpp @@ -0,0 +1,203 @@ +/* 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 // get_time +#include +#include +#include +#include "easy.hpp" +#include "macros.hpp" + +using namespace Mastodon; +using std::string; +using std::chrono::system_clock; + +Easy::Entity::Entity(const string &json) +: _tree(Json::nullValue) +, _valid(false) +{ + from_string(json); +} + +const void Easy::Entity::from_string(const string &json) +{ + std::stringstream ss(json); + ss >> _tree; + + // If the JSON is a single object encapsulated in an array, + // transform it into an object. If the JSON string is [], transform to null + if (_tree.type() == Json::ValueType::arrayValue && _tree.size() <= 1) + { + _tree = _tree[0]; + } + + if (_tree.isNull()) + { + ttdebug << "ERROR: JSON string holds no object\n"; + ttdebug << "String was: " << json << '\n'; + } + else if (!_tree["error"].isNull()) + { + ttdebug << "ERROR: Server returned an error\n"; + ttdebug << "String was: " << json << '\n'; + } + else + { + _valid = true; + } +} + +const Json::Value Easy::Entity::to_object() const +{ + return _tree; +} + +Easy::Entity::Entity() +: _valid(false) +{} + +const bool Easy::Entity::valid() const +{ + return _valid; +} + +const string Easy::Entity::error() const +{ + return get_string("error"); +} + +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 uint_fast64_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(); +} + +const std::vector Easy::Entity::get_vector(const string &key) const +{ + const Json::Value node = get(key); + + if (node.isArray()) + { + std::vector vec; + for (const Json::Value &value : node) + { + vec.push_back(value.asString()); + } + return vec; + } + + return {}; +} diff --git a/src/easy/instance.hpp b/src/easy/instance.hpp index d80c418..0b26c69 100644 --- a/src/easy/instance.hpp +++ b/src/easy/instance.hpp @@ -23,11 +23,11 @@ // If we are compiling mastodon-cpp, use another include path #ifdef MASTODON_CPP #include "mastodon-cpp.hpp" - #include "easy.hpp" - #include "account.hpp" + #include "easy/easy.hpp" + #include "easy/account.hpp" #else #include - #include + #include #include #endif diff --git a/src/easy/list.hpp b/src/easy/list.hpp index 654cbda..4e1bda8 100644 --- a/src/easy/list.hpp +++ b/src/easy/list.hpp @@ -24,10 +24,10 @@ // If we are compiling mastodon-cpp, use another include path #ifdef MASTODON_CPP #include "mastodon-cpp.hpp" - #include "easy.hpp" + #include "easy/easy.hpp" #else #include - #include + #include #endif using std::string; diff --git a/src/easy/mention.hpp b/src/easy/mention.hpp index 9865b16..0789c24 100644 --- a/src/easy/mention.hpp +++ b/src/easy/mention.hpp @@ -23,10 +23,10 @@ // If we are compiling mastodon-cpp, use another include path #ifdef MASTODON_CPP #include "mastodon-cpp.hpp" - #include "easy.hpp" + #include "easy/easy.hpp" #else #include - #include + #include #endif using std::string; diff --git a/src/easy/notification.hpp b/src/easy/notification.hpp index c9db8b7..d746eb7 100644 --- a/src/easy/notification.hpp +++ b/src/easy/notification.hpp @@ -24,12 +24,12 @@ // If we are compiling mastodon-cpp, use another include path #ifdef MASTODON_CPP #include "mastodon-cpp.hpp" - #include "easy.hpp" - #include "account.hpp" - #include "status.hpp" + #include "easy/easy.hpp" + #include "easy/account.hpp" + #include "easy/status.hpp" #else #include - #include + #include #include #include #endif diff --git a/src/easy/relationship.hpp b/src/easy/relationship.hpp index 72402c7..b0ff9b0 100644 --- a/src/easy/relationship.hpp +++ b/src/easy/relationship.hpp @@ -23,10 +23,10 @@ // If we are compiling mastodon-cpp, use another include path #ifdef MASTODON_CPP #include "mastodon-cpp.hpp" - #include "easy.hpp" + #include "easy/easy.hpp" #else #include - #include + #include #endif using std::string; diff --git a/src/easy/report.hpp b/src/easy/report.hpp index 93905cb..0d0abc7 100644 --- a/src/easy/report.hpp +++ b/src/easy/report.hpp @@ -23,10 +23,10 @@ // If we are compiling mastodon-cpp, use another include path #ifdef MASTODON_CPP #include "mastodon-cpp.hpp" - #include "easy.hpp" + #include "easy/easy.hpp" #else #include - #include + #include #endif using std::string; diff --git a/src/easy/results.hpp b/src/easy/results.hpp index 81859e2..f9167d4 100644 --- a/src/easy/results.hpp +++ b/src/easy/results.hpp @@ -23,14 +23,14 @@ // If we are compiling mastodon-cpp, use another include path #ifdef MASTODON_CPP #include "mastodon-cpp.hpp" - #include "easy.hpp" - #include "account.hpp" - #include "status.hpp" + #include "easy/easy.hpp" + #include "easy/account.hpp" + #include "easy/status.hpp" #else #include - #include - #include - #include + #include + #include + #include #endif using std::string; diff --git a/src/easy/status.hpp b/src/easy/status.hpp index e842a86..d31e78f 100644 --- a/src/easy/status.hpp +++ b/src/easy/status.hpp @@ -25,22 +25,22 @@ // If we are compiling mastodon-cpp, use another include path #ifdef MASTODON_CPP #include "mastodon-cpp.hpp" - #include "easy.hpp" - #include "account.hpp" - #include "emoji.hpp" - #include "attachment.hpp" - #include "mention.hpp" - #include "tag.hpp" - #include "application.hpp" + #include "easy/easy.hpp" + #include "easy/account.hpp" + #include "easy/emoji.hpp" + #include "easy/attachment.hpp" + #include "easy/mention.hpp" + #include "easy/tag.hpp" + #include "easy/application.hpp" #else #include - #include - #include - #include - #include - #include - #include - #include + #include + #include + #include + #include + #include + #include + #include #endif using std::string; diff --git a/src/easy/tag.hpp b/src/easy/tag.hpp index 39447e5..032f928 100644 --- a/src/easy/tag.hpp +++ b/src/easy/tag.hpp @@ -22,10 +22,10 @@ // If we are compiling mastodon-cpp, use another include path #ifdef MASTODON_CPP #include "mastodon-cpp.hpp" - #include "easy.hpp" + #include "easy/easy.hpp" #else #include - #include + #include #endif using std::string; From 364ac268fde9b54b6733c3be663bc1075a59013f Mon Sep 17 00:00:00 2001 From: tastytea Date: Sun, 1 Apr 2018 03:37:01 +0200 Subject: [PATCH 54/63] fixed example07 --- examples/example07_register_app.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/examples/example07_register_app.cpp b/examples/example07_register_app.cpp index a96e464..9ff72eb 100644 --- a/examples/example07_register_app.cpp +++ b/examples/example07_register_app.cpp @@ -30,8 +30,7 @@ int main(int argc, char *argv[]) std::uint16_t ret; std::string client_id, client_secret, url; - ret = masto.register_app1(argv[1], - "test123", + ret = masto.register_app1("test123", "urn:ietf:wg:oauth:2.0:oob", "read follow", "", @@ -47,8 +46,7 @@ int main(int argc, char *argv[]) std::cin >> code; std::string access_token; - ret = masto.register_app2(argv[1], - client_id, + ret = masto.register_app2(client_id, client_secret, "urn:ietf:wg:oauth:2.0:oob", code, From 88e2846e835cb4c873b6bd1c4fa49c780079519e Mon Sep 17 00:00:00 2001 From: tastytea Date: Sun, 1 Apr 2018 04:16:40 +0200 Subject: [PATCH 55/63] added example 13, fixed other examples --- examples/example10_simplify.cpp | 3 +- examples/example12_easy_laststatus.cpp | 1 + examples/example13_easy_stream.cpp | 97 ++++++++++++++++++++++++++ src/mastodon-cpp.hpp | 1 + 4 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 examples/example13_easy_stream.cpp diff --git a/examples/example10_simplify.cpp b/examples/example10_simplify.cpp index baf4a53..0212cf6 100644 --- a/examples/example10_simplify.cpp +++ b/examples/example10_simplify.cpp @@ -43,11 +43,10 @@ int main(int argc, char *argv[]) return 1; } - std::string answer; std::uint16_t ret = 0; EasyToot masto(argv[1], argv[2]); - masto.toot("Test"); + ret = masto.toot("Test"); if (ret != 0) { std::cerr << "Error: " << ret << '\n'; diff --git a/examples/example12_easy_laststatus.cpp b/examples/example12_easy_laststatus.cpp index 8c481ad..f17d2b7 100644 --- a/examples/example12_easy_laststatus.cpp +++ b/examples/example12_easy_laststatus.cpp @@ -1,4 +1,5 @@ /* This file is part of mastodon-cpp. + * Prints some information about your last status. */ // Don't compile this if the Easy-interface is turned off diff --git a/examples/example13_easy_stream.cpp b/examples/example13_easy_stream.cpp new file mode 100644 index 0000000..0831f5a --- /dev/null +++ b/examples/example13_easy_stream.cpp @@ -0,0 +1,97 @@ +/* This file is part of mastodon-cpp. + * Prints some information from the public timeline. + */ + +// Don't compile this if the Easy-interface is turned off +#ifndef WITHOUT_EASY + +#include +#include +#include +#include +#include +#include + +// If we are compiling mastodon-cpp, use another include path +#ifdef MASTODON_CPP + #include "mastodon-cpp.hpp" + #include "easy/all.hpp" +#else + #include + // Include all headers in mastodon-cpp/easy/ + #include +#endif + +using Mastodon::API; +using Mastodon::Easy; +using std::cout; + +int main(int argc, char *argv[]) +{ + if (argc < 3) + { + std::cerr << "usage: " << argv[0] << " \n"; + return 1; + } + + cout << "I'll show you the public timeline. Press CTRL-C to abort\n"; + + // These have to be static in order to use them in- and outside the thread + static std::string stream; + // You can abort the stream with this pointer (ptr->abort_stream()) + static std::unique_ptr ptr; + + // Start a new thread for the stream + std::thread pub_tl([=] + { + Easy masto(argv[1], argv[2]); + masto.get_stream(Mastodon::API::v1::streaming_public, stream, ptr); + }); + + while (true) + { + // Parse event stream and clear it afterwards + std::vector events = Easy::parse_stream(stream); + stream.clear(); + + // The contents of the stream are now in a vector of pairs + // { Easy::event_type, std::string } + for (const Easy::stream_event &event : events) + { + Easy::Status status; + Easy::Notification notification; + + // Print out some information about the events + switch (event.first) + { + case Easy::event_type::Update: + status.from_string(event.second); + cout << "Status from: " << status.account().acct() + << " (" << status.url() << ")\n"; + break; + case Easy::event_type::Notification: + notification.from_string(event.second); + cout << "Notification involving: " + << notification.account().acct() + << " (" << notification.id() << ")\n"; + break; + case Easy::event_type::Delete: + cout << "Deleted: " << event.second << '\n'; + break; + default: + cout << "Something undefined happened. 😱\n"; + } + } + std::this_thread::sleep_for(std::chrono::seconds(1)); + } + + pub_tl.join(); +} + +#else +int main() +{ + printf("mastodon-cpp was compiled without Easy support.\n"); + return 255; +} +#endif // WITHOUT_EASY diff --git a/src/mastodon-cpp.hpp b/src/mastodon-cpp.hpp index 4b1b535..c328864 100644 --- a/src/mastodon-cpp.hpp +++ b/src/mastodon-cpp.hpp @@ -38,6 +38,7 @@ * @example example10_simplify.cpp * @example example11_post_media.cpp * @example example12_easy_laststatus.cpp + * @example example13_easy_laststatus.cpp */ namespace Mastodon { From 4f66554271891048917f9ed2dda091007b511e43 Mon Sep 17 00:00:00 2001 From: tastytea Date: Sun, 1 Apr 2018 04:27:29 +0200 Subject: [PATCH 56/63] updated documentation --- CMakeLists.txt | 2 +- Doxyfile | 2 +- src/easy/easy.hpp | 3 +++ src/mastodon-cpp.hpp | 3 ++- 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6abd554..162936f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project (mastodon-cpp - VERSION 0.7.20 + VERSION 0.7.21 LANGUAGES CXX ) diff --git a/Doxyfile b/Doxyfile index 2f36d41..d08f3ed 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/ src/easy/ +INPUT = README.md src/ src/api/ src/easy/ USE_MDFILE_AS_MAINPAGE = README.md CREATE_SUBDIRS = NO ALLOW_UNICODE_NAMES = YES diff --git a/src/easy/easy.hpp b/src/easy/easy.hpp index 360aa29..59e2b21 100644 --- a/src/easy/easy.hpp +++ b/src/easy/easy.hpp @@ -136,6 +136,9 @@ public: static const std::vector parse_stream(const std::string &streamdata); + /*! + * @brief Base class for all entities. + */ class Entity { public: diff --git a/src/mastodon-cpp.hpp b/src/mastodon-cpp.hpp index c328864..354e83e 100644 --- a/src/mastodon-cpp.hpp +++ b/src/mastodon-cpp.hpp @@ -38,8 +38,9 @@ * @example example10_simplify.cpp * @example example11_post_media.cpp * @example example12_easy_laststatus.cpp - * @example example13_easy_laststatus.cpp + * @example example13_easy_stream.cpp */ + namespace Mastodon { /*! From 04f9a34dcdb9865e48c751b0eaf430c12bfb4ee3 Mon Sep 17 00:00:00 2001 From: tastytea Date: Sun, 1 Apr 2018 04:30:58 +0200 Subject: [PATCH 57/63] fixed build script (header location) --- CMakeLists.txt | 3 --- 1 file changed, 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 162936f..d4e4e99 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -54,9 +54,6 @@ 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() From 1bd9dbff307754da1e610114d234f37b454cfbf9 Mon Sep 17 00:00:00 2001 From: tastytea Date: Sun, 1 Apr 2018 05:03:35 +0200 Subject: [PATCH 58/63] Enhanced documentation --- README.md | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1f0f37b..588e596 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ **mastodon-cpp** is a C++ wrapper for the Mastodon API. -The library takes care of the network stuff. You submit a query and get the raw JSON. +The library takes care of the network stuff. You submit a query and use the raw +JSON or abstract classes. [TODO-list](https://github.com/tastytea/mastodon-cpp/milestones) @@ -29,6 +30,34 @@ int main() } ``` +## Another simple example + +Using the `Easy-class`. + +```C++ +#include +#include +#include +#include +#include + +using Mastodon::Easy; + +int main() +{ + Easy masto("social.example", ""); + std::string answer; + masto.get(Mastodon::API::v1::timelines_public, answer); + + for (const std::string &str : Easy::json_array_to_vector(answer)) + { + Easy::Status status(str); + std::cout << " " << status.account().acct() << " wrote:\n"; + std::cout << status.content() << '\n'; + } +} +``` + ## Compiling your project A project consisting of one file can be compiled as follows: From 65bd76a5ce85b609e74731a38f3f066432683b13 Mon Sep 17 00:00:00 2001 From: tastytea Date: Sun, 1 Apr 2018 05:03:50 +0200 Subject: [PATCH 59/63] Added a basic Easy-test --- tests/test_02_easy_get_public_timeline.cpp | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 tests/test_02_easy_get_public_timeline.cpp diff --git a/tests/test_02_easy_get_public_timeline.cpp b/tests/test_02_easy_get_public_timeline.cpp new file mode 100644 index 0000000..8da6c7d --- /dev/null +++ b/tests/test_02_easy_get_public_timeline.cpp @@ -0,0 +1,27 @@ +/* This file is part of mastodon-cpp. + */ + +#include +#include +#include "mastodon-cpp.hpp" +#include "easy/easy.hpp" +#include "easy/status.hpp" + +int main(int argc, char *argv[]) +{ + Mastodon::Easy test("botsin.space", ""); + std::string answer; + std::uint16_t ret = test.get(Mastodon::API::v1::timelines_public, + { { "limit", { "1" } } }, answer); + if (ret == 0) + { + Mastodon::Easy::Status status(answer); + if (status.valid()) + { + return 0; + } + } + + std::cout << ret << ": " << answer << '\n'; + return 1; +} From 3acd8536d82518376c97a556c337ba09441c85ae Mon Sep 17 00:00:00 2001 From: tastytea Date: Sun, 1 Apr 2018 05:07:21 +0200 Subject: [PATCH 60/63] include ctest in travis script --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 5ede8ce..eb8d8ae 100644 --- a/.travis.yml +++ b/.travis.yml @@ -19,6 +19,7 @@ script: - cmake -DWITH_EXAMPLES=ON .. - make VERBOSE=1 - make install DESTDIR=install + - ctest .. - cd .. before_deploy: From f298de7cff07da7f3e9486951232868edf990787 Mon Sep 17 00:00:00 2001 From: tastytea Date: Sun, 1 Apr 2018 05:14:02 +0200 Subject: [PATCH 61/63] Rewrote enums for consistency --- CMakeLists.txt | 2 +- src/easy/attachment.cpp | 10 +++++----- src/easy/card.cpp | 10 +++++----- src/easy/easy.hpp | 32 +++++++++++++++----------------- src/easy/notification.cpp | 10 +++++----- 5 files changed, 31 insertions(+), 33 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d4e4e99..283aeae 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project (mastodon-cpp - VERSION 0.7.21 + VERSION 0.7.22 LANGUAGES CXX ) diff --git a/src/easy/attachment.cpp b/src/easy/attachment.cpp index 415c1c3..d91a7a0 100644 --- a/src/easy/attachment.cpp +++ b/src/easy/attachment.cpp @@ -135,16 +135,16 @@ const Easy::attachment_type Attachment::type() const { const string strtype = get_string("type"); if (strtype.compare("image") == 0) - return attachment_type::image; + return attachment_type::Image; else if (strtype.compare("video") == 0) - return attachment_type::video; + return attachment_type::Video; else if (strtype.compare("gifv") == 0) - return attachment_type::gifv; + return attachment_type::Gifv; else if (strtype.compare("unknown") == 0) - return attachment_type::unknown; + return attachment_type::Unknown; ttdebug << "Could not get data: type\n"; - return attachment_type::unknown; + return attachment_type::Undefined; } const string Attachment::url() const diff --git a/src/easy/card.cpp b/src/easy/card.cpp index 306baaa..c8dec9d 100644 --- a/src/easy/card.cpp +++ b/src/easy/card.cpp @@ -77,16 +77,16 @@ const Easy::card_type Card::type() const { const string strtype = get_string("type"); if (strtype.compare("link") == 0) - return card_type::link; + return card_type::Link; else if (strtype.compare("photo") == 0) - return card_type::photo; + return card_type::Photo; else if (strtype.compare("video") == 0) - return card_type::video; + return card_type::Video; else if (strtype.compare("rich") == 0) - return card_type::rich; + return card_type::Rich; ttdebug << "Could not get data: type\n"; - return card_type::unknown; + return card_type::Undefined; } const string Card::url() const diff --git a/src/easy/easy.hpp b/src/easy/easy.hpp index 59e2b21..439a19f 100644 --- a/src/easy/easy.hpp +++ b/src/easy/easy.hpp @@ -56,9 +56,6 @@ 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_type { @@ -74,10 +71,11 @@ public: */ enum class attachment_type { - image, - video, - gifv, - unknown + Image, + Video, + Gifv, + Unknown, + Undefined }; /*! @@ -85,11 +83,11 @@ public: */ enum class card_type { - link, - photo, - video, - rich, - unknown + Link, + Photo, + Video, + Rich, + Undefined }; /*! @@ -97,11 +95,11 @@ public: */ enum class notification_type { - mention, - reblog, - favourite, - follow, - unknown + Mention, + Reblog, + Favourite, + Follow, + Undefined }; typedef std::pair stream_event; diff --git a/src/easy/notification.cpp b/src/easy/notification.cpp index af5234b..52d0d08 100644 --- a/src/easy/notification.cpp +++ b/src/easy/notification.cpp @@ -66,13 +66,13 @@ const Easy::notification_type Notification::type() const { const string strtype = get_string("type"); if (strtype.compare("mention") == 0) - return notification_type::mention; + return notification_type::Mention; else if (strtype.compare("reblog") == 0) - return notification_type::reblog; + return notification_type::Reblog; else if (strtype.compare("favourite") == 0) - return notification_type::favourite; + return notification_type::Favourite; else if (strtype.compare("follow") == 0) - return notification_type::follow; + return notification_type::Follow; - return notification_type::unknown; + return notification_type::Undefined; } From 002c77ca21f1b2a74aefc3dc49649d21525012c1 Mon Sep 17 00:00:00 2001 From: tastytea Date: Sun, 1 Apr 2018 05:19:17 +0200 Subject: [PATCH 62/63] fixed travis script --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index eb8d8ae..e8e9aca 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,7 +16,7 @@ before_install: - sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-5 60 script: - mkdir -p build && cd build - - cmake -DWITH_EXAMPLES=ON .. + - cmake -DWITH_EXAMPLES=ON -DWITH_TESTS=ON .. - make VERBOSE=1 - make install DESTDIR=install - ctest .. From b918af5a195e885f9ff7675b1151cc99165acba1 Mon Sep 17 00:00:00 2001 From: tastytea Date: Mon, 2 Apr 2018 00:49:28 +0200 Subject: [PATCH 63/63] Enhanced documentation --- src/easy/easy.hpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/easy/easy.hpp b/src/easy/easy.hpp index 439a19f..e9d75ba 100644 --- a/src/easy/easy.hpp +++ b/src/easy/easy.hpp @@ -172,7 +172,7 @@ public: const bool valid() const; /*! - * @brief Returns error string + * @brief Returns error string sent by the server */ const string error() const; @@ -180,28 +180,29 @@ public: /*! * @brief Returns the value of key as Json::Value * - * Returns an empty object on error. + * Returns an empty object if the value does not exist or is + * null. */ const Json::Value get(const string &key) const; /*! * @brief Returns the value of key as std::string * - * returns "" on error. + * returns "" if the value does not exist or is null. */ const string get_string(const string &key) const; /*! * @brief Returns the value of key as std::uint_fast64_t * - * Returns 0 on error. + * Returns 0 if the value does not exist or is null. */ const uint_fast64_t get_uint64(const string &key) const; /*! * @brief Returns the value of key as double * - * Returns 0.0 on error. + * Returns 0.0 if the value does not exist or is null. */ const double get_double(const string &key) const; @@ -209,21 +210,22 @@ public: /*! * @brief Returns the value of key as bool * - * Returns false on error. + * Returns false if the value does not exist or is null. */ const bool get_bool(const string &key) const; /*! * @brief Returns the value of key as time_point * - * Returns clocks epoch on error. + * Returns clocks epoch if the value does not exist or is null. */ const system_clock::time_point get_time_point(const string &key) const; /*! * @brief Returns the value of key as vector * - * Returns false on error. + * Returns an empty vector if the value does not exist or is + * null. */ const std::vector get_vector(const string &key) const;