This repository has been archived on 2020-05-10. You can view files and clone it, but cannot push or open issues or pull requests.
mastodon-cpp/src/easy/attachment.cpp

219 lines
5.1 KiB
C++
Raw Normal View History

2018-03-21 20:29:33 +01:00
/* This file is part of mastodon-cpp.
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <string>
#include <sstream>
2018-03-21 23:28:48 +01:00
#include <array>
2018-03-21 20:29:33 +01:00
#include <jsoncpp/json/json.h>
2018-03-22 00:33:36 +01:00
#include "attachment.hpp"
2018-03-21 20:29:33 +01:00
#include "macros.hpp"
using namespace Mastodon;
using Attachment = Easy::Attachment;
using std::string;
2018-03-25 19:43:48 +02:00
using std::uint64_t;
2018-03-21 20:29:33 +01:00
Attachment::Attachment(const string &json)
2018-03-22 00:33:36 +01:00
: Entity(json)
2018-03-21 20:29:33 +01:00
{
2018-03-22 00:33:36 +01:00
//
2018-03-21 20:29:33 +01:00
}
2018-03-21 23:28:48 +01:00
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;
}
2018-03-21 20:29:33 +01:00
const string Attachment::description() const
{
if (_tree["description"].isString())
{
return _tree["description"].asString();
}
ttdebug << "Could not get attachment data: description\n";
return "";
}
2018-03-21 23:28:48 +01:00
const std::array<uint64_t, 2> Attachment::focus() const
{
if (_tree["meta"]["focus"]["x"].isUInt64())
{
return
2018-03-22 00:33:36 +01:00
{{
2018-03-21 23:28:48 +01:00
_tree["meta"]["focus"]["x"].asUInt64(),
_tree["meta"]["focus"]["y"].asUInt64()
2018-03-22 00:33:36 +01:00
}};
2018-03-21 23:28:48 +01:00
}
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;
}
2018-03-21 20:29:33 +01:00
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 "";
}
2018-03-21 23:28:48 +01:00
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 "";
}
2018-03-21 20:29:33 +01:00
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 "";
}
2018-03-21 23:28:48 +01:00
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;
}