From d78c699955d57ce4d1899f4820b57e5750aaaa6f Mon Sep 17 00:00:00 2001 From: tastytea Date: Sat, 31 Mar 2018 04:29:55 +0200 Subject: [PATCH] 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"); }