Added meta info to attachment class

This commit is contained in:
tastytea 2018-03-21 23:28:48 +01:00
parent 29581f92fc
commit c72715a63d
Signed by: tastytea
GPG Key ID: 59346E0EA35C67E5
3 changed files with 197 additions and 3 deletions

View File

@ -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());
}

View File

@ -16,6 +16,7 @@
#include <string>
#include <sstream>
#include <array>
#include <jsoncpp/json/json.h>
#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<uint64_t, 2> 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;
}

View File

@ -20,6 +20,7 @@
#include <string>
#include <cstdint>
#include <chrono>
#include <array>
#include <jsoncpp/json/json.h>
// 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<uint64_t, 2> 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;