From c72715a63de39ceae2ac36a27c20fa01ceece452 Mon Sep 17 00:00:00 2001 From: tastytea Date: Wed, 21 Mar 2018 23:28:48 +0100 Subject: [PATCH] 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;